Polkadot Target

Formagine compiles Forms to ink!-compatible WASM for deployment on any Substrate-based chain running pallet-contracts — Astar, Shiden, Aleph Zero, Phala, and others in the Polkadot and Kusama ecosystems. The compiler emits WASM conforming to ink!'s expected ABI and SCALE codec encoding, with constructor and message selectors matching what pallet-contracts dispatches. Substrate's weight system charges for both execution time (ref_time) and state proof size (proof_size), making two-dimensional optimization essential. Formagine addresses both: invariant erasure reduces ref_time by eliminating runtime verification, and compact state encoding reduces proof_size by minimizing trie node data. Contract binaries range from ~128KB to 2MB depending on chain configuration; Formagine typically produces under 40KB, well within even the strictest limits. Cross-chain composability via XCM means forms on one parachain can interact with forms or pallets on another, with invariant guarantees holding independently on each side.

Runtime Specifications

WASM Runtimepallet-contracts (Substrate module, Wasmi interpreter)
Max Binary Size~128KB to 2MB (chain-configurable; Astar allows up to 2MB)
Gas ModelSubstrate weight system — two dimensions: ref_time + proof_size
Entry PointsConstructor selectors (deploy) + message selectors (call)
EncodingSCALE codec (Substrate's native binary encoding)
State BackendSubstrate storage with storage deposit mechanism
Cross-chainXCM (Cross-Consensus Messaging) for parachain interop
Supported ChainsAstar, Shiden, Aleph Zero, Phala, and any Substrate chain with pallet-contracts

How Formagine Targets ink!

Formagine generates WASM that conforms to the ink! contract ABI. Each Form's initial state maps to a constructor selector — a 4-byte identifier that pallet-contracts dispatches during instantiation. Each transition maps to a message selector, and query methods map to read-only message selectors.

State encoding uses SCALE codec, Substrate's native binary format. Unlike JSON or Borsh, SCALE is a compact fixed-schema codec that Substrate nodes parse natively. Formagine generates SCALE encoding/decoding directly, without linking the parity-scale-codec Rust crate, producing tighter serialization code.

The ink! metadata format (a JSON descriptor of the contract's ABI) is also generated by the compiler, enabling integration with Polkadot.js, Contracts UI, and other Substrate tooling. Developers and users interact with Formagine contracts using the same tools they use for any ink! contract.

Weight Optimization

Substrate weight has two dimensions that matter for contract execution:

ref_time measures execution time in picoseconds. Each WASM instruction contributes to ref_time. Formagine reduces ref_time by erasing proven invariants (zero execution cost for verification) and minimizing instruction counts (no ink! framework overhead, no Rust std).

proof_size measures the size of the state proof needed for light client verification. Smaller state means smaller proofs. Formagine's compact SCALE encoding and optimal storage key layout minimize proof_size, which is particularly important for parachains where state proofs are validated by the relay chain.

The compiler can emit weight annotations for each message, enabling pallet-contracts to estimate costs accurately before execution. This prevents over-charging users while ensuring the block weight limit is respected.

Cross-Chain Composability via XCM

Polkadot's Cross-Consensus Messaging (XCM) protocol enables contracts on one parachain to interact with contracts and pallets on other parachains. Formagine can emit XCM integration code for forms that need cross-chain functionality:

A token form on Astar can transfer value to a vault form on Aleph Zero. A governance form on one parachain can execute decisions on another. Each side maintains independent compile-time invariant proofs — the cross-chain message is just data transfer between two verified systems.

XCM messages are constructed using Substrate's standard XCM builder patterns, and the receiving parachain verifies execution within its own pallet-contracts runtime.

Example: Token on Polkadot

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
  }
  -- Constructor selector: 0x9bae9d5e
  -- Message selector: 0x84a15da1 (transfer)
  -- SCALE-encoded params, compatible with Polkadot.js and Contracts UI
}

Frequently Asked Questions

What is the maximum contract size on Polkadot's pallet-contracts?
It varies by chain: Astar and Shiden allow up to 2MB, while more conservative chains limit to 128KB or 256KB. Formagine-compiled contracts typically produce under 40KB of WASM — well within even the tightest limits — because the Rust-free approach avoids ink! standard library, scale codec crate, and storage abstraction overhead.
How does Formagine optimize for Substrate's weight system?
Substrate weight has two dimensions: ref_time (execution time) and proof_size (state proof size). Formagine reduces ref_time via invariant erasure and minimal instructions. It reduces proof_size via compact SCALE encoding and optimal storage key layout. The compiler can also emit per-message weight annotations for accurate pre-dispatch estimation.
Can Formagine contracts use XCM for cross-chain composability?
Yes. Contracts on any pallet-contracts-enabled parachain can send and receive XCM messages. Formagine can emit XCM integration code for cross-parachain form interaction — e.g., a token on Astar interacting with a vault on Aleph Zero. Each side maintains independent compile-time invariant proofs.
Which Substrate chains support Formagine contract deployment?
Any chain with pallet-contracts: Astar (Polkadot's primary smart contract hub), Shiden (Kusama equivalent), Aleph Zero, Phala, and any custom Substrate chain that includes the module. The same WASM binary works across all of them, though weight costs and storage deposit requirements vary per chain.

Form Templates for Polkadot

Other Chains

Resources