English
 找回密码
 立即注册

How to get all holders of a token on

Anatoly 2025-12-20 06:44 66453人围观 SOL

In this guide, we will look at how to get all holders of a fungible token like USDC. This is useful in situations where you want to track token holders or reward holders for airdrops. Overview Let's first look at tokens, specifically how they work on Sola


In this guide, we will look at how to obtain all holders of a fungible token such as USDC. This is useful in situations where you want to track token holders or reward holders for airdrops.

Overview


Let's first look at tokens, specifically how they work on Solana. When developers create a token, they use the Token Program to create a minting account. This minting account holds information about a specific token, such as name, token address, and image. Once the minting account is created, Tokens can be minted and stored in a Token account. Token account is an account that saves information about a specific Token and is owned by a specific address. The Token account will include details such as the minting address (corresponding to the Token Address in the figure below), the owner's address, and the number of specific Tokens in the account. For example, holding some USDC (an SPL Token [1] )'s address will have a USDC Token account.





Account breakdown diagram

‍ Now that we understand how Tokens and Token Accounts work, we can look at how to get all Token holders for a given Token. Each wallet holding a specific Token will have a Token Account for that Token. This means that the Token is associated with the Token account that holds the Token wallet. This is how we will find out all the holders. If we could find a way to get all the token accounts associated with a token, and then get the owners of those accounts, we would have a list of all holders!

Translator's Note: Unlike Ethereum, in which the user's Token balance is stored in the Token contract, in Solana, the balance held by each wallet account is stored in a separate Token account. There is not even a separate Token contract in Solana, but the Token's casting address is used to distinguish different Tokens.

getTokenAccounts method




Fortunately, the Helius **getTokenAccounts** API method[2]Allow us to do this precisely. We can include the minting address of any token in the API call parameter and we will get a list of all token accounts created for that token. In addition to this, the API also returns the owner of each Token account ; This owner is usually called the Token holder. One small thing to note is that one account can have multiple Token accounts for the same Token. it's not a big problem ; We just need to set up some logic to handle the shared owner's token account. ‍

accomplish


Now let's dive into some code and see how we actually do this. You'll need a Helius API key to follow. You can access https://dev.helius.xyz [3] Sign up for a free account to get one. First, we have to create a Javascript file called getTokenHolders.js. We can save our results to a JSON file by adding our Helius URL and importing the fs library.
const url = `https://mainnet.helius-rpc.com/?api-key=`;
const fs = require("fs");

Next, we will create a method to get all token accounts associated with a specific token. We can start by creating a method called findHolders that will use the getTokenAccounts method to get the required data. you can here [4]Learn more about the getTokenAccounts method. One important thing to note is that each call to the API can only return a maximum of 1000 Token accounts. Most large tokens on Solana have over 100,000 token accounts. To solve this problem, we will use pagination to browse all token accounts and continue making API calls until we have fetched data for all existing token accounts. In this method, we will include the Token minting account number in the parameters of the getTokenAccounts call. As we iterate through all token accounts, we will add each unique token account owner to a list. Once the method completes, we will save this list to a JSON file containing all token holders.
const findHolders = async () => {
  // Pagination logic
  let page = 1;
  // allOwners will store all the addresses that hold the token
  let allOwners = new Set();

  while (true) {
    const response = await fetch(url, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        jsonrpc: "2.0",
        method: "getTokenAccounts",
        id: "helius-test",
        params: {
          page: page,
          limit: 1000,
          displayOptions: {},
     //我们关注的token 铸币地址
          mint: "CKfatsPMUf8SkiURsDXs7eK6GWb4Jsd6UDbs7twMCWxo",
        },
      }),
    });
    const data = await response.json();
   // Pagination logic.
    if (!data.result || data.result.token_accounts.length === 0) {
      console.log(`No more results. Total pages: ${page - 1}`);
      break;
    }
   
    console.log(`Processing results from page ${page}`);
   // Adding unique owners to a list of token owners.
    data.result.token_accounts.forEach((account) =>
      allOwners.add(account.owner)
    );
    page++;
  }

  fs.writeFileSync(
    "output.json",
    JSON.stringify(Array.from(allOwners), null, 2)
  );
};

Copy



In the above example, the getTokenAccounts method is called multiple times while paging through all Token accounts. The API response will provide the following data for each Token account:
{
"address": "CVMR1nbxTcQ7Jpa1p137t5TyKFii3Y7Vazt9fFct3tk9",
"mint": "SHDWyBxihqiCj6YekG2GUr7wqKLeLAMK1gHZck9pL6y",
"owner": "CckxW6C1CjsxYcXSiDbk7NYfPLhfqAm3kSB5LEZunnSE",
"amount": 100000000,
"delegated_amount": 0,
"frozen": false
},

We extracted the owners from these token accounts and added them to our list. If we wanted, we could also store the number of tokens held by each token account to find the largest holders.

Now once this operation is complete, all we need to do is call this method:
findHolders();

The complete code in our getTokenHolders.js file should look like this:
const url = `https://mainnet.helius-rpc.com/?api-key=`;
const fs = require("fs");

const findHolders = async () => {
  let page = 1;
  let allOwners = new Set();

  while (true) {
    const response = await fetch(url, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        jsonrpc: "2.0",
        method: "getTokenAccounts",
        id: "helius-test",
        params: {
          page: page,
          limit: 1000,
          displayOptions: {},
          mint: "DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263",
        },
      }),
    });
    const data = await response.json();

    if (!data.result || data.result.token_accounts.length === 0) {
      console.log(`No more results. Total pages: ${page - 1}`);

      break;
    }
    console.log(`Processing results from page ${page}`);
    data.result.token_accounts.forEach((account) =>
      allOwners.add(account.owner)
    );
    page++;
  }

  fs.writeFileSync(
    "output.json",
    JSON.stringify(Array.from(allOwners), null, 2)
  );
};

findHolders();

output


The output of our code will be a list of all holders, looking similar to this:
[
 "111An9SVxuPpgjnuXW9Ub7hcVmZpYNrYZF4edsGwJEW",
 "11Mmng3DoMsq2Roq8LBcqdz6d4kw9oSD8oka9Pwfbj",
 "112uNfcC8iwX9P2TkRdJKyPatg6a4GNcr9NC5mTc2z3",
 "113uswn5HNgEfBUKfK4gVBmd2GpZYbxd1N6h1uUWReg",
 "11CyvpdYTqFmCVWbJJeKFNX8F8RSjNSYW5VVUi8eX4P",
 "11MANeaiHEy9S9pRQNu3nqKa2gpajzX2wrRJqWrf8dQ",

]



You can use our replit example[5]Let's test this.


in conclusion


To summarize below, we have successfully introduced the process of identifying Solana Token holders through the Helius getTokenAccounts API. This step should provide you with the necessary skills to interact directly with your token community.

Original text: https://www.helius.dev/blog/how-to-get-token-holders-on-solana




The Denglian community is the home of blockchain developers. Here you can not only read articles, learn courses, participate in Q&A and discussions, but also publish activities and conduct talent recruitment. We help developers better enter web3.


Technical content of the learning system/release activities, recruitment

https://learnblockchain.cn/


On-chain skills certification platform for developers

https://decert.me/


An open source, free toolbox for developers

https://chaintool.tech/


Follow the community on Twitter

@UpchainDAO


Join the community Discord

https://discord.gg/pZxy3CU8mh


Join WeChat group



References

[1]
SPL Token: https://spl.solana.com/
[2]
Helius getTokenAccounts API method: https://docs.helius.dev/compression-and-das-api/digital-asset-standard-das-api/get-token-accounts
[3]
https://dev.helius.xyz: https://dev.helius.xyz/
[4]
here: https://docs.helius.dev/compression-and-das-api/digital-asset-standard-das-api/get-token-accounts
[5]
replit example: https://replit.com/@owen47/getTokenAccountsExample


精彩评论0
我有话说......
TA还没有介绍自己。