Open app

Hook

The fee formula

The hook computes the fee in beforeSwap and returns it with the override flag, so the pool needs the dynamic fee flag (0x800000). Fees are in pips: 1,000,000 is 100%, 100 is 0.01%.

fee, 0 to 1%gap closed, 0 to 40 bps
Model v1, computed live by the fee function in /sdk/toxicflow.ts: a neutral sender (thin), tier 2 (dashed) and tier 3 (thick), for a swap that closes the gap it is sized to.

Formula

closed    = toward ? min(gap, impact) : 0
surcharge = k * closed + size * min(impact, 10000)^2 / 10000 + step * min(repeats, 10)
surcharge = surcharge * tierMult[tier] / 100 + tierAdd[tier]
fee       = clamp(base + surcharge, floor, cap)
score     = (fee - base) * 100 / (cap - base)

Impact

The size signal is the swap's estimated price impact against the liquidity in range, computed from amountSpecified with no loop over ticks.

amount in token1:  impact = 2 * amount * 2^96 / (sqrtPriceX96 * L)
amount in token0:  impact = 2 * amount * sqrtPriceX96 / (2^96 * L)
(in bps, capped at 10,000)

Gap and direction

gap = 2 * |sqrtRef - sqrtPool| / sqrtPool, in bps. A zeroForOne swap pushes the price down, so it moves toward the reference when the reference sits below the pool price, and the other way round.

The model fee

On a swap with a surcharge, the hook keeps 10% of the surcharge in the pool's unspecified currency through afterSwap's return delta. The LP fee is the rest, so the swapper pays the quoted fee in total. A swap at the base fee leaves nothing to the model.

Same code everywhere

The Solidity library ToxicFee and the TypeScript fee function this site runs (served as /sdk/toxicflow.ts) are the same integer math. A Foundry test replays 120 random vectors produced by the TypeScript file through the Solidity library and checks every field.