2

I have a serialized transaction from the Jupiter Swap API from which I can create a versioned transaction.

raw_tx = VersionedTransaction.from_bytes(base64.b64decode(serialized_transaction))

But I want to add priority fees my transaction, I use the following to create the instruction:

priorityFee = set_compute_unit_price(100000)

Since the priority fee would look like this when printed out:

Instruction { program_id: ComputeBudget111111111111111111111111111111, accounts: [], data: [3, 160, 134, 1, 0, 0, 0, 0, 0] }

How would I add this to my versioned transaction which looks like this:

Message { header: MessageHeader { num_required_signatures: 2, num_readonly_signed_accounts: 1, num_readonly_unsigned_accounts: 6 }, account_keys: [FerNfB9EMGLgdKMsQpP8Q5hzQLhcQUXGRc1wuC5Ybq3Y, HVcqYDdERth6wcemnvejAY6X2AWZaex31o94TsboRfjT, 9D5b1NHqqveAF4LTTXXJJbm5mnh3ugQ2jzRu1T3auQjd, GmnWWYa4gcZihoHeNpChSF9Lq8nfSMz4AJPzC2sawSAV, GTNw5SSuVxcdGZHdKuzx7qguS6JEcQV9oSVw8vCLqfwt, 11111111111111111111111111111111, 7GCihgDB8fe6KNjn2MYtkzZcRjQy3t9GHdC8uHYmW2hr, jupoNjAxXgZ4rjzxzPMP4oxduvQsQtZzyknqvzYNrNu, So11111111111111111111111111111111111111112, SysvarRent111111111111111111111111111111111, TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA], recent_blockhash: 3ShkR1fNXRxJV4aPtUApN6rRb6HHY8VwbGohu29RrsmQ, instructions: [CompiledInstruction { program_id_index: 7, accounts: [1, 0, 3, 4, 2, 8, 2, 7, 6, 5, 10, 9], data: [133, 110, 74, 175, 112, 159, 245, 159, 0, 45, 49, 1, 0, 0, 0, 0, 0, 48, 239, 125, 186, 2, 0, 0, 0] }] }

Do I have to create a new transaction from scratch or is there a way I can append it to the existing transaction?

2
  • You can ask Jupiter to add this priority fee, which will be easier than modifying the serialized transaction: station.jup.ag/docs/apis/…
    – Callum M
    Commented Dec 29, 2023 at 15:14
  • 1
    Can't believe I missed this, thanks 🥲 Commented Dec 30, 2023 at 20:20

2 Answers 2

2

You can always add multiple instructions in whatever order you want in a transaction (assuming it doesn't exceed the max transaction size of course). But how you do it depends on the SDK/language you're using.

Seeing the tag, seems like you're using Python's Solana.py. In that case. First add the priority fees instruction to your transaction and then the swap.

Before creating a version transaction object directly from serialized_transaction. First load it as a normal transaction using Transaction object. Then, get the instructions from data attribute and store it in an array with your priority fees instruction already stored and then finally form a version transaction using those instructions. Pseudo code example:

serialized_transaction = "your_tx_data"
loaded_tx = Transaction.from_bytes(serialized_transaction)
priorityFee = set_compute_unit_price(100000)
instructions = [priorityFee]

existing_ix = loaded_tx.data
instructions = instructions.append(existing_ix)

raw_tx = VersionedTransaction(instructions, ...other_data)
3
  • I figured it out in the end, thanks for the help anyways! Commented Dec 29, 2023 at 21:04
  • 1
    Came back to this over a month later and it fixed another issue I came across, thanks! Commented Feb 15 at 10:06
  • How did you do eventually ? I am stuck too Commented Mar 23 at 16:18
1

I used the method below and it works

priorityFee = set_compute_unit_price(priority_fee)

raw_tx = VersionedTransaction.from_bytes(base64.b64decode(serialized_transaction))

account_keys = raw_tx.message.account_keys
length_account_keys = len(account_keys)
account_keys.append(priorityFee.program_id)

raw_instructions = raw_tx.message.instructions

new_instruction = CompiledInstruction(
    program_id_index=length_account_keys,
    data=priorityFee.data,
    accounts=raw_instructions[0].accounts,
)

raw_instructions.append(new_instruction)

new_tx = Message.new_with_compiled_instructions(
    num_required_signatures=raw_tx.message.header.num_required_signatures,
    num_readonly_signed_accounts=raw_tx.message.header.num_readonly_signed_accounts,
    num_readonly_unsigned_accounts=raw_tx.message.header.num_readonly_unsigned_accounts,
    account_keys=account_keys,
    recent_blockhash=raw_tx.message.recent_blockhash,
    instructions=raw_instructions,
)
1
  • this does not work for me. Could you tell what corresponds program_id_index=length_account_keys and accounts=raw_instructions[0].accounts in the CompiledInstruction please Commented Mar 23 at 19:04

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.