-
Notifications
You must be signed in to change notification settings - Fork 37
SqrtPriceLimit computed from pool price/tick reading storage #424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
duncancmt
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It still seems to me that there ought to be a more efficient way of conditionally clamping the stopping price between its min and max values than what has been implemented here. But if you've thought about it and can't come up with anything better, then I'll leave it be.
Can you also triple check that you're rounding in the correct direction with all your fixed-point approximations? I'm extra paranoid about this now after Bunni and BalancerV2.
How does the gas compare for this technique vs the previous technique of passing in the limit in calldata? Make sure to account for the increased intrinsic gas costs of the additional nonzero bytes of calldata. At least from spot checking a couple of snapshots, this seems like a pretty severe gas pessimization, much more than I would've anticipated. Do you know why?
src/core/Ekubo.sol
Outdated
| // mask can only decrease if it is over 0. If it is not and priceSqrt is | ||
| // less than (1 << 62), then priceSqrt will be clamped later on to MIN_SQRT_RATIO | ||
| // as it will still be lower than (1 << 62) | ||
| if (mask > 0 && priceSqrt < (1 << 62)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
I wonder if this if/else tree can be more efficiently implemented. What does the reference do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
they uses the above everytime they do math with sqrtPrices. The match in our case can only move to the upper/lower class as it is just multiplying/dividing for sqrt(2), so I tried to just check that to adjust the result
Co-authored-by: duncancmt <[email protected]>
|
Match was improved and comments made explicit |
duncancmt
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I probably need to make 1 more pass over this before approval. How's the gas consumption looking?
duncancmt
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@iamkunal9's AI agent caught some bugs. Please thoroughly go back over your code for MaverickV2 and make sure there are no more lingering issues
src/core/MaverickV2.sol
Outdated
| uint256 spacing = pool.fastTickSpacing(); | ||
| // everything is rounded down to ensure that the selected tick | ||
| // has at most 100% price impact | ||
| int256 delta = int256(6931 / spacing); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens when spacing is full-range? I believe we end up with delta = 0 and then no swaps are possible. Even in the absence of this edge case, I think that this logic is not correct. Please refer back to the MaverickV2 documentation and fix this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't found anything about full range liquidity in Maverick docs, but I can tell that delta = 0 doesn't prevent a swap from happening (tried it to double check) as the price can move without moving the tick, and that is what we use in Maverick, tick, not price. If I round up what we can guarantee is that the impact is at most 200%
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I initially rounded up, but then wanted to enforce the at most 100% as everywhere else.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you suspect it doesn't work due to something else please let me know
duncancmt
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still need to go over Dodo, Maverick, Ekubo, and UniV2
|
|
||
| uint256 limit = (!zeroForOne).ternary(uint256(1461446703485210103287273052203988822378723970341), uint256(4295128740)); | ||
| (uint256 lo, uint256 hi) = zeroForOne.maybeSwap(priceSqrtX96, limit); | ||
| // All operations where rounded down to ensure that the selected price has at most 100% price impact |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this comment is justified. Can you explain more?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔, it is kind of naive, the math was done to get to the price which exact 100% price impact, as this is exact math, so in floating math we will get to the exact price (==). But, as this is exact math, rounding down in the middle changes the inequality to less or equal (<=). That is why I said at most but we should not be able to reach the boundary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to the price which exact 100% price impact
are you sure it's exactly 100% price impact? you're using fixnums, which are not typically exact and demand attention to rounding direction. given that the direction that the price moves depends on zeroForOne, I would expect the correct computation to require that we round up in one direction and round down in the other. I would not expect that rounding down gives the correct <= bounds in both cases.
of course, given that we're working with fairly large fixnum bases here (Q96 and Q95), it's possible that the error introduced by inconsistent rounding either doesn't practically matter or literally does not matter even at the extremes of sqrtPriceX96. it would be good to call this out explicitly in this comment.
Co-authored-by: duncancmt <[email protected]>
Co-authored-by: duncancmt <[email protected]>
Co-authored-by: duncancmt <[email protected]>
Co-authored-by: duncancmt <[email protected]>
Co-authored-by: duncancmt <[email protected]>
Co-authored-by: duncancmt <[email protected]>
duncancmt
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have reviewed everything except your changes to the DodoV1 action. I am having some trouble with that one figuring out whether what you've written is exactly equivalent to the old code. Can you walk me through your argument about why it's the same?
Co-authored-by: duncancmt <[email protected]>
Co-authored-by: duncancmt <[email protected]>
Co-authored-by: duncancmt <[email protected]>
Co-authored-by: duncancmt <[email protected]>
Co-authored-by: duncancmt <[email protected]>
Co-authored-by: duncancmt <[email protected]>
|
Let me list what I discover and did in DodoV1:
|
🛡️ Immunefi PR ReviewsWe noticed that your project isn't set up for automatic code reviews. If you'd like this PR reviewed by the Immunefi team, you can request it manually using the link below: Once submitted, we'll take care of assigning a reviewer and follow up here. |

100% price impact used as limit