-
-
Save Phonbopit/7e215fb5161e7bb44aa83f23aad03ab7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { | |
| createPublicClient, | |
| http, | |
| formatUnits, | |
| parseAbi, | |
| type Address, | |
| } from 'viem' | |
| import { mainnet } from 'viem/chains' | |
| const erc20Abi = parseAbi([ | |
| 'function balanceOf(address _owner) view returns (uint256 balance)', | |
| 'function decimals() view returns (uint8)', | |
| 'function symbol() view returns (string)', | |
| ]) | |
| const client = createPublicClient({ | |
| chain: mainnet, | |
| transport: http(), | |
| }) | |
| interface Token { | |
| address: Address | |
| symbol: string | |
| decimals: number | |
| } | |
| interface TokenBalance { | |
| symbol: string | |
| address: Address | |
| balance: string | |
| rawBalance?: string | |
| error?: string | |
| } | |
| const tokens: Token[] = [ | |
| { | |
| address: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48', | |
| symbol: 'USDC', | |
| decimals: 6, | |
| }, | |
| { | |
| address: '0xdAC17F958D2ee523a2206206994597C13D831ec7', | |
| symbol: 'USDT', | |
| decimals: 6, | |
| }, | |
| { | |
| address: '0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599', | |
| symbol: 'WBTC', | |
| decimals: 8, | |
| }, | |
| { | |
| address: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', | |
| symbol: 'WETH', | |
| decimals: 18, | |
| }, | |
| { | |
| address: '0xB8c77482e45F1F44dE1745F52C74426C631bDD52', | |
| symbol: 'BNB', | |
| decimals: 18, | |
| }, | |
| ] | |
| async function getTokenBalances( | |
| walletAddress: Address, | |
| ): Promise<TokenBalance[]> { | |
| console.log(`Getting token balances for wallet: ${walletAddress}\n`) | |
| const balances = await Promise.all( | |
| tokens.map(async (token) => { | |
| try { | |
| const balance = await client.readContract({ | |
| address: token.address, | |
| abi: erc20Abi, | |
| functionName: 'balanceOf', | |
| args: [walletAddress], | |
| }) | |
| const formattedBalance = formatUnits(balance, token.decimals) | |
| return { | |
| symbol: token.symbol, | |
| address: token.address, | |
| balance: formattedBalance, | |
| rawBalance: balance.toString(), | |
| } | |
| } catch (error) { | |
| return { | |
| symbol: token.symbol, | |
| address: token.address, | |
| balance: 'Error', | |
| error: error.message, | |
| } | |
| } | |
| }), | |
| ) | |
| return balances | |
| } | |
| async function main(): Promise<void> { | |
| // Example wallet address (Vitalik's public address) | |
| const walletAddress: Address = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045' | |
| try { | |
| const balances = await getTokenBalances(walletAddress) | |
| console.log('Token Balances:') | |
| console.log('===============') | |
| balances.forEach((token) => { | |
| if (token.error) { | |
| console.log(`${token.symbol}: Error - ${token.error}`) | |
| } else { | |
| console.log(`${token.symbol}: ${token.balance}`) | |
| } | |
| }) | |
| } catch (error) { | |
| console.error('Error fetching balances:', error) | |
| } | |
| } | |
| main().catch(console.error) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment