-
Notifications
You must be signed in to change notification settings - Fork 270
feat: Universal Router v2.1.1 #470
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
Open
gretzke
wants to merge
16
commits into
main
Choose a base branch
from
ur-2.1.1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,049
−241
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
f18ee8a
feat: add PAY_PORTION_FULL_PRECISION command with 1e18 precision
gretzke e902459
feat: add TRANSFER_FROM command using standard ERC20 approvals
gretzke 2125fc2
chore: update v4-periphery submodule
gretzke ca7f32f
feat: add per-hop slippage checks to V2 and V3 swap commands
gretzke 7206c96
test: add integration tests for PAY_PORTION_FULL_PRECISION and TRANSF…
gretzke 11a5c54
bytecodehash none
gretzke 53b297c
revert: remove TRANSFER_FROM command (e902459)
hensha256 562b48f
update submodule, and tiny tweaks
hensha256 dbbdaa1
linting
hensha256 c908caf
snapshots
hensha256 cc89977
oz m01
hensha256 74ff047
OZ l03
hensha256 132f629
snapshots
hensha256 ef061d4
OZ N05, plus accurage bytecode snapshot
hensha256 e7f31a6
OZ N08
hensha256 edf7265
update submodule
hensha256 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,11 +13,13 @@ abstract contract V2SwapRouter is UniswapImmutables, Permit2Payments { | |
| error V2TooLittleReceived(); | ||
| error V2TooMuchRequested(); | ||
| error V2InvalidPath(); | ||
| error V2TooLittleReceivedPerHop(uint256 hopIndex, uint256 minPrice, uint256 price); | ||
| error V2InvalidHopPriceLength(); | ||
|
|
||
| function _v2Swap(address[] calldata path, address recipient, address pair) private { | ||
| function _v2Swap(address[] calldata path, address recipient, address pair, uint256[] calldata minHopPriceX36) | ||
| private | ||
| { | ||
| unchecked { | ||
| if (path.length < 2) revert V2InvalidPath(); | ||
|
|
||
| // cached to save on duplicate operations | ||
| (address token0,) = UniswapV2Library.sortTokens(path[0], path[1]); | ||
| uint256 finalPairIndex = path.length - 1; | ||
|
|
@@ -29,6 +31,11 @@ abstract contract V2SwapRouter is UniswapImmutables, Permit2Payments { | |
| input == token0 ? (reserve0, reserve1) : (reserve1, reserve0); | ||
| uint256 amountInput = ERC20(input).balanceOf(pair) - reserveInput; | ||
| uint256 amountOutput = UniswapV2Library.getAmountOut(amountInput, reserveInput, reserveOutput); | ||
| if (minHopPriceX36.length != 0) { | ||
| uint256 price = amountOutput * Constants.PRICE_PRECISION / amountInput; | ||
|
Comment on lines
33
to
+35
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Btw this operation will overflow for any values over 2^137, which I assume is fine but just flagging since seems like V2 amounts are uint256 instead of uint128 like v3/v4 |
||
| uint256 minPrice = minHopPriceX36[i]; | ||
| if (price < minPrice) revert V2TooLittleReceivedPerHop(i, minPrice, price); | ||
| } | ||
| (uint256 amount0Out, uint256 amount1Out) = | ||
| input == token0 ? (uint256(0), amountOutput) : (amountOutput, uint256(0)); | ||
| address nextPair; | ||
|
|
@@ -49,16 +56,22 @@ abstract contract V2SwapRouter is UniswapImmutables, Permit2Payments { | |
| /// @param amountOutMinimum The minimum desired amount of output tokens | ||
| /// @param path The path of the trade as an array of token addresses | ||
| /// @param payer The address that will be paying the input | ||
| /// @param minHopPriceX36 Per-hop minimum price array in 1e36 precision (empty to disable) | ||
| function v2SwapExactInput( | ||
| address recipient, | ||
| uint256 amountIn, | ||
| uint256 amountOutMinimum, | ||
| address[] calldata path, | ||
| address payer | ||
| address payer, | ||
| uint256[] calldata minHopPriceX36 | ||
| ) internal { | ||
| address firstPair = UniswapV2Library.pairFor( | ||
| UNISWAP_V2_FACTORY, UNISWAP_V2_PAIR_INIT_CODE_HASH, path[0], path[1] | ||
| ); | ||
| if (path.length < 2) revert V2InvalidPath(); | ||
| if (minHopPriceX36.length != 0 && minHopPriceX36.length != path.length - 1) { | ||
| revert V2InvalidHopPriceLength(); | ||
| } | ||
|
|
||
| address firstPair = | ||
| UniswapV2Library.pairFor(UNISWAP_V2_FACTORY, UNISWAP_V2_PAIR_INIT_CODE_HASH, path[0], path[1]); | ||
| if ( | ||
| amountIn != Constants.ALREADY_PAID // amountIn of 0 to signal that the pair already has the tokens | ||
| ) { | ||
|
|
@@ -68,7 +81,7 @@ abstract contract V2SwapRouter is UniswapImmutables, Permit2Payments { | |
| ERC20 tokenOut = ERC20(path[path.length - 1]); | ||
| uint256 balanceBefore = tokenOut.balanceOf(recipient); | ||
|
|
||
| _v2Swap(path, recipient, firstPair); | ||
| _v2Swap(path, recipient, firstPair, minHopPriceX36); | ||
|
|
||
| uint256 amountOut = tokenOut.balanceOf(recipient) - balanceBefore; | ||
| if (amountOut < amountOutMinimum) revert V2TooLittleReceived(); | ||
|
|
@@ -80,19 +93,25 @@ abstract contract V2SwapRouter is UniswapImmutables, Permit2Payments { | |
| /// @param amountInMaximum The maximum desired amount of input tokens | ||
| /// @param path The path of the trade as an array of token addresses | ||
| /// @param payer The address that will be paying the input | ||
| /// @param minHopPriceX36 Per-hop minimum price array in 1e36 precision (empty to disable) | ||
| function v2SwapExactOutput( | ||
| address recipient, | ||
| uint256 amountOut, | ||
| uint256 amountInMaximum, | ||
| address[] calldata path, | ||
| address payer | ||
| address payer, | ||
| uint256[] calldata minHopPriceX36 | ||
| ) internal { | ||
| (uint256 amountIn, address firstPair) = UniswapV2Library.getAmountInMultihop( | ||
| UNISWAP_V2_FACTORY, UNISWAP_V2_PAIR_INIT_CODE_HASH, amountOut, path | ||
| ); | ||
| if (path.length < 2) revert V2InvalidPath(); | ||
| if (minHopPriceX36.length != 0 && minHopPriceX36.length != path.length - 1) { | ||
| revert V2InvalidHopPriceLength(); | ||
| } | ||
|
|
||
| (uint256 amountIn, address firstPair) = | ||
| UniswapV2Library.getAmountInMultihop(UNISWAP_V2_FACTORY, UNISWAP_V2_PAIR_INIT_CODE_HASH, amountOut, path); | ||
| if (amountIn > amountInMaximum) revert V2TooMuchRequested(); | ||
|
|
||
| payOrPermit2Transfer(path[0], payer, firstPair, amountIn); | ||
| _v2Swap(path, recipient, firstPair); | ||
| _v2Swap(path, recipient, firstPair, minHopPriceX36); | ||
| } | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
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.
Not immediately clear that 1e18 is precision value. Maybe use a constant here?