Learn how to retrieve ERC-20 token balances from wallets using Chainbase, Alchemy, and Moralis APIs. Get step-by-step guides and code examples.

Finding balances of ERC-20 tokens in a designated wallet proves straightforward; upon wallet access, the interface automatically showcases all token balances imported. Nevertheless, direct wallet access becomes infeasible when an entity, be it law enforcement or a business, seeks to ascertain ERC-20 token balances of another address.

For such scenarios, the solution lies in utilizing an Application Programming Interface (API) to verify wallet balances. An API facilitates the retrieval of all ERC-20 token balances associated with an address and streamlines the process of balance verification.

This piece explores the utilization of Chainbase, Alchemy, and Moralis APIs to extract information regarding ERC-20 tokens held by a specific address.

Guide to Leveraging Chainbase for Identifying ERC-20 Tokens Associated with an Address

Chainbase, functioning as a Web3 blockchain interaction layer infrastructure, furnishes API services enabling swift access to blockchain networks and facilitating on-chain data inquiries.

To engage with Chainbase effectively, users must possess an account on the platform along with an API key, an integrated development environment, and a wallet address as input.

Step 1: Account Creation and Login

Commence by registering for a complimentary account on Chainbase to gain access to diverse APIs and its data cloud. Subsequently, log in to Chainbase and navigate to the dashboard. Initiate a fresh project within the console to procure an API key.

Step 2: Utilize Chainbase API to Script

The subsequent step involves making an API call to Chainbase to fetch ERC-20 token balances. The provided examples employ Javascript. The ensuing action entails executing an HTTP request using JavaScript, leveraging the Axios library to extract ERC-20 token balances from Chainbase.

Employ "fetch" to initiate an HTTP request for retrieving data from a server, as depicted in the code snippet below:

The code snippet queries Chainbase to retrieve the initial five ERC-20 token balances of a wallet (illustrated using Vitalik Buterin's address) on the Ethereum mainnet, presenting the outcomes or any encountered errors.

Although the native Fetch API commonly performs HTTP requests with JavaScript, in the provided code example, "fetching" conceptually transpires through Axios, rather than the native Fetch API. To utilize Axios, users must install it via "npm install axios --save" in the terminal.

The above code snippet utilizes Axios in JavaScript to interrogate Chainbase for the initial five ERC-20 token balances of Vitalik Buterin's wallet on the Ethereum mainnet.

Step 3: Display Token Balances

Chainbase's getAccountTokens API necessitates the chain ID and wallet address as parameters and returns all ERC-20 token balances owned by the wallet. Users can also access information regarding a specific token by populating the "contract_address" field. To obtain printed data, users must execute the command "node .js" in the terminal.

The aforementioned code snippet exhibits balances and information pertaining to various ERC-20 tokens, including Eterium, Uniswap V2, dYdX, and others, from a specified wallet.

Guide to Utilizing Alchemy to Uncover ERC-20 Tokens Held by an Address

Alchemy furnishes APIs serving as a platform layer, facilitating interactions with the blockchain and delivering precise information desired by developers. The following outlines the procedure for obtaining token balances via Alchemy:

Step 1: Node and NPM Installation

Install Node and the Node Package Manager (NPM) on the local machine. Node functions as a cross-platform, open-source JavaScript runtime environment, while NPM provides access to a vast ecosystem of libraries and tools.

Step 2: Alchemy Account Creation and App Setup

On the Alchemy website, create a free account. Subsequently, sign in and navigate to the "Alchemy Dashboard" to generate a new app. Specify the blockchain as "Ethereum" and the network as "Mainnet." Access the app's "View Key" button on the dashboard and take note of the HTTP URL and API key.

The URL adopts the following format: https://eth-mainnet.g.alchemy.com/v2/xxxxxxxxx

Step 3: Establish a Node Project

The subsequent step entails initializing an empty repository and installing the requisite Node.js dependencies. For API interactions, particularly with the token API, users may utilize the Alchemy software development kit (SDK), although Axios or Fetch could serve as viable alternatives.

Axios serves as a JavaScript library facilitating HTTP requests from nodes. The Fetch API constitutes a built-in module in Node, simplifying API request initiation and response handling on the server. The Alchemy SDK functions as a program function library supporting access to the underlying data model and technologies.

The code snippet is provided below:

The code establishes a repository "eth-balance," housing all files and dependencies, with code written within the "main.js" file.

Step 4: Retrieve Token Balances of an Address

To extract token balances, users may employ the getTokenBalances method, necessitating a singular argument: the wallet address for which token balances are sought. Subsequently, users must incorporate the following code into the main.js file.

Execute the following command to run the script:

The output will be showcased as follows:

The above code exhibits token balances for the wallet with the address 0xd8da6bf26964af9d7eed9e03e53415d37aa96045, listing each token by its contract address alongside the respective balance in hexadecimal format.

However, the output from the previous step — delineating token contract