1

hi i'm using versioned tx but i'm not quite sure how to include instructions. here is my initial transaction code:

    const payload = {
      quoteResponse: quoteData,
      userPublicKey: WALLET_ADDRESS,
      wrapUnwrapSOL: true,
      dynamicComputeUnitLimit: true,
      dynamicSlippage: { maxBps: 300 },
      prioritizationFeeLamports: { priorityLevelWithMaxLamports: { maxLamports: 10000000, priorityLevel: "veryHigh" } },
    };


    const swapResponse = await fetch(SWAP_URL, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(payload),
    });
    const { swapTransaction } = await swapResponse.json();


    // Decode and sign transaction
    const transactionBuffer = Buffer.from(swapTransaction, 'base64');
    const transaction = VersionedTransaction.deserialize(transactionBuffer);
    transaction.sign([PRIVATE_KEY]);

    // Send transaction
    const txid = await connection.sendRawTransaction(transaction.serialize(), {
      skipPreflight: false,
      preflightCommitment: 'processed',
      maxRetries: 3,
    });

....

would you know how i may push an instruction such as:

MY TRANSACTION.push(
        SystemProgram.transfer({
          fromPubkey: owner.publicKey,
          toPubkey: new PublicKey(ACCOUNT), accounts
          lamports: X,
        }),
      );

to my transaction before it is sent?

Thanks

1
  • I think i need to construct a new txn based on this one, put all instructions from this txn to the new one together with ur tip ix, then sign and send the new txn. But not sure how it would work
    – fboi1
    Commented Nov 2 at 22:22

1 Answer 1

2

Keeping in mind the transaction size limit of 1232 bytes, If using a versioned transaction you'll need to create a new transaction message and include your instruction there,

import { TransactionMessage, VersionedTransaction } from "@solana/web3.js";

// -- snip

const transaction = VersionedTransaction.deserialize(transactionBuffer);
transaction.sign([PRIVATE_KEY]);

let swapIx = TransactionMessage.decompile(transaction.message).instructions;

const messageV0 = new TransactionMessage({
    payerKey: owner.publicKey,
    recentBlockhash: (await connection.getLatestBlockhash()).blockhash,
    instructions: [...swapIx, ], // add additional instructions here
}).compileToV0Message();

let tx = new VersionedTransaction(messageV0);

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.