Discovering the ERC-20 token balances in a given wallet is a straightforward process. When users open their wallets, they can immediately see the balances of all the imported tokens. However, if someone needs to determine the ERC-20 token balances of another address, such as law enforcement or a business, they cannot directly access the wallet.
In such cases, an application programming interface (API) can be utilized to check the balances in a wallet. An API can retrieve all ERC-20 token balances owned by an address and automate the process of checking token balances.
This article explores how Chainbase, Alchemy, and Moralis APIs can be used to obtain information about ERC-20 tokens owned by an address.
Step-by-step guide to using Chainbase to find ERC-20 tokens owned by an address:
Chainbase, a Web3 blockchain interaction layer infrastructure, provides API services that enable developers to quickly access and utilize blockchain networks. Its APIs facilitate on-chain data queries.
To use Chainbase, users need 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 for a free account on Chainbase to gain access to different APIs and its data cloud. After logging in, visit the dashboard, initiate a new project within the console, and obtain an API key.
Step 2: Use Chainbase API to write a script:
The next step involves making an API call to Chainbase to retrieve ERC-20 token balances. The examples below demonstrate the use of JavaScript. To obtain ERC-20 token balances from Chainbase, execute an HTTP request with JavaScript, utilizing the Axios library.
To make an HTTP request and retrieve data from a server, use “fetch” as shown in the code snippet below:
“`
// Code snippet
“`
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.
While the native Fetch API is commonly used for HTTP requests with JavaScript, the example code conceptually uses Axios for fetching instead of the native Fetch API. To use Axios, the user must install it by running “npm install axios –save” in the terminal.
The above code snippet 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 requires the chain ID and wallet address as parameters and returns all ERC-20 token balances owned by the wallet. Users can also find information about a specific token by populating the “contract_address” field. To print the data, run the command “node .js” in the terminal.
The above code snippet displays balances and information for several ERC-20 tokens, including Ethereum, Uniswap V2, dYdX, and others, from a given wallet.
Step-by-step guide to using Alchemy to find ERC-20 tokens owned by an address:
Alchemy provides APIs that function as a platform layer, allowing developers to read from or write to the blockchain and retrieve precise information. The following steps explain how to obtain token balances through Alchemy:
Step 1: Install Node and NPM:
Install Node.js and the Node Package Manager (NPM) on the local machine. Node.js is a cross-platform, open-source JavaScript runtime environment, while NPM provides access to a wide range 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 and 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 the form: https://eth-mainnet.g.alchemy.com/v2/xxxxxxxxx
Step 3: Create a Node project:
Initialize an empty repository and install the necessary Node.js dependencies. For API interactions, particularly with the token API, users need to use the Alchemy Software Development Kit (SDK), although Axios or Fetch can also be suitable alternatives.
The code snippet below demonstrates this:
“`
// Code snippet
“`
The code creates a repository called “eth-balance,” which contains all the files and dependencies. The code is written in the “main.js” file.
Step 4: Get the token balances of an address:
To retrieve token balances, users can utilize the getTokenBalances method, which only requires the wallet address as an argument. Add the following code to the main.js file:
“`
// Code snippet
“`
Use the following command to run the script:
“`
// Code snippet
“`
The output will be displayed as follows:
“`
// Code snippet
“`
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, is not 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.
“`
// Code snippet
“`
Users can also refine their script to integrate 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 owned by an address:
Moralis offers 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 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 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. Address represents the specific wallet address being queried for token balances, and chain indicates the blockchain network, such as Ethereum, on which the tokens are held.
Use the following JavaScript code:
“`
// Code snippet
“`
The 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:
Run the script to find the number of tokens. In JavaScript, enter the following command:
“`
// Code snippet
“`
The terminal will display the JSON response:
“`
// Code snippet
“`
The JSON snippet above provides details about the holdings of ERC-20 tokens in a specific blockchain wallet, including two tokens: ApeCoin (APE) and Wrapped Ether (WETH). It includes essential information such as contract addresses, names, symbols, logo URLs (if available), decimal precision, and the balances of the tokens within the wallet, expressed in the smallest denomination of each token.