Operating state
Maintain a deployment-scoped state record for each market:- Program ID and checksum
- Pool key
- Canonical token0 and token1
- Token display decimals and wrapper relationships
- Fee tier in fee pips
- Current
fee_protocolnumerator - Tick spacing
- Current tick and square-root price
- Active liquidity
- Nearest initialized ticks above and below
- Pause and enable controls
- Open position token IDs
- Latest unspent
PositionNFTrecord for each position
Quote construction
Before quoting:- Read the slot and pool state at a known height.
- Confirm token order, decimals, and wrapper relationships.
- Walk the initialized-tick index in the proposed direction.
- Apply the fixed iteration limit used by the selected swap path.
- Apply fee and protocol-share rounding per step.
- Keep final output and any refund in native token base units.
- Enforce a nonzero minimum output appropriate to the instruction.
Tiny inputs
After fee adjustment, a small input can have zero amount available for price movement. The bounded partial path returns the full input without moving price or charging a fee. Routers and market makers should still reject quote sizes whose expected output is zero.Range and inventory management
Each position is active over:- Distance to each boundary
- Inventory by token
- Active liquidity share
- Realized and pending fees by input token
- Expected proving and confirmation time for a rebalance
- Available token records for the next mint or increase
- Current predecessor hints if a boundary must be inserted
Exact-limit tick behavior
Reaching an initialized tick completes the crossing even when the tick price equals the user’s square-root-price limit. At the boundary, the contract:- Charges the step fee.
- Adds LP growth using pre-crossing active liquidity.
- Flips both token outside-growth counters.
- Applies
liquidity_net. - Stops if no further price movement is allowed.
Rebalancing workflow
A range cannot be changed in place. A controlled rebalance normally uses:- Decrease liquidity in the old range.
- Confirm the resulting principal and settled fees in
tokens_owed. - Collect the required amounts into token records.
- Select and align the new range.
- Refresh initialized-tick insertion hints.
- Mint the new position with unspent records.
- Verify public position and slot changes.
- Burn the old position after its liquidity and owed balances are zero.
Settlement cadence
Pending fees do not need to be collected for the position to keep earning. They remain represented by Q128.128 growth until increase, decrease, collect, or freeze settles them. More frequent settlement can:- Improve treasury availability
- Simplify period-end realization
- Increase proof and custody operations
- Discard more sub-unit fee dust
Fee reconciliation
Track at least four fee categories:
There is also a rounding surplus:
- Fee growth floors once per step.
- Position entitlement floors at settlement.
- The contract does not carry a pool-level residual across liquidity epochs.
- The contract has no generic sweep for the resulting surplus.
Public and confidential records
Publicly observable data includes:- Pool fee and protocol share
- Slot price, tick, liquidity, and fee growth
- Initialized ticks and outside growth
- Position range, liquidity, checkpoints, and owed amounts
- Public amounts requested by collect
Position. Amount and timing correlation remain possible.
Controls and incident response
Monitor:- Global, token, pair, and pool pause state
- Position freeze mappings
- Program checksum changes
- Protocol fee setting changes
- Boundary-list consistency
- Active liquidity versus boundary net changes
- Unexpected fee-growth wraps
- Settlement amounts approaching
u128limits - Persistent contract balance not explained by principal, protocol fees, or LP claims
Fresh-deployment cutover
The current schema uses two-limb Q128.128 fee-growth values and no residual state. It is incompatible with deployments that use scalar growth fields in the same mapping structures. Aleo program upgrades freeze existing mapping and struct interfaces, so adopting the current schema requires a fresh deployment. For a cutover:- Stop routing new flow to the old deployment.
- Disable old pools while preserving LP decrease and collect access.
- Reconcile and collect recorded protocol fees.
- Deploy and bootstrap the new program.
- Establish new pools and approvals.
- Update simulators, decoders, routers, and custody systems together.
- Exit and remint liquidity rather than copying old fee checkpoints.
- Keep old and new histories under separate schema versions.
Daily control checklist
- Confirm deployment checksum and configuration.
- Reconcile token metadata, wrapper relationships, and canonical order.
- Refresh slots and initialized ticks.
- Review exact-limit boundary exposure.
- Compare pending and settled fees.
- Confirm the current protocol share.
- Check unspent ownership and token records.
- Review pause and freeze state.
- Investigate unexplained surplus without assuming it is withdrawable.
- Preserve evidence for each range, quote, settlement, and rebalance decision.