Skip to content

Commit

Permalink
use multicall to getBalance
Browse files Browse the repository at this point in the history
  • Loading branch information
MickWang committed Apr 7, 2024
1 parent d1c552f commit ed870c7
Showing 1 changed file with 64 additions and 21 deletions.
85 changes: 64 additions & 21 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, multicall } 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,75 @@ 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) => {
console.log("chain: ", l1Network.value, network.value);
if (l1Network.value?.id === network.value.chainId) {
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 multicall(wagmiConfig as Config, {
contracts: erc20Tokens.map((item) => ({
address: item.address as Hash,
abi: erc20Abi,
functionName: "balanceOf",
args: [account.value.address!],
})),
});
console.log("filterTokenBalances: ", filterTokenBalances);
const searchTokens = (searchTokenBalance.value ?? []).filter(
(token) => !Object.values(l1Tokens.value ?? []).find((e) => e.address === token.address)
);
const searchTokenBalances = await multicall(wagmiConfig as Config, {
contracts: searchTokens.map((item) => ({
address: item.address as Hash,
abi: erc20Abi,
functionName: "balanceOf",
args: [account.value.address!],
})),
});
console.log("searchTokenBalances: ", searchTokenBalances);
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",
})),
];
} else {
return await Promise.all([
...filterL1tokens.map(async (token) => {
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),
token: token.address === ETH_TOKEN.l1Address ? undefined : (token.address! as Hash),
});
return {
...e,
...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(),
};
}),
]);
}
};
let isSaveToken = false,
oldBalance: TokenAmount[];
Expand Down

0 comments on commit ed870c7

Please sign in to comment.