Skip to content

feat(amm): apply trading fees to LP accounting#21

Closed
3esmit wants to merge 3 commits intologos-blockchain:mainfrom
3esmit:feat/fee-system/apply
Closed

feat(amm): apply trading fees to LP accounting#21
3esmit wants to merge 3 commits intologos-blockchain:mainfrom
3esmit:feat/fee-system/apply

Conversation

@3esmit
Copy link
Copy Markdown
Collaborator

@3esmit 3esmit commented Apr 1, 2026

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

  • Cherry-picked the surplus-removal fix from feat: enhance remove liquidity function to handle surplus balances #16 so this branch includes the live-vault withdrawal behavior required for LP fee realization.
  • Applied fee-aware swap pricing using the pool's configured fee tier while continuing to transfer the full input amount into the vault.
  • Updated tracked reserves with the fee-adjusted input amount so the charged fee remains in the vault as LP-owned surplus.
  • Updated add_liquidity to mint LP against live vault balances when surplus exists, preventing new LPs from receiving fees accrued before they joined.
  • Kept remove_liquidity reserve accounting intact while distributing proportional live vault balances, allowing withdrawing LPs to realize accrued trading fees.
  • Added AMM unit and integration coverage for fee rounding edge cases, repeated swaps, fee payout on liquidity removal, and add-liquidity behavior after fees accrue.

Review Focus

  • Confirm the cherry-picked surplus-removal change from fix/remove-lp-surplus-missing is the intended base for this PR.
  • Swap reserve updates now intentionally differ from vault balance changes by the configured fee amount.
  • Live vault balances now affect LP minting when surplus exists.
  • Liquidity removal should pay out accumulated surplus proportionally without mutating tracked reserves incorrectly.
  • Multi-transaction integration scenarios now rely on current account nonces as fees accumulate across sequential operations.

How To Test

  1. cargo +nightly fmt --all -- --check
  2. taplo fmt --check .
  3. RISC0_SKIP_BUILD=1 cargo +1.94.0 clippy --workspace --all-targets -- -D warnings
  4. RISC0_DEV_MODE=1 cargo +1.94.0 test --workspace --exclude integration_tests
  5. RISC0_DEV_MODE=1 cargo +1.94.0 test -p integration_tests

Copilot AI review requested due to automatic review settings April 1, 2026 00:33
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread amm/src/tests.rs
Comment thread amm/src/tests.rs
Comment thread amm/src/tests.rs
Comment thread amm/src/tests.rs
@3esmit 3esmit changed the title Feat/fee system/apply feat(amm): apply trading fees to LP accounting Apr 1, 2026
@3esmit 3esmit requested a review from Copilot April 1, 2026 00:51
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@3esmit 3esmit force-pushed the feat/fee-system/apply branch from 7d825b7 to 695b032 Compare April 1, 2026 02:15
@3esmit
Copy link
Copy Markdown
Collaborator Author

3esmit commented Apr 1, 2026

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):

@3esmit 3esmit self-assigned this Apr 9, 2026
3esmit added 3 commits April 10, 2026 11:10
- 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
@3esmit 3esmit force-pushed the feat/fee-system/apply branch from 695b032 to d634f05 Compare April 10, 2026 14:20
@0x-r4bbit
Copy link
Copy Markdown
Collaborator

@3esmit I've rebased the work in #46 and updated the changes, such that fees accumulate in the reserves (no surplus).

Please take a look.
Will close this one in favour of #46

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AMM: Implement fee system - Trading Fees For LPs

3 participants