Unable to retrieve all token balances with Alchemy SDK

490 Views Asked by At

I have a react-native project which will fetch all of a users Ethereum tokens, hopefully, using the Alchemy SDK. I have a function that should retrieve all token balances according to the documentation.

import 'react-native-get-random-values';
import '@ethersproject/shims';

import { Network, Alchemy, Wallet } from 'alchemy-sdk';
import {ALCHEMY_API_KEY} from '@env';

const settings = {
    apiKey: ALCHEMY_API_KEY,
    network: Network.ETH_GOERLI
};
const alchemy = new Alchemy(settings);

export const getAllBalances = async (address) => {
    try {
        const balances = await alchemy.core.getTokenBalances(address, 'erc20');
        return balances;
    } catch (err) {
        console.log(err.message);
    }
}

However, when this runs I get this error message.

"invalid 2nd argument: contract_addresses was not a valid contract address array, string literals 'DEFAULT_TOKENS' or 'erc20', or a valid options object.\"

As you can see, my 2nd argument is 'erc20' as the message states that the 2nd argument should be. I also tried 'DEFAULT_TOKENS' and receive the same error message. If I just try to retrieve the basic eth tokens in an account I have no issues so I believe that my settings are correct. Does anyone know how to fix this issue?

2

There are 2 best solutions below

0
Jake Mulhern On BEST ANSWER

I spoke with someone from the alchemy team and this is how the line should be written

const balances = await alchemy.core.getTokenBalances(address, { type: TokenBalanceType.ERC20 });
0
josh hoffer On

You can pass it without the ERC.20 type... Just use the address...

from my understanding if you list erc.20 it will give you zerobalances as well as nonzeros -> if you use just your address

 const balances = await alchemy.core.getTokenBalances(address)
 ...

then you should be all set.