Return to site
Return to site

Bug Deep Dive #14

XYK reflect_curve is incorrect and decreases K over time leading to loss of funds for LP providers - $1493

· Bug Deep Dive
Section image

The XYK reflect_curve algorithm that generates passive orders is unfavorable to the pool, computing more unfavorable prices than what the actual AMM algorithm would give.

Section image

The reason for this is that for instance, for bid orders, the function uses the following to determine the order size at step:​

Section image

The flaw here is that we are using the initial quote_reserve value Q0 when dividing by price. This assumes that the quote assets do not change as we move away from our initial point of the AMM but in reality it does change. The only thing that does not change should be the K = Q * B. Instead, it should be using the formula:

Section image

This works because for a given AMM, at the price Pn = Qn / Bn, we can compute the value of Bn in the AMM using:

Section image

Alpha: this finding can be found by plugging in some numbers and check the output to verify it adds up. For AMMs, the K invariant is supposed to stay the same or increase to provide fees, but in this case it was found out to decrease, so they are losing value. There is a python notebook which helps running some values, otherwise just run some tests.

As such, due to the incorrect pricing, the pool is charging lower than what an regular CP-AMM would charge. This causes the K-invariant to decrease, instead of increase over time causing a loss of funds for LP providers.

Conclusion

This finding would earn you $1493, and the way you would find it is by plugging some numbers to verify that the K increases after pool operations. Additionally, with enough experience intuition would tell you that using a fixed initial price when calculating the delta assets to swap is a red flag.

Full Report
Codebase

Subscribe
Previous
Bug Deep Dive #13
Next
Bug Deep Dive #15
 Return to site
Cookie Use
We use cookies to improve browsing experience, security, and data collection. By accepting, you agree to the use of cookies for advertising and analytics. You can change your cookie settings at any time. Learn More
Accept all
Settings
Decline All
Cookie Settings
Necessary Cookies
These cookies enable core functionality such as security, network management, and accessibility. These cookies can’t be switched off.
Analytics Cookies
These cookies help us better understand how visitors interact with our website and help us discover errors.
Preferences Cookies
These cookies allow the website to remember choices you've made to provide enhanced functionality and personalization.
Save