How to get all ERC-20 tokens owned by an address
Wallet APIs enable users to quickly locate ERC-20 tokens by wallet address. Chainbase, Alchemy and Moralis provide APIs to get token-related information.
Finding ERC-20 token balances in any given wallet is simple; when users open their wallets, they automatically display the balances of all the imported tokens. However, when an individual or an entity, such as law enforcement or a business, needs to ascertain the ERC-20 token balances of another address, direct access to the wallet is not possible.
In such cases, an application programming interface (API) can be used to check the balances in a wallet. An API would return all ERC-20 token balances owned by an address and could automate the process of checking token balances.
This article discusses how one can use the APIs of Chainbase, Alchemy and Moralis to get information about ERC-20 tokens owned by an address.
Step-by-step guide to using Chainbase to find all ERC-20 tokens owned by an address
Chainbase — a Web3 blockchain interaction layer infrastructure — offers API services to help developers quickly access and utilize blockchain networks. Its APIs facilitate on-chain data queries.
To work with Chainbase, a user needs an account on the platform with an API key, an integrated development environment and a wallet address as input.
Step 1: Create a free account and log in
Register a free account on Chainbase and get access to different APIs and its data cloud. Then log into Chainbase and visit the dashboard. Initiate a new project within the console and obtain an API key.
Step 2: Use Chainbase API to write script
The next is to make an API call to Chainbase to retrieve ERC-20 token balances. The examples below use Javascript. The subsequent action entails executing a HTTP request with JavaScript, utilizing the Axios library to obtain ERC-20 token balances from Chainbase.
Use “fetch” to make a HTTP request to retrieve data from a server, as shown in the code snippet below:
The code in the above example queries Chainbase to fetch the first five ERC-20 token balances of a wallet (using Vitalik Buterin’s address as an example) on the Ethereum mainnet, displaying the results or any errors encountered.
While the native Fetch API is a common way to perform HTTP requests with JavaScript, in the example code, “fetching” is conceptually done by Axios, not by the native Fetch API. To use Axios, the user must install it using “npm install axios –save” in the terminal.
The above code uses Axios in JavaScript to query Chainbase for the first five ERC-20 token balances of Vitalik Buterin’s wallet on the Ethereum mainnet.
Step 3: Print the token balances
The getAccountTokens API of Chainbase uses the chain ID and wallet address as parameters and returns all ERC-20 token balances the wallet owns. Users can also find information about a specific token by populating the “contract_address” field. To get data printed, the user must run the command “node <filename>.js” in the terminal.
The above code snippet displays balances and information for several ERC-20 tokens, including Eterium, Uniswap V2, dYdX and others, from a given wallet.
Step-by-step guide to using Alchemy to find all ERC-20 tokens owned by an address
Alchemy offers APIs that work as a platform layer, reading or writing to the blockchain and delivering the precise information developers want. The following is the procedure to get token balances via Alchemy:
Step 1: Install Node and the NPM
Install Node and the Node Package Manager (NPM) on the local machine. Node is a cross-platform, open-source JavaScript runtime environment, while the NPM provides access to a vast ecosystem of libraries and tools.
Step 2: Sign up for an Alchemy account and create an app
On the Alchemy website, sign up for a free account. Then, sign in and navigate to the “Alchemy Dashboard” to create a new app. Set the blockchain to “Ethereum” and the network to “Mainnet.” Click on the app’s “View Key” button on the dashboard and note the HTTP URL and API key.
The URL will be in this form: https://eth-mainnet.g.alchemy.com/v2/xxxxxxxxx
Step 3: Create a Node project
The next step involves initializing an empty repository and installing the required Node.js dependencies. For API interactions, particularly with the token API, users need to use the Alchemy software development kit (SDK), though Axios or Fetch could serve as suitable alternatives.
Axios is a Javascript library used to make HTTP requests from nodes. Fetch API is a built-in module in Node that facilitates making API requests and handling responses on the server. The Alchemy SDK is a program function library that supports access to the underlying data model and technologies.
The code is under-given:
The code will create a repository “eth-balance,” which holds all the files and dependencies, and the code will be written in the “main.js” file.
Step 4: Get the token balances of an address
To retrieve token balances, users may utilize the getTokenBalances method, which requires a single argument: the wallet address for which the token balances are desired. Then, a user needs to add the following code to the main.js file.
Use the following command to run the script:
The output will be displayed as follows:
The above code displays the token balances for the wallet at address 0xd8da6bf26964af9d7eed9e03e53415d37aa96045, listing each token by its contract address along with the respective balance in hexadecimal format.
However, the output of the previous step — which lists token contract addresses and balances in the smallest units — is not easy to understand. To obtain detailed token information like name, symbol and decimal count, users should use the getTokenMetadata method. This function — requiring the contract address as input — delivers data in a structured format.
Moreover, users can refine their script to integrate the getTokenBalances with the getTokenMetadata method. For instance, they can eliminate zero-balance tokens and convert token balances into human-readable forms.
Step-by-step guide to using Moralis to find all ERC-20 tokens owned by an address
Moralis has enterprise-grade Web3 APIs, facilitating the integration of Web3 in any tech stack. The following is the process for using Moralis to find all ERC-20 tokens owned by an address:
Step 1: Set up Moralis
Install Node.js v14+ and NPM. Unpack the binary to the installation folder and set the system environment variable for Node. Command npm-install sets up an NPM package and any other packages that it might depend on.
Create a free Moralis account, log in to the Moralis dashboard and get the API key. Users can find it in Settings > Secrets. Here, users should locate the “Web3 API Key – Default” or a similarly named key and copy its value for use in their projects.
Step 2: Find all ERC-20 tokens owned by an address
Moralis provides a “getWalletTokenBalances” endpoint to find all ERC-20 tokens owned by an address. It requires two parameters: address and chain. In this context, address represents the specific wallet address being queried for token balances. Chain indicates the particular blockchain network, such as Ethereum, on which those tokens are held.
Here is the Javascript code to use:
The above code initializes the Moralis SDK, sets it up with a provided API key, and then queries for all ERC-20 token balances associated with a specified Ethereum wallet address, printing the results in a JSON format to the console.
Step 3: Run the script
Now, users can run the script and find the number of tokens. In JavaScript, users need to enter the following command:
The terminal will display the below JSON response:
The JSON snippet above details the holdings of ERC-20 tokens in a specific blockchain wallet featuring two tokens: ApeCoin (APE) and Wrapped Ether (WETH). It includes essential information like the contract addresses, names, symbols, logo URLs (when available), decimal precision, and the balances of the tokens within the wallet, expressed in the smallest denomination of each token.
Responses