Skip to main content

Don't have an API key yet?

Sign-up to Moralis to get your free API key and start building today.

Get Your Free API Key
Version: 2.2

How to Get ERC-20 Tokens of a Smart Account

tip

If you'd like examples of account types, read the Ethereum Accounts article.

The Moralis Wallet API streamlines the retrieval of ERC-20 tokens, allowing developers to access token data for various purposes.

When you create a new token on the Ethereum network, it usually follows ERC-20 specifications. For an Ethereum token to align with the ERC-20 standard, it must possess an array of diverse methods and attributes. Collectively, these elements form the essential building blocks that enable smart contracts to function seamlessly as tokens within the Ethereum network.

The ERC-20 standard serves as a universal blueprint and interface. Further, the standard provides a common set of guidelines for smart contracts to adhere to when operating as tokens on the Ethereum blockchain. This standardized framework fosters interoperability, consistency, and compatibility, making ERC-20 tokens the cornerstone of decentralized finance (DeFi) and blockchain-based ecosystems.

This step-by-step tutorial shows how to easily retrieve and query an ERC-20 token in a smart account using an account address and token contract. What's more, this tutorial offers code examples in multiple programming languages.

tip

This tutorial uses Moralis' getWalletTokenBalances RPC method.

Step 1: Set Up Moralis

Read our Setting Up Moralis: Getting Started article and finish all of its steps. Only after that can you go ahead to complete this guide.

Step 2: Method to Get and Retrieve ERC-20 Tokens for a Given Smart Account Address

You can use the getWalletTokenBalances API endpoint to get all transactions for an address of a smart account. This endpoint allows you to fetch transactions for a given address of a smart account.

You will need two essential parameters:

  • address
  • chain

Once you have obtained both address and chain, you can use the code snippets below to retrieve the transactions:

// Import Moralis and EvmChain from Moralis and common EVM utils respectively
const Moralis = require("moralis").default;
const { EvmChain } = require("@moralisweb3/common-evm-utils");

// Define an asynchronous function runApp
const runApp = async () => {
// Initialize Moralis with your API key and other configurations
await Moralis.start({
apiKey: "YOUR_API_KEY",
// ...and any other configuration
});

// Define the smart account address you want to retrieve ERC-20 tokens for
const address = "0x9b722B2aA4Cc119aCf8c95bBF5244092Be6880b9";

// Specify the (EVM) you are working with
const chain = EvmChain.POLYGON;

// Use Moralis EvmApi to get wallet token balances for the address and chain
const response = await Moralis.EvmApi.token.getWalletTokenBalances({
address,
chain,
});

// Log the JSON response to the console
console.log(response.toJSON());
};

// Call the runApp function to start the application
runApp();

Step 3: Run the Script

To run the script, enter the following command:

node index.js

Code example of the JSON response:

[
{
"token_address": "0x350845DD3f03F1355233a3A7CEBC24b5aAD05eC5",
"name": "APE",
"symbol": "APE",
"logo": null,
"thumbnail": null,
"decimals": 18,
"balance": "101715701444169451516503179"
},
{
"token_address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"name": "Wrapped Ether",
"symbol": "WETH",
"logo": "https://cdn.moralis.io/eth/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2.webp",
"thumbnail": "https://cdn.moralis.io/eth/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2_thumb.webp",
"decimals": 18,
"balance": "85000000000000000"
}
]

Congratulations! 🥳 You have successfully fetched ERC-20 tokens for a given smart account address on Polygon using the Moralis Wallet API.

Video Tutorial: How to Get All ERC-20 Tokens

For a visual guide, you can check out our YouTube tutorial:

Get 24/7 Developer Support

Should you encounter any challenges while following this tutorial, our community engineers are here to assist you. Reach out to us on Discord or our Moralis forum to receive 24/7 developer support. Your success is our priority!

API Reference

If you want to explore more details about other wallet endpoints and optional parameters, check out our API Reference page.

See Also