fix: correct constant-product pricing math for selling in PredictionMarketAMM - #3
Open
mrnetwork0001 wants to merge 1 commit into
Open
fix: correct constant-product pricing math for selling in PredictionMarketAMM#3mrnetwork0001 wants to merge 1 commit into
mrnetwork0001 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.
Description
This PR fixes a critical mathematical vulnerability in the
PredictionMarketAMMcontract.The Problem
In the original implementation, the$noOut$ or $yesOut$ ), and then immediately redeemed those output shares for the underlying collateral (USDC).
sellYesandsellNofunctions used the standard constant-product formula to swap the sold asset into the pool to calculate the output shares (This redemption process burns the shares, which subtracts$noOut$ /$yesOut$ from both reserves (YES and NO). Because subtracting the same quantity from both sides of a product does not preserve the product ($R_Y \cdot R_N$ ), the constant-product invariant ($k$ ) decreased on every sell transaction. This created a severe arbitrage loop where users could buy shares and immediately sell them back to receive more USDC than they paid, draining the AMM's liquidity.
The Solution
To preserve the constant-product invariant$k$ (and allow it to increase when fees are active), we must solve for the exact USDC payout $x$ that satisfies the invariant after the swap and redemption:
$$(R_Y + Y_{eff} - x) \cdot (R_N - x) = R_Y \cdot R_N$$
Expanding this yields the quadratic equation:
$$x^2 - (R_Y + R_N + Y_{eff}) \cdot x + Y_{eff} \cdot R_N = 0$$
Solving for the smaller root gives the correct payout amount:
$$x = \frac{b - \sqrt{b^2 - 4c}}{2}$$
where:
This PR:
Math.soland usesMath.sqrtto solve the quadratic equation on-chain.sellYes,sellNo,calcSellYes, andcalcSellNoto use this pricing logic.