Coinmonks
Published in

Coinmonks

How to get the balance of an ERC-20 token

  • Installing Nodejs on your system
  • Text editor
  • Terminal/Command Line
  • mkdir ERC20Balance
  • cd ERC20Balance.
  1. Connect to the Ethereum node
  2. Write an ABI for the smart contract we want to use to interact with the blockchain.
  3. Find the ERC20 token to get the balance
  4. Find a wallet to get the balance
  5. Put it all together.
const Web3 = require('web3');
const provider = 'https://ethereum.api.watchdata.io/node/jsonrpc?api_key=YOUR_API_KEY';
const web3 = new Web3(new Web3.providers.HttpProvider(provider));
const minABI = [
{
constant: true,
inputs: [{ name: "_owner", type: "address" }],
name: "balanceOf",
outputs: [{ name: "balance", type: "uint256" }],
type: "function",
},
];
console.log('Hello, World.');curl --request POST \
const Web3 = require('web3');
const provider = 'https://ethereum.api.watchdata.io/node/jsonrpc?api_key='YOUR_API_KEY';
const web3 = new Web3(new Web3.providers.HttpProvider(provider));
const token = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48';
const wallet = '0x2e91728aF3a54aCDCeD7938fE9016aE2cc5948C9';
const minABI = [
{
constant: true,
inputs: [{ name: "_owner", type: "address" }],
name: "balanceOf",
outputs: [{ name: "balance", type: "uint256" }],
type: "function",
},
];
const contract = new web3.eth.Contract(minABI, token);
const getBalance = async () => {
const res = await contract.methods.balanceOf(wallet).call();
const format = web3.utils.fromWei(res);
console.log(format);
}
getBalance();

HELPFUL LINKS

Join Coinmonks Telegram Channel and Youtube Channel learn about crypto trading and investing

Also, Read

--

--

Coinmonks (http://coinmonks.io/) is a non-profit Crypto Educational Publication. Follow us on Twitter @coinmonks and Our other project —  https://coincodecap.com, Email  — gaurav@coincodecap.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store