const axios = require('axios');
// Replace this with your actual Etherscan API key
const apiKey = 'YOUR_ETHERSCAN_API_KEY';
// Replace this with the Ethereum address you want to query
const address = '0xYourEthereumAddress';
// Replace this with the contract address of the ERC-20 token
const contractAddress = '0xYourTokenContractAddress';
// Etherscan API URL to fetch the ERC-20 token balance
const url = `https://api.etherscan.io/api?module=account&action=tokenbalance&contractaddress=${contractAddress}&address=${address}&tag=latest&apikey=${apiKey}`;
async function getTokenBalance() {
try {
// Make the API request to Etherscan
const response = await axios.get(url);
const tokenBalance = response.data.result;
// Log the token balance (Note: Token balances are often in very small denominations)
console.log(`Token Balance: ${tokenBalance}`);
} catch (error) {
console.error('Error fetching token balance:', error);
}
}
// Call the function to get the token balance
getTokenBalance();
- Replace
'YOUR_ETHERSCAN_API_KEY'
with your Etherscan API key. - Replace
'0xYourEthereumAddress'
with the address you want to query. - Replace
'0xYourTokenContractAddress'
with the ERC-20 token's contract address (e.g., USDT or DAI token contract). - This script queries the ERC-20 token balance for a specific Ethereum address.