Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: SPL token balances #204

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 83 additions & 8 deletions packages/client/src/getBalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,26 @@ export const getBalances = async function (
symbol: token.symbol,
});
} else if (token.coin_type === "ERC20") {
tokens.push({
chain_id: token.foreign_chain_id,
coin_type: "ERC20",
contract: token.asset,
symbol: token.symbol,
zrc20: token.zrc20_contract_address,
});
const supportedChain = supportedChains.find(
(c: any) => c.chain_id === token.foreign_chain_id
);
if (supportedChain.vm === "evm") {
fadeev marked this conversation as resolved.
Show resolved Hide resolved
tokens.push({
chain_id: token.foreign_chain_id,
coin_type: "ERC20",
contract: token.asset,
symbol: token.symbol,
zrc20: token.zrc20_contract_address,
});
} else if (supportedChain.vm === "svm") {
tokens.push({
chain_id: token.foreign_chain_id,
coin_type: "SPL",
contract: token.asset,
symbol: token.symbol,
zrc20: token.zrc20_contract_address,
});
}
tokens.push({
chain_id: this.getChainId(`zeta_${this.network}`),
coin_type: "ZRC20",
Expand Down Expand Up @@ -193,7 +206,6 @@ export const getBalances = async function (
}
});
} catch (error) {
console.error(`Multicall failed for ${chainName}:`, error);
// Fallback to individual calls if multicall fails
for (const token of tokens.filter(
(t) =>
Expand Down Expand Up @@ -303,5 +315,68 @@ export const getBalances = async function (
})
);

const splTokens = tokens.filter(
(token) =>
token.coin_type === "SPL" &&
["solana_mainnet", "solana_testnet", "solana_devnet"].includes(
token.chain_name
)
);

await Promise.all(
splTokens.map(async (token) => {
try {
const API = this.getEndpoint("solana", token.chain_name);
const response = await fetch(API, {
body: JSON.stringify({
id: 1,
jsonrpc: "2.0",
method: "getTokenAccountsByOwner",
params: [
solanaAddress,
{ mint: token.contract },
{
encoding: "jsonParsed",
},
],
}),
headers: {
"Content-Type": "application/json",
},
method: "POST",
});

if (!response.ok) {
console.error(
`Failed to fetch SPL token accounts for ${token.symbol}`,
response
);
balances.push({ ...token, balance: "0" });
return;
}

const r = await response.json();

if (r.result && r.result.value && r.result.value.length > 0) {
let totalBalance = 0;
for (const acc of r.result.value) {
const amount = acc.account.data.parsed.info.tokenAmount.amount;
const decimals = acc.account.data.parsed.info.tokenAmount.decimals;
totalBalance += parseFloat(amount) / Math.pow(10, decimals);
}
balances.push({ ...token, balance: totalBalance.toString() });
} else {
balances.push({ ...token, balance: "0" });
}
} catch (err) {
console.error(
`Failed to get SPL balance for ${token.symbol} on ${token.chain_name}:`,
err
);
balances.push({ ...token, balance: "0" });
}
})
);

return balances;
};
2 changes: 1 addition & 1 deletion packages/tasks/src/balances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
} else {
spinner.stop();
console.log(`
EVM: ${evmAddress} ${btcAddress ? `\nBitcoin: ${btcAddress}` : ""} ${
EVM: ${evmAddress} ${btcAddress ? `\nBitcoin: ${btcAddress}` : ""} ${
solanaAddress ? `\nSolana: ${solanaAddress}` : ""
}
`);
Expand Down
Loading