|
| 1 | +import { SupportedNetworkNames, TokenBalance } from "./types/tokenbalance-mew"; |
| 2 | +import { NetworkEndpoints } from "@/providers/ethereum/libs/activity-handlers/providers/etherscan/configs"; |
| 3 | +import { NATIVE_TOKEN_ADDRESS } from "../common"; |
| 4 | +import { numberToHex } from "web3-utils"; |
| 5 | + |
| 6 | +interface TokenBalanceType { |
| 7 | + token: string; |
| 8 | + quantity: string; |
| 9 | + error?: unknown; |
| 10 | +} |
| 11 | + |
| 12 | +interface TokenResponseItem { |
| 13 | + token: { |
| 14 | + address: string; |
| 15 | + decimals: string; |
| 16 | + name: string; |
| 17 | + symbol: string; |
| 18 | + }; |
| 19 | + value: string; |
| 20 | +} |
| 21 | + |
| 22 | +interface TokenResponse { |
| 23 | + items: TokenResponseItem[]; |
| 24 | +} |
| 25 | + |
| 26 | +const getBlockscoutBalances = ( |
| 27 | + chain: SupportedNetworkNames, |
| 28 | + address: string |
| 29 | +): Promise<TokenBalance[]> => { |
| 30 | + const encodedAddress = encodeURIComponent(address); |
| 31 | + const nativeTokenUrl = `${NetworkEndpoints[chain]}api/v2/addresses/${encodedAddress}`; |
| 32 | + const tokenBalancesUrl = `${NetworkEndpoints[chain]}api/v2/addresses/${encodedAddress}/tokens?type=ERC-20`; |
| 33 | + |
| 34 | + return Promise.all([ |
| 35 | + fetch(nativeTokenUrl).then((res) => res.json()), |
| 36 | + fetch(tokenBalancesUrl).then((res) => res.json()), |
| 37 | + ]) |
| 38 | + .then(([nativeResponse, tokenResponse]: [any, TokenResponse]) => { |
| 39 | + if (!nativeResponse?.coin_balance || !tokenResponse?.items) { |
| 40 | + return Promise.reject("Error fetching balance data"); |
| 41 | + } |
| 42 | + |
| 43 | + if (Number.isNaN(Number(nativeResponse.coin_balance))) { |
| 44 | + return Promise.reject("Invalid native token balance"); |
| 45 | + } |
| 46 | + |
| 47 | + // Map native token balance |
| 48 | + const nativeBalance: TokenBalanceType = { |
| 49 | + token: NATIVE_TOKEN_ADDRESS, |
| 50 | + quantity: nativeResponse.coin_balance, |
| 51 | + }; |
| 52 | + |
| 53 | + // Map token balances |
| 54 | + const tokenBalances: TokenBalanceType[] = Array.isArray( |
| 55 | + tokenResponse?.items |
| 56 | + ) |
| 57 | + ? tokenResponse.items |
| 58 | + .filter( |
| 59 | + (item) => |
| 60 | + item?.token?.address && |
| 61 | + typeof item.token.address === "string" && |
| 62 | + item.value !== undefined |
| 63 | + ) |
| 64 | + .map((item) => ({ |
| 65 | + token: item.token.address.toLowerCase(), |
| 66 | + quantity: item.value, |
| 67 | + })) |
| 68 | + : []; |
| 69 | + |
| 70 | + // Merge native token and token balances |
| 71 | + const allBalances = [nativeBalance, ...tokenBalances]; |
| 72 | + |
| 73 | + // Convert to TokenBalance format |
| 74 | + return allBalances.map((tb) => ({ |
| 75 | + contract: tb.token, |
| 76 | + balance: numberToHex(tb.quantity), // Convert to hex format |
| 77 | + })); |
| 78 | + }) |
| 79 | + .catch((error) => { |
| 80 | + console.error("Error fetching balances:", error); |
| 81 | + throw error; |
| 82 | + }); |
| 83 | +}; |
| 84 | + |
| 85 | +export default getBlockscoutBalances; |
0 commit comments