2

I've been trying to use the getProgramAccounts method in the Solana Python API to get the top 5 holders of a specific token. My code looks like this:

from solana.rpc.types import MemcmpOpts
from typing import List, Union
from solders.pubkey import Pubkey
from solana.rpc.api import Client

def get_token_holders(mint_address: str):
    solana_client = Client("https://api.mainnet-beta.solana.com/")
    
    memcmp_opts = MemcmpOpts(offset=4, bytes="3Mc6vR")
    
    pubkey = Pubkey.from_string(mint_address)
    
    filters: List[Union[int, MemcmpOpts]] = [17, memcmp_opts]
    
    response = solana_client.get_program_accounts(pubkey, filters=filters)
    return response

mint_address = "GwCDiUnJjmGhQrtpXQ1T9an7kbEb76ouwcFsEMjkBXey"
response = get_token_holders(mint_address)
print(response)

However, this code returns an empty list. My goal is to get the top 5 holders of the token specified by mint_address. What am I doing wrong, and how can I achieve this?

1 Answer 1

1

I didn't end up using that method, so I'm not entirely sure what the issue was. However, the most optimal way to get the largest holders is by using the getTokenLargestAccounts RPC method. This method will return the largest holders of a given PubKey.

Here's a sample code snippet to demonstrate how to use getTokenLargestAccounts:


def get_token_largest_accounts(token_mint_address):
    url = "https://api.mainnet-beta.solana.com"
    headers = {"Content-Type": "application/json"}
    data = {
        "jsonrpc": "2.0",
        "id": 1,
        "method": "getTokenLargestAccounts",
        "params": [
            token_mint_address
        ]
    }

    response = requests.post(url, headers=headers, data=json.dumps(data))
    if response.status_code == 200:
        return response.json()
    else:
        print(f"Error: Failed to fetch data with status code {response.status_code}")
        return None

Replace "YourTokenMintAddressHere" with the actual token mint address you are interested in. This script will fetch and print the largest accounts holding the specified token.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.