Discovering the balances of ERC-20 tokens in a given wallet is a straightforward process. When users open their wallets, they are provided with a display of all imported token balances. However, situations arise where individuals or entities, such as law enforcement or businesses, need to determine the ERC-20 token balances of another address, but direct access to the wallet is not feasible.
In such cases, an application programming interface (API) can be utilized to check the balances in a wallet. By using an API, one can retrieve all ERC-20 token balances owned by an address and automate the process of checking token balances.
This article explores how to leverage the APIs of Chainbase, Alchemy, and Moralis to obtain information about ERC-20 tokens owned by a specific address.
Step-by-Step Guide to Using Chainbase to Find ERC-20 Tokens
Chainbase, a Web3 blockchain interaction layer infrastructure, offers API services that enable developers to quickly access and utilize blockchain networks. Its APIs facilitate on-chain data queries.
To utilize Chainbase, users must have 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 to gain access to various APIs and its data cloud. Once registered, log in to Chainbase and navigate to the dashboard. Within the console, initiate a new project and obtain an API key.
Step 2: Utilize the 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 utilize JavaScript. The subsequent action requires executing an HTTP request with JavaScript, utilizing the Axios library to obtain ERC-20 token balances from Chainbase.
To make an HTTP request and retrieve data from a server, the “fetch” function is used:
(The code snippet for this step is provided in the original article.)
The code above 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. The results or any encountered errors are then displayed.
While the native Fetch API is commonly used for HTTP requests in JavaScript, the example code conceptually employs Axios for fetching, rather than the native Fetch API. To use Axios, the user must install it by running “npm install axios –save” in the terminal.
(The code snippet for this step is provided in the original article.)
The above code utilizes 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 takes 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, users must run the command “node .js” in the terminal.
(The code snippet for this step is provided in the original article.)
The code snippet above 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 ERC-20 Tokens
Alchemy offers APIs that serve as a platform layer, allowing developers to read from or write to the blockchain and obtain precise information. The following steps outline the process of obtaining token balances via Alchemy.
Step 1: Install Node and NPM
Install 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. After signing in, 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 code snippet for this step is provided in the original article.)
The URL will be in the 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 necessary Node.js dependencies. For API interactions, specifically with the token API, users need to use the Alchemy software development kit (SDK), although Axios or Fetch can serve as suitable alternatives.
(The code snippet for this step is provided in the original article.)
The code will create a repository called “eth-balance” that holds all the files and dependencies, and the code will be written in the “main.js” file.
Step 4: Obtain Token Balances of an Address
To retrieve token balances, users can utilize the “getTokenBalances” method, which requires a single argument: the wallet address for which the token balances are desired. Users need to add the following code to the “main.js” file.
(The code snippet for this step is provided in the original article.)
Use the following command to run the script:
(The code snippet for this step is provided in the original article.)
The output will be displayed as follows:
(The code snippet for this step is provided in the original article.)
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 from the previous step, which lists token contract addresses and balances in the smallest units, may not be easily understandable. To obtain detailed token information such as name, symbol, and decimal count, users should use the “getTokenMetadata” method. This function requires the contract address as input and delivers data in a structured format.
(The code snippet for this step is provided in the original article.)
Additionally, users can refine their script by integrating the “getTokenBalances” with the “getTokenMetadata” method. For example, they can eliminate zero-balance tokens and convert token balances into human-readable forms.
Step-by-Step Guide to Using Moralis to Find ERC-20 Tokens
Moralis provides enterprise-grade Web3 APIs that facilitate the integration of Web3 in any tech stack. The following steps outline the process of 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. Use the command “npm install” 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. The API key can be found in Settings > Secrets. Locate 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 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, and chain indicates the particular blockchain network, such as Ethereum, on which those tokens are held.
(The code snippet for this step is provided in the original article.)
The above code initializes the Moralis SDK, sets it up with the provided API key, and then queries for all ERC-20 token balances associated with the specified Ethereum wallet address. The results are printed in a JSON format to the console.
Step 3: Run the Script
Users can now run the script and find the number of tokens. In JavaScript, enter the following command:
(The code snippet for this step is provided in the original article.)
The terminal will display the JSON response:
(The code snippet for this step is provided in the original article.)
The JSON snippet above provides details of ERC-20 token holdings in a specific blockchain wallet, featuring two tokens: ApeCoin (APE) and Wrapped Ether (WETH). It includes essential information such as contract addresses, names, symbols, logo URLs (when available), decimal precision, and token balances within the wallet, expressed in the smallest denomination of each token.