Contract

The Contract module provides a high-level interface for working with Soroban smart contracts, including deployment, invocation, and state reading.

Contract Class

The Contract class is the main interface for interacting with Soroban contracts.

Creating a Contract Instance

import { Contract, NetworkConfig } from "@colibri/core";

const network = NetworkConfig.TestNet();

// Create from existing contract ID
const contract = new Contract({
  networkConfig: network,
  contractConfig: {
    contractId: "CABC..." as ContractId,
  },
});

// Create from WASM buffer
const contractFromWasm = new Contract({
  networkConfig: network,
  contractConfig: {
    wasm: wasmBuffer,
  },
});

// Create from WASM hash
const contractFromHash = new Contract({
  networkConfig: network,
  contractConfig: {
    wasmHash: "abc123...",
  },
});

// Optionally provide a custom RPC server
import { Server } from "stellar-sdk/rpc";
const customRpc = new Server("https://custom-rpc.example.com");

const contractWithCustomRpc = new Contract({
  networkConfig: network,
  rpc: customRpc,
  contractConfig: {
    contractId: "CABC..." as ContractId,
  },
});
circle-info

For Stellar Asset Contracts (SAC), use the dedicated StellarAssetContract class instead. See Stellar Asset Contract.

Contract Methods

Getters

getContractId()

Returns the contract ID (requires contract to have an ID):

getSpec()

Returns the contract specification (requires spec to be loaded):

getWasm()

Returns the WASM buffer (requires WASM to be loaded):

getWasmHash()

Returns the WASM hash:

getContractFootprint()

Returns the ledger key for the contract:

getContractCodeLedgerEntry()

Fetches the contract code entry from the network:

getContractInstanceLedgerEntry()

Fetches the contract instance entry from the network:

Invoking Contract Methods

invoke() - State-Changing Calls

For methods that modify contract state:

read() - Read-Only Calls

For methods that only read state (no transaction required):

invokeRaw() - Low-Level Invocation

For passing pre-encoded ScVal arguments:

readRaw() - Low-Level Read

For reading with pre-encoded ScVal arguments:

Deploying Contracts

uploadWasm()

Upload WASM to the network:

deploy()

Deploy a contract from an uploaded WASM hash:

Loading Contract Metadata

loadSpecFromWasm()

Extract and load the contract specification from a local WASM buffer:

loadSpecFromDeployedContract()

Load the contract specification from an on-chain deployed contract:

loadWasmHashFromContractInstance()

Load the WASM hash from an on-chain contract instance:

Contract Types

ContractId

A branded string type for contract IDs:

Contract IDs always start with C and are 56 characters long.

Validating Contract IDs

Using Pipelines Directly

For more control, use pipelines directly:

PIPE_InvokeContract

PIPE_ReadFromContract

Errors

Code
Class
Description

CONTR_001

MISSING_ARG

Required argument not provided

CONTR_002

MISSING_RPC_URL

RPC URL required but not provided

CONTR_003

INVALID_CONTRACT_CONFIG

Must provide contractId, wasm, or wasmHash

CONTR_004

FAILED_TO_UPLOAD_WASM

WASM upload to network failed

CONTR_005

MISSING_REQUIRED_PROPERTY

Required contract property not set

CONTR_006

PROPERTY_ALREADY_SET

Property is immutable once set

CONTR_007

MISSING_SPEC_IN_WASM

WASM doesn't contain valid spec

CONTR_008

FAILED_TO_DEPLOY_CONTRACT

Contract deployment failed

CONTR_009

CONTRACT_INSTANCE_NOT_FOUND

Contract instance not found on network

CONTR_010

CONTRACT_CODE_NOT_FOUND

Contract code not found on network

CONTR_011

INVALID_CONTRACT_ID

Invalid contract ID format

Next Steps

Last updated