Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(base-token): automatic fetching #187

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 38 additions & 18 deletions store/zksync/tokens.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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,
Expand All @@ -66,10 +60,36 @@ export const useZkSyncTokensStore = defineStore("zkSyncTokens", () => {
};
}

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,
symbol: ERC20_SYMBOL,
name: ERC20_SYMBOL,
decimals: ERC20_DECIMALS,
iconUrl: "/img/era.svg",
};
} else {
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] : []),
Expand Down