使用web3js从adress获取指定令牌的余额



我想获得指定代币费用的余额。仅来自给定地址的ETH。我不知道如何实现这一点。web3文档对我帮助不大。

您可以尝试此操作,首先使用web3:连接到提供商

let web3 = new Web3('provider url');

我建议您使用快速节点。

然后创建一个令牌ABI的常量,这样您就可以使用合约的所有方法:

const tokenAbi = [Token ABI code]

如果你想的话,你可以使用这个标准:

const tokenAbi = [
{
inputs: [
{ internalType: 'string', name: '_name', type: 'string' },
{ internalType: 'string', name: '_symbol', type: 'string' },
{ internalType: 'uint256', name: '_decimals', type: 'uint256' },
{ internalType: 'uint256', name: '_supply', type: 'uint256' },
{ internalType: 'uint256', name: '_txFee', type: 'uint256' },
{ internalType: 'uint256', name: '_burnFee', type: 'uint256' },
{ internalType: 'uint256', name: '_charityFee', type: 'uint256' },
{ internalType: 'address', name: '_FeeAddress', type: 'address' },
{ internalType: 'address', name: 'tokenOwner', type: 'address' },
],
stateMutability: 'nonpayable',
type: 'constructor',
},
{
anonymous: false,
inputs: [
{ indexed: true, internalType: 'address', name: 'owner', type: 'address' },
{ indexed: true, internalType: 'address', name: 'spender', type: 'address' },
{ indexed: false, internalType: 'uint256', name: 'value', type: 'uint256' },
],
name: 'Approval',
type: 'event',
},
{
anonymous: false,
inputs: [
{ indexed: true, internalType: 'address', name: 'previousOwner', type: 'address' },
{ indexed: true, internalType: 'address', name: 'newOwner', type: 'address' },
],
name: 'OwnershipTransferred',
type: 'event',
},
{
anonymous: false,
inputs: [
{ indexed: true, internalType: 'address', name: 'from', type: 'address' },
{ indexed: true, internalType: 'address', name: 'to', type: 'address' },
{ indexed: false, internalType: 'uint256', name: 'value', type: 'uint256' },
],
name: 'Transfer',
type: 'event',
},
{ inputs: [], name: 'FeeAddress', outputs: [{ internalType: 'address', name: '', type: 'address' }], stateMutability: 'view', type: 'function' },
{ inputs: [], name: '_BURN_FEE', outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }], stateMutability: 'view', type: 'function' },
{ inputs: [], name: '_CHARITY_FEE', outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }], stateMutability: 'view', type: 'function' },
{ inputs: [], name: '_TAX_FEE', outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }], stateMutability: 'view', type: 'function' },
{ inputs: [], name: '_owner', outputs: [{ internalType: 'address', name: '', type: 'address' }], stateMutability: 'view', type: 'function' },
{
inputs: [
{ internalType: 'address', name: 'owner', type: 'address' },
{ internalType: 'address', name: 'spender', type: 'address' },
],
name: 'allowance',
outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
{
inputs: [
{ internalType: 'address', name: 'spender', type: 'address' },
{ internalType: 'uint256', name: 'amount', type: 'uint256' },
],
name: 'approve',
outputs: [{ internalType: 'bool', name: '', type: 'bool' }],
stateMutability: 'nonpayable',
type: 'function',
},
{ inputs: [{ internalType: 'address', name: 'account', type: 'address' }], name: 'balanceOf', outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }], stateMutability: 'view', type: 'function' },
{ inputs: [{ internalType: 'uint256', name: '_value', type: 'uint256' }], name: 'burn', outputs: [], stateMutability: 'nonpayable', type: 'function' },
{ inputs: [], name: 'decimals', outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }], stateMutability: 'view', type: 'function' },
{
inputs: [
{ internalType: 'address', name: 'spender', type: 'address' },
{ internalType: 'uint256', name: 'subtractedValue', type: 'uint256' },
],
name: 'decreaseAllowance',
outputs: [{ internalType: 'bool', name: '', type: 'bool' }],
stateMutability: 'nonpayable',
type: 'function',
},
{ inputs: [{ internalType: 'uint256', name: 'tAmount', type: 'uint256' }], name: 'deliver', outputs: [], stateMutability: 'nonpayable', type: 'function' },
{ inputs: [{ internalType: 'address', name: 'account', type: 'address' }], name: 'excludeAccount', outputs: [], stateMutability: 'nonpayable', type: 'function' },
{ inputs: [{ internalType: 'address', name: 'account', type: 'address' }], name: 'includeAccount', outputs: [], stateMutability: 'nonpayable', type: 'function' },
{
inputs: [
{ internalType: 'address', name: 'spender', type: 'address' },
{ internalType: 'uint256', name: 'addedValue', type: 'uint256' },
],
name: 'increaseAllowance',
outputs: [{ internalType: 'bool', name: '', type: 'bool' }],
stateMutability: 'nonpayable',
type: 'function',
},
{ inputs: [{ internalType: 'address', name: 'account', type: 'address' }], name: 'isCharity', outputs: [{ internalType: 'bool', name: '', type: 'bool' }], stateMutability: 'view', type: 'function' },
{ inputs: [{ internalType: 'address', name: 'account', type: 'address' }], name: 'isExcluded', outputs: [{ internalType: 'bool', name: '', type: 'bool' }], stateMutability: 'view', type: 'function' },
{
inputs: [
{ internalType: 'address', name: 'account', type: 'address' },
{ internalType: 'uint256', name: 'amount', type: 'uint256' },
],
name: 'mint',
outputs: [],
stateMutability: 'nonpayable',
type: 'function',
},
{ inputs: [], name: 'name', outputs: [{ internalType: 'string', name: '', type: 'string' }], stateMutability: 'view', type: 'function' },
{ inputs: [], name: 'owner', outputs: [{ internalType: 'address', name: '', type: 'address' }], stateMutability: 'view', type: 'function' },
{
inputs: [
{ internalType: 'uint256', name: 'tAmount', type: 'uint256' },
{ internalType: 'bool', name: 'deductTransferFee', type: 'bool' },
],
name: 'reflectionFromToken',
outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
{ inputs: [], name: 'renounceOwnership', outputs: [], stateMutability: 'nonpayable', type: 'function' },
{ inputs: [{ internalType: 'address', name: 'account', type: 'address' }], name: 'setAsCharityAccount', outputs: [], stateMutability: 'nonpayable', type: 'function' },
{ inputs: [], name: 'symbol', outputs: [{ internalType: 'string', name: '', type: 'string' }], stateMutability: 'view', type: 'function' },
{ inputs: [{ internalType: 'uint256', name: 'rAmount', type: 'uint256' }], name: 'tokenFromReflection', outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }], stateMutability: 'view', type: 'function' },
{ inputs: [], name: 'totalBurn', outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }], stateMutability: 'view', type: 'function' },
{ inputs: [], name: 'totalCharity', outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }], stateMutability: 'view', type: 'function' },
{ inputs: [], name: 'totalFees', outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }], stateMutability: 'view', type: 'function' },
{ inputs: [], name: 'totalSupply', outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }], stateMutability: 'view', type: 'function' },
{
inputs: [
{ internalType: 'address', name: 'recipient', type: 'address' },
{ internalType: 'uint256', name: 'amount', type: 'uint256' },
],
name: 'transfer',
outputs: [{ internalType: 'bool', name: '', type: 'bool' }],
stateMutability: 'nonpayable',
type: 'function',
},
{
inputs: [
{ internalType: 'address', name: 'sender', type: 'address' },
{ internalType: 'address', name: 'recipient', type: 'address' },
{ internalType: 'uint256', name: 'amount', type: 'uint256' },
],
name: 'transferFrom',
outputs: [{ internalType: 'bool', name: '', type: 'bool' }],
stateMutability: 'nonpayable',
type: 'function',
},
{ inputs: [{ internalType: 'address', name: 'newOwner', type: 'address' }], name: 'transferOwnership', outputs: [], stateMutability: 'nonpayable', type: 'function' },
{
inputs: [
{ internalType: 'uint256', name: '_txFee', type: 'uint256' },
{ internalType: 'uint256', name: '_burnFee', type: 'uint256' },
{ internalType: 'uint256', name: '_charityFee', type: 'uint256' },
],
name: 'updateFee',
outputs: [],
stateMutability: 'nonpayable',
type: 'function',
},
];

然后创建合约(其中TokenAddress是您感兴趣的代币(:

let tokenContract = new web3.eth.Contract(tokenAbi, tokenAddress);

现在你可以问合同你想知道什么:

var WalletTokenBalance = await tokenContract.methods.balanceOf(WalletAddress).call();
console.log(WalletTokenBalance);

最后一个问题,我们需要调整小数:

var decimals = await tokenContract.methods.decimals().call();
var adjustedBalance = WalletTokenBalance * 10 ** -decimals;
console.log(adjustedBalance);

如果我正确理解了这个问题,这应该是解决方案。返回钱包中的特定代币数量。

您可以尝试使用以下答案:Web3.js-getBalance to Web3.utils.fromWei,将值存储在要在表中使用的变量中

文件:https://web3js.readthedocs.io/en/v1.2.0/web3-eth.html#getbalance

最新更新