feat(amm): apply trading fees to LP accounting#21
feat(amm): apply trading fees to LP accounting#213esmit wants to merge 3 commits intologos-blockchain:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Implements LP-trading-fee accrual and realization for the AMM by applying a configured fee tier to swap pricing while keeping the fee portion as vault surplus, and by adjusting liquidity add/remove accounting so LPs realize fees via normal AMM flows.
Changes:
- Adds fee-tier plumbing and validation (core constants/helpers, NewDefinition instruction/IDL, guest entrypoint, and runtime asserts in add/swap/remove).
- Applies fee-aware swap reserve accounting (full input deposited to vault, fee-adjusted amount applied to reserves) and preserves fee surplus during add/remove liquidity.
- Expands unit + integration tests for fee tiers, fee rounding edge cases, multi-swap accumulation, and surplus payout behavior.
Reviewed changes
Copilot reviewed 8 out of 9 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| integration_tests/tests/amm.rs | Updates integration scenarios to use fee tiers, current nonces, and adds multi-swap + add/remove-after-fee tests. |
| amm/src/tests.rs | Extends unit tests for fee tiers, swap rounding edge cases, add-liquidity with surplus, and remove-liquidity vault/reserve mismatch checks. |
| amm/src/swap.rs | Applies fee tier to swap pricing and updates tracked reserves using fee-adjusted input. |
| amm/src/remove.rs | Computes withdraw payouts from live vault balances (including surplus) while updating tracked reserves from reserve amounts. |
| amm/src/new_definition.rs | Adds fees parameter, validates supported tiers, and stores the chosen tier in PoolDefinition. |
| amm/src/add.rs | Quotes/mints LP against live vault balances to avoid diluting pre-existing fee surplus. |
| amm/methods/guest/src/bin/amm.rs | Wires fees through the guest entrypoint for pool creation. |
| amm/core/src/lib.rs | Adds fee tier constants + validation helpers, and extends the instruction schema and PoolDefinition docs. |
| amm/amm-idl.json | Extends the NewDefinition IDL to include the fees field. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 8 out of 9 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
7d825b7 to
695b032
Compare
|
To review the changes regarding only the apply of fee on the swaps, see the commit 695b032 changes, otherwise you would also see the changes of the dependent PRs (remove lp fix, and fee setup): |
- charge configured swap fees through fee-adjusted reserve updates - preserve accrued LP fees when adding new liquidity - add AMM coverage for fee accrual, fee payout, and anti-dilution
695b032 to
d634f05
Compare
Purpose
Build on the #20 stage-1 fee-tier work by applying trading fees to swap and liquidity accounting so LPs earn and realize fees through normal AMM usage.
Closes #19
What Changed
add_liquidityto mint LP against live vault balances when surplus exists, preventing new LPs from receiving fees accrued before they joined.remove_liquidityreserve accounting intact while distributing proportional live vault balances, allowing withdrawing LPs to realize accrued trading fees.Review Focus
fix/remove-lp-surplus-missingis the intended base for this PR.How To Test
cargo +nightly fmt --all -- --checktaplo fmt --check .RISC0_SKIP_BUILD=1 cargo +1.94.0 clippy --workspace --all-targets -- -D warningsRISC0_DEV_MODE=1 cargo +1.94.0 test --workspace --exclude integration_testsRISC0_DEV_MODE=1 cargo +1.94.0 test -p integration_tests