Skip to content

[Contract] Add Flash Loan Protection Guard in Carbon AMM - #940

Open
Topmatrixmor2014 wants to merge 8 commits into
Farm-credit:mainfrom
Topmatrixmor2014:feat/789-flash-loan-protection
Open

[Contract] Add Flash Loan Protection Guard in Carbon AMM#940
Topmatrixmor2014 wants to merge 8 commits into
Farm-credit:mainfrom
Topmatrixmor2014:feat/789-flash-loan-protection

Conversation

@Topmatrixmor2014

Copy link
Copy Markdown
Contributor

[Contract] Add Flash Loan Protection Guard in Carbon AMM

Closes #789

Summary

Adds a flash loan protection guard to the carbon-dex liquidity pool contract that prevents single-block flash loan manipulations in pricing queries.

Changes Made

New Error Code:

  • FlashLoanAttempt = 15 emitted when a same-ledger interaction is detected on an existing position

Storage:

  • LastInteractionLedger(Address, Address) tracks the last ledger sequence for each (token, provider) position

Constants:

  • FLASH_LEDGER_BUFFER = 1 minimum number of ledgers that must elapse between consecutive interactions

Guarded Functions:

  • deposit() calls assert_no_flash_loan() before processing and update_interaction_ledger() after
  • withdraw() calls assert_no_flash_loan() before processing and update_interaction_ledger() after

New Public View Function:

  • get_last_interaction_ledger(token, provider) -> u32 returns the last ledger sequence when the position was modified

Internal Helpers:

  • assert_no_flash_loan(env, token, provider) panics with FlashLoanAttempt if current ledger is within buffer
  • update_interaction_ledger(env, token, provider) persists the current ledger sequence

CI Fixes Included

  • package.json: removed duplicate devDependencies keys causing JSON parse error
  • harvesta-errors: removed duplicate enum variants with same discriminants
  • species-voting: replaced env.invoker() with explicit Address parameter
  • species-voting: prefixed unused variable with underscore

Testing

  • Flash loan deposit-after-deposit in same ledger rejected
  • Flash loan withdraw-after-deposit in same ledger rejected
  • Operation allowed after advancing to next ledger
  • Different tokens are independently tracked
  • Different providers are independently tracked
  • get_last_interaction_ledger returns correct values across round-trip operations
  • All existing tests continue to pass (backward compatible)

Acceptance Criteria

  • Code compiles cleanly with zero warnings
  • All unit tests pass
  • Proper error codes defined in Error enum
  • Complete documentation comments added to public functions

@Topmatrixmor2014
Topmatrixmor2014 force-pushed the feat/789-flash-loan-protection branch from c491796 to d52beb4 Compare July 29, 2026 14:31
@Topmatrixmor2014

Copy link
Copy Markdown
Contributor Author

Conflicts Resolved

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.

[Contract] Add Flash Loan Protection Guard in Carbon AMM

1 participant