Skip to content

Avoid recomputing amountIn when exactIn and `amountRemainingLessF…#718

Merged
snreynolds merged 2 commits into
Uniswap:mainfrom
shuhuiluo:no-recompute-amount-in
Aug 29, 2024
Merged

Avoid recomputing amountIn when exactIn and `amountRemainingLessF…#718
snreynolds merged 2 commits into
Uniswap:mainfrom
shuhuiluo:no-recompute-amount-in

Conversation

@shuhuiluo

Copy link
Copy Markdown
Contributor

…ee < amountIn`

Originally part of #280.

Related Issue

Which issue does this pull request resolve?

Description of changes

Simplify the flow in SwapMath library by removing unnecessary calculations related to input amounts. The removed calculations are replaced with a direct assignment of the remaining amount which simplifies the logic. Moreover, the potential for any rounding issues is significantly reduced.

In the case of exactIn and amountRemainingLessFee < amountIn, the original logic computes sqrtPriceNextX96 from amountRemainingLessFee. It then uses sqrtPriceNextX96 to compute amountIn, which shouldn't differs much from amountRemainingLessFee, while amountIn can simply exhaust amountRemainingLessFee.

hensha256
hensha256 previously approved these changes Aug 26, 2024
@hensha256

Copy link
Copy Markdown
Contributor

@snreynolds is also going to review this. Please dont merge without approvals from both of us 🙏

…ee < amountIn`

Simplify the flow in SwapMath library by removing unnecessary calculations related to input amounts. The removed calculations are replaced with a direct assignment of the remaining amount which simplifies the logic. Moreover, the potential for any rounding issues is significantly reduced.
@shuhuiluo shuhuiluo force-pushed the no-recompute-amount-in branch from a36d08a to 03a2948 Compare August 27, 2024 17:34
snreynolds
snreynolds previously approved these changes Aug 28, 2024

@snreynolds snreynolds left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

makes sense to me! that edge case I believe is really just a rounding error and with this change it rounds the fee down which I believe is more accurate behavior?

@snreynolds snreynolds merged commit 8f147f1 into Uniswap:main Aug 29, 2024
@shuhuiluo shuhuiluo deleted the no-recompute-amount-in branch August 29, 2024 21:01
GianfrancoBazzani pushed a commit to GianfrancoBazzani/v4-core that referenced this pull request Aug 30, 2024
Uniswap#718)

Avoid recomputing `amountIn` when `exactIn` and `amountRemainingLessFee < amountIn`

Simplify the flow in SwapMath library by removing unnecessary calculations related to input amounts. The removed calculations are replaced with a direct assignment of the remaining amount which simplifies the logic. Moreover, the potential for any rounding issues is significantly reduced.
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.

3 participants