Skip to content

feat(SC-006): AMM reentrancy guards, lp_token module, 0.3% fee tests - #1711

Open
esthertitilayo-dev wants to merge 1 commit into
StellarDevHub:mainfrom
esthertitilayo-dev:fix/1627-amm-reentrancy-lp-token
Open

esthertitilayo-dev wants to merge 1 commit into
StellarDevHub:mainfrom
esthertitilayo-dev:fix/1627-amm-reentrancy-lp-token

Conversation

@esthertitilayo-dev

Copy link
Copy Markdown
Contributor

Closes #1627

Summary

  • Reentrancy guards (flash-swap defense): every state-mutating entry point (add_liquidity, remove_liquidity, swap_a_for_b, swap_b_for_a) now holds a temporary-storage mutex for the whole call; a reentrant entry reverts with new typed error AmmError::Reentrancy (405). Temporary storage guarantees a stale lock cannot survive ledger close.
  • lp_token.rs (spec key file): extracted LP share math into a dedicated, fully unit-tested module — initial_shares (sqrt minus dead shares), deposit_shares (min of the two reserve ratios ⇒ no dilution), withdrawal_amounts (pro-rata inverse of mint), isqrt. lib.rs delegates to it.
  • 0.3% fee (spec): test-setup base fee changed from 50 bps to 30 bps (0.3%), plus a new fee-accumulator test asserting the charged fee equals 0.3% base + price-impact uplift and out = out_before - fee.
  • Tests: reentrancy double-entry blocked (#405), lock released after successful calls, LP no-dilution property, share mint/burn round-trip.

Acceptance criteria

  • k invariant: existing test_constant_product_invariant_swap + fuzz tests verify k_after >= k_before and slippage reverts protect reserves.
  • LP shares without dilution: deposit_shares_does_not_dilute_existing_lps proves pre-existing claims never shrink; test_remove_liquidity covers withdrawal.
  • Fuzz ≥ 10,000 trades: existing test_fuzz_amm_k_invariant_one_million_continuous_actions runs 1,000,000 continuous actions; prop_fuzz_amm_arbitrary_trades adds 50 proptest cases.

Test plan

  • cargo test -p amm — 28 unit + 2 fuzz tests pass.
  • cargo check -p amm — clean; rustfmt --check clean on new/edited code (pre-existing fmt drift in twap.rs/get_twap_cumulative untouched).

…e math module (StellarDevHub#1627)

- Add temporary-storage reentrancy mutex (error 405) to add_liquidity,
  remove_liquidity, swap_a_for_b, and swap_b_for_a (flash-swap defense)
- Create contracts/amm/src/lp_token.rs with testable mint/burn share math
  (initial_shares, deposit_shares, withdrawal_amounts, isqrt) and no-dilution
  unit tests; lib.rs now delegates share accounting to this module
- Align base swap fee with spec (0.3% = 30 bps) and add fee-accumulator test
  asserting 0.3% base + price-impact uplift
- Add reentrancy tests: guard blocks double entry (#405) and is released
  after successful calls
@drips-wave

drips-wave Bot commented Oct 18, 2026

Copy link
Copy Markdown

@esthertitilayo-dev Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@vercel

vercel Bot commented Oct 18, 2026

Copy link
Copy Markdown

@esthertitilayo-dev is attempting to deploy a commit to the Ayomide Adeniran's projects Team on Vercel.

A member of the Team first needs to authorize it.

This branch has not been deployed

No deployments
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.

[SC-006] Constant Product AMM Pool with Flash Swap Defense & Fee Accumulator

1 participant