react-nativealchemy

Unable to retrieve all token balances with Alchemy SDK


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?


Solution

  • 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 });