ContractSpec

Utility class for working with Soroban contract specifications.

This class provides methods to find spec entries, convert native Kotlin values to SCValXdr objects based on contract specifications, and simplify argument preparation for contract function invocations.

Core Features

  • Automatic Type Conversion: Convert native Kotlin types to XDR values based on contract specs

  • Address Auto-Detection: Strings starting with "G" or "M" become account addresses, "C" becomes contract addresses

  • Collection Handling: Automatic conversion of Lists, Maps, and tuples

  • Complex Types: Full support for structs, unions, and enums

  • BigInteger Support: Handle large numbers (u128/i128/u256/i256)

Usage

// Create ContractSpec from spec entries
val spec = ContractSpec(specEntries)

// Convert function arguments - much simpler than manual XDR construction!
val args = spec.funcArgsToXdrSCValues("swap", mapOf(
"a" to "GABC...", // String → Address (auto-detected as account)
"token_a" to "CABC...", // String → Address (auto-detected as contract)
"amount_a" to 1000, // Int → i128
"min_b_for_a" to 4500 // Int → i128
))

// Introspection
val functions = spec.funcs()
val helloFunc = spec.getFunc("hello")
val structEntry = spec.findEntry("MyStruct")

Constructors

Link copied to clipboard
constructor(entries: List<SCSpecEntryXdr>)

Functions

Link copied to clipboard

Returns all event specifications from the contract spec.

Link copied to clipboard

Finds any spec entry by name. Searches across functions, structs, unions, enums, error enums, and events.

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

Converts function arguments to XDR SCVal objects based on the function specification.

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

Converts a contract function result from XDR to a native Kotlin value.

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

Converts a contract function result from base64-encoded XDR to a native Kotlin value.

Link copied to clipboard

Returns all function specifications from the contract spec.

Link copied to clipboard

Finds a specific function specification by name.

Link copied to clipboard

Converts a native Kotlin value to an SCValXdr based on the type specification.

Link copied to clipboard
fun scValToNative(scVal: SCValXdr, typeDef: SCSpecTypeDefXdr?): Any?

Converts an SCValXdr to a native Kotlin value based on the type specification.

Link copied to clipboard

Returns all UDT enum specifications from the contract spec.

Link copied to clipboard

Returns all UDT error enum specifications from the contract spec.

Link copied to clipboard

Returns all UDT struct specifications from the contract spec.

Link copied to clipboard

Returns all UDT union specifications from the contract spec.