Skip to main content

Smart Contracts

XYZ Chain supports CosmWasm smart contracts - Rust-based programs that run on the blockchain, similar to Solana programs.

Why CosmWasm?

Rust Safety

Memory-safe language prevents common vulnerabilities

Familiar to Solana Devs

Rust-based like Solana programs

Rich Ecosystem

CW20, CW721, and many more standards

IBC Compatible

Cross-chain contracts via IBC

Contract Lifecycle

┌─────────────┐     ┌───────────────┐     ┌─────────────┐     ┌─────────────┐
│  Scaffold   │────▶│    Build      │────▶│   Deploy    │────▶│  Interact   │
│  (xyz init) │     │(xyz program   │     │(xyz program │     │(execute/    │
│             │     │    build)     │     │   deploy)   │     │   query)    │
└─────────────┘     └───────────────┘     └─────────────┘     └─────────────┘
  1. Scaffold - Create project structure with xyz init
  2. Build - Compile Rust to optimized Wasm with xyz program build
  3. Deploy - Upload and instantiate with xyz program deploy
  4. Interact - Execute methods and query state

Commands

CommandDescription
xyz initScaffold new contract project
xyz program buildCompile to optimized Wasm
xyz program deployUpload and instantiate contract
xyz program executeCall contract method
xyz program queryRead contract state
xyz program listList deployed contracts
xyz program infoContract details
xyz localnetLocal development network

Quick Start

1. Start Local Network

xyz localnet start

2. Scaffold Project

xyz init my-contract
cd my-contract

3. Build Contract

xyz program build

4. Deploy

xyz program deploy artifacts/my_contract.wasm --from alice --label "My Contract"

5. Interact

# Query state
xyz program query xyz1contract... '{"get_count":{}}'

# Execute method
xyz program execute xyz1contract... '{"increment":{}}' --from alice

Project Structure

xyz init creates:
my-contract/
├── Cargo.toml           # Rust dependencies
├── src/
│   ├── lib.rs          # Contract entry point
│   ├── contract.rs     # Main logic
│   ├── msg.rs          # Message types
│   ├── state.rs        # State storage
│   └── error.rs        # Custom errors
├── .gitignore
└── README.md

Key Files

FilePurpose
lib.rsExports contract entry points
contract.rsinstantiate, execute, query functions
msg.rsInstantiateMsg, ExecuteMsg, QueryMsg
state.rsStorage using cw-storage-plus
error.rsCustom error types

Contract Entry Points

Every CosmWasm contract has three entry points:
// Called once when contract is created
pub fn instantiate(
    deps: DepsMut,
    env: Env,
    info: MessageInfo,
    msg: InstantiateMsg,
) -> Result<Response, ContractError>

// Called to modify state
pub fn execute(
    deps: DepsMut,
    env: Env,
    info: MessageInfo,
    msg: ExecuteMsg,
) -> Result<Response, ContractError>

// Called to read state (no state changes)
pub fn query(
    deps: Deps,
    env: Env,
    msg: QueryMsg,
) -> StdResult<Binary>

Message Types

InstantiateMsg

Configuration when creating the contract:
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct InstantiateMsg {
    pub count: i32,
    pub owner: String,
}

ExecuteMsg

Actions that modify state:
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub enum ExecuteMsg {
    Increment {},
    Decrement {},
    Reset { count: i32 },
}

QueryMsg

Read-only requests:
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub enum QueryMsg {
    GetCount {},
    GetOwner {},
}

Gas & Fees

CosmWasm operations use more gas than standard transactions:
OperationTypical Gas
Store (upload)1,000,000 - 5,000,000
Instantiate200,000 - 500,000
Execute100,000 - 300,000
QueryFree (no tx)
The CLI automatically estimates gas with a 1.3x buffer.

Development Workflow

1

Write Code

Modify src/contract.rs with your logic
2

Build

xyz program build
3

Test Locally

xyz localnet start --reset
xyz program deploy artifacts/my_contract.wasm --from alice
4

Iterate

Repeat until working correctly
5

Deploy to Testnet

xyz config set node tcp://rpc.testnet.xyz.com:26657
xyz program deploy artifacts/my_contract.wasm --from mykey

Standards

Common contract standards available:
StandardDescriptionUse Case
CW20Fungible tokensToken creation
CW721Non-fungible tokensNFTs
CW1Proxy contractsUpgradability
CW3MultisigDAOs, shared wallets
CW4GroupsMembership management

Resources

Next Steps