SorobanRpcResponse

@Serializable
data class SorobanRpcResponse<T>(val jsonRpc: String, val id: String, val result: T? = null, val error: SorobanRpcResponse.Error? = null)

Represents a JSON-RPC 2.0 response returned by the Soroban RPC server.

All Soroban RPC API responses follow the JSON-RPC 2.0 protocol. This class provides a type-safe wrapper for parsing responses and detecting errors.

JSON-RPC 2.0 Response Structure

A valid JSON-RPC 2.0 response contains:

  • jsonrpc: Protocol version (always "2.0")

  • id: Identifier matching the request ID

  • Either:

  • result: The successful result data (when no error occurred)

  • error: Error information (when the request failed)

A response MUST contain either result or error, but never both.

Example Usage

val response: SorobanRpcResponse<GetHealthResponse> = parseResponse(json)

if (response.isSuccess()) {
val health = response.result!!
println("Server status: ${health.status}")
} else if (response.isError()) {
val error = response.error!!
println("Error ${error.code}: ${error.message}")
}

Parameters

T

The type of the successful result data

See also

Constructors

Link copied to clipboard
constructor(jsonRpc: String, id: String, result: T? = null, error: SorobanRpcResponse.Error? = null)

Types

Link copied to clipboard
@Serializable
data class Error(val code: Int, val message: String, val data: String? = null)

Represents a JSON-RPC 2.0 error object.

Properties

Link copied to clipboard

Error information.

Link copied to clipboard
val id: String

Request identifier matching the original request.

Link copied to clipboard
@SerialName(value = "jsonrpc")
val jsonRpc: String

JSON-RPC protocol version.

Link copied to clipboard
val result: T?

Successful result data.

Functions

Link copied to clipboard

Checks if this response represents an error.

Link copied to clipboard

Checks if this response represents a successful result.

Link copied to clipboard
open override fun toString(): String

Returns a string representation of this response.