Reputation: 27
Good day! Can you please tell me how you can get the balance of the USDT address? I found a way to get BNB balance (https://docs.binance.org/smart-chain/developer/BEP20.html), but there is no example for USDT
Upvotes: 1
Views: 8540
Reputation: 620
Fetch USDT balance on BNB chain using BSC scan API
const axios = require('axios');
const bscApiKey = 'YOUR_BSCSCAN_API_KEY'; // Replace with your BSCScan API key
const targetAddress = 'TARGET_ADDRESS'; // Replace with the address you want to query
const usdtContractAddress = '0x337610d27c682E347C9cD60BD4b3b107C9d34dDd'; // USDT BEP-20 contract address on BSC
const bscScanApiUrl = `https://api.bscscan.com/api?module=account&action=tokenbalance&contractaddress=${usdtContractAddress}&address=${targetAddress}&tag=latest&apikey=${bscApiKey}`;
async function fetchUSDTBalance() {
try {
const response = await axios.get(bscScanApiUrl);
const usdtBalance = response.data.result;
console.log(`USDT Balance of ${targetAddress}: ${usdtBalance}`);
} catch (error) {
console.error('Error:', error.message);
}
}
Upvotes: 0
Reputation: 43581
Token balance of an address is not a property of the address. It is stored in each of the token's contract. So you need to call the balanceOf()
function of the token contract, passing it the holder address as a parameter.
For example the BUSD token:
const busdAddress = "0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56";
const holderAddress = "0x8894e0a0c962cb723c1976a4421c95949be2d4e3";
// just the `balanceOf()` is sufficient in this case
const abiJson = [
{"constant":true,"inputs":[{"name":"who","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},
];
const contract = new web3.eth.Contract(abiJson, busdAddress);
const balance = await contract.methods.balanceOf(holderAddress).call();
// note that this number includes the decimal places (in case of BUSD, that's 18 decimal places)
console.log(balance);
Upvotes: 3