Skip to content

Commit

Permalink
Adding tests for versioned transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
godmodegalactus committed Jul 1, 2024
1 parent 129d6a8 commit c822ec5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
4 changes: 2 additions & 2 deletions services/src/transaction_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -122,7 +122,7 @@ pub struct TransactionService {
impl TransactionService {
pub async fn send_transaction(
&self,
tx: Transaction,
tx: VersionedTransaction,
max_retries: Option<u16>,
) -> anyhow::Result<String> {
let raw_tx = bincode::serialize(&tx)?;
Expand Down
37 changes: 35 additions & 2 deletions tests/client.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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,
Expand All @@ -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();

Expand Down

0 comments on commit c822ec5

Please sign in to comment.