Skip to content

Commit

Permalink
fix: keep eth in the top of the tokens list
Browse files Browse the repository at this point in the history
  • Loading branch information
JackHamer09 committed Feb 29, 2024
1 parent d974f2f commit 1511bdf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
10 changes: 7 additions & 3 deletions store/zksync/ethereumBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ export const useZkSyncEthereumBalanceStore = defineStore("zkSyncEthereumBalances
...e,
amount: "0",
})),
];
].sort((a, b) => {
if (a.address === ETH_TOKEN.l1Address) return -1; // Always bring ETH to the beginning
if (b.address === ETH_TOKEN.l1Address) return 1; // Keep ETH at the beginning if comparing with any other token
return 0; // Keep other tokens' order unchanged
});
};
const getBalancesFromRPC = async (): Promise<TokenAmount[]> => {
await tokensStore.requestTokens();
Expand Down Expand Up @@ -76,9 +80,9 @@ export const useZkSyncEthereumBalanceStore = defineStore("zkSyncEthereumBalances
([l1Networks.mainnet.id, l1Networks.goerli.id] as number[]).includes(l1Network.value?.id) &&
runtimeConfig.public.ankrToken
) {
return getBalancesFromApi();
return await getBalancesFromApi();
} else {
return getBalancesFromRPC();
return await getBalancesFromRPC();
}
},
{ cache: 30000 }
Expand Down
14 changes: 10 additions & 4 deletions store/zksync/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,16 @@ export const useZkSyncWalletStore = defineStore("zkSyncWallet", () => {
const balance = computed<TokenAmount[]>(() => {
if (!balancesResult.value) return [];

const knownTokens: TokenAmount[] = Object.entries(tokens.value ?? {}).map(([, token]) => {
const amount = balancesResult.value!.find((e) => e.address === token.address)?.amount ?? "0";
return { ...token, amount };
});
const knownTokens: TokenAmount[] = Object.entries(tokens.value ?? {})
.map(([, token]) => {
const amount = balancesResult.value!.find((e) => e.address === token.address)?.amount ?? "0";
return { ...token, amount };
})
.sort((a, b) => {
if (a.address === ETH_TOKEN.address) return -1; // Always bring ETH to the beginning
if (b.address === ETH_TOKEN.address) return 1; // Keep ETH at the beginning if comparing with any other token
return 0; // Keep other tokens' order unchanged
});
const knownTokenAddresses = new Set(knownTokens.map((token) => token.address));

// Filter out the tokens in `balancesResult` that are not in `tokens`
Expand Down

0 comments on commit 1511bdf

Please sign in to comment.