michaelhly / solana-py

Solana Python SDK
https://michaelhly.github.io/solana-py
MIT License
979 stars 252 forks source link

Solana Transaction Error #419

Open Anthonyslav1 opened 5 months ago

Anthonyslav1 commented 5 months ago

I have this error while trying to create a python code to do solana transfer

Traceback (most recent call last): File "/workspaces/Web3-Wallet/transfer.py", line 118, in Sol_Transfer(1, 'EccxYg7rViwYfn9EMoNu7sUaV82QGyFt6ewiQaH1GYjv', 9) File "/workspaces/Web3-Wallet/transfer.py", line 114, in Sol_Transfer return solana_client.send_transaction(txn) File "/home/codespace/.python/current/lib/python3.10/site-packages/solana/rpc/api.py", line 1048, in send_transaction txn.recent_blockhash = recent_blockhash AttributeError: 'solders.transaction.Transaction' object has no attribute 'recent_blockhash'

' ' ' Python

def Sol_Transfer(user_number, recipient: str, amount: float):

    private_key = Gen_private_key.get_private_key(user_number)

    private_key_bytes = bytes.fromhex(private_key)

    # Ensure the private key bytes are 32 bytes long
    if len(private_key_bytes) != 32:
        raise ValueError("Private key must be 32 bytes long")

    # Create a Keypair from the 32-byte private key
    sender_keypair = Keypair.from_seed(private_key_bytes)

    # Correctly derive the public key from the Keypair
    sender_pubkey = sender_keypair.pubkey()

    # Create a Message instance
    message = Message(
        # account_keys=[sender_pubkey, PublicKey.from_string(recipient)],
        instructions=[transfer(TransferParams(from_pubkey=sender_pubkey, to_pubkey=PublicKey.from_string(recipient), lamports=amount * 1_000_000))],
    )

    # Get a recent blockhash
    solana_client = Client("https://api.mainnet-beta.solana.com")
    # Get the latest blockhash
    latest_blockhash_resp = solana_client.get_latest_blockhash()
    recent_blockha = getattr(latest_blockhash_resp.value, 'blockhash', None) 
    # Assuming recent_blockhash is already obtained as shown in your code
    # Create a Transaction instance
    txn = Transaction([sender_keypair],message,recent_blockha)

    # Sign the transaction with the recent_blockhash
    txn.sign([sender_keypair], recent_blockha)

    # Send the transaction
    return solana_client.send_transaction(txn)

' ' '

i am referred to /solana/rpc/api.py

if recent_blockhash is None: blockhash_resp = self.get_latest_blockhash(Finalized) recent_blockhash = self.parse_recent_blockhash(blockhash_resp) last_valid_block_height = blockhash_resp.value.last_valid_block_height txn.recent_blockhash = recent_blockhash

medvi commented 1 month ago

I have the same problem. It looks like a bug..?

I use the following versions of solders and solana:

solders = "^0.21.0"
solana = "^0.34.2"

and python version 3.11.4

Did you find a solution?

kevinheavey commented 1 month ago

this is not a bug because using a Solders legacy Transaction in send_transaction isn't supported, but it is confusing so we are considering removing the solana-py Transaction class entirely to avoid this confusion https://github.com/michaelhly/solana-py/pull/442