Discovering the balances of ERC-20 tokens in a given wallet is a straightforward process. When users open their wallets, the balances of all imported tokens are automatically displayed. However, accessing the balances of another address, such as for law enforcement or businesses, is not as simple. In such cases, an application programming interface (API) can be utilized to check the token balances in a wallet. APIs can return all ERC-20 token balances owned by an address and automate the process of balance checking.
In this article, we will explore how to use the APIs provided by Chainbase, Alchemy, and Moralis to obtain information about ERC-20 tokens owned by a specific address.
Step-by-step guide to using Chainbase API:
Chainbase is a Web3 blockchain interaction layer infrastructure that offers API services to quickly access and utilize blockchain networks. Its APIs enable on-chain data queries.
Step 1: Create a free account and log in
First, register for a free account on Chainbase and gain access to its data cloud and various APIs. Afterward, log in to Chainbase and visit the dashboard. Within the console, start a new project and obtain an API key.
Step 2: Utilize Chainbase API to write a script
The next step involves making an API call to Chainbase to retrieve ERC-20 token balances. The examples provided below use JavaScript. To execute an HTTP request with JavaScript and obtain ERC-20 token balances from Chainbase, utilize the Axios library.
To make an HTTP request using the Fetch API, refer to the code snippet below:
The above example code 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. It displays the results or any encountered errors.
Although the native Fetch API is commonly used for HTTP requests with JavaScript, the example code conceptualizes the “fetching” process using Axios instead. To use Axios, you must install it by running “npm install axios –save” in the terminal.
The code above demonstrates how to use 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 accepts the chain ID and wallet address as parameters and returns all ERC-20 token balances owned by the wallet. Users can also retrieve information about a specific token by populating the “contract_address” field. To print the data, run the command “node .js” in the terminal.
The provided 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 API:
Alchemy provides APIs that function as a platform layer, allowing developers to read from and write to the blockchain and access specific information. The following steps explain how to obtain token balances through Alchemy.
Step 1: Install Node and NPM
Begin by installing Node and the Node Package Manager (NPM) on your local machine. Node is a cross-platform, open-source JavaScript runtime environment, while NPM provides access to a vast ecosystem of libraries and tools.
Step 2: Sign up for an Alchemy account and create an app
Sign up for a free account on the Alchemy website. Afterward, log 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 make note of the HTTP URL and API key.
The URL will be in the following format: https://eth-mainnet.g.alchemy.com/v2/xxxxxxxxx
Step 3: Create a Node project
Next, initialize an empty repository and install the necessary Node.js dependencies. To interact with the API, particularly the token API, you will need to use the Alchemy software development kit (SDK). Alternatively, Axios or Fetch can serve as suitable alternatives.
Axios is a JavaScript library used to make HTTP requests from nodes, while Fetch API is a built-in module in Node that facilitates API requests and response handling on the server. The Alchemy SDK is a program function library that supports access to the underlying data model and technologies.
Here is the code to create a repository called “eth-balance” that holds all the files and dependencies. The code will be written in the “main.js” file.
Step 4: Retrieve the token balances of an address
To fetch token balances, utilize the getTokenBalances method, which requires a single argument: the wallet address for which you desire the token balances. 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 code above displays the token balances for the wallet at address 0xd8da6bf26964af9d7eed9e03e53415d37aa96045. It lists each token by its contract address along with the respective balance in hexadecimal format.
However, the output from the previous step, which lists token contract addresses and balances in the smallest units, may not be easily understood. To obtain detailed token information such as name, symbol, and decimal count, use the getTokenMetadata method. This function requires the contract address as input and delivers data in a structured format.
Additionally, you can refine your script by integrating the getTokenBalances with the getTokenMetadata method. For example, you can eliminate zero-balance tokens and convert token balances into human-readable forms.
Step-by-step guide to using Moralis API:
Moralis offers enterprise-grade Web3 APIs, making it easy to integrate Web3 into any tech stack. Follow the steps below to use Moralis for finding ERC-20 tokens owned by a specific 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. Use the “npm-install” command to set up an NPM package and any other required packages.
Create a free Moralis account, log in to the Moralis dashboard, and obtain the API key. You can find it in Settings > Secrets. Look for the “Web3 API Key – Default” or a similarly named key, and copy its value for use in your projects.
Step 2: Find all ERC-20 tokens owned by an address
Moralis provides a “getWalletTokenBalances” endpoint to retrieve all ERC-20 tokens owned by an address. It requires two parameters: address and chain. In this context, address represents the wallet address being queried, while chain indicates the specific blockchain network (e.g., Ethereum) on which the tokens are held.
Here is the JavaScript code to use:
The provided code initializes the Moralis SDK, sets it up with the provided API key, and queries for all ERC-20 token balances associated with a specified Ethereum wallet address. The results are printed in JSON format to the console.
Step 3: Run the script
Now, you can run the script to find the number of tokens. In JavaScript, use the following command:
The terminal will display the JSON response:
The JSON snippet above provides details about the holdings of ERC-20 tokens in a specific blockchain wallet. It includes information such as contract addresses, names, symbols, logo URLs (if available), decimal precision, and token balances expressed in the smallest denomination of each token.