Skip to content

Commit

Permalink
fix: undefined in usetokenbalance
Browse files Browse the repository at this point in the history
  • Loading branch information
foodaka committed Oct 31, 2024
1 parent 04aaa1f commit b6de0be
Showing 1 changed file with 31 additions and 33 deletions.
64 changes: 31 additions & 33 deletions src/hooks/generic/useTokensBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,59 +19,57 @@ export const useTokensBalance = (tokenList: TokenInfo[], chainId: number, user:
const tokensWithoutNative = tokenList.filter((elem) => !elem.extensions?.isNative);
const nativeToken = tokenList.find((token) => token.extensions?.isNative);
const multicall = new Multicall({
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
ethersProvider: provider as unknown as providers.Provider,
tryAggregate: true,
multicallCustomContractAddress: '0xcA11bde05977b3631167028862bE2a173976CA11',
});
const contractCallContext = tokensWithoutNative.map((token) => {
return {
reference: token.address,
contractAddress: token.address,
abi: [
{
name: 'balanceOf',
type: 'function',
stateMutability: 'view',
inputs: [{ name: 'account', type: 'address' }],
outputs: [{ name: 'balance', type: 'uint256' }],
},
],
calls: [
{ reference: 'balanceOfCall', methodName: 'balanceOf', methodParameters: [user] },
],
};
});
const contractCallContext = tokensWithoutNative.map((token) => ({
reference: token.address,
contractAddress: token.address,
abi: [
{
name: 'balanceOf',
type: 'function',
stateMutability: 'view',
inputs: [{ name: 'account', type: 'address' }],
outputs: [{ name: 'balance', type: 'uint256' }],
},
],
calls: [{ reference: 'balanceOfCall', methodName: 'balanceOf', methodParameters: [user] }],
}));

if (!nativeToken) {
const { results } = await multicall.call(contractCallContext);
return tokenList
.map((elem, index) => ({
...elem,
balance: formatUnits(
results[index].callsReturnContext[0].returnValues[0],
elem.decimals
),
}))
.map((elem, index) => {
const balanceValue = results[index]?.callsReturnContext[0]?.returnValues[0];
return {
...elem,
balance: balanceValue ? formatUnits(balanceValue, elem.decimals) : '0',
};
})
.sort((a, b) => Number(b.balance) - Number(a.balance));
}

const [balanceResult, multicallResult] = await Promise.all([
provider.getBalance(user),
multicall.call(contractCallContext),
]);

return tokenList
.map((elem) => {
const balanceValue = elem.extensions?.isNative
? balanceResult
: multicallResult.results[elem.address]?.callsReturnContext[0]?.returnValues[0];
return {
...elem,
balance: elem.extensions?.isNative
? formatUnits(balanceResult, elem.decimals)
: formatUnits(
multicallResult.results[elem.address].callsReturnContext[0].returnValues[0],
elem.decimals
),
balance: balanceValue ? formatUnits(balanceValue, elem.decimals) : '0',
};
})
.sort((a, b) => Number(b.balance) - Number(a.balance));
},
onError: (error) => {
console.error('Error fetching token balances:', error);
},
});
};

0 comments on commit b6de0be

Please sign in to comment.