Torus Invariant

The torus invariant is the master equation of Orbital AMM. It combines the interior sphere constraint and the boundary subspace constraint into a single equation that the smart contract verifies on every swap.

Derivation

Start with the consolidated state:

  • Interior sphere: radius r_int, centered at (r_int, ..., r_int)
  • Boundary circle: radius s_bound in the orthogonal subspace

A point x is on the torus if:

  1. Its projection onto the equal-price axis satisfies the interior sphere constraint
  2. Its orthogonal component lies on the boundary circle

Let α_int = x · v be the position along the equal-price axis, and ‖w‖ be the orthogonal norm. The torus condition is:

(α_int − α_center)² + (‖w‖ − s_bound)² = r_int²

where α_center = r_int√n is the center's position along the equal-price axis.

Rearranging:

r_int² = (α_int − r_int√n)² + (‖w‖ − s_bound)²

This is the torus invariant — the master equation.

Verification Approach

Given the pre-trade state (r_int, s_bound, k_bound) and a proposed trade (Δx_in, Δx_out), the contract:

  1. Computes new sumX and sumXSq from the trade
  2. Derives α_int' = sumX'/√n and ‖w'‖² = sumXSq' − sumX'²/n
  3. Evaluates the RHS: (α_int' − r_int√n)² + (‖w'‖ − s_bound)²
  4. Checks that |LHS − RHS| ≤ tolerance

This is O(1) verification — constant time regardless of how many tokens or ticks are involved.

Special Cases

Pure Interior (s_bound = 0)

When there are no boundary ticks, s_bound = 0, and the invariant becomes:

r_int² = (α_int − r_int√n)² + ‖w‖²

This is just the sphere invariant in polar coordinates — the torus degenerates to a sphere.

Pure Boundary (r_int = 0)

When all ticks are at the boundary, r_int = 0, and the invariant becomes:

0 = α_int² + (‖w‖ − s_bound)²

This forces α_int = 0 and ‖w‖ = s_bound — the pool state is constrained to a lower-dimensional sphere (the boundary circle itself).

Tolerance

Due to integer arithmetic and the AMOUNT_SCALE factor, the contract uses a tolerance:

|LHS − RHS| ≤ TOLERANCE

where TOLERANCE = 1000 in the scaled math space. This allows for small rounding errors while still catching invalid trades.

Why It Works

The torus invariant encodes the entire liquidity structure in a single equation. By verifying this equation holds after a trade, the contract ensures:

  • No liquidity was created or destroyed (conservation)
  • The trade followed the AMM curve (correct pricing)
  • Tick boundaries were respected (concentrated liquidity constraints)

This is the breakthrough of Orbital: complex multi-token, multi-tick liquidity verification in O(1) time.

Connection to code: See the Swap Verification page for the actual PyTeal implementation.