ContractClient

A client to interact with Soroban smart contracts.

Beginner-Friendly Usage (Recommended)

// Load spec from network - enables automatic type conversion and result parsing
val client = ContractClient.forContract(contractId, rpcUrl, network)

// Invoke with native types - automatic argument conversion and execution
val balance = client.invoke(
functionName = "balance",
arguments = mapOf("account" to "GABC..."),
source = sourceAccount,
signer = null,
parseResultXdrFn = { xdr ->
(xdr as SCValXdr.I128).value.lo.value.toLong()
}
)

// Or use manual result parsing with funcResToNative
val resultXdr = client.invoke(
functionName = "balance",
arguments = mapOf("account" to "GABC..."),
source = sourceAccount,
signer = null
)
val balance = client.funcResToNative("balance", resultXdr) as BigInteger

Advanced Usage (Transaction Control)

// Build transaction for manual control before signing
val tx = client.buildInvoke(
functionName = "transfer",
arguments = mapOf(
"from" to fromAccount,
"to" to toAccount,
"amount" to 1000
),
source = account.accountId,
signer = keypair
)

// Customize transaction
tx.raw?.addMemo(Memo.text("Payment"))

// Sign and submit
tx.signAndSubmit(keypair)

Result Parsing

ContractClient provides helper methods for manual result parsing:

  • funcResToNative(functionName, scVal) - Convert XDR result to native Kotlin types

  • funcArgsToXdrSCValues(functionName, args) - Convert native arguments to XDR

Type mapping (Soroban → Kotlin): | Soroban Type | Kotlin Type | |--------------|-------------| | u32, i32 | UInt, Int | | u64, i64 | ULong, Long | | u128, i128 | BigInteger | | bool | Boolean | | symbol, string | String | | address | String | | bytes | ByteArray | | vec | List | | map | Map | | option | T? | | void | null |

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

The contract ID to interact with (C... address)

Link copied to clipboard

The network to interact with

Link copied to clipboard

The RPC server URL

Link copied to clipboard

The SorobanServer instance for RPC calls

Functions

Link copied to clipboard
suspend fun <T> buildInvoke(functionName: String, arguments: Map<String, Any?>, source: String, signer: KeyPair?, parseResultXdrFn: (SCValXdr) -> T? = null, options: ClientOptions = ClientOptions( sourceAccountKeyPair = signer ?: KeyPair.fromAccountId(source), contractId = contractId, network = network, rpcUrl = rpcUrl )): AssembledTransaction<T>

Build a transaction for invoking a contract method with Map arguments.

Link copied to clipboard
fun close()

Close the underlying SorobanServer connection.

Link copied to clipboard
fun funcArgsToXdrSCValues(functionName: String, arguments: Map<String, Any?>): List<SCValXdr>

Convert function arguments from native Kotlin types to XDR.

Link copied to clipboard
fun funcResToNative(functionName: String, scVal: SCValXdr): Any?

Convert a contract function result from XDR to native Kotlin types.

fun funcResToNative(functionName: String, base64Xdr: String): Any?

Convert a contract function result from base64-encoded XDR to native Kotlin types.

Link copied to clipboard

Get the contract specification.

Link copied to clipboard

Get available method names from contract spec. Returns empty set if spec not loaded.

Link copied to clipboard
suspend fun <T> invoke(functionName: String, arguments: Map<String, Any?>, source: String, signer: KeyPair?, parseResultXdrFn: (SCValXdr) -> T? = null, options: ClientOptions = ClientOptions( sourceAccountKeyPair = signer ?: KeyPair.fromAccountId(source), contractId = contractId, network = network, rpcUrl = rpcUrl )): T

Invoke a contract function with automatic type conversion (RECOMMENDED).

Link copied to clipboard

Convert a single native value to XDR based on type definition.