Skip to content

Instantly share code, notes, and snippets.

@syshen
Created February 15, 2023 10:34
Show Gist options
  • Save syshen/babb002a1da35f52c2f30bb656c98d95 to your computer and use it in GitHub Desktop.
Save syshen/babb002a1da35f52c2f30bb656c98d95 to your computer and use it in GitHub Desktop.
Retrieve the balance of an ERC20 token with the block number
const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider('<Node API endpoint>'));
const balanceOfABI = [
{
"constant": true,
"inputs": [
{
"name": "_owner",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"name": "balance",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
];
const usdt = new web3.eth.Contract(balanceOfABI, '0xdAC17F958D2ee523a2206206994597C13D831ec7');
async function getUSDTBalance(walletAddress) {
const result = await usdt.methods.balanceOf(walletAddress).call({}, '16632997');
console.log(result);
}
getUSDTBalance('0xDFd5293D8e347dFe59E90eFd55b2956a1343963d').then(console.log).catch(console.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment