API Reference
Transactions
Execute and simulate transactions with LiteSVM in TypeScript
Transactions
Methods for sending, simulating, and managing transactions.
sendTransaction
sendTransaction(tx: Transaction): TransactionMetadata | FailedTransactionMetadataExecute a transaction. Returns metadata on success or failure details on error.
const result = client.svm.sendTransaction(signedTx);
// Check for errors
if ('err' in result && result.err()) {
console.error('Transaction failed:', result.err());
return;
}
// Access result properties (note: these are getter functions)
console.log('Signature:', result.signature());
console.log('Compute units:', result.computeUnitsConsumed());
console.log('Logs:', result.logs());Transaction result properties like err(), computeUnitsConsumed(), logs(), and signature() are getter functions, not properties. Always call them with parentheses.
TransactionMetadata Methods
| Method | Returns | Description |
|---|---|---|
signature() | Uint8Array | Transaction signature bytes |
computeUnitsConsumed() | bigint | Compute units used |
logs() | string[] | Program logs |
innerInstructions() | InnerInstruction[] | Inner (CPI) instructions |
returnData() | TransactionReturnData | Return data from programs |
prettyLogs() | string | Formatted logs with colors |
simulateTransaction
simulateTransaction(tx: Transaction): SimulatedTransactionInfo | FailedTransactionMetadataSimulate a transaction without modifying state.
const simResult = client.svm.simulateTransaction(signedTx);
if ('err' in simResult && simResult.err()) {
console.error('Simulation failed:', simResult.err());
return;
}
// Use .meta() to get TransactionMetadata
const meta = simResult.meta();
console.log('Would use', meta.computeUnitsConsumed(), 'compute units');
console.log('Logs:', meta.logs());
// Get post-execution account states
const postAccounts = simResult.postAccounts();
console.log('Post-state accounts:', postAccounts);Simulation doesn't change state. Use it to check transactions before sending.
Blockhash Management
latestBlockhash
latestBlockhash(): BlockhashGet the current blockhash.
const blockhash = client.svm.latestBlockhash();
console.log('Blockhash:', blockhash);latestBlockhashLifetime
latestBlockhashLifetime(): TransactionBlockhashLifetimeGet the blockhash with lifetime information. Use this when building transactions with Kit.
import {
createTransactionMessage,
setTransactionMessageLifetimeUsingBlockhash,
} from '@solana/kit';
const blockhashLifetime = client.svm.latestBlockhashLifetime();
const tx = setTransactionMessageLifetimeUsingBlockhash(
blockhashLifetime,
createTransactionMessage({ version: 0 })
);expireBlockhash
expireBlockhash(): LiteSVMAdvance to a new blockhash. Useful for testing blockhash expiration scenarios.
const oldBlockhash = client.svm.latestBlockhash();
client.svm.expireBlockhash();
const newBlockhash = client.svm.latestBlockhash();
console.log('Old:', oldBlockhash);
console.log('New:', newBlockhash);
// Blockhashes will be differentTransaction History
Enable transaction history to retrieve transactions after sending:
// Enable history storage (stores last N transactions)
client.svm.withTransactionHistory(100n);
// Send a transaction
const result = client.svm.sendTransaction(signedTx);
// Later, retrieve it by signature (base58 string)
// Note: getTransaction expects a base58-encoded signature stringComplete Transaction Flow
import {
appendTransactionMessageInstruction,
createEmptyClient,
createTransactionMessage,
generateKeyPairSigner,
lamports,
pipe,
setTransactionMessageFeePayerSigner,
setTransactionMessageLifetimeUsingBlockhash,
signTransactionMessageWithSigners,
} from '@solana/kit';
import { getTransferSolInstruction } from '@solana-program/system';
import { litesvm } from "@solana/kit-plugins";
// Setup
const client = createEmptyClient().use(litesvm());
client.svm.withSigverify(false).withBlockhashCheck(false);
const sender = await generateKeyPairSigner();
const recipient = await generateKeyPairSigner();
client.svm.airdrop(sender.address, lamports(10_000_000_000n));
// Build transaction
const transferIx = getTransferSolInstruction({
source: sender,
destination: recipient.address,
amount: lamports(1_000_000_000n),
});
const transactionMessage = pipe(
createTransactionMessage({ version: 0 }),
tx => setTransactionMessageFeePayerSigner(sender, tx),
tx => setTransactionMessageLifetimeUsingBlockhash(
client.svm.latestBlockhashLifetime(),
tx
),
tx => appendTransactionMessageInstruction(transferIx, tx),
);
// Sign
const signedTx = await signTransactionMessageWithSigners(transactionMessage);
// Simulate first
const simResult = client.svm.simulateTransaction(signedTx);
const simMeta = simResult.meta();
console.log('Simulation CU:', simMeta.computeUnitsConsumed());
// Send
const result = client.svm.sendTransaction(signedTx);
console.log('Actual CU:', result.computeUnitsConsumed());
// Verify
const balance = client.svm.getBalance(recipient.address);
console.log('Recipient balance:', balance);