Commit a7d3212
Edward (Mike's bot)
feat(token-swap): production-shape AMM example - protocol fees, slippage, correctness fixes, u128 math
Brings the constant-product AMM example to production shape:
purpose-led naming, a configurable admin protocol-fee mechanism,
slippage protection on every state-changing handler, correctness
fixes on deposit and LP-mint math, a constant-product invariant
check on swap, and a full migration from fixed-point (I64F64) to
u128 + checked_* integer arithmetic. Drops the `fixed` crate
dependency entirely.
Correctness fixes
- deposit_liquidity used K = pool_a * pool_b as a "ratio" and then
multiplied/divided the caller's amount by it. That's nonsense
math, unexercised because all prior tests deposited into an empty
pool. Replaced with Uniswap V2's mint() ratio-clamp pattern in
u128 integer math: caller amounts are upper bounds; the contract
picks the largest pair on the current price line. Uses *effective*
reserves (vault balance minus admin's owed fees). Adds
AmmError::DepositAmountTooSmall for sub-base-unit deposits that
clamp to zero.
- deposit_liquidity LP-mint formula used sqrt(a * b) for *every*
deposit, breaking proportionality on subsequent deposits. Now
matches Uniswap V2:
initial: liquidity = sqrt(a*b) - MINIMUM_LIQUIDITY
subsequent: liquidity = min(a*supply/pool_a, b*supply/pool_b)
using effective reserves. Initial-deposit sqrt is computed with a
u128 Newton's-method integer_sqrt helper (no more I64F64::sqrt).
- claim_admin_fees silently succeeded when both fee accumulators
were zero. Adds AmmError::NothingToClaim so the admin gets a real
signal and the test suite can exercise the no-op path. (Also
fixes a litesvm gotcha where two byte-identical claim txs share a
signature; tests now expire the blockhash between repeat claims.)
New features
- Admin protocol-fee mechanism (Uniswap V2 / Raydium accumulator
pattern). `Config.admin_share_bps` splits each swap's trading fee
between LPs and a configurable admin. `PoolConfig.admin_fees_owed_a`
and `_b` accumulate the admin's per-side claim virtually inside
the existing pool_a/pool_b vaults (no extra CPI per swap). New
`claim_admin_fees` handler lets the admin sweep accrued fees,
authorised via `has_one = admin` against `Config`.
- Slippage protection on every state-changing handler:
swap_tokens: min_output_amount (existing arg; error
renamed OutputTooSmall -> SlippageExceeded)
deposit_liquidity: minimum_lp_tokens_out (new)
withdraw_liquidity: minimum_token_a_out, minimum_token_b_out (new)
Pass 0 to opt out. Withdraw bounds are checked *before* any
transfers. New AmmError variants DepositBelowMinimum and
WithdrawalBelowMinimum.
- Constant-product invariant check on swap. Post-trade
k = effective_a * effective_b is recomputed in u128 (raw u64
multiplication overflows at large reserves) and compared against
the pre-trade invariant; the trade reverts if k decreases.
Defence-in-depth against curve-math regressions.
Renames & structural changes (purpose over implementation)
Account/struct/field renames so a first-time reader can infer
each thing's role from its name:
- Pool account fields:
pool_account_a/b -> pool_a / pool_b
mint_liquidity -> liquidity_provider_mint
depositor_account_a/b -> token_a / token_b
depositor_account_liquidity -> liquidity_provider_token
trader_account_a/b -> token_a / token_b
(the `trader_`/`depositor_` prefix duplicates the signer field
already on the Accounts struct).
- Vault terminology dropped: vault_a/b -> pool_a/b. "Vault" implies
yield-bearing structures (Drift/Kamino/Marinade); these are just
the pool's reserves.
- State struct Amm -> Config (program-level Config, not "the AMM").
- State struct Pool -> PoolConfig (it holds per-pool config /
identity, not pool state).
- Error type TutorialError -> AmmError.
- Accounts derive structs: CreateAmm/CreatePool/DepositLiquidity/
WithdrawLiquidity/SwapExactTokensForTokens ->
CreateConfigAccounts / CreatePoolAccounts / DepositLiquidityAccounts /
WithdrawLiquidityAccounts / SwapTokensAccounts. Structs name what
they *are* (the bag of accounts for handler X), not a verb they
pretend to do.
- Handler renames: create_amm -> create_config (says which account
it creates); swap_exact_tokens_for_tokens -> swap_tokens (the
Uniswap-style disambiguator earns no keep without a sibling).
- swap parameter swap_a: bool -> input_is_token_a: bool (purpose,
not internal direction flag).
- Config is now a singleton: seeds = [b"config"], `id: Pubkey`
parameter removed from create_config. Real DEXes ship one program
per AMM; the `id` was leftover complexity.
Math discipline
- All money math is u128 + checked_mul / checked_div / try_into,
multiply-before-divide, floor rounding in the pool's favour
(Uniswap V2 convention).
- swap_tokens constant-product output formula and fee math
rewritten as u128 numerator / u128 denominator.
- withdraw_liquidity proportional-withdraw formula rewritten in
u128 with the same discipline.
- deposit_liquidity already uses u128 throughout.
- integer_sqrt (u128 Newton's method) replaces I64F64::sqrt for the
initial-deposit branch.
- AmmError::MathOverflow added for the checked_* path.
- Removed `fixed = "1.27.0"` from Cargo.toml.
Documentation
- README rewritten: aligns identifiers with the code, documents the
singleton `Config`, the admin protocol-fee design, slippage args,
the corrected deposit / LP-mint math, and notes integer_sqrt.
- Adds an NVDAx/USDC lifecycle walkthrough with four actors (admin,
LP, retail trader, arbitrageur) and the arithmetic for every
handler call.
Tests
- 18/18 Rust + LiteSVM integration tests passing.
- Coverage: matching-ratio deposit, deposit clamp on either side,
deposit-after-swap effective-ratio, sub-base-unit deposit revert,
proportional LP-mint on subsequent deposits, LP-mint uses
effective reserves after a swap, swap slippage revert, deposit
slippage revert (with exact-floor sanity), withdraw slippage
revert (both sides), invariant preservation across many swaps,
admin-fee accrual and claim, NothingToClaim revert on empty
claim.
Scope
- tokens/token-swap/anchor/programs/token-swap/ (source + tests)
- tokens/token-swap/quasar/ (mirror source + tests)
- tokens/token-swap/README.md1 parent 5540080 commit a7d3212
28 files changed
Lines changed: 2888 additions & 874 deletions
File tree
- tokens/token-swap
- anchor/programs/token-swap
- src
- instructions
- tests
- quasar/src
- instructions
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
26 | 28 | | |
27 | 29 | | |
28 | 30 | | |
| |||
Lines changed: 3 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
6 | 9 | | |
7 | 10 | | |
8 | 11 | | |
| |||
Lines changed: 54 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
8 | 15 | | |
9 | 16 | | |
10 | 17 | | |
11 | | - | |
12 | | - | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
13 | 49 | | |
14 | 50 | | |
15 | 51 | | |
| |||
20 | 56 | | |
21 | 57 | | |
22 | 58 | | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
23 | 74 | | |
Lines changed: 174 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
0 commit comments