Tokens

如何使用 etherjs 獲取賬戶的 ERC-20 代幣餘額?

  • June 14, 2021

我想使用 ethers.js 獲取帳戶的 ERC-20 代幣餘額。你可以幫幫我嗎?

您可以執行以下操作:

const ethers = require('ethers');
const genericErc20Abi = require(..../.../Erc20.json);
const tokenContractAddress = '0x...';
const provider = ...; (use ethers.providers.InfuraProvider for a Node app or ethers.providers.Web3Provider(window.ethereum/window.web3) for a React app)
const contract = new ethers.Contract(tokenContractAddress, genericErc20Abi, provider);
const balance = (await contract.balanceOf((await provider.getSigners())[0].address)).toString();

引用自:https://ethereum.stackexchange.com/questions/101858