Skip to content

Commit

Permalink
Fix v1/multicall balances (#276)
Browse files Browse the repository at this point in the history
* fix: get native balances using getBalance

* fix: add kava to chains without multicall list
  • Loading branch information
genaroibc authored Jan 17, 2024
1 parent f72e086 commit 975dc42
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions src/services/getEvmBalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {

type ContractAddress = `0x${string}`;

const CHAINS_WITHOUT_MULTICALL = [314, 3141]; // Filecoin, & Filecoin testnet
const CHAINS_WITHOUT_MULTICALL = [314, 3141, 2222]; // Filecoin, Filecoin testnet and Kava

const getTokensBalanceSupportingMultiCall = async (
tokens: TokenData[],
Expand Down Expand Up @@ -94,23 +94,12 @@ const getTokensBalanceWithoutMultiCall = async (
): Promise<TokenBalance[]> => {
const balances: (TokenBalance | null)[] = await Promise.all(
tokens.map(async t => {
let balance: TokenBalance | null;
try {
if (t.address === NATIVE_EVM_TOKEN_ADDRESS) {
balance = await fetchBalance({
token: t,
userAddress,
rpcUrl: rpcUrlsPerChain[t.chainId]
});
} else {
balance = await fetchBalance({
token: t,
userAddress,
rpcUrl: rpcUrlsPerChain[t.chainId]
});
}

return balance;
return await fetchBalance({
token: t,
userAddress,
rpcUrl: rpcUrlsPerChain[t.chainId]
});
} catch (error) {
return null;
}
Expand Down Expand Up @@ -199,6 +188,10 @@ async function fetchBalance({
userAddress,
rpcUrl
}: FetchBalanceParams): Promise<TokenBalance | null> {
if (token.address.toLowerCase() === NATIVE_EVM_TOKEN_ADDRESS.toLowerCase()) {
return fetchNativeBalance({ token, userAddress, rpcUrl });
}

try {
const provider = new ethers.providers.JsonRpcProvider(rpcUrl);

Expand Down Expand Up @@ -228,3 +221,25 @@ async function fetchBalance({
return null;
}
}

async function fetchNativeBalance({
userAddress,
rpcUrl,
token
}: FetchBalanceParams) {
try {
const provider = new ethers.providers.JsonRpcProvider(rpcUrl);
const balance = await provider.getBalance(userAddress);

return {
address: token.address,
balance: balance.toString(),
decimals: token.decimals,
symbol: token.symbol,
chainId: token.chainId
};
} catch (error) {
console.error("Error fetching native token balance:", error);
return null;
}
}

0 comments on commit 975dc42

Please sign in to comment.