Skip to content

Commit

Permalink
Add useMulticall hook for fetching ETH balances
Browse files Browse the repository at this point in the history
  • Loading branch information
truemiller committed Feb 18, 2024
1 parent 75d6209 commit 170bf4a
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions frontend/hooks/useMulticall.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { MULTICALL } from "@/constants/contracts";
import { BigNumber, ethers } from "ethers";
import { Contract, Provider } from "ethers-multicall";
import { multicall3Abi } from "@/abi/multicall3Abi";

export const useMulticall = () => {
const getETHBalances = async (
addresses: string[],
rpc: string,
): Promise<{ [address: string]: number }> => {
const provider = new ethers.providers.JsonRpcProvider(rpc);
const multicallProvider = new Provider(provider, 100);

const multicallContract = new Contract(MULTICALL, multicall3Abi);

const callData = addresses.map((address) => {
console.log(address);
console.log(multicallContract.getEthBalance(address));
return multicallContract.getEthBalance(address);
});

return multicallProvider.all(callData).then((r: BigNumber[]) =>
r.reduce(
(
acc: { [address: string]: number },
balance: BigNumber,
index: number,
) => ({
...acc,
[addresses[index]]: parseFloat(ethers.utils.formatEther(balance)),
}),
{},
),
);
};

return { getETHBalances };
};

0 comments on commit 170bf4a

Please sign in to comment.