diff --git a/src/config/tokens/index.ts b/src/config/tokens/index.ts index dab69eb0..39c263c2 100644 --- a/src/config/tokens/index.ts +++ b/src/config/tokens/index.ts @@ -1,28 +1,28 @@ import { usdcEvm, usdcNear, usdcSol, usdcChains, usdcAptos } from "@/config/tokens/usdc"; import { usdtAptos, usdtEvm, usdtNear, usdtSol, usdtTron, usdtChains } from "@/config/tokens/usdt"; -export const evmBalancesTokens = (() => { +const evmTokenChains = [ + usdcEvm.chains, + usdtEvm.chains, +]; +export const evmBalancesTokens: { chain_id: number; tokens: string[]; decimals: number[]; }[] = (() => { const map: any = {}; - usdcEvm.chains.forEach((chain: any) => { - if (map[chain.chainName]) { - map[chain.chainName].tokens.push(chain.contractAddress); - } else { - map[chain.chainName] = { - chain_id: chain.chainId, - tokens: [chain.contractAddress] - }; - } - }); - usdtEvm.chains.forEach((chain: any) => { - if (map[chain.chainName]) { - map[chain.chainName].tokens.push(chain.contractAddress); - } else { - map[chain.chainName] = { - chain_id: chain.chainId, - tokens: [chain.contractAddress] - }; - } - }); + for (const chains of evmTokenChains) { + chains.forEach((chain: any) => { + if (map[chain.chainName]) { + map[chain.chainName].tokens.push(chain.contractAddress); + map[chain.chainName].decimals.push(chain.decimals); + map[chain.chainName].symbols.push(chain.symbol); + } else { + map[chain.chainName] = { + chain_id: chain.chainId, + tokens: [chain.contractAddress], + decimals: [chain.decimals], + symbols: [chain.symbol], + }; + } + }); + } return Object.values(map); })(); diff --git a/src/hooks/use-evm-balances.ts b/src/hooks/use-evm-balances.ts index 9537d52d..c9e1e10d 100644 --- a/src/hooks/use-evm-balances.ts +++ b/src/hooks/use-evm-balances.ts @@ -35,6 +35,43 @@ export default function useEvmBalances(auto = false) { const _balances: any = {}; const _data = res.data.data; + const setBalances = (__data: any) => { + let usdcBalance = Big(0); + let usdtBalance = Big(0); + + Object.entries(__data).forEach(([key, item]: any) => { + if (!item) return; + const currentTokenChain = evmBalancesTokens.find((token) => Number(token.chain_id) === Number(key)); + item.forEach((sl: any) => { + const currentTokenIndex = currentTokenChain?.tokens?.map?.((address) => address.toLowerCase())?.indexOf?.(sl.address.toLowerCase()); + let currentTokenDecimals = 6; + if (currentTokenChain && typeof currentTokenIndex === "number" && currentTokenIndex > -1) { + currentTokenDecimals = currentTokenChain.decimals[currentTokenIndex]; + } + const _balance = Big(sl.balance).div(10 ** currentTokenDecimals); + if (usdcAddresses.includes(sl.address)) { + usdcBalance = usdcBalance.plus(_balance); + } + if (usdtAddresses.includes(sl.address)) { + usdtBalance = usdtBalance.plus(_balance); + } + _balances[sl.address] = _balance.toString(); + }); + }); + + if (wallet?.account) { + balancesStore.set({ + evmBalances: { + ..._balances, + usdcBalance: usdcBalance.toString(), + usdtBalance: usdtBalance.toString() + } + }); + } + }; + + setBalances(_data); + const unsupportedChainIds = evmBalancesTokens.map((token: any) => Object.keys(_data).includes(token.chain_id.toString()) ? null : token.chain_id).filter(Boolean); const unsupportedBalances: any = {}; // get unsupported tokens balances from provider @@ -69,8 +106,9 @@ export default function useEvmBalances(auto = false) { return _result; }); - const balances = await Promise.all(balancePromises); - unsupportedBalances[_token.chain_id] = balances; + const balances = await Promise.allSettled(balancePromises); + const validBalances = balances.filter((balance) => balance.status === "fulfilled").map((balance) => balance.value); + unsupportedBalances[_token.chain_id] = validBalances; } } @@ -80,34 +118,7 @@ export default function useEvmBalances(auto = false) { } } - let usdcBalance = Big(0); - let usdtBalance = Big(0); - - Object.entries(_data).forEach(([key, item]: any) => { - if (!item) return; - item.forEach((sl: any) => { - const _balance = Big(sl.balance).div( - 10 ** (Number(key) === 56 ? 18 : 6) - ); - if (usdcAddresses.includes(sl.address)) { - usdcBalance = usdcBalance.plus(_balance); - } - if (usdtAddresses.includes(sl.address)) { - usdtBalance = usdtBalance.plus(_balance); - } - _balances[sl.address] = _balance.toString(); - }); - }); - - if (wallet?.account) { - balancesStore.set({ - evmBalances: { - ..._balances, - usdcBalance: usdcBalance.toString(), - usdtBalance: usdtBalance.toString() - } - }); - } + setBalances(_data); setLoading(false); } catch (error) { diff --git a/src/sections/wallet/total.tsx b/src/sections/wallet/total.tsx index dccefcd8..91fcbe7d 100644 --- a/src/sections/wallet/total.tsx +++ b/src/sections/wallet/total.tsx @@ -29,7 +29,7 @@ export default function Total() { if (!key.includes("Balances")) return; const chainType = key.split("Balances")[0]; const currentChain = chainTypes[chainType]; - const currentTokenWithChains = stablecoinWithChains[chainType][walletStore.selectedToken]; + const currentTokenWithChains = stablecoinWithChains[chainType]?.[walletStore.selectedToken]; _balanceSummaries[chainType] = { balance: Big(0), balanceString: "0.00", @@ -104,7 +104,7 @@ export default function Total() { _balanceSummariesListWithBalance.length, finalPercentages.map(percent => percent + "%").join(" ") ]; - }, [balancesStore]); + }, [balancesStore, walletStore.selectedToken]); return (