Skip to content

Commit

Permalink
chore: undo debug
Browse files Browse the repository at this point in the history
  • Loading branch information
foodaka committed Oct 31, 2024
1 parent b8df3fc commit 5361d06
Showing 1 changed file with 33 additions and 31 deletions.
64 changes: 33 additions & 31 deletions src/hooks/generic/useTokensBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,57 +19,59 @@ 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) => ({
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) => {
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] },
],
};
});
if (!nativeToken) {
const { results } = await multicall.call(contractCallContext);
return tokenList
.map((elem, index) => {
const balanceValue = results[index]?.callsReturnContext[0]?.returnValues[0];
return {
...elem,
balance: balanceValue ? formatUnits(balanceValue, elem.decimals) : '0',
};
})
.map((elem, index) => ({
...elem,
balance: formatUnits(
results[index].callsReturnContext[0].returnValues[0],
elem.decimals
),
}))
.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: balanceValue ? formatUnits(balanceValue, elem.decimals) : '0',
balance: elem.extensions?.isNative
? formatUnits(balanceResult, elem.decimals)
: formatUnits(
multicallResult.results[elem.address].callsReturnContext[0].returnValues[0],
elem.decimals
),
};
})
.sort((a, b) => Number(b.balance) - Number(a.balance));
},
onError: (error) => {
console.error('Error fetching token balances:', error);
},
});
};

0 comments on commit 5361d06

Please sign in to comment.