1

Solana community!

I'm working on a Python script to filter transactions executed through the SPL Token Program. I aim to specifically identify and list new token creation transactions. However, I'm encountering an issue when attempting to read the SPL Token details within the transaction. Here's a brief overview of my approach:

  • I'm using the PubKey function from the solana package.
  • I pass the relevant addresses to this function to filter the transactions.

Unfortunately, I'm facing an error at this stage of my script. The error message is as follows: [Insert the error message here].

Here's a snippet of the code that's causing the problem:

 url = "https://api.mainnet-beta.solana.com"
    client = Client(url)
    spl_token_program = Pubkey("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA")
    filters = [
        {"dataSize": 165}  
    ]
    response = client.get_program_accounts(spl_token_program, {"filters": filters})
    token_accounts = response['result']

But I got

spl_token_program = Pubkey("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA")
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: expected a sequence of length 32 (got 43)

Could anyone please help me identify what might be going wrong here or suggest an alternative approach to filter for new token creations in the SPL Token Program?

Thank you in advance for your assistance!

1 Answer 1

2

The error you're encountering is because the Pubkey constructor expects a byte sequence of length 32, but you're passing a string that is longer. In the Solana Python library, there's a specific method to handle such cases where public keys are provided as strings.

Correct Usage with solana.rpc.types.Pubkey.from_string()

You should use the from_string() method to convert the string representation of the public key into a valid Pubkey object. Here’s how you can adjust your code:

import solana
from solana.rpc.api import Client

spl_token_program = solana.rpc.types.Pubkey.from_string("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9

Using Solders for Enhanced Key Handling

Solders is a comprehensive library that offers functionalities around cryptography and transaction signing in Solana. It is a great tool to handle keypairs and signatures when working with Solana in Python.

To use Solders for signature handling, you can do something like this:

from solders.signature import Signature

# If you need to handle or verify a signature as a string:
signature_string = "5eykt4UsF...PSKWJH"  # Example signature
signature = Signature.from_string(signature_string)

This method can be particularly useful if you are dealing with transaction signatures and need to verify or process them in your application.

Summary

For handling public keys from strings, use PublicKey.from_string().

For cryptographic operations and enhanced handling of keypairs and signatures, consider integrating Solders into your project.

Both methods will help in ensuring your code handles key-related operations more effectively and aligns with best practices in Solana development.

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.