mirror of
https://github.com/logos-blockchain/lez-programs.git
synced 2026-07-03 05:29:50 +00:00
The AMM multiplied amounts in u128 — `token_a * token_b` for the initial LP in `new_definition`, `reserve * amount` in swaps, and the mul/div steps in add/remove liquidity. For realistic 18-decimal token amounts the intermediate product exceeds `u128::MAX` (~3.4e38): opening a pool with 100/200 tokens is `1e20 * 2e20 = 2e40`, which panicked and caused the sequencer to skip the transaction. Widen the intermediate arithmetic, not the stored types. Add `mul_div_floor`, `mul_div_ceil`, and `isqrt_product` to `amm_core` (using `alloy_primitives::U256`, as `spot_price_q64_64` already does): they compute the product/division/sqrt in U256 and downcast the result back to u128. Route `new_definition`, `swap_exact_input`/`swap_exact_output`, `add_liquidity`, and `remove_liquidity` through them. `swap_exact_output` keeps its ceil rounding (required input rounded up, in the pool's favour) via `mul_div_ceil`. Balances, reserves, and LP supply stay u128, so account data formats, IDLs, and the token/ata/stablecoin programs are unchanged. This lifts the usable amount range to the full u128.