Solana: Error “KeyExcludedFromSecondaryIndexMessage” When Using get_program_accounts for SPL Token on Solana

“KeyExcludedFromSecondaryIndexMessage” error when using get_program_accounts for SPL token on Solana

As a developer building applications on the Solana blockchain, it is essential to understand the intricacies of the Solana ecosystem. In this article, we will dive into an error that occurs when using the get_program_accounts function from the solana-py library to retrieve the top 100 holders of a specific SPL token.

The Problem: KeyExcludedFromSecondaryIndexMessage

When you call get_program_accounts, Solana returns a list of program accounts, which are the addresses that have access to the blockchain. However, in some cases, a key may be excluded from this secondary index message due to various reasons, such as:

  • The account is not authorized to transact or read.
  • The account has been disabled.
  • There is a problem with the program account configuration.

In our case, the “KeyExcludedFromSecondaryIndexMessage” error occurs when get_program_accounts returns a list of accounts that do not have any programs associated with them. This usually happens for the following reasons:

  • The user has restricted access to the account.
  • The program that owns the account does not have permission to be included in the secondary index.

Why you might encounter an error

If you encounter an “Error: KeyExcludedFromSecondaryIndexMessage” when using get_program_accounts for your SPL token, here are some potential reasons:

  • Missing programs

    : The program that owns the account may not have any associated with it.

  • Restricted access: The user has restricted access to the account or program.
  • Program configuration issues: There is a problem with the way the program is configured.

How ​​to fix the issue

To resolve this issue, you can try the following steps:

  • Check the get_program_accounts call: Make sure you are calling get_program_accounts correctly and passing the required arguments.
  • Check account access: Verify that the user has access to the account by checking their permissions against the Solana API documentation.
  • Adjust the program configuration: If possible, adjust the program configuration to grant access to the account or modify its rules.

Code Example

Here is an example of how you can use get_program_accounts to retrieve the top 100 holders of a specific SPL token on Solana:

import solana.publickey as pk

from solana programs import get_account, get_account_info

def main():


Set your account ID and SPL token name

account_id = "your_account_id"

spl_token_name = "your_spl_token_name"


Create a client instance with the Solana CLI tool

from solana.web3 import Web3

web3 = Web3()


Get the account information for your SPL token holder

spl_token_holders = get_program_accounts(web3, account_id, spl_token_name)

print("Top 100 holders of", spl_token_name)

print(spl_token_holders[:100])

if __name__ == "__main__":

main()

Conclusion

In this article, we explored an error that occurs when using get_program_accounts to retrieve the top 100 holders of a specific SPL token on Solana. By understanding the possible reasons behind this error and following the steps outlined in the example, you should be able to resolve the issue and successfully use get_program_accounts with your SPL token holder.

Leave a Reply

Your email address will not be published. Required fields are marked *