0

I am trying to get the balance of user for specific token type using web3.js.

Here is my code block:

    var tokenContract = new web3.eth.Contract(erc20abi, tokenAddress)
    tokenContract.methods.balanceOf(accounts[0]).call().then(balance => {
              console.log(balance);
    });

It throws an error as soon as this function is called from web3.

Following is the error at my console:

Uncaught (in promise) Error: invalid type (arg="type", value="a")

at Object.n [as throwError] (web3.min.js:1)
at D (web3.min.js:1)
at web3.min.js:1
at Array.forEach (<anonymous>)
at web3.min.js:1
at D (web3.min.js:1)
at e.<anonymous> (web3.min.js:1)
at Array.forEach (<anonymous>)
at e.encode (web3.min.js:1)
at a.encodeParameters (web3.min.js:1)**

My web3.js version is: 1.2.7

Here are values from the variables used:

  1. accounts[0] = "0x572ACD5370909b2C0aCde66995c1BE8aC25238bE"

  2. tokenAddress = "0xfd60bC45f816b575C2c938F744CFb801505824E7"

  3. erc20abi = [{ "constant": true, "inputs": [], "name": "name", "outputs": [{ "name": "", "type": "string" }], "payable": false, "type": "function" }, { "constant": true, "inputs": [], "name": "decimals", "outputs": [{ "name": "", "type": "uint8" }], "payable": false, "type": "function" }, { "constant": true, "inputs": [{ "type": "address" }], "name": "balanceOf", "outputs": [{ "type": "uint256" }], "payable": false, "type": "function" }, { "constant": true, "inputs": [], "name": "symbol", "outputs": [{ "name": "", "type": "string" }], "payable": false, "type": "function" }, { "constant": true, "inputs": [{ "internalType": "address", "type": "address" }], "name": "balanceOf", "outputs": [{ "internalType": "uint256", "type": "uint256" }], "payable": false, "stateMutability": "view", "type": "function" } ];

4
  • What line in your code triggers this error? Commented Jun 7, 2020 at 15:43
  • The problem is in erc20abi or in tokenAddress or in accounts[0]. Check the value of each one of these variables, and make sure that it is legal for the purpose of the corresponding variable. Commented Jun 7, 2020 at 15:46
  • ok, I have edited the question with values as well, please take a look Thanks Commented Jun 7, 2020 at 16:05
  • error triggers at line 2 Commented Jun 7, 2020 at 16:08

1 Answer 1

1

Looks like you're using an older ABI format (or at least an unconventional one).

Change this:

"internalType":"address"

And this:

"internalType":"uint256"

To this:

"name":""

UPDATE:

It also seems that you have two balanceOf functions in your ABI, god knows why (maybe an attempt to support both formats). So to generalize the answer above, just use this one:

{
    "constant"        : true,
    "inputs"          : [{"name":"","type":"address"}],
    "name"            : "balanceOf",
    "outputs"         : [{"name":"","type":"uint256"}],
    "payable"         : false,
    "stateMutability" : "view",
    "type"            : "function"
}

It should be one of the elements in the erc20abi array of course.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.