If you’re involved in the world of cryptocurrency, you may find it useful to retrieve the balances of ERC20 tokens owned by a specific address. By using the Chainbase API’s getAccountTokens
endpoint, you can effortlessly obtain the balances of all ERC20 tokens associated with a particular wallet address. This article will guide you through the process of setting up a Chainbase account, writing a script using the Chainbase API, and getting the ERC20 token balances. Let’s get started!
Outline:
- Introduction
- Overview of Tools Needed
- Set up a Free Account at Chainbase
- Write a Script Using Chainbase API
- Print the ERC20 Token Balances
- Conclusion
- FAQs
1. Introduction
Cryptocurrency enthusiasts often seek ways to efficiently manage their token holdings and check their balances. With the Chainbase API’s getAccountTokens
functionality, you can automate the process of retrieving ERC20 token balances owned by a specific wallet address. This not only saves time but also provides a convenient way to stay updated on your token investments.
2. Overview of Tools Needed
Before diving into the implementation, it’s essential to ensure you have the necessary tools in place. Here’s what you’ll need:
1. Free Account at Chainbase with an API Key
To leverage the power of Chainbase, you should register for a free account. This account grants you access to various APIs and data cloud services provided by Chainbase.
2. IDE Recommendation: VS Code
While the examples provided in this article are in JavaScript, you can use any IDE of your choice. However, we recommend using Visual Studio Code (VS Code) due to its extensive features, code editing capabilities, and popularity within the developer community.
3. Wallet Address as Input
To retrieve ERC20 token balances, you’ll need a known wallet address as your input. This could be your own address or any other address you wish to inspect.
3. Set up a Free Account at Chainbase
To begin, let’s set up your free account at Chainbase and obtain an API key. Follow these steps:
- Register for a free account at Chainbase by visiting our website.
- Once you’ve registered and logged in, navigate to the dashboard to get an overview of the available features.
- Create a new project in the Chainbase console. This project will be associated with your account and allow you to manage your API key and other settings.
- After creating the project, you’ll be provided with an API key. This key serves as a unique identifier for your account when making API requests.
4. Write a Script Using Chainbase API
Now that you have your Chainbase account and API key, you can proceed to write a script that utilizes the Chainbase API. Here are examples using both the Fetch and Axios libraries in JavaScript:
Using Fetch in JavaScript:
network_id = '1'; // See to get the id of different chains.
wallet_addr = '0xd8dA6
BF26964aF9D7eEd9e03E53415D37aA96045'; // Take Vitalik's wallet address as an example.
fetch(`https://api.chainbase.online/v1/account/tokens?chain_id=${network_id}&address=${wallet_addr}&limit=5&page=1`, {
method: 'GET',
headers: {
'x-api-key': CHAINBASE_API_KEY, // Replace the field with your API key.
'accept': 'application/json'
}
}).then(response => response.json())
.then(data => console.log(data.data))
.catch(error => console.error(error));
Using Axios in JavaScript:
You need to install axios
using npm install axios --save
in the terminal first.
network_id = '1'; // See to get the id of different chains.
wallet_addr = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'; // Take Vitalik's wallet address as an example.
const axios = require('axios');
const options = {
url: `https://api.chainbase.online/v1/account/tokens?chain_id=${network_id}&address=${wallet_addr}&limit=5&page=1`,
method: 'GET',
headers: {
'x-api-key': CHAINBASE_API_KEY, // Replace the field with your API key.
'accept': 'application/json'
}
};
axios(options)
.then(response => console.log(response.data.data))
.catch(error => console.log(error));
Make sure to replace CHAINBASE_API_KEY
with your actual API key obtained from your Chainbase account.
5. Print the ERC20 Token Balances
The Chainbase API’s getAccountTokens
endpoint takes the chain ID and wallet address as parameters and returns the ERC20 token balances associated with the provided address. You can also specify a particular token by supplying its contract address.
To print the ERC20 token balances, follow these steps:
- Save your script file with a
.js
suffix. - Open your terminal.
- Navigate to the directory where you saved the script file.
- Execute the command
node .js
, replacing `` with the actual name of your script file.
Upon running the script, you’ll receive a response similar to the following, displaying the balances of ERC20 tokens owned by the specified address:
{
"balance": "0x2386f26fc10000",
"contract_address": "0x954b7997b8bfa9b3d642c477549e284551012f05",
"decimals": 9,
"name": "Eterium",
"symbol": "ETE"
},
{
"balance": "0x97e328b058fe88019f7b",
"contract_address": "0xff58ece2d4584139e3f136e18cae27deda947d3b",
"decimals": 18,
"name": "Uniswap V2",
"symbol": "UNI-V2"
},
{
"balance": "0x186a0",
"contract_address": "0xa6de609807c7258a0d34f5307c1808f062a59794",
"decimals": 0,
"name": "$ USDCDrop.com",
"symbol": "$ USDCDrop.com <- Visit to claim"
},
{
"balance": "0x36f4bc072a511af5",
"contract_address": "0x92
d6c1e31e14520e676a687f0a93788b716beff5",
"decimals": 18,
"name": "dYdX",
"symbol": "DYDX"
},
{
"balance": "0x4700c3e20f38dcc",
"contract_address": "0xa0a85f43c5e286187266833a5e986cb8a1a8b9f9",
"decimals": 9,
"name": "Apollo 11",
"symbol": "APOLLO"
}
Congratulations! You have successfully retrieved the ERC20 token balances owned by the specified wallet address.
6. Conclusion
In this article, we explored how to retrieve ERC20 token balances owned by a specific address using the Chainbase API’s getAccountTokens
endpoint. We covered the necessary tools, such as setting up a Chainbase account and obtaining an API key, as well as writing a script using JavaScript and popular libraries like Fetch and Axios. By following the provided steps, you can automate the process of checking your token balances, saving both time and effort.
Remember to keep your API key secure and adhere to our usage guidelines. Now you can confidently build a simple wallet or integrate ERC20 token balance checks into your cryptocurrency projects!
7. FAQs
Q1: Can I use any wallet address with the Chainbase API’s getAccountTokens
endpoint?
Yes, you can use any wallet address as input for the getAccountTokens
endpoint. It will return the ERC20 token balances associated with the provided address.
Q2: How can I obtain an API key from Chainbase?
After registering for a free account at Chainbase and creating a project, you’ll be able to generate an API key associated with that project. The API key serves as a unique identifier for your account when making API requests.
Q3: Are there limitations on the number of tokens I can retrieve using the getAccountTokens
endpoint?
The getAccountTokens
endpoint allows you to specify the number of tokens to retrieve using the limit
parameter. By default, it returns the first 5 tokens. However, you can adjust the limit according to your requirements.
Q4: Can I retrieve balances for tokens on different blockchain networks using the Chainbase API?
Yes, you can specify the chain ID when making API requests to the Chainbase API. This allows you to retrieve balances for tokens on different blockchain networks. Refer to the our documentation for a list of supported chains and their respective IDs.
Q5: How often are the token balances updated by the Chainbase API?
The token balances provided by the our API are based on real-time data from the blockchain networks. Therefore, they are as up-to-date as the underlying blockchain itself. However, keep in mind that delays or congestion on the blockchain network may affect the accuracy of the balances output.
About Chainbase
Chainbase is an all-in-one data infrastructure for Web3 that allows you to index, transform, and use on-chain data at scale. By leveraging enriched on-chain data and streaming computing technologies across one data infrastructure, Chainbase automates the indexing and querying of blockchain data, enabling developers to accomplish more with less effort.
Want to learn more about Chainbase?
Visit our website chainbase.com Sign up for a free account, and Check out our documentation.