Mnigos
Mnigos

Reputation: 161

getting balance of specify tokens from adress with web3js

I want to get balance of specify token fee. only ETH from given address. And I have no idea how to implement that. The web3 docs doesn't help me too much.

Upvotes: 1

Views: 3162

Answers (2)

Nicolò Sartor
Nicolò Sartor

Reputation: 96

You can try this, first connect to a provider with web3:

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

I recommend you to use speedy nodes.

Then create a constant of the token's ABI, so that you can use all the methods of the contract:

const tokenAbi = [Token ABI code]

You can use this standard one if you want:

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',
   },
];

Then create the contract (where TokenAddress is the token you are interested in):

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

Now you can ask the contract what you want to know:

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

Last problem, we need to adjust the decimals:

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

If I understand the question correctly, this should be the solution. Returns how many specific Tokens a wallet has.

Upvotes: 2

Related Questions