0

I'm trying to get balance of every erc20 token's balance in polygon zkevm network with using Ethers.js

But I'm getting the error:

    Error: could not decode result data (value="0x", info={ "method": "balanceOf", "signature": "balanceOf(address)" }, code=BAD_DATA, version=6.11.1)
        at makeError (..project_path/node_modules/ethers/lib.esm/utils/errors.js:124:21)
        at assert (..project_path/node_modules/ethers/lib.esm/utils/errors.js:143:15)
        at Interface.decodeFunctionResult (..project_path/node_modules/ethers/lib.esm/abi/interface.js:772:9)
        at staticCallResult (..project_path/node_modules/ethers/lib.esm/contract/contract.js:249:35)
        at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
        at async staticCall (..project_path/node_modules/ethers/lib.esm/contract/contract.js:214:24)
        at async Proxy.balanceOf (..project_path/node_modules/ethers/lib.esm/contract/contract.js:254:20)
        at async getTokenBalances (..project_path/src/balances.js:32:24){
      code: 'BAD_DATA',
  value: '0x',
  info: { method: 'balanceOf', signature: 'balanceOf(address)' },
  shortMessage: 'could not decode result data'

here is my getTokenBalances Method:

async function getTokenBalance() {

  let result = [];
  //ERC20 balances
  for (const token of token_list) {
    if (token.chainId == "1101") {
      const contract = new ethers.Contract(token.address, abi, mainnet_provider);
      const balance = (await contract.balanceOf(bridge_address));
      const res = parseFloat(ethers.formatUnits(balance, Number(token.decimals))).toFixed(2);

      if (res != 0) {
        result.push({
          type: "erc20",
          accountAddress: token.address,
          balance: formattedBalance
        });
      }
    }
  };

  return result;
}

where token list from: https://github.com/maticnetwork/polygon-token-list/blob/dev/src/tokens/zkevmPopularTokens.json

and bridge address is: "0x2a3DD3EB832aF982ec71669E178424b10Dca2EDe"

What causes the problem, what is the possible solution?

1 Answer 1

1

The issue could not decode result data (value="0x", usually came from not existed contract at address. One or some address in your list is wrong.

You should add try-catch in your code, and log what address you're calling, when it's fail and try by yourself on explorer.

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.