2

When I use the method of setting the compute unit price of a transaction message for example a token swap, it sets the priority fee as intended. But when I try to set this compute unit price for a simple transfer message, it doesn't seem to utilize the priority fees. I am not having issues with the transfer itself, it executes perfectly fine and very quickly, but I don't really seem to understand why it doesn't utilize it. I understand that priority fees are based on the number of instructions, compute limit per instruction, etc. Is it because it is a system program instruction? I am just looking for some insight at why this happens.

Code below is just for context:

recentBlockhash = ((await w3.get_latest_blockhash(commitment=Commitment("confirmed"))).value).blockhash
priorityFee = set_compute_unit_price(gas)
amount = int(amount * 10 ** 9)

transactionMessage = Message(
    [
        transfer(
            TransferParams(
                from_pubkey=userKeypair.pubkey(),
                to_pubkey=Pubkey.from_string(toAddress),
                lamports=amount,
            )
        )
    ],
    userKeypair.pubkey(),
)
accountKeys = transactionMessage.account_keys
rawInstructions = transactionMessage.instructions

newInstruction = CompiledInstruction(
    program_id_index=len(accountKeys),
    data=priorityFee.data,
    accounts=rawInstructions[0].accounts,
)

rawInstructions.append(newInstruction)

newTransactionMessage = Message.new_with_compiled_instructions(
    num_required_signatures=transactionMessage.header.num_required_signatures,
    num_readonly_signed_accounts=transactionMessage.header.num_readonly_signed_accounts,
    num_readonly_unsigned_accounts=transactionMessage.header.num_readonly_unsigned_accounts,
    account_keys=accountKeys,
    recent_blockhash=recentBlockhash,
    instructions=transactionMessage.instructions,
)

userSignature = userKeypair.sign_message(message.to_bytes_versioned(newTransactionMessage))
signedTransaction = VersionedTransaction.populate(newTransactionMessage, [userSignature])

return signedTransaction

1 Answer 1

2

You aren't using your new rawInstructions when you create and send this transaction.

You create the rawInstructions with the original transactionMessage.instructions

rawInstructions = transactionMessage.instructions

Then you make the newInstruction and append to rawInstruction

newInstruction = CompiledInstruction(...)
rawInstructions.append(newInstruction)

The error is that the transaction you sign and send uses the original transactionMessage.instructions, not the rawInstructions which has the priority fee.

newTransactionMessage = Message.new_with_compiled_instructions(
    ...,
    instructions=transactionMessage.instructions,
    // should be:   instructions=rawInstructions,

)

Aside from that issue, adding instructions then using the original transaction for signers and accounts is not good practice. There may not be new signers/accounts with the priority instruction but there could be if you add other instructions the same way and then use the original lists of signers and accounts. You may want to rewrite this to create all transaction instructions, then add them to the transaction and check signers/accounts for the full instruction set, and finally add blockhash, sign, and send.

1
  • Thanks for the feedback, I'll be sure to update it. Commented Feb 14 at 18:46

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.