Token Balance Development Tutorials, Guides & Insights
Unlock 1+ expert-curated token balance tutorials, real-world code snippets, and modern dev strategies. From fundamentals to advanced topics, boost your token balance skills on DeveloperBreeze.
Adblocker Detected
It looks like you're using an adblocker. Our website relies on ads to keep running. Please consider disabling your adblocker to support us and access the content.
How to Query ERC-20 Token Balances and Transactions Using Ethers.js and Etherscan API
Tutorial October 24, 2024
Now let’s query the ERC-20 token transfer history for a specific wallet address using the Etherscan API.
const axios = require('axios');
// Replace with your Etherscan API key
const apiKey = 'YOUR_ETHERSCAN_API_KEY';
// Replace with the wallet address you want to query
const address = '0xYourEthereumAddress';
// Replace with the ERC-20 token contract address
const contractAddress = '0xTokenContractAddress';
// Etherscan API URL to fetch ERC-20 token transactions
const url = `https://api.etherscan.io/api?module=account&action=tokentx&contractaddress=${contractAddress}&address=${address}&startblock=0&endblock=99999999&sort=asc&apikey=${apiKey}`;
async function getTokenTransactions() {
try {
// Make the API request to Etherscan
const response = await axios.get(url);
const transactions = response.data.result;
// Log the token transactions
transactions.forEach(tx => {
console.log(`
From: ${tx.from}
To: ${tx.to}
Value: ${ethers.utils.formatUnits(tx.value, 18)} Tokens
Transaction Hash: ${tx.hash}
`);
});
} catch (error) {
console.error('Error fetching token transactions:', error);
}
}
// Call the function to get the token transactions
getTokenTransactions();