-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add useMulticall hook for fetching ETH balances
- Loading branch information
1 parent
75d6209
commit 170bf4a
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
}; |