KeyExcludedFromSecondaryIndexMessage error when using get_program_accounts for SPL tokens on Solana
As a developer building applications on the Solana blockchain, it’s important to understand the intricacies of the Solana ecosystem. In this article, we will look at an error that occurs when using the get_program_accounts function from the solana-py library to get the top 100 holders of a given 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 can be excluded from this secondary index message for 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 have no programs associated with them. Common reasons for this are:
- The user has limited access to the account.
- The program that owns the account does not have permission to be included in the secondary index.
Why this error occurs
If you encounter an “Error: KeyExcludedFromSecondaryIndexMessage” when using “get_program_accounts” for your SPL token, here are some possible reasons:
- Missing programs: The program that owns the account may not have any associated with it.
- Limited access
: The user has limited access to the account or program.
- Program configuration issues: There is a problem with the program’s configuration.
How to fix the issue
To fix this issue, you can try the following steps:
- Check the call to
get_program_accounts
: Make sure you are callingget_program_accounts
correctly and passing the required arguments.
- Check account access: Check if the user has access to the account by checking their permissions against Solana’s API documentation.
- Adjust program configuration: If possible, adjust the program configuration to grant access to the account or change its rules.
Example code
Here is an example of how you can use get_program_accounts
to get the top 100 holders of a given SPL token on Solana:
import solana.public and pk
from solana programs import get_account, get_account_info
do main():
Define your account ID and SPL token nameaccount_id = "your_account_id"
spl_token_name = "your_spl_token_name"
Create a client instance using the Solana CLI toolfrom solana.web3 import Web3
web3 = Web3()
Get the account information for your SPL token holderspl_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 investigated an error that occurs when using get_program_accounts
to retrieve the top 100 holders of a given SPL token on Solana. By understanding the possible reasons for 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.