Introduction
In the world of cryptocurrency, knowing the holders of a particular token can provide valuable insights and opportunities for collaboration. Chainbase, a leading platform, offers a powerful API called getTokenHolders
that allows you to retrieve a list of addresses for all the holders of a specific ERC20 token. This tutorial will guide you through the process of using Chainbase API to get the holders of a cryptocurrency deployed on various chains. By leveraging this information, you can devise strategies to engage with these holders and enhance the overall value of your cryptocurrency.
Outline:
Overview - Tools you need to work with Chainbase Step 1: Set up a free account at Chainbase Step 2: Write a script using Chainbase API Step 3: Print token holders Conclusion FAQs
Overview - Tools you need to work with Chainbase
To interact with the Chainbase API and retrieve NFT ownership information, you’ll need the following:
- A free account at Chainbase with an API key.
- An Integrated Development Environment (IDE) such as Visual Studio Code.
- The contract address of a known NFT collection and the token ID of the specific NFT you want to query.
Step 1: Set up a free account at Chainbase
To take full advantage of Chainbase’s capabilities, you need to create a free account. Follow these steps:
- Register for a free account on the Chainbase website.
- Once registered, log in to your account and visit the dashboard to gain an overview of the available features.
- Create a new project within the Chainbase console.
- Obtain an API key associated with your project.
Step 2: Write a script using Chainbase API
To fetch the holders of an ERC20 token, you need to write a script that interacts with the Chainbase API. We’ll demonstrate two methods using JavaScript: fetch and axios.
Using fetch in JavaScript
network_id = '1'; // See https://docs.chainbase.com/reference/supported-chains to get the id of different chains.
token_addr = '0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0'; // Take MATIC token address as an example.
fetch(`https://api.chainbase.online/v1/token/holders?chain_id=${network_id}&contract_address=${token_addr}&page=1&limit=20`, {
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 it by running npm install axios --save
in your terminal.
network_id = '1'; // See https://docs.chainbase.com/reference/supported-chains to get the id of different chains.
token_addr = '0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0'; // Take MATIC token address as an example.
const axios = require('axios');
const options = {
url: `https://api.chainbase.online/v1/token/holders?chain_id=${network_id}&contract_address=${token_addr}&page=1&limit=20`,
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));
Step 3: Print token holders
Chainbase API’s getTokenHolders
requires the chain ID and the contract address of the ERC20 token as parameters. It returns a list of token holders. You can also utilize pagination and set limits using the page
and limit
parameters.
To retrieve and print the data, execute the following command in your terminal: node .js
. The returned data will be similar to the following example:
[
"0xe31fcac26fccf1fda8a99b033bfae82c8c44df63",
"0xf60328d7eabe4196a7d530721d4da5316c435fc5",
"0x38a0c3aaaac2736915f0e5e7044aa6fd90c22f7e",
"0xfa79dcccb2c4c8eaaf21de271020919e1a4b093e",
"0x00e4487f5b43540614ddcb835b5afdabe19f1ed4",
"0x2f13499c1e391896762b8c1a6c88390113ca54c4",
"0xcad779f8b51f1043efbd365f6b7353670633415c",
"0x880e295d5fabc24340b547146b29119a7aa3ce9f",
"0xc75b46aea8538e6f1269d2fe73d1ca92002b1c37",
"0x6067aedebf0704d178674b16dbf0d7c6c5d5d03e",
"0xcd97b55b07d2a4f397ea513c0fd9f899fa01fbe1",
"0xed6425f466ebd178fc3060a0d60eb1a609cf5558",
"0x8c372e79195f0113942012e7e52dfe30c5606962",
"0xbdc034c242e4ef08e56539bc6141c1e3847b60b9",
"0x28c497c274bfae45bcadd2f19a941d346fc741ab",
"0x1ad9a52e8d1f1e957415d3407ab9a8bdcf5decd1",
"0x73c4eb25fadf7edb6087f130fea23a90aab88a65",
"0xad270298f03ae7ed9f211fe25f4b90a2c711262d",
"0xde85dfd2e53d0f01852a4a64d0e3ab644fb0e965",
"0x51edc446851a6063c6489f575602e4482e9a0bff"
]
Please note that the order of token holders may vary each time due to the API returning unsorted data. If you specifically require the top token holders, you can explore another API called getTopTokenHolders
.
Conclusion
Knowing the holders of an ERC20 token is essential for effective engagement and collaboration within the cryptocurrency ecosystem. Chainbase’s getTokenHolders
API empowers you to retrieve the addresses of all token holders with ease. By leveraging this information, you can develop strategies to enhance the value and impact of your cryptocurrency.
Now that you have learned how to use Chainbase’s API to obtain token holders, you can explore new possibilities and create innovative solutions that contribute to the growth and success of your cryptocurrency.
FAQs
-
Q: Can I use Chainbase’s
getTokenHolders
API for any ERC20 token?A: Yes, you can utilize the
getTokenHolders
API for any ERC20 token deployed on supported chains. -
Q: How can I obtain an API key for Chainbase?
A: Once you have created a free account on our website, you can generate an API key within the project dashboard.
-
Q: Can I retrieve more than 20 token holders in a single API call?
A: Yes, you can adjust the
limit
parameter in the API call to retrieve a higher number of token holders. -
Q: Are the token holders returned in a specific order?
A: The
getTokenHolders
API returns unsorted token holders. If you need the top token holders, consider using thegetTopTokenHolders
API. -
Q: How often does the data of token holders change?
A: The data of token holders can change frequently, depending on various factors such as transactions and token transfers.
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.