ContractSpec
public final class ContractSpec : Sendable
Parser and utility for Soroban contract specifications.
ContractSpec provides tools for working with contract interface definitions, including:
- Parsing contract spec entries (functions, events, types)
- Converting between native Swift types and Soroban contract values (SCVal)
- Validating function arguments against contract specifications
- Extracting contract metadata and type definitions
Contract specifications are embedded in the WebAssembly contract code and describe:
- Available contract functions with parameters and return types
- Custom data types (structs, enums, unions)
- Events that the contract can emit
- Error types the contract may return
Use this class to:
- Introspect contract interfaces
- Build type-safe contract invocations
- Parse and validate contract arguments
- Convert contract return values to Swift types
Example usage:
// Get contract spec from deployed contract
let client = try await SorobanClient.forClientOptions(options: clientOptions)
let spec = client.getContractSpec()
// List all available functions
let functions = spec.funcs()
for func in functions {
print("Function: \(func.name)")
print("Parameters: \(func.inputs.count)")
}
// Get specific function details
if let transferFunc = spec.getFunc(name: "transfer") {
print("Transfer function inputs: \(transferFunc.inputs)")
print("Transfer function outputs: \(transferFunc.outputs)")
}
See also:
- [SorobanClient] for high-level contract interaction
- Stellar developer docs
-
The spec entries of the contract
Declaration
Swift
public let entries: [SCSpecEntryXDR] -
Initialize ContractSpec with spec entries
Declaration
Swift
public init(entries: [SCSpecEntryXDR])Parameters
entriesArray of SCSpecEntryXDR objects
-
Gets the XDR functions from the spec.
Declaration
Swift
public func funcs() -> [SCSpecFunctionV0XDR]Return Value
Array of SCSpecFunctionV0XDR objects
-
Gets the XDR function spec for the given function name if available.
Declaration
Swift
public func getFunc(name: String) -> SCSpecFunctionV0XDR?Parameters
nameName of the function
Return Value
The function spec or nil if not found
-
Gets the XDR events from the spec.
Declaration
Swift
public func events() -> [SCSpecEventV0XDR]Return Value
Array of SCSpecEventV0XDR objects
-
Gets the XDR event spec for the given event name if available.
Declaration
Swift
public func getEvent(name: String) -> SCSpecEventV0XDR?Parameters
nameName of the event
Return Value
The event spec or nil if not found
-
Gets all UDT struct specifications from the contract spec.
Declaration
Swift
public func udtStructs() -> [SCSpecUDTStructV0XDR]Return Value
Array of SCSpecUDTStructV0XDR objects representing all struct definitions
-
Gets all UDT union specifications from the contract spec.
Declaration
Swift
public func udtUnions() -> [SCSpecUDTUnionV0XDR]Return Value
Array of SCSpecUDTUnionV0XDR objects representing all union definitions
-
Gets all UDT enum specifications from the contract spec.
Declaration
Swift
public func udtEnums() -> [SCSpecUDTEnumV0XDR]Return Value
Array of SCSpecUDTEnumV0XDR objects representing all enum definitions
-
Gets all UDT error enum specifications from the contract spec.
Declaration
Swift
public func udtErrorEnums() -> [SCSpecUDTErrorEnumV0XDR]Return Value
Array of SCSpecUDTErrorEnumV0XDR objects representing all error enum definitions
-
Finds the XDR spec entry for the given name.
Declaration
Swift
public func findEntry(name: String) -> SCSpecEntryXDR?Parameters
nameThe name to find
Return Value
The entry or nil if not found
-
Converts native arguments to SCValXDR values for calling a contract function.
Throws
ContractSpecError if function not found or arguments are invalidDeclaration
Swift
public func funcArgsToXdrSCValues(name: String, args: [String : Any]) throws -> [SCValXDR]Parameters
nameName of the function
argsDictionary of argument names to values
Return Value
Array of SCValXDR objects ordered by position
-
Converts a native Swift value to an SCValXDR based on the given type.
Throws
ContractSpecError if conversion failsDeclaration
Swift
public func nativeToXdrSCVal(val: Any?, ty: SCSpecTypeDefXDR) throws -> SCValXDRParameters
valNative Swift value
tyThe expected type
Return Value
The converted SCValXDR
View on GitHub
Install in Dash