Near Protocol Target
Formagine compiles Forms to NEAR Protocol's native WASM runtime, emitting binaries with near-sdk-compatible exports that deploy directly via the NEAR CLI or RPC API. NEAR's 4MB binary limit is generous, but its storage staking model — where contracts must lock NEAR proportional to on-chain state size at ~1 NEAR per 100KB — makes state efficiency critical. Formagine's minimal state approach is a natural fit: compact binary encoding replaces JSON serialization, optimal trie key layout reduces prefix overhead, and proven invariants are erased rather than stored as runtime check state. The result is contracts that stake less NEAR for the same functionality, with every transition consuming fewer gas units due to smaller instruction counts and zero-cost verified invariants.
Runtime Specifications
| WASM Runtime | NEAR native WASM VM (Wasmer-based, single-pass compilation) |
| Max Binary Size | 4MB |
| Gas Model | NEAR gas units — prepaid gas (up to 300 TGas/tx) + attached gas for cross-contract calls |
| Entry Points | Exported functions matching method names (no fixed entry point convention) |
| Message Format | JSON-encoded arguments passed to named methods |
| State Backend | Trie-based key-value store, storage staking at ~1 NEAR per 100KB |
| Standards | NEP-141 (fungible tokens), NEP-171 (NFTs), NEP-145 (storage management) |
How Formagine Targets NEAR
Each Form transition compiles to a named WASM export function. Unlike CosmWasm's four-entry-point model, NEAR exposes each contract method as a separate export. Formagine generates one export per transition (e.g., transfer, mint, burn) plus query exports for each state field.
State fields map to NEAR's trie-based storage. Scalar fields become single key-value entries. per mappings use prefixed keys with compact binary serialization — each key is the prefix byte concatenated with the address bytes, avoiding the overhead of string-based JSON keys that near-sdk-rs uses by default.
NEAR's storage staking model means every byte of on-chain state has an ongoing cost. Formagine's compact encoding reduces per-entry storage by approximately 40% compared to Borsh-serialized near-sdk-rs structures, directly lowering the NEAR that must be locked.
Gas Optimization
NEAR meters gas at the WASM instruction level, with base costs for host function calls (storage read: ~5 TGas, storage write: ~10 TGas). The 300 TGas per-transaction limit means efficiency matters for complex operations.
Invariant erasure: Proven invariants cost zero gas at runtime. A conservation check that would otherwise consume tens of TGas is eliminated entirely at compile time.
Cross-contract gas allocation: For forms that make cross-contract calls, the compiler generates efficient gas splitting — reserving precise amounts for downstream calls rather than naive equal splits.
Minimal instruction overhead: Without Rust's iterator chains, Result unwrapping, and Borsh serialization framework, each transition compiles to fewer WASM instructions, leaving more of the 300 TGas budget for application logic.
Storage Staking
NEAR's storage staking model charges ~1 NEAR per 100KB of contract state. For a token contract with 10,000 holders, the difference between efficient and inefficient storage encoding compounds:
A near-sdk-rs token with Borsh serialization uses ~120 bytes per account entry (key + value). Formagine uses ~72 bytes per entry with compact binary encoding. At 10,000 holders, that is 1.2MB vs 720KB — saving ~0.48 NEAR in perpetual staking costs. At 100,000 holders, the savings reach ~4.8 NEAR.
Formagine's minimal state approach aligns with NEAR's economic model: less state means less locked capital, making Formagine contracts cheaper to operate at scale.
Example: Token on NEAR
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 NEAR export: pub fn transfer(from: AccountId, to: AccountId, amount: U128) }
Frequently Asked Questions
- How does Formagine optimize for NEAR's storage staking model?
- NEAR requires staking ~1 NEAR per 100KB of state. Formagine minimizes state with compact binary encoding (not JSON or Borsh), optimal trie key layout, and invariant erasure. This reduces storage per account entry by roughly 40% compared to near-sdk-rs, directly lowering the NEAR that must be staked to operate the contract.
- What is the maximum contract size on NEAR Protocol?
- NEAR allows contract binaries up to 4MB. Formagine-compiled contracts typically come in under 80KB — less than 2% of the limit. The small binary size means faster deployment and lower deployment gas costs.
- How does Formagine handle NEAR's prepaid gas model?
- NEAR uses prepaid gas with a 300 TGas per-transaction cap. Formagine contracts consume less gas per operation due to invariant erasure and minimal instruction counts. For cross-contract calls, the compiler generates efficient gas allocation, reserving precise amounts for downstream calls rather than splitting equally.
- Can Formagine contracts implement NEAR standards like NEP-141?
- Yes. Formagine emits near-sdk-compatible WASM exports, making contracts interoperable with the full NEAR ecosystem. Token forms can implement NEP-141 (fungible tokens) and NEP-171 (NFTs). Other NEAR contracts can call Formagine contracts via standard cross-contract call mechanisms, and Formagine contracts can call any deployed NEAR contract.