From e35f14aaf47b788ffc1071521cbbf2c0171eb556 Mon Sep 17 00:00:00 2001 From: fborello-lambda Date: Mon, 5 Aug 2024 13:46:57 -0300 Subject: [PATCH 1/2] feat(base-token): automatic fetching --- store/zksync/tokens.ts | 58 +++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 18 deletions(-) diff --git a/store/zksync/tokens.ts b/store/zksync/tokens.ts index 19f54f425..89190913d 100644 --- a/store/zksync/tokens.ts +++ b/store/zksync/tokens.ts @@ -1,5 +1,7 @@ +import * as ethers from "ethers"; import { $fetch } from "ofetch"; import { utils } from "zksync-ethers"; +import { IERC20 } from "zksync-ethers/build/utils"; import { customBridgeTokens } from "@/data/customBridgeTokens"; @@ -37,24 +39,16 @@ export const useZkSyncTokensStore = defineStore("zkSyncTokens", () => { if (eraNetwork.value.getTokens && (!baseToken || !ethToken)) { configTokens = await eraNetwork.value.getTokens(); - if (!baseToken) { - baseToken = configTokens.find((token) => token.address === L2_BASE_TOKEN_ADDRESS); - } - if (!ethToken) { - ethToken = configTokens.find((token) => token.address === ethL2TokenAddress); + if (configTokens) { + if (!baseToken) { + baseToken = configTokens.find((token) => token.address === L2_BASE_TOKEN_ADDRESS); + } + if (!ethToken) { + ethToken = configTokens.find((token) => token.address === ethL2TokenAddress); + } } } - if (!baseToken) { - baseToken = { - address: "0x000000000000000000000000000000000000800A", - l1Address: await provider.getBaseTokenContractAddress(), - symbol: "BASETOKEN", - name: "Base Token", - decimals: 18, - iconUrl: "/img/eth.svg", - }; - } if (!ethToken) { ethToken = { address: ethL2TokenAddress, @@ -66,10 +60,38 @@ export const useZkSyncTokensStore = defineStore("zkSyncTokens", () => { }; } + const btL1Address = await provider.getBaseTokenContractAddress(); + if (btL1Address !== utils.ETH_ADDRESS) { + const l1Rpc = useNetworkStore(); + const l1Provider = new ethers.providers.JsonRpcProvider(l1Rpc.l1Network?.rpcUrls.default.http[0]); + const walletEthers = ethers.Wallet.createRandom(); + const connectedWallet = walletEthers.connect(l1Provider); + const ERC20_L1 = new ethers.Contract(btL1Address, IERC20, connectedWallet); + const ERC20_SYMBOL: string = (await ERC20_L1.symbol()) || "BT"; + const ERC20_DECIMALS = (await ERC20_L1.decimals()) || 18; + if (!baseToken) { + baseToken = { + address: L2_BASE_TOKEN_ADDRESS, + l1Address: btL1Address, + symbol: ERC20_SYMBOL, + name: ERC20_SYMBOL, + decimals: ERC20_DECIMALS, + iconUrl: "/img/era.svg", + }; + } + } + + if (!baseToken) { + baseToken = ethToken; + } + const tokens = explorerTokens.length ? explorerTokens : configTokens; - const nonBaseOrEthExplorerTokens = tokens.filter( - (token) => token.address !== L2_BASE_TOKEN_ADDRESS && token.address !== ethL2TokenAddress - ); + let nonBaseOrEthExplorerTokens: Token[] = []; + if (tokens) { + nonBaseOrEthExplorerTokens = tokens.filter( + (token) => token.address !== L2_BASE_TOKEN_ADDRESS && token.address !== ethL2TokenAddress + ); + } return [ baseToken, ...(baseToken.address !== ethToken.address ? [ethToken] : []), From 7a264848780309a5621af57b7832411d97aa40f1 Mon Sep 17 00:00:00 2001 From: fborello-lambda Date: Mon, 5 Aug 2024 15:03:01 -0300 Subject: [PATCH 2/2] refactor: only ask the rpc if everything fails --- store/zksync/tokens.ts | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/store/zksync/tokens.ts b/store/zksync/tokens.ts index 89190913d..8104e7eb2 100644 --- a/store/zksync/tokens.ts +++ b/store/zksync/tokens.ts @@ -60,16 +60,16 @@ export const useZkSyncTokensStore = defineStore("zkSyncTokens", () => { }; } - const btL1Address = await provider.getBaseTokenContractAddress(); - if (btL1Address !== utils.ETH_ADDRESS) { - const l1Rpc = useNetworkStore(); - const l1Provider = new ethers.providers.JsonRpcProvider(l1Rpc.l1Network?.rpcUrls.default.http[0]); - const walletEthers = ethers.Wallet.createRandom(); - const connectedWallet = walletEthers.connect(l1Provider); - const ERC20_L1 = new ethers.Contract(btL1Address, IERC20, connectedWallet); - const ERC20_SYMBOL: string = (await ERC20_L1.symbol()) || "BT"; - const ERC20_DECIMALS = (await ERC20_L1.decimals()) || 18; - if (!baseToken) { + if (!baseToken) { + const btL1Address = await provider.getBaseTokenContractAddress(); + if (btL1Address !== utils.ETH_ADDRESS) { + const l1Rpc = useNetworkStore(); + const l1Provider = new ethers.providers.JsonRpcProvider(l1Rpc.l1Network?.rpcUrls.default.http[0]); + const walletEthers = ethers.Wallet.createRandom(); + const connectedWallet = walletEthers.connect(l1Provider); + const ERC20_L1 = new ethers.Contract(btL1Address, IERC20, connectedWallet); + const ERC20_SYMBOL: string = (await ERC20_L1.symbol()) || "BT"; + const ERC20_DECIMALS = (await ERC20_L1.decimals()) || 18; baseToken = { address: L2_BASE_TOKEN_ADDRESS, l1Address: btL1Address, @@ -78,13 +78,11 @@ export const useZkSyncTokensStore = defineStore("zkSyncTokens", () => { decimals: ERC20_DECIMALS, iconUrl: "/img/era.svg", }; + } else { + baseToken = ethToken; } } - if (!baseToken) { - baseToken = ethToken; - } - const tokens = explorerTokens.length ? explorerTokens : configTokens; let nonBaseOrEthExplorerTokens: Token[] = []; if (tokens) {