CosmWasm
Developing and deploying CosmWasm smart contracts on LumiWave
What is CosmWasm?
CosmWasm is a module for executing WebAssembly (Wasm) smart contracts on Cosmos SDK-based blockchains. Unlike Ethereum's Solidity, contracts are written in Rust and compiled to Wasm, providing high execution performance and memory safety. LumiWave has CosmWasm built in, allowing you to deploy and run smart contracts without any additional setup.
Key features of CosmWasm:
- Rust-based: Memory safety is verified at compile time, minimizing runtime errors
- IBC Compatible: Interact with contracts on other Cosmos chains via IBC
- Upgradeable: Contracts with an admin set can have their code updated for flexible operation
- Isolated Execution: Each contract runs in an independent environment, preventing interference with other contracts
Development Environment Setup
CosmWasm contract development requires the Rust toolchain and Wasm compile target. Install the following tools in order.
Prerequisites
# Install Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# Add Wasm target
rustup target add wasm32-unknown-unknown
# Install cargo-generate (for template generation)
cargo install cargo-generate
# Install cosmwasm-check (contract verification)
cargo install cosmwasm-checkCreating a Contract
Using the official CosmWasm template lets you quickly generate a project with the basic structure in place. The template includes a counter example that helps you understand the contract structure.
Generate from Template
cargo generate --git https://github.com/CosmWasm/cw-template.git --name my-contract
cd my-contractProject Structure
CosmWasm contracts follow the standard structure below. Understanding each file's role makes contract development much easier.
my-contract/
├── Cargo.toml
├── src/
│ ├── contract.rs # Core contract logic (instantiate, execute, query entry points)
│ ├── error.rs # Custom error type definitions
│ ├── msg.rs # Message types (InstantiateMsg, ExecuteMsg, QueryMsg)
│ ├── state.rs # On-chain state storage definitions (Item, Map, etc.)
│ └── lib.rs # Module declarations and Wasm entry point macros
└── examples/
└── schema.rs # Auto-generate JSON schemas (useful for frontend integration)- contract.rs: The core file implementing the 3 entry points (
instantiate,execute,query) of the contract. - msg.rs: Defines the message types the contract can receive.
InstantiateMsgfor contract creation,ExecuteMsgfor state changes, andQueryMsgfor read-only queries. - state.rs: Defines data structures permanently stored on-chain. Uses
Item,Map, etc. from thecw-storage-pluslibrary.
Building the Contract
Optimized Build
The smaller the Wasm binary uploaded to the chain, the lower the gas cost. Using the cosmwasm/optimizer Docker image generates reproducible optimized builds. This approach always produces the same binary from the same source code, making it useful for code verification as well.
# Optimized build using Docker (recommended)
docker run --rm -v "$(pwd)":/code \
--mount type=volume,source="$(basename "$(pwd)")_cache",target=/target \
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
cosmwasm/optimizer:0.16.0
# Output: artifacts/my_contract.wasmVerification
Before uploading, verify that the binary conforms to CosmWasm standards using cosmwasm-check. This detects issues like missing required entry points or incorrect imports before deployment.
# Verify the Wasm binary — outputs "OK" if no issues found
cosmwasm-check artifacts/my_contract.wasmDeploying the Contract
The CosmWasm deployment process consists of 3 steps: Store → Instantiate → Execute/Query. Store uploads the code to the chain, and Instantiate creates a contract instance from that code. Multiple instances can be created from a single code (code_id).
1. Upload Code (Store)
Upload the Wasm binary to the chain. A unique code_id is assigned upon successful upload. This code_id is used when creating instances later.
lumiwave-protocold tx wasm store artifacts/my_contract.wasm \
--from=<WALLET> \
--gas=auto \
--gas-adjustment=1.5 \
--fees=50000ulwp \
--chain-id=lumiwaveprotocolAfter uploading, verify the code_id:
lumiwave-protocold query tx <TX_HASH> | grep -A1 "code_id"2. Create an Instance (Instantiate)
Create a contract instance from the uploaded code. Specify the initialization message (InstantiateMsg) along with the contract's label and admin. --label is an identifier name, and --admin is the address that will have migration authority.
lumiwave-protocold tx wasm instantiate <CODE_ID> \
'{"count": 0}' \
--label "my-contract-v1" \
--admin <ADMIN_ADDRESS> \
--from=<WALLET> \
--gas=auto \
--gas-adjustment=1.5 \
--fees=10000ulwp \
--chain-id=lumiwaveprotocolOnce the instance is created, a unique contract address is assigned. All subsequent Execute/Query operations use this address:
lumiwave-protocold query wasm list-contract-by-code <CODE_ID>3. Execute
Send a transaction that changes the contract's state. Only messages defined in ExecuteMsg can be used, and gas fees apply.
lumiwave-protocold tx wasm execute <CONTRACT_ADDRESS> \
'{"increment": {}}' \
--from=<WALLET> \
--gas=auto \
--fees=5000ulwp \
--chain-id=lumiwaveprotocol4. Query
Read-only query of the contract's state. Since it's not a transaction, no gas fees are charged and results are returned immediately.
lumiwave-protocold query wasm contract-state smart <CONTRACT_ADDRESS> \
'{"get_count": {}}'Querying via REST API
Contract information can also be queried via REST API without the CLI. This is useful when fetching contract data from web frontends. For smart queries, the JSON message is Base64-encoded before being passed.
# Code list — all Wasm codes uploaded to the chain
curl http://localhost:1317/cosmwasm/wasm/v1/code
# Specific code info — uploader, checksum, and other details for a code
curl http://localhost:1317/cosmwasm/wasm/v1/code/<CODE_ID>
# Contract info — code ID, admin, label, and other contract details
curl http://localhost:1317/cosmwasm/wasm/v1/contract/<CONTRACT_ADDRESS>
# Contract state query — pass QueryMsg as Base64 encoded
curl http://localhost:1317/cosmwasm/wasm/v1/contract/<CONTRACT_ADDRESS>/smart/$(echo -n '{"get_count":{}}' | base64)Contract Migration
Migration is a feature that replaces only the code with a new version while preserving the existing contract's state (data). This allows you to upgrade contracts for bug fixes or feature additions without redeploying. Only contracts with an admin set via --admin during instantiation can be migrated.
# Upload new code
lumiwave-protocold tx wasm store artifacts/my_contract_v2.wasm \
--from=<WALLET> --gas=auto --fees=50000ulwp
# Migrate
lumiwave-protocold tx wasm migrate <CONTRACT_ADDRESS> <NEW_CODE_ID> \
'{"migration_msg": {}}' \
--from=<ADMIN> \
--gas=auto \
--fees=10000ulwpAn admin must be specified with the --admin flag when instantiating the contract for migration to be possible. Contracts created with --no-admin cannot be modified.
Key Resources
Related Docs
- API Reference — REST/gRPC API
- Token Factory — Native token creation
- Architecture — Technical stack overview