New SDK and token balance

It used to be possible to get balance of a specific token using the following SDK function:

options = { chain: "mumbai", tokenAddress: myTokenAddress};
await Moralis.Web3.getERC20(options)

The function still works, However I don’t see this function in the documentation page https://docs.moralis.io/moralis-server/web3-sdk/account

The closest thing I see on there is the following:

await Moralis.Web3API.account.getTokenBalances(options);

But this function gets balances for ALL tokens. If i include the tokenAddress, value in options, it ignores that value.

Given that the SDK was updated a few days ago, does this mean that the Moralis.Web3.getERC20() function has been deprecated? Or will it continue to get supported?

2 Likes

Hey @bobbington

Moralis.Web3 is an old syntax and will be deleted soon. We have expanded the functionality of the Moralis SDK and now you need to use Moralis.Web3API it has more functions.

Take a look at:

If you want to get balance for specific token you can use .find:

options = { chain: "mumbai" };
const balances = await Moralis.Web3API.account.getTokenBalances(options);
const tokenAddress =  "0x....9"; // You can specify for example: tokenAddress, name or symbol
const tokenBalance= balance.find((token) => token.token_address === tokenAddress);
console.log(tokenBalance);

Happy BUIDLing :man_mechanic:

2 Likes

Cheers! Thank you so much for the clarification, and for providing a workaround that is compatible with the new SDK :slight_smile:

Do you know if something like Moralis.Web3.getERC20(options) will make its way back into the new SDK? It was such a simple and clean way of getting the desired information.

1 Like

You are welcome!

Yes, it’s on the roadmap. It will be added :man_mechanic:

2 Likes