feat: fair-LP valuation and MIN_OUT slippage on all swap paths#51
Open
sotoJ24 wants to merge 1 commit into
Open
feat: fair-LP valuation and MIN_OUT slippage on all swap paths#51sotoJ24 wants to merge 1 commit into
sotoJ24 wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replaces the spot-price LP valuation in
adapter-soroswapwith amanipulation-resistant constant-product fair-value formula, and adds
configurable slippage enforcement to every swap and liquidity call.
Closes #49.
Problem
Two vulnerabilities in the previous implementation:
NAV manipulation —
position_valuepriced LP positions using theAMM spot price (
reserve_a / reserve_b). An attacker could sandwich avault deposit or withdrawal (skew reserves → transact at distorted NAV
→ swap back) to extract value from other depositors.
Zero slippage protection — every
swap_exact_tokens_for_tokens,add_liquidity, andremove_liquiditycall usedMIN_OUT = 0,meaning the vault accepted any output no matter how bad.
Changes
common/types.rsSlippageConfigstruct withmax_slippage_bpsandmax_price_impact_bpsfields (units: basis points, 1 bps = 0.01%)SlippageConfig::default()(50 bps slippage, 100 bps priceimpact) and
min_out(expected)helper for computing floor amountscommon/storage.rsDataKey::SlippageCfg— a second instance-storage key separatefrom
DataKey::Storageso the config can be updated withoutreinitialising the adapter
load_slippagefalls back toSlippageConfig::default()if never set,so existing deployed adapters are safe on upgrade
common/error.rsPriceImpactTooHigh = 9— operation rejected before hitting AMMSlippageTooHigh = 10— swap output fell below computed floorsoroswap_pool/mod.rs— core logic changeget_fair_value(replaces spot-priceposition_value)Uses the constant-product fair-value formula: