Introduction

This tutorial will show you how to use the Chainbase API’s getTopTokenHolders function to get the top holders of a cryptocurrency that is on the Ethereum blockchain. Knowing who holds the most tokens can be helpful for many reasons, such as reaching out to them for collaborations and keeping track of the flow of funds.

Table of Contents

  1. Overview - Tools you need to work with Chainbase
  2. Step 1: Set up a free account at Chainbase
  3. Step 2: Write a script using the Chainbase API
  4. Step 3: Print token holders
  5. Conclusion
  6. FAQs

1. Overview - Tools you need to work with Chainbase

To get started, you will need the following tools:

  • A free account at Chainbase with an API key.
  • An integrated development environment (IDE). We recommend using Visual Studio Code (VS Code).

2. Step 1: Set up a free account at Chainbase

To leverage the capabilities of Chainbase, follow these steps:

  • Register for a free account on the Chainbase website.
  • Log in to your Chainbase account and visit the dashboard to get an overview.
  • Create a new project in the console to obtain an API key.

Untitled

3. Step 2: Write a script using the Chainbase API

You can use JavaScript and the Chainbase API to retrieve the top token holders. Here are two examples using different libraries: fetch and axios.

Using fetch in JavaScript

javascriptCopy code
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/top-holders?chain_id=${network_id}&contract_address=${token_addr}&page=1&limit=5`, {
    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.

javascriptCopy code
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/top-holders?chain_id=${network_id}&contract_address=${token_addr}&page=1&limit=5`,
    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));

4. Step 3: Print token holders

The getTopTokenHolders function from the Chainbase API requires the chain ID and the contract address of the ERC20 token as input parameters. This function returns information regarding the top token holders. Also, you can use the page and limit parameters for pagination and to retrieve a specific number of top holders.

To print the token holders, execute the command node .js in the terminal, where `` represents the name of your script. Here is an example of the expected JSON data returned by the API:

jsonCopy code
{
  "count": 593525,
  "holders": [
    {
      "wallet_address": "0x5e3ef299fddf15eaa0432e6e66473ace8c13d908",
      "amount": "3814264335.329752",
      "usd_value": "4167560569.389670"
    },
    {
      "wallet_address": "0x401f6c983ea34274ec46f84d70b31c151321188b",
      "amount": "910440417.955799",
      "usd_value": "994769961.668955"
    },
    {
      "wallet_address": "0xcd6507d87f605f5e95c12f7c4b1fc3279dc944ab",
      "amount": "590639315.000000",
      "usd_value": "645347281.551875"
    },
    {
      "wallet_address": "0xb316fa9fa91700d7084d377bfdc81eb9f232f5ff",
      "amount": "434226114.715070",
      "usd_value": "474446308.590553"
    },
    {
      "wallet_address": "0xcbfe11b78c2e6cb25c6eda2c6ff46cd4755c8fca",
      "amount": "273304816.000000",
      "usd_value": "298619674.582000"
    }
  ]
}

5. Conclusion

By following the tutorial steps, you have learned how to use the Chainbase API’s getTopTokenHolders function to get details about the top holders of an ERC20 token deployed on the Ethereum blockchain.

6. FAQs

Q1: Can I use the Chainbase API for tokens deployed on other blockchain networks?

Currently, we only provide information on Ethereum ERC-20 token holders. Data on additional chains will be available soon.

Q2: How can I increase the number of token holders fetched?

You can adjust the limit parameter in the API request to fetch more token holders. Increase the value to retrieve a larger list, bearing in mind that excessively high values may impact performance.

Q3: Is it possible to sort the token holders based on their holdings?

The Chainbase API already returns the token holders in descending order based on their holdings. The first holder in the list represents the top holder.

Q4: Can I obtain additional information about the token holders?

Yes, the API response provides the wallet address, the amount of tokens held, and the equivalent USD value. You can use this data to analyze and engage with the token holders.

Q5: How frequently does the Chainbase API update the token holder information?

Our API is unique in the industry because it provides real-time token holder information that can be sorted. Other APIs, like those used in browsers, usually have a cache value that lasts about 20 minutes.


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.

WebsiteBlogTwitterDiscordLink3