Created
August 10, 2023 17:28
-
-
Save samsonajulor/b63af254b7f22174e529c3c835bf7b75 to your computer and use it in GitHub Desktop.
This file contains 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 { ethers } from 'ethers'; | |
async function getTokenInfo(tokenAddress: string, walletAddress: string) { | |
const provider = ethers.getDefaultProvider('homestead'); | |
// Create a contract instance for the USDC token using the ERC-20 ABI | |
const usdcToken = new ethers.Contract( | |
tokenAddress, | |
[ | |
'function name() view returns (string)', | |
'function symbol() view returns (string)', | |
'function balanceOf(address account) view returns (uint256)', | |
'function totalSupply() view returns (uint256)', | |
'function decimals() view returns (uint8)', | |
], | |
provider | |
); | |
const name = await usdcToken.name(); | |
const symbol = await usdcToken.symbol(); | |
const balance = await usdcToken.balanceOf(walletAddress); | |
const totalSupply = await usdcToken.totalSupply(); | |
const decimals = await usdcToken.decimals(); | |
return { | |
name, | |
symbol, | |
balance: balance.toString(), | |
totalSupply: totalSupply.toString(), | |
decimals, | |
}; | |
} | |
(async () => { | |
const usdcAddress = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'; // USDC token address on Mainnet | |
const walletAddress = '0x67147840C8eca35C819C7523861E6A1D75D93200'; | |
try { | |
const tokenInfo = await getTokenInfo(usdcAddress, walletAddress); | |
console.log('Token Name:', tokenInfo.name); | |
console.log('Token Symbol:', tokenInfo.symbol); | |
console.log('Your USDC Balance:', tokenInfo.balance); | |
console.log('Total Supply:', tokenInfo.totalSupply); | |
console.log('Token Decimals:', tokenInfo.decimals); | |
return { | |
tokenInfo, | |
} | |
} catch (error) { | |
console.error('Error:', error); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment