Skip to content
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

Refactor swap logic and add getSqrtPriceTarget function #662

Merged
merged 2 commits into from
May 21, 2024

Conversation

shuhuiluo
Copy link
Contributor

@shuhuiluo shuhuiluo commented May 17, 2024

Part of #280.

Related Issue

Which issue does this pull request resolve?

Description of changes

Improvements to the SwapMath library have been implemented. A new function, getSqrtPriceTarget, written in Yul using bitwise operations, has been introduced to replace a nested ternary operation within Pool.swap:

(
    params.zeroForOne
        ? step.sqrtPriceNextX96 < params.sqrtPriceLimitX96
        : step.sqrtPriceNextX96 > params.sqrtPriceLimitX96
) ? params.sqrtPriceLimitX96 : step.sqrtPriceNextX96

which basically was doing

zeroForOne ? max(sqrtPriceNextX96, sqrtPriceLimitX96) : min(sqrtPriceNextX96, sqrtPriceLimitX96)

The commit refactors the swap logic to use a newly added function, getSqrtPriceTarget. This function, defined in the SwapMath library, computes the square root price target for the next swap step. A corresponding fuzz test has been included in test/libraries/SwapMath.t.sol to confirm the correctness of the function. The update simplifies the code and enhances readability by abstracting complex operations into a standalone function.

The commit refactors the swap logic to use a newly added function, getSqrtPriceTarget. This function, defined in the SwapMath library, computes the square root price target for the next swap step. A corresponding fuzz test has been included in test/libraries/SwapMath.t.sol to confirm the correctness of the function. The update simplifies the code and enhances readability by abstracting complex operations into a standalone function.
@shuhuiluo shuhuiluo force-pushed the get-sqrt-price-target branch from 8ee3cf0 to 7f4c9bd Compare May 17, 2024 18:01
// sqrtPriceTargetX96 = min(sqrtPriceNextX96, sqrtPriceLimitX96)
let nextOrLimit := xor(lt(sqrtPriceNextX96, sqrtPriceLimitX96), zeroForOne)
let symDiff := xor(sqrtPriceNextX96, sqrtPriceLimitX96)
sqrtPriceTargetX96 := xor(sqrtPriceLimitX96, mul(symDiff, nextOrLimit))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤯 woah thats cool

hensha256
hensha256 previously approved these changes May 21, 2024
@hensha256 hensha256 merged commit eb6a671 into Uniswap:main May 21, 2024
5 of 6 checks passed
@shuhuiluo shuhuiluo deleted the get-sqrt-price-target branch May 21, 2024 21:07
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.

2 participants