Skip to content

feat: enhance remove liquidity function to handle surplus balances#16

Closed
3esmit wants to merge 1 commit intologos-blockchain:mainfrom
3esmit:fix/remove-lp-surplus-missing
Closed

feat: enhance remove liquidity function to handle surplus balances#16
3esmit wants to merge 1 commit intologos-blockchain:mainfrom
3esmit:fix/remove-lp-surplus-missing

Conversation

@3esmit
Copy link
Copy Markdown
Collaborator

@3esmit 3esmit commented Mar 31, 2026

Summary

  • update remove_liquidity to compute user payouts from the live vault balances while keeping pool reserve bookkeeping based on tracked reserves
  • keep LP burn semantics unchanged by burning exactly the requested LP amount instead of the previous redundant delta_lp identity
  • add unit and integration coverage for fee-surplus exits, actual-payout slippage checks, and reserve-vs-vault mismatch rejection

Fixes #15
Replaces logos-blockchain/logos-execution-zone#420

Testing

  • cargo +1.94.0 test -p amm_program remove_liquidity
  • RISC0_DEV_MODE=1 cargo +1.94.0 test -p integration_tests amm_remove_liquidity
  • cargo +nightly fmt --all -- --check
  • taplo fmt --check .
  • RISC0_SKIP_BUILD=1 cargo +1.94.0 clippy --workspace --all-targets -- -D warnings
  • RISC0_DEV_MODE=1 cargo +1.94.0 test --workspace --exclude integration_tests
  • RISC0_DEV_MODE=1 cargo +1.94.0 test -p integration_tests

Notes

  • remove-liquidity transfers now use each LP's pro-rata share of the actual vault holdings, so accumulated fee surplus is paid out on exit
  • pool reserve_a and reserve_b remain reserve-based after exit, which preserves any remaining surplus as vault_balance - reserve for the LPs who stay in the pool
  • remove-liquidity now rejects invalid states where a vault balance falls below its tracked reserve, matching the existing add/swap safety checks
  • there are no Instruction, IDL, guest entrypoint, or account-schema changes in this PR

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

This PR updates AMM remove_liquidity to pay exiting LPs pro-rata from live vault balances (thereby distributing accumulated fee surplus), while keeping tracked reserves as the bookkeeping source-of-truth for pool reserves.

Changes:

  • Compute remove-liquidity payouts from actual vault_a/vault_b token balances and run slippage checks against those actual payout amounts.
  • Keep reserve accounting based on tracked reserves and add a guard that rejects vault_balance < tracked_reserve states.
  • Simplify LP burn semantics to burn exactly the requested LP amount, and add unit + integration coverage for surplus exits and mismatch rejection.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
amm/src/remove.rs Uses live vault balances for payout math + slippage checks; burns exact requested LP; asserts reserves do not exceed vault balances.
amm/src/tests.rs Adds unit tests for reserve-vs-vault mismatch panics and surplus-based payout chained calls; updates expected transfer amounts.
integration_tests/tests/amm.rs Adds an integration test covering surplus distribution on remove-liquidity and expected resulting vault/user balances.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Collaborator

@0x-r4bbit 0x-r4bbit left a comment

Choose a reason for hiding this comment

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

As discussed in #12

Let's first clarify whether we want "recover surplus" in the first place.
Then we can discuss whether we want the changes here.

/cc @gravityblast

@3esmit 3esmit self-assigned this Apr 9, 2026
@0x-r4bbit
Copy link
Copy Markdown
Collaborator

Closing this for now as #38 has been closed as well.

@0x-r4bbit 0x-r4bbit closed this Apr 16, 2026
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: distribute fee surplus on LP exit using actual vault balances

3 participants