Skip to content

Solana Transaction Error #419

Description

@Anthonyslav1

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions