Adding tests for versioned transactions (#406)

This commit is contained in:
galactus 2024-07-01 16:12:06 +02:00 committed by GitHub
parent 129d6a830e
commit 78afa5d8a5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 4 deletions

View File

@ -21,7 +21,7 @@ use solana_lite_rpc_core::{
}; };
use solana_sdk::{ use solana_sdk::{
compute_budget::{self, ComputeBudgetInstruction}, compute_budget::{self, ComputeBudgetInstruction},
transaction::{Transaction, VersionedTransaction}, transaction::VersionedTransaction,
}; };
use tokio::{ use tokio::{
sync::mpsc::{self, Sender, UnboundedSender}, sync::mpsc::{self, Sender, UnboundedSender},
@ -122,7 +122,7 @@ pub struct TransactionService {
impl TransactionService { impl TransactionService {
pub async fn send_transaction( pub async fn send_transaction(
&self, &self,
tx: Transaction, tx: VersionedTransaction,
max_retries: Option<u16>, max_retries: Option<u16>,
) -> anyhow::Result<String> { ) -> anyhow::Result<String> {
let raw_tx = bincode::serialize(&tx)?; let raw_tx = bincode::serialize(&tx)?;

View File

@ -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 fs from "fs";
import * as os from "os"; import * as os from "os";
import * as crypto from "crypto"; import * as crypto from "crypto";
@ -24,10 +24,28 @@ function createTransaction(): Transaction {
return 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 () => { test('send and confirm transaction BlockheightBasedTransactionConfirmationStrategy', async () => {
const tx = createTransaction(); const tx = createTransaction();
const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash(); 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}`); console.log(`https://explorer.solana.com/tx/${signature}`);
await connection.confirmTransaction({ await connection.confirmTransaction({
blockhash, blockhash,
@ -45,6 +63,21 @@ test('send and confirm transaction legacy confrim', async () => {
await connection.confirmTransaction(signature); 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 () => { test('send and confirm transaction', async () => {
const tx = createTransaction(); const tx = createTransaction();