我们如何使用Nodejs从web3获取数据

How can we get Data from web3 using Nodejs

如何使用 Nodejs 和 web3js 获取代币详细信息,例如名称、符号和小数位详细信息

  1. 您需要连接到部署令牌的同一网络上的节点。例如,如果您想从以太坊主网上的令牌获取令牌信息,则该节点也需要在以太坊主网上。

  2. 然后你需要代币合约地址。

  3. 最后是您要与之交互的合约的 ABI JSON。由于 namesymboldecimalsERC-20 标准中定义的标准化函数,您可以为此使用通用的 ERC-20 ABI JSON。

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();

输出:

name: Tether USD
symbol: USDT
decimals: 6