Coffer Docs
Technical

Pricing Model

Weighted pricing, integer rounding, fees, and reserve accounting

Virtual balances price trades; LP-owned actual balances fund payouts. Use arithmetic matching the target program.

Quantities and units

SymbolMeaning
V_iVirtual balance, raw token units
A_iLP-owned actual balance, raw units
P_iSeparate protocol-fee balance, raw units
w_iCurrent weight, basis points; pool sum 10_000
d_iMint decimals

Use current weights/reserves. Same-token ratios require no decimal rescaling; ratios/exponents use fixed-point precision Q = 10^18.

Exact-input swap

The contract's calc_out_given_in computes output directly from virtual balances, weights, and fee-adjusted input. Actual reserves bound the payout; the swap does not compute or store a pool-wide invariant.

For input x received by the pool after any token transfer fee, base rate f (scale 1_000_000), and protocol share p (scale 10_000):

F = ceil(x × f / 1,000,000)
P_in_fee = ceil(F × p / 10,000)
x_curve = x − F
y_gross = V_out × (1 − (V_in / (V_in + x_curve))^(w_in / w_out))

The last line is idealized. Require y_gross <= A_out before the dynamic fee: excess output rejects the swap; it is never capped to inventory. For output-side dynamic fee S:

y_transfer = y_gross − S
y_received = y_transfer − tokenTransferFee(y_transfer)
require y_received >= minimum_amount_out

Enabled sell-off windows count x before the base fee. The dynamic fee applies to output attributable to taxed window segments, not the entire output at the final rate; see Fees.

Fixed-point rounding

Use the contract's integer arithmetic, not Math.pow:

base = ceil(V_in × Q / (V_in + x_curve))
exponent = floor(w_in × Q / w_out)
power_raw = base if w_in == w_out else powFixed(base, exponent)
power = min(Q, power_raw + 1)
y_gross = floor(V_out × (Q − power) / Q)

These rounding directions avoid increasing payout through ordinary rounding. Equal weights bypass log/exp but retain the same rounding bias. Reproduce bounded arithmetic and the integer power implementation for other weights.

SDK calcOutGivenIn computes the curve only. Full quotes must also apply token transfer fees, flags, dynamic fees, sell-off limits and reserve checks. Use a client compatible with the target program and simulate the transaction.

Reserve accounting

Accounted spendable vault holdings satisfy vault_i = A_i + P_i. Token-2022 withheld transfer fees are separate from both. Unsolicited vault transfers do not update stored pricing balances. For received input x, swap transitions are:

A_in' = A_in + x − P_in_fee
V_in' = V_in + x − P_in_fee
P_in' = P_in + P_in_fee

A_out' = A_out − y_gross
V_out' = V_out − y_gross
P_out' = P_out + S

The LP base-fee portion enters both input actual/virtual balances; the entire dynamic fee enters the output token's protocol counter. It is separate from LP reserves, not a separate LP reward balance. Collection transfers and clears P_i without reducing A_i or V_i. Never subtract protocol fees from actual balances again, or scale virtual balances by that subtraction.

LPs currently have no dynamic-fee claim. A separate LP claim mechanism is planned.

Ordinary swaps preserve each token's A_i − V_i, while V_i / A_i can change.

Spot price and decimals

Pre-fee price in raw input per output units:

spotPriceRaw = (V_in / w_in) / (V_out / w_out)
spotPriceHuman = spotPriceRaw × 10^(d_out − d_in)

Its reciprocal is output per input, the direction used by route prices.

Liquidity calculations

Initial LP supply uses calculateInvariant on virtual balances, normalized toward six decimals. This internal fixed-point value is not USD or an oracle price. Subsequent proportional joins/exits do not calculate an invariant: they use actual balances and LP supply, scaling virtual balances alongside them; their exact rules and rounding are in Adding and Removing Liquidity.

On this page