'How can we get Data from web3 using Nodejs

how can I get the token details like name, symbol, and decimals details using Nodejs and web3js



Solution 1:[1]

  1. You'll need to be connected to a node on the same network where the token is deployed. For example if you want to get token info from a token on Ethereum mainnet, the node needs to be on Ethereum mainnet as well.

  2. Then you'll need the token contract address.

  3. And finally the ABI JSON of the contract that you want to interact with. Since name, symbol and decimals are standardized functions defined in the ERC-20 standard, you can use a generic ERC-20 ABI JSON for that.

const Web3 = require("web3");
// A node provider connected to the Ethereum mainnet
const web3 = new Web3("https://mainnet.infura.io/v3/<api_key>");

// Generic ERC-20 ABI JSON
const ABI = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"payable":true,"stateMutability":"payable","type":"fallback"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}];

// The token contract address
const ADDRESS = "0xdAC17F958D2ee523a2206206994597C13D831ec7";

async function run() {
    const contract = new web3.eth.Contract(ABI, ADDRESS);
    const name = await contract.methods.name().call();
    console.log("name:", name);
    const symbol = await contract.methods.symbol().call();
    console.log("symbol:", symbol);
    const decimals = await contract.methods.decimals().call();
    console.log("decimals:", decimals);
}

run();

Output:

name: Tether USD
symbol: USDT
decimals: 6

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Petr Hejda