gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 05:41:46 26 4
gpt4 key购买 nike

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

最佳答案

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

  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

关于javascript - 我们如何使用Nodejs从web3获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72233013/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com