alchemyplatform/alchemy-web3

Error getting token balances when using web3.alchemy.getTokenBalances

Closed this issue · 14 comments

Hi there, I have the following code:

const web3 = createAlchemyWeb3(
  `https://eth-mainnet.alchemyapi.io/v2/${process.env.NEXT_PUBLIC_ALCHEMY_API_KEY}`
)
const getTokenBalanceWithAlchemy = async (address, tokens) => {
  try {
    let tokenBalances = await web3.alchemy.getTokenBalances(address, tokens)
    return tokenBalances
  } catch (error) {
    console.error(error)
  }
}
getTokenBalanceWithAlchemy("0x87BA155A86Cf63be05445bABB184e327551810F8", ["0x1f9840a85d5af5bf1d1762f925bdaddc4201f984"])

For some reason, the request gives me an error...

index.js?d1cc:41 TypeError: this.decodeParameters is not a function
    at ABICoder.decodeParameter (index.js?8fce:273)
    at eval (index.js?1558:113)
    at Array.map (<anonymous>)
    at processTokenBalanceResponse (index.js?1558:111)
    at eval (index.js?1558:102)
    at step (tslib.es6.js?61e8:102)
    at Object.eval [as next] (tslib.es6.js?61e8:83)
    at fulfilled (tslib.es6.js?61e8:73)

Can someone help me figure out why this is happening? Thanks!

Same issue for me.

So I did some investigation to try and work out when this issue crept in.

All versions from 1.1.4 onwards, up to at least 1.1.9 (the current version as I write this) have this issue. Versions 1.1.3 and before do not have this issue, at least down as far as 1.1.0.

So, while not an ideal solution, and understanding that you won't be benefitting from improvements introduced from version 1.1.4 onwards, you can switch to a version without this issue by changing the entry in your packaje.json to:

"@alch/alchemy-web3": "1.1.3"

That is correct. 1.1.3 works, latest throws an error.

Getting this same error... I can bump down to the last working version but wondering if there is a planned fix for this or an alternative part of the alchemy-web3 API that would allow me to use the latest alchemy-web3 version?

@seldomatt Which version of alchemy-web3 are you using? I'm using the latest version (1.2.4), and it works fine for me.

@thebrianchen I was using 1.1.12 when the issue was first encountered. Bumped down to 1.1.3 as previous commenters noted and the issue resolved. Tried bumping up to 1.2.4 as per your comment but the issue is back. The code I'm using to load up the alchemy web3 provider and call the getTokenBalances function is below, any guidance would be much appreciated!

import { createAlchemyWeb3 } from '@alch/alchemy-web3';
const alchemyAPI = createAlchemyWeb3(`https://eth-mainnet.alchemyapi.io/v2/${process.env.REACT_APP_ALCHEMY_API_KEY}`);

export const getTokenBalancesForAddress = (address) => {
    return new Promise((resolve, reject) => {
       alchemyAPI.alchemy.getTokenBalances(address, "DEFAULT_TOKENS").then((tokenBalancesResponse) => {
          resolve(tokenBalancesResponse);
       });
    })
}

With version 1.2.4 this results in the this.decodeParameters is not a function error after a successful API call (can see logs on Alchemy dashboard) when trying to decode the balances response.

@seldomatt Can you share a stack trace of the error if you have one?

@thebrianchen sure! Stacktrace below:

index.js:273 Uncaught (in promise) TypeError: this.decodeParameters is not a function
    at ABICoder.decodeParameter (index.js:273:1)
    at index.ts:287:1
    at Array.map (<anonymous>)
    at processTokenBalanceResponse (index.ts:283:1)
    at index.ts:257:1
    at step (tslib.es6.js:102:1)
    at Object.next (tslib.es6.js:83:1)
    at fulfilled (tslib.es6.js:73:1)

The index.js file is from web3 lib I believe..

@seldomatt I was hoping the stack trace would have some more info :(

I can't seem to repro the issue in my dummy web-app with the same code. Can you repro this in a package that I can repro with and investigate?

My current hypothesis is that you may have a conflicting dependency of web3-eth-abi that has a different definition of decodeParameters. Appreciate your help!

Closing due to inactivity but happy to reopen with more information!

Hello !
I am having the exact same issue and stack trace with this call (latest alchemy-web version).

@thebrianchen have you tried reproducing on nodejs 14 or 16 ?
thanks for your help

@LouisPinsard I tried reproing on node v12, 14, and 16, but couldn't get the repro. If you could create a repro app with it, that would be super helpful! Thanks!

tg44 commented

I have this too... Falling back to fetch. Also, it would be nice to know what are the 100 tokens, for example in Rinkeby.

Reopening since multiple people have reported this issue.