feat(token-swap): constant-product AMM with protocol fees, slippage protection, and u128 math#38
Merged
Merged
Conversation
f107365 to
3f33723
Compare
…rotection, and u128 math Adds a production-shape constant-product AMM (CPAMM) example for both the Anchor and Quasar frameworks, covering the complete lifecycle of a DEX: deployment, pool creation, liquidity provision, token swaps, and admin fee management. ## Instruction handlers - `create_config` — initialises the singleton Config PDA with trading fee and admin-share parameters (both in basis points). One per deployed program. - `create_pool` — initialises a PoolConfig PDA, an LP-token mint, and two pool reserve token accounts for a given (mint_a, mint_b) pair. - `deposit_liquidity` — transfers tokens from the depositor to the pool and mints LP tokens. Amounts are clamped to the current pool ratio (Uniswap V2 mint() pattern); supports a minimum-LP-tokens slippage floor. - `withdraw_liquidity` — burns LP tokens and returns a proportional share of the effective reserves (vault balance minus admin's owed slice) to the LP. Per-side slippage floors supported. - `swap_tokens` — constant-product swap with a configurable trading fee split between LPs and the admin. Supports a minimum-output slippage floor and re-verifies the x*y=k invariant after every trade as defence-in-depth. - `claim_admin_fees` — lets the Config.admin sweep their accumulated fee claim from both sides of a pool. Resets the accumulators to zero. ## Key design properties - Admin protocol-fee mechanism: Config.admin_share_bps splits each swap's trading fee between LPs and the program operator. Admin fees accrue as virtual claims on the existing pool reserves (no extra CPI per swap) and are swept on demand via claim_admin_fees. - Effective-reserve accounting: all LP math operates on pool_X.amount - admin_fees_owed_X so the admin's owed slice does not distort LP pricing or yield. - u128 checked arithmetic throughout: no floating-point, multiply-before- divide, floor rounding in the pool's favour. - Slippage protection on every state-changing instruction. - Constant-product invariant check on every swap as defence-in-depth. - Singleton Config at seeds [b"config"]; unique PoolConfig per (config, mint_a, mint_b) with mint_a < mint_b. - LP positions as SPL tokens for composability. ## Tests - Anchor: 18 LiteSVM integration tests - Quasar: 14 QuasarSvm integration tests ## Documentation README covers the CPAMM formula, design rationale, full state and instruction reference, and an end-to-end program-flow walkthrough (Alice/Bob/Carol/Dave, NVDAx/TSLAx/USDC pools) with financial terms linked to Investopedia and Solana terminology on first use. https://claude.ai/code/session_01DFHVK3tVoPfz6MJMwEAGBf
3f33723 to
54dd196
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a production-shape constant-product AMM (CPAMM) example for both the Anchor and Quasar frameworks, covering the complete lifecycle of a DEX: deployment, pool creation, liquidity provision, token swaps, and admin fee management.
Instruction handlers
create_config— initialises the singleton Config PDA with trading fee and admin-share parameters (both in basis points). One per deployed program.create_pool— initialises a PoolConfig PDA, an LP-token mint, and two pool reserve token accounts for a given(mint_a, mint_b)pair.deposit_liquidity— transfers tokens from the depositor to the pool and mints LP tokens. Amounts are clamped to the current pool ratio (Uniswap V2mint()pattern); supports a minimum-LP-tokens slippage floor.withdraw_liquidity— burns LP tokens and returns a proportional share of the effective reserves (vault balance minus admin's owed slice) to the LP. Per-side slippage floors supported.swap_tokens— constant-product swap with a configurable trading fee split between LPs and the admin. Supports a minimum-output slippage floor and re-verifies thex*y=kinvariant after every trade as defence-in-depth.claim_admin_fees— lets theConfig.adminsweep their accumulated fee claim from both sides of a pool. Resets accumulators to zero.Key design properties
Config.admin_share_bpssplits each swap's trading fee between LPs and the program operator. Admin fees accrue as virtual claims on the existing pool reserves (no extra CPI per swap) and are swept on demand viaclaim_admin_fees.pool_X.amount - admin_fees_owed_Xso the admin's owed slice does not distort LP pricing or yield.InvariantViolatedif it decreases.[b"config"]. Unique PoolConfig per(config, mint_a, mint_b)withmint_a < mint_b.Tests
Documentation
README covers the CPAMM formula, design rationale, full state and instruction reference, and an end-to-end program-flow walkthrough (Alice/Bob/Carol/Dave, NVDAx/TSLAx/USDC pools) with financial terms linked to Investopedia and Solana terminology on first use.