From c822ec5d26f5d8758ddd45c8b1f26068d7c02cea Mon Sep 17 00:00:00 2001 From: godmodegalactus Date: Mon, 1 Jul 2024 15:47:41 +0200 Subject: [PATCH] Adding tests for versioned transactions --- services/src/transaction_service.rs | 4 ++-- tests/client.test.ts | 37 +++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/services/src/transaction_service.rs b/services/src/transaction_service.rs index ce34caef..477e4d97 100644 --- a/services/src/transaction_service.rs +++ b/services/src/transaction_service.rs @@ -21,7 +21,7 @@ use solana_lite_rpc_core::{ }; use solana_sdk::{ compute_budget::{self, ComputeBudgetInstruction}, - transaction::{Transaction, VersionedTransaction}, + transaction::VersionedTransaction, }; use tokio::{ sync::mpsc::{self, Sender, UnboundedSender}, @@ -122,7 +122,7 @@ pub struct TransactionService { impl TransactionService { pub async fn send_transaction( &self, - tx: Transaction, + tx: VersionedTransaction, max_retries: Option, ) -> anyhow::Result { let raw_tx = bincode::serialize(&tx)?; diff --git a/tests/client.test.ts b/tests/client.test.ts index d3dbaeb2..6a64fb88 100644 --- a/tests/client.test.ts +++ b/tests/client.test.ts @@ -1,4 +1,4 @@ -import { Connection, Keypair, sendAndConfirmTransaction, Transaction, PublicKey, TransactionInstruction, BlockheightBasedTransactionConfirmationStrategy, TransactionMessage, VersionedTransaction } from "@solana/web3.js"; +import { Connection, Keypair, sendAndConfirmTransaction, Transaction, PublicKey, TransactionInstruction, BlockheightBasedTransactionConfirmationStrategy, TransactionMessage, VersionedTransaction, VersionedMessage, MessageV0 } from "@solana/web3.js"; import * as fs from "fs"; import * as os from "os"; import * as crypto from "crypto"; @@ -24,10 +24,28 @@ function createTransaction(): Transaction { return transaction; } +function createVersionedMessage(blockhash: string, payer: PublicKey): MessageV0 { + return new MessageV0({ + header: { + numRequiredSignatures: 1, + numReadonlySignedAccounts: 0, + numReadonlyUnsignedAccounts: 0, + }, + staticAccountKeys: [payer, MEMO_PROGRAM_ID], + recentBlockhash: blockhash, + compiledInstructions: [{ + programIdIndex: 1, + accountKeyIndexes: [], + data: Buffer.from(crypto.randomBytes(20).toString('hex')), + }], + addressTableLookups: [], + }) +} + test('send and confirm transaction BlockheightBasedTransactionConfirmationStrategy', async () => { const tx = createTransaction(); const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash(); - const signature = await connection.sendTransaction(tx, [payer]); + const signature = await connection.sendTransaction(tx, [payer], {}); console.log(`https://explorer.solana.com/tx/${signature}`); await connection.confirmTransaction({ blockhash, @@ -45,6 +63,21 @@ test('send and confirm transaction legacy confrim', async () => { await connection.confirmTransaction(signature); }); +test('send and confirm versioned transaction', async () => { + const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash(); + const message = createVersionedMessage(blockhash, payer.publicKey); + let versionedTransaction = new VersionedTransaction( message, [payer.secretKey]) + versionedTransaction.sign([payer]) + const signature = await connection.sendRawTransaction(versionedTransaction.serialize(), {}); + console.log(`https://explorer.solana.com/tx/${signature}`); + await connection.confirmTransaction({ + blockhash, + lastValidBlockHeight, + signature, + abortSignal: undefined + }); +}); + test('send and confirm transaction', async () => { const tx = createTransaction();