Contract Spec
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")Functions
Returns all event specifications from the contract spec.
Finds any spec entry by name. Searches across functions, structs, unions, enums, error enums, and events.
Converts a contract function result from XDR to a native Kotlin value.
Converts a contract function result from base64-encoded XDR to a native Kotlin value.
Returns all function specifications from the contract spec.
Finds a specific function specification by name.
Converts a native Kotlin value to an SCValXdr based on the type specification.
Converts an SCValXdr to a native Kotlin value based on the type specification.
Returns all UDT enum specifications from the contract spec.
Returns all UDT error enum specifications from the contract spec.
Returns all UDT struct specifications from the contract spec.
Returns all UDT union specifications from the contract spec.