Skip to content

Commit

Permalink
Merge pull request #84 from zkLinkProtocol/fix/multicall_getBalance
Browse files Browse the repository at this point in the history
use multicall to getBalance
  • Loading branch information
Evan-zkLinkLabs authored Apr 9, 2024
2 parents 2d0e607 + 57d6675 commit e7e0803
Showing 1 changed file with 47 additions and 29 deletions.
76 changes: 47 additions & 29 deletions store/zksync/ethereumBalance.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { getBalance } from "@wagmi/core";
import { getBalance, getPublicClient } from "@wagmi/core";

import type { Hash, TokenAmount } from "@/types";
import type { Config } from "@wagmi/core";
import type { Address } from "viem";

import { erc20Abi } from "viem";
import { l1Networks } from "@/data/networks";
import { useEthereumBalanceStore } from "@/store/ethereumBalance";
import { useNetworkStore } from "@/store/network";
Expand All @@ -17,7 +17,7 @@ export const useZkSyncEthereumBalanceStore = defineStore("zkSyncEthereumBalances
const tokensStore = useZkSyncTokensStore();
const { l1Network, selectedNetwork } = storeToRefs(useNetworkStore());
const wagmiConfig = onboardStore.wagmiConfig;
const { account } = storeToRefs(onboardStore);
const { account, network } = storeToRefs(onboardStore);
const { balance: ethereumBalance } = storeToRefs(ethereumBalancesStore);
const { l1Tokens } = storeToRefs(tokensStore);
const searchToken = useSearchtokenStore();
Expand Down Expand Up @@ -55,32 +55,50 @@ export const useZkSyncEthereumBalanceStore = defineStore("zkSyncEthereumBalances
const filterL1tokens = Object.values(l1Tokens.value ?? []).filter(
(e) => e.networkKey === selectedNetwork.value.key || e.address === ETH_TOKEN.l1Address
);
return await Promise.all([
...filterL1tokens.map(async (token) => {
const amount = await getBalance(wagmiConfig as Config, {
address: account.value.address!,
chainId: l1Network.value!.id,
token: token.address === ETH_TOKEN.l1Address ? undefined : (token.address! as Hash),
});
return {
...token,
amount: amount.value.toString(),
};
}),
...(searchTokenBalance.value ?? [])
.filter((token) => !Object.values(l1Tokens.value ?? []).find((e) => e.address === token.address))
.map(async (e) => {
const amount = await getBalance(wagmiConfig as Config, {
address: account.value.address!,
chainId: l1Network.value!.id,
token: e.address === ETH_TOKEN.l1Address ? undefined : (e.address! as Hash),
});
return {
...e,
amount: amount.value.toString(),
};
}),
]);

const publicClient = getPublicClient(wagmiConfig as Config, { chainId: l1Network.value?.id });

const ethBalance = await getBalance(wagmiConfig as Config, {
address: account.value.address!,
chainId: l1Network.value!.id,
token: undefined,
});
const erc20Tokens = filterL1tokens.filter((t) => t.address !== ETH_TOKEN.l1Address);
const filterTokenBalances =
(await publicClient?.multicall({
contracts: erc20Tokens.map((item) => ({
address: item.address as Hash,
abi: erc20Abi,
functionName: "balanceOf",
args: [account.value.address!],
})),
})) ?? [];

const searchTokens = (searchTokenBalance.value ?? []).filter(
(token) => !Object.values(l1Tokens.value ?? []).find((e) => e.address === token.address)
);
const searchTokenBalances =
(await publicClient?.multicall({
contracts: searchTokens.map((item) => ({
address: item.address as Hash,
abi: erc20Abi,
functionName: "balanceOf",
args: [account.value.address!],
})),
})) ?? [];

const ethToken = filterL1tokens.find((item) => item.address === ETH_TOKEN.l1Address);
return [
{ ...ethToken!, amount: ethBalance.value.toString() },
...filterTokenBalances.map((item, index) => ({
...erc20Tokens[index],
amount: item.result?.toString() ?? "0",
})),
...searchTokenBalances.map((item, index) => ({
...searchTokens[index],
amount: item.result?.toString() ?? "0",
})),
];
};
let isSaveToken = false,
oldBalance: TokenAmount[];
Expand Down

0 comments on commit e7e0803

Please sign in to comment.