3

This is my current code.

CODE:

from solana.rpc.api import Client
from solana.keypair import Keypair
from solana.transaction import Transaction
from solana.system_program import TransferParams, transfer

LAMPORT_PER_SOL = 1000000000

client = Client(devnetrpc)

transaction = Transaction().add(transfer(TransferParams(
    from_pubkey=sender.public_key,
    to_pubkey=receiver.public_key,
    lamports=int(0.01*LAMPORT_PER_SOL))
))

response = client.send_transaction(transaction, sender)
print (response)
send_signature = response["result"]
status = client.confirm_transaction(send_signature)
print (status)

This will send a legacy transaction. I want to add priority fee to the transaction. Any sample code is appreciated.

2 Answers 2

2

You'll need to use the compute_budget module directly from the solders library: https://kevinheavey.github.io/solders/api_reference/compute_budget.html

Here's an example of how to add it to your transaction:

from solana.rpc.api import Client
from solana.keypair import Keypair
from solana.transaction import Transaction
from solana.system_program import TransferParams, transfer
from solders.compute_budget import set_compute_unit_price

LAMPORT_PER_SOL = 1000000000

client = Client(devnetrpc)

transaction = Transaction().add(set_compute_unit_price(1_000)).add(transfer(TransferParams(
    from_pubkey=sender.public_key,
    to_pubkey=receiver.public_key,
    lamports=int(0.01*LAMPORT_PER_SOL))
))
2
  • I was looking for the same solution, but the example above throws error "solders.SignerError: not enough signers" Commented Mar 13 at 7:23
  • There's a bug somewhere, I haven't had the chance to look into it yet, but here's a workaround: solana.stackexchange.com/a/11568/84
    – Jon C
    Commented Mar 13 at 12:51
2

fixed 'not engouh signers' error

from solders.keypair import Keypair
from solders.pubkey import Pubkey
from solders.system_program import TransferParams, transfer
from solders.compute_budget import set_compute_unit_limit, 
set_compute_unit_price

transaction = (
    Transaction(fee_payer=sender.pubkey(), recent_blockhash=recent_blockhash)
    .add(set_compute_unit_price(transfer_fee))
    .add(
        transfer(
            TransferParams(
                from_pubkey=sender.pubkey(),
                to_pubkey=Pubkey.from_string(receiver_address),
                lamports=amount,
            )
        )
    )
)
result = solana_client.send_transaction(
    transaction,
    sender,
    opts=TxOpts(preflight_commitment=Processed),
)

specifying 'fee_payer' when initializing Transaction() fixed the problem for me

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.