CosmWasm Target

Formagine compiles Forms to CosmWasm-compatible WASM binaries that run on any Cosmos SDK chain with the wasmd module — Osmosis, Neutron, Injective, Sei, Stargaze, Juno, Terra, and others. The compiler emits WASM with the four standard entry points (instantiate, execute, query, migrate) and JSON message dispatch, producing binaries that wasmd accepts without modification. Because Formagine bypasses the Rust toolchain entirely — no cosmwasm-std, no serde, no std library — the resulting binaries are an order of magnitude smaller than equivalent Rust-compiled contracts, typically under 50KB uncompressed versus 200-800KB from standard CosmWasm Rust templates. Proven invariants are erased during compilation, so the on-chain binary contains only the state transitions themselves with zero verification overhead at execution time.

Runtime Specifications

WASM Runtimewasmd (Cosmos SDK module wrapping Wasmer/Wasmtime)
Max Binary Size~800KB uncompressed, ~400KB gzipped (chain-configurable)
Gas ModelCosmos SDK gas — per-instruction metering plus storage read/write costs
Entry Pointsinstantiate, execute, query, migrate
Message FormatJSON-encoded messages dispatched to entry points
State BackendKey-value store accessed via CosmWasm storage API
Supported ChainsOsmosis, Neutron, Injective, Sei, Stargaze, Juno, Terra, Archway, and any wasmd-enabled chain

How Formagine Targets CosmWasm

Each Form transition compiles to an execute message handler. The compiler generates a dispatch table mapping JSON message variants to transition implementations. State fields map to CosmWasm storage items — scalar fields become single storage entries, and per mappings become prefixed key-value ranges.

The instantiate entry point initializes all state fields from the deployment message. Query handlers are auto-generated for each state field, supporting both point queries (single address balance) and aggregate queries (total supply). The migrate entry point handles schema evolution when a contract is upgraded.

Because Formagine does not link against the Rust standard library or cosmwasm-std, the binary contains only the compiled transition logic and a minimal runtime for storage access and message parsing. This eliminates the ~200KB baseline overhead that every Rust CosmWasm contract carries.

Gas Optimization

Cosmos SDK gas charges per WASM instruction executed, with additional costs for storage reads (typically 1000 gas per read) and writes (typically 2000-3000 gas per write). Formagine optimizes for this model:

Invariant erasure: Proven invariants become zero-cost. A conservation check that would cost hundreds of gas instructions per transaction is eliminated entirely.

Minimal instruction count: Without Rust's iterator abstractions, error handling machinery, and serialization overhead, each transition compiles to fewer WASM instructions.

Storage batching: The compiler analyzes state access patterns and batches reads and writes, minimizing the number of storage operations per transition.

Deployment Process

Formagine produces a .wasm file ready for upload. The deployment pipeline is:

1. Compile the Form to CosmWasm WASM via the Formagine compiler or API.

2. Upload the binary to any CosmWasm-enabled chain using wasmd tx wasm store or the chain's CLI.

3. Instantiate the contract with initial state parameters.

4. The contract is live — execute transitions, query state, migrate as needed.

Formagine's automated deployment pipeline handles steps 2-3 for members, supporting multi-chain deployment from a single form definition.

Example: Token on CosmWasm

form Token {
  state {
    balance : Nat per Address
    supply  : Nat
    owner   : Address
  }

  invariant conservation :
    sum(balance) == supply

  transition transfer(from, to, amount) {
    require balance[from] >= amount
    balance[from] -= amount
    balance[to]   += amount
  }
  -- Compiles to CosmWasm execute handler: {"transfer":{"from":"...","to":"...","amount":"..."}}
}

Frequently Asked Questions

What is the maximum WASM binary size for CosmWasm contracts?
CosmWasm enforces an approximate 800KB uncompressed binary limit, with gzipped uploads typically constrained to ~400KB. This is configurable per chain. Formagine's Rust-free compilation produces significantly smaller binaries — a typical Token form compiles to under 50KB uncompressed, well within limits even on the most restrictive chains.
How does Formagine generate CosmWasm entry points?
Formagine emits WASM with the four standard CosmWasm entry points: instantiate (deployment initialization), execute (state-mutating transitions), query (read-only state access), and migrate (schema evolution). Each Form transition maps to a JSON-dispatched execute variant. Entry point signatures match wasmd expectations exactly, so the binary uploads directly.
Which Cosmos SDK chains can I deploy Formagine contracts to?
Any chain running the wasmd module: Osmosis, Neutron, Injective, Sei, Stargaze, Juno, Terra, Archway, and others. The same WASM binary deploys to all of them — no recompilation needed per chain. Chain-specific parameters (gas prices, storage costs) vary but the contract binary is universal.
How does Formagine handle the Cosmos SDK gas model?
Cosmos SDK charges per WASM instruction plus storage read/write costs. Formagine optimizes by erasing proven invariants (zero runtime verification cost), producing minimal instruction counts (no Rust std overhead), and batching storage operations. The result is measurably lower gas per transaction compared to equivalent Rust CosmWasm contracts.

Form Templates for CosmWasm

Other Chains

Resources