SorobanServer

class SorobanServer(serverUrl: String, httpClient: HttpClient = defaultHttpClient()) : AutoCloseable

Main client class for connecting to a Soroban RPC instance and making requests.

Soroban RPC is Stellar's smart contract platform RPC server that provides APIs to:

  • Simulate and submit smart contract transactions

  • Query contract state and ledger entries

  • Monitor transaction status and events

  • Fetch network metadata and fee statistics

JSON-RPC 2.0 Protocol

All Soroban RPC methods use JSON-RPC 2.0 over HTTP. This client handles:

  • Request/response serialization

  • Error handling and exception mapping

  • Automatic request ID generation

  • HTTP connection management

Basic Usage

// Create server instance
val server = SorobanServer("https://soroban-testnet.stellar.org:443")

// Check server health
val health = server.getHealth()
println("Server status: ${health.status}")

// Get account for transaction building
val account = server.getAccount("GABC...")

// Simulate a transaction
val simulation = server.simulateTransaction(transaction)

// Prepare transaction with simulation results
val prepared = server.prepareTransaction(transaction)

// Sign and submit
prepared.sign(keypair)
val response = server.sendTransaction(prepared)

// Poll for completion
val result = server.pollTransaction(response.hash!!)

// Close when done
server.close()

Transaction Preparation Flow

Soroban transactions require simulation before submission to:

  1. Calculate resource requirements (CPU, memory, storage)

  2. Determine authorization entries needed

  3. Estimate resource fees

The typical flow is:

  1. Build transaction with com.soneso.stellar.sdk.TransactionBuilder

  2. Call simulateTransaction to get resource estimates

  3. Call prepareTransaction to apply estimates to transaction

  4. Sign the prepared transaction

  5. Submit with sendTransaction

  6. Poll for completion with pollTransaction

Or use the simplified flow:

val prepared = server.prepareTransaction(transaction)  // Simulates internally
prepared.sign(keypair)
val response = server.sendTransaction(prepared)

Resource Management

This class implements AutoCloseable and should be closed when no longer needed to release HTTP client resources:

SorobanServer(url).use { server ->
// Use server
}

Error Handling

Methods throw:

See also

Constructors

Link copied to clipboard
constructor(serverUrl: String, httpClient: HttpClient = defaultHttpClient())

Types

Link copied to clipboard
object Companion
Link copied to clipboard

Durability level for contract data storage.

Functions

Link copied to clipboard
open override fun close()

Closes the HTTP client and releases resources.

Link copied to clipboard

Fetches a minimal set of current info about a Stellar account.

Link copied to clipboard

Reads contract data for a specific key.

Link copied to clipboard

Fetches all events matching the given filters.

Link copied to clipboard

Gets statistics for charged inclusion fees.

Link copied to clipboard

Performs a general node health check.

Link copied to clipboard

Fetches the latest ledger metadata.

Link copied to clipboard

Reads the current value of ledger entries directly.

Link copied to clipboard

Gets a paginated list of ledgers.

Link copied to clipboard

Fetches metadata about the network.

Link copied to clipboard
suspend fun getSACBalance(contractId: String, asset: Asset, network: Network): GetSACBalanceResponse

Fetches the balance of a Stellar Asset Contract (SAC).

Link copied to clipboard

Fetches the details of a submitted transaction.

Link copied to clipboard

Gets a paginated list of transactions.

Link copied to clipboard

Fetches version information about the RPC server and Captive Core.

Link copied to clipboard

Loads the contract code entry (including WASM bytecode) for a given contract ID.

Link copied to clipboard

Loads the contract code entry (including WASM bytecode) for a given WASM ID.

Link copied to clipboard

Loads contract information (Environment Meta, Contract Spec, Contract Meta) for a given contract ID.

Link copied to clipboard

Loads contract information (Environment Meta, Contract Spec, Contract Meta) for a given WASM ID.

Link copied to clipboard
suspend fun pollTransaction(hash: String, maxAttempts: Int = 30, sleepStrategy: (Int) -> Long = { 1000L }): GetTransactionResponse

Polls for transaction completion.

Link copied to clipboard
suspend fun prepareTransaction(transaction: Transaction): Transaction

Prepares a transaction by simulating it and applying resource estimates.

suspend fun prepareTransaction(transaction: Transaction, simulateResponse: SimulateTransactionResponse): Transaction

Prepares a transaction using existing simulation results.

Link copied to clipboard

Submits a transaction to the Stellar network.

Link copied to clipboard

Simulates a transaction to preview its effects.