SimulateTransactionResponse

public struct SimulateTransactionResponse : Decodable, Sendable

Response from simulating a Soroban contract invocation.

Contains all information needed to understand what will happen when a transaction executes:

  • Return values from contract function calls
  • Resource requirements (CPU instructions, memory, I/O)
  • Ledger footprint (which ledger entries will be read/written)
  • Authorization requirements for multi-party transactions
  • Events emitted during simulation
  • Required fees and resource costs

Use this response to:

  • Get results from read-only contract calls without submitting a transaction
  • Determine resource limits before submitting a write transaction
  • Check if ledger entries need restoration before invocation
  • Validate that a transaction will succeed before submission

Before submitting a write transaction, you must:

  1. Simulate the transaction
  2. Use transactionData and minResourceFee from the simulation
  3. Add these to your transaction
  4. Sign and submit

Example:

let simResponse = await server.simulateTransaction(simulateTxRequest: request)
switch simResponse {
case .success(let simulation):
    // Check for errors
    if let error = simulation.error {
        print("Simulation failed: \(error)")
        return
    }

    // Check if restoration is needed
    if let restore = simulation.restorePreamble {
        print("Must restore footprint first")
        // Submit RestoreFootprint operation
    }

    // Get return value for read calls
    if let result = simulation.results?.first?.returnValue {
        print("Contract returned: \(result)")
    }

    // For write calls, use simulation data
    transaction.setSorobanTransactionData(simulation.transactionData!)
    transaction.addResourceFee(simulation.minResourceFee!)
case .failure(let error):
    print("RPC error: \(error)")
}

See also:

  • Simulation results including resource costs, return values, and auth requirements.

    Declaration

    Swift

    public let results: [SimulateTransactionResult]?
  • The sequence number of the latest ledger known to Soroban RPC at the time it handled the request.

    Declaration

    Swift

    public let latestLedger: Int
  • The recommended Soroban Transaction Data to use when submitting the simulated transaction. This data contains the refundable fee and resource usage information such as the ledger footprint and IO access data. Not present in case of error.

    Declaration

    Swift

    public let transactionData: SorobanTransactionDataXDR?
  • Recommended minimum resource fee to add when submitting the transaction. This fee is to be added on top of the Stellar network fee. Not present in case of error.

    Declaration

    Swift

    public let minResourceFee: UInt32?
  • Array of the events emitted during the contract invocation(s). The events are ordered by their emission time. (an array of serialized base64 strings - DiagnosticEventXdr)

    Declaration

    Swift

    public let events: [String]?
  • (optional) only present if the transaction failed. This field will include more details from stellar-core about why the invoke host function call failed.

    Declaration

    Swift

    public let error: String?
  • It can only present on successful simulation (i.e. no error) of InvokeHostFunction operations. If present, it indicates the simulation detected expired ledger entries which requires restoring with the submission of a RestoreFootprint operation before submitting the InvokeHostFunction operation. The restorePreamble.minResourceFee and restorePreamble.transactionData fields should be used to construct the transaction containing the RestoreFootprint

    Declaration

    Swift

    public let restorePreamble: RestorePreamble?
  • If present, it indicates how the state (ledger entries) will change as a result of the transaction execution.

    Declaration

    Swift

    public let stateChanges: [LedgerEntryChange]?
  • Declaration

    Swift

    public init(from decoder: Decoder) throws
  • The ledger footprint indicating which ledger entries will be read or written during transaction execution.

    Declaration

    Swift

    public var footprint: Footprint? { get }
  • The soroban authorization entries if available.

    Declaration

    Swift

    public var sorobanAuth: [SorobanAuthorizationEntryXDR]? { get }