invoke

suspend fun <T> invoke(functionName: String, arguments: Map<String, Any?>, source: String, signer: KeyPair?, parseResultXdrFn: (SCValXdr) -> T? = null, options: ClientOptions = defaultOptions(source, signer)): T

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

This is the primary method for contract interaction. It provides:

  • Automatic type conversion from native Kotlin types to XDR

  • Method validation against contract spec

  • Auto read/write detection - reads return immediately, writes are signed/submitted

  • Direct result return - no manual .result() or .signAndSubmit() needed

Result Parsing Options

You have two options for parsing results:

Option 1: Custom Parser (parseResultXdrFn)

val balance = client.invoke(
functionName = "balance",
arguments = mapOf("id" to accountId),
source = sourceAccount,
signer = null,
parseResultXdrFn = { xdr ->
(xdr as SCValXdr.I128).value.lo.value.toLong()
}
)

Option 2: Manual Parsing with funcResToNative

val resultXdr = client.invoke(
functionName = "balance",
arguments = mapOf("id" to accountId),
source = sourceAccount,
signer = null
)
val balance = client.funcResToNative("balance", resultXdr) as BigInteger

API Comparison

Simple API (this method):

  • Automatic argument conversion: Map → XDR

  • Flexible result parsing: parseResultXdrFn or funcResToNative

  • Auto-execution (reads return results, writes auto-submit)

Advanced API (buildInvoke):

  • Automatic argument conversion: Map → XDR

  • Manual transaction control: Returns AssembledTransaction

  • Allows customization (memos, preconditions) before signing

Return

The parsed result value (using parseResultXdrFn if provided, otherwise raw SCValXdr)

Parameters

functionName

The contract function to invoke

arguments

Function arguments as Map (native Kotlin types)

source

The source account (G... or M... address)

signer

KeyPair for signing (null for read-only calls)

parseResultXdrFn

Optional custom function to parse result XDR

options

Invocation options

Throws

if contract spec not loaded

if method not found or invalid arguments


suspend fun <T> invoke(functionName: String, parameters: List<SCValXdr>, source: String, signer: KeyPair?, parseResultXdrFn: (SCValXdr) -> T? = null, options: ClientOptions = defaultOptions(source, signer)): T

Invoke a contract function with pre-encoded XDR arguments.

This is the spec-free counterpart to the Map-based invoke. Arguments are supplied as SCValXdr values that the caller has already encoded, so no ContractSpec is required and the method works on clients created via forContractWithoutSpec. It is the primary entry point for generated bindings, which embed all type knowledge and encode arguments themselves.

Semantics match the Map-based invoke exactly, minus the spec-driven argument conversion and method-name validation (an unknown function fails at simulation time instead of before the request):

  • The transaction is built and (when ClientOptions.simulate is set) simulated.

  • Read/write is auto-detected from the simulation's auth entries and read-write footprint.

  • Read calls return the simulated result; write calls are signed and submitted when ClientOptions.autoSubmit is set.

  • A write call requires a non-null signer when auto submission applies; otherwise IllegalArgumentException is thrown.

Return

The parsed result value (using parseResultXdrFn if provided, otherwise raw SCValXdr)

Parameters

functionName

The contract function to invoke

parameters

Function arguments as pre-encoded SCValXdr values, in declaration order

source

The source account (G... or M... address)

signer

KeyPair for signing (null for read-only calls)

parseResultXdrFn

Optional custom function to parse result XDR

options

Invocation options

Throws

if a write call is attempted without a signer