简体   繁体   中英

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

  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 mai.net, the node needs to be on Ethereum mai.net 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM