0

Is there a way to convert a VersionedTransaction to a Transaction? I need to combine two transactions into one but with the type of VersionedTransaction it can't be done. Any help?

1 Answer 1

1

Here is how you can convert a versioned transaction to a legacy transaction

// your versioned transaction
    let tx: VersionedTransaction = ....;

    let txInstructions = TransactionMessage.decompile(tx.message).instructions;
    const legacyMessage = new TransactionMessage({
        payerKey: signer.publicKey,
        recentBlockhash: (await connection.getLatestBlockhash()).blockhash,
        instructions: [...txInstructions],
    }).compileToLegacyMessage();

    const legacyTx = Transaction.populate(legacyMessage); 

    const txId = await sendAndConfirmTransaction(connection, legacyTx, [signer]); 

I am unsure of your needs, but instead of converting the tx to a legacy tx, you could pull the instructions to a single versioned transaction and send that.


    let tx1Instructions = TransactionMessage.decompile(tx1.message).instructions;
    let tx2Instructions = TransactionMessage.decompile(tx2.message).instructions;

    const messageV0 = new TransactionMessage({
        payerKey: signer.publicKey,
        recentBlockhash: (await connection.getLatestBlockhash()).blockhash,
        instructions: [...tx1Instructions, ...tx2Instructions],
    }).compileToV0Message();
 
    let tx = new VersionedTransaction(messageV0);
    const txId = await connection.sendTransaction(tx);
    console.log(`https://explorer.solana.com/tx/${txId}?cluster=devnet`);
1
  • I want to combine the VersionedTransaction with another Transaction and send them both. VersionedTransaction is from swap API and another one is normal Transaction. Commented Jul 7 at 17:52

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.