Skip to main content

Simulation

getSimulationResult

Simulates a transaction and returns compute units consumed and any errors.
async function getSimulationResult(
  connection: Connection,
  instructions: Array<TransactionInstruction>,
  payer: PublicKey,
  lookupTables?: Array<AddressLookupTableAccount>,
  staging?: boolean,
): Promise<{
  unitsConsumed?: number;
  error?: Error;
  serializedTx?: String;
}>
connection
Connection
required
Solana connection instance
instructions
TransactionInstruction[]
required
Array of transaction instructions to simulate
payer
PublicKey
required
Transaction fee payer
lookupTables
AddressLookupTableAccount[]
Optional address lookup tables
staging
boolean
default:"false"
Whether to use staging environment for error resolution
unitsConsumed
number | undefined
Compute units consumed (if successful)
error
Error | undefined
Error object (if simulation failed)
serializedTx
String | undefined
Base64 encoded serialized transaction

Error parsing

parseProgramLogs

Parses program logs to extract a human-readable error message.
function parseProgramLogs(
  logs: string[],
  staging: boolean,
): string
logs
string[]
required
Array of program log strings from transaction simulation or execution
staging
boolean
required
Whether to use staging environment for error code resolution
return
string
Human-readable error message. Checks in order:
  • Anchor “Error Message:” from program logs
  • Insufficient funds/lamports errors
  • Custom program error codes (resolved via IDL)
  • Returns “Unknown error” if no pattern matches
Example usage:
const logs = [
  "Program log: Instruction: Transfer",
  "Program log: Error Message: Insufficient balance",
];

const errorMsg = parseProgramLogs(logs, false);
// Returns: "Insufficient balance"

Build docs developers (and LLMs) love