invoke

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).

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