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
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`);
-
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