Skip to content
LogoLogo

For engineers

Math and reject codes live in pure Rust. CosmWasm settle reuses that posture on chain. CosmWasm is the WebAssembly smart-contract runtime on Cosmos chains.

Where the code lives

PieceRole
crates/terp-seams dex modulequote_exact_in, apply_reserves, apply_swap, oracle bound checks, hard reject of oracle mint
contracts/revenue/private-dex (cw-private-dex)CosmWasm settle of a public swap statement plus proof bytes
Transparent crates/dex pairSeparate cleartext token-pair reference for message shape — not the private note venue

Quote (exact in)

A pool is a public pair record with reserves R_in and R_out plus fee factor gamma / gamma_den. Exact-in quote:

delta_out = floor( (R_out * gamma * delta_in) / (R_in * gamma_den + gamma * delta_in) )

On accept: R_in += delta_in, R_out -= delta_out, and R_out stays greater than zero. The pool does not custody note openings.

Settle pipeline

Transparent pair habitPrivate settle
ExecuteMsg::Swap + bank/CW20 inSettleSwap { statement, proof } with proven note spend
compute_swapSeam quote_exact_in with gamma / gamma_den
assert_max_spreadmin_out plus optional oracle mid band
Ask asset transfer outEmit output commitment attributes (cm_out_*)
QueryMsg::SimulationQueryMsg::QuoteExactIn

Host recompute loads the pool, checks Active, optionally checks oracle bounds, recomputes delta_out, enforces min_out, verifies the proof path in use, inserts nullifiers, appends commitments, and updates reserves.

Dual-path verify

ModeWhenBehavior
mock_verifyConfig.mock_verify = true or unit testsAccept non-empty proof after host seam checks (lab)
proof_instance_verifymock_verify = false, zk-api feature, host import presentReal verifying-key check against registered zkid (circuit id for the verifying key)
Fail closedProduction-style config without zk host supportReject settle; no silent pass

A zk host import is the chain-host API that verifies proof bytes against a registered verifying key. Default CosmWasm guest builds omit that import so the wasm module runs on stock wasmd without requiring a zk-enabled host.

Normative reject codes

CodeCondition
ErrMinOutdelta_out < min_out
ErrInsufficientReserveOutput would empty the out leg
ErrOracleStale / ErrOracleMissingRequired mid absent or too old
ErrOracleSlippageImplied price outside mid band
ErrOracleDisabledMintAny attempt to credit balance from mid alone
ErrNullifierExistsReused spent marker
ErrWrongAssetNote asset ≠ pool input leg
ErrPoolPausedPool not Active
Loading diagram...

Topology

v1 seams target hub edges only (Hub–B, Hub–C). A full pair matrix and atomic multi-hop are not in v1. Hub denomination is demo config (for example a lab hub token, or a wrapped-Bitcoin-style cBTC-class unit in fixtures — a demo denomination, not a frozen mainnet symbol).

Bridge mint, light-client deposit gates, and Zcash egress are Private Bridge concerns. Swap conservation does not treat oracle mids or bridge authorities as AMM balance creators.

Tests

Pure math: cargo test -p terp-seams --lib. Contract unit path: cargo test -p cw-private-dex.