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);getLatestBlockhash (via client.rpc)
Get the blockhash with lifetime information for building transactions manually. Use the RPC helper since it returns both blockhash and lastValidBlockHeight.
import {
createTransactionMessage,
setTransactionMessageLifetimeUsingBlockhash,
} from '@solana/kit';
const { value: blockhashLifetime } = await client.rpc.getLatestBlockhash().send();
const tx = setTransactionMessageLifetimeUsingBlockhash(
blockhashLifetime,
createTransactionMessage({ version: 0 })
);When using program plugins like systemProgram() or tokenProgram(), you never need to fetch the blockhash yourself — client.sendTransaction() (and the .sendTransaction() helper on instruction chains) handle it for you.
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 string