-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Remove mandatory viaIR #872
Merged
Merged
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
36c948c
scoping to avoid IR
saucepoint 0d1cf66
pick 4 variables to pack
saucepoint 223d3f9
save 12 gas
saucepoint fb5d9e4
micro gas savings i hate it here
saucepoint cbf9c81
cursed micro optimizations
saucepoint a6a5364
what im calling good enough 😭
saucepoint f6a3837
cleanup deadcode
saucepoint 7dfd7b4
reorganize comments
saucepoint df5583b
wip move feeGrowth to step
saucepoint d437e98
remove swapstart
saucepoint 20362d0
Merge branch 'main' into i-hate-ir
saucepoint 1a49f8a
merge in main; regenerate gas
saucepoint 1c25afe
gas things
saucepoint 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
2 changes: 1 addition & 1 deletion
2
.forge-snapshots/add liquidity to already existing position with salt.snap
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
144608 | ||
144632 |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
170897 | ||
170928 |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
274175 | ||
274199 |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
135101 | ||
135122 |
2 changes: 1 addition & 1 deletion
2
.forge-snapshots/create new liquidity to a position with salt.snap
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
292800 | ||
292824 |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
106321 | ||
106333 |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
145736 | ||
145754 |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
57439 | ||
57442 |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
774 | ||
777 |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
949 | ||
952 |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
774 | ||
777 |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
949 | ||
952 |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
60021 | ||
60025 |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
59723 | ||
59726 |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
24006 | ||
24035 |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
141204 | ||
141229 |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
130579 | ||
130597 |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
112523 | ||
112535 |
2 changes: 1 addition & 1 deletion
2
.forge-snapshots/simple addLiquidity second addition same range.snap
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
98819 | ||
98831 |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
161364 | ||
161376 |
2 changes: 1 addition & 1 deletion
2
.forge-snapshots/simple removeLiquidity some liquidity remains.snap
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
92974 | ||
92983 |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
85087 | ||
85096 |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
108369 | ||
108521 |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
123186 | ||
123341 |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
126821 | ||
124663 |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
154631 | ||
154789 |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
105493 | ||
105640 |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
116558 | ||
116708 |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
129116 | ||
129272 |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
118569 | ||
118728 |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
139603 | ||
139750 |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
155027 | ||
155182 |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
206132 | ||
206434 |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
139207 | ||
139362 |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
132195 | ||
132346 |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
169398 | ||
169609 |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
145534 | ||
145689 |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
147814 | ||
147972 |
This file contains 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 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 | ||||
---|---|---|---|---|---|---|
|
@@ -262,12 +262,14 @@ library Pool { | |||||
uint256 amountOut; | ||||||
// how much fee is being paid in | ||||||
uint256 feeAmount; | ||||||
// the global fee growth of the input token. updated in storage at the end of swap | ||||||
uint256 feeGrowthGlobalX128; | ||||||
} | ||||||
|
||||||
struct SwapParams { | ||||||
int256 amountSpecified; | ||||||
int24 tickSpacing; | ||||||
bool zeroForOne; | ||||||
int256 amountSpecified; | ||||||
uint160 sqrtPriceLimitX96; | ||||||
uint24 lpFeeOverride; | ||||||
} | ||||||
|
@@ -281,22 +283,19 @@ library Pool { | |||||
Slot0 slot0Start = self.slot0; | ||||||
bool zeroForOne = params.zeroForOne; | ||||||
|
||||||
uint128 liquidityStart = self.liquidity; | ||||||
uint256 protocolFee = | ||||||
zeroForOne ? slot0Start.protocolFee().getZeroForOneFee() : slot0Start.protocolFee().getOneForZeroFee(); | ||||||
|
||||||
hensha256 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
// the amount remaining to be swapped in/out of the input/output asset. initially set to the amountSpecified | ||||||
int256 amountSpecifiedRemaining = params.amountSpecified; | ||||||
// the amount swapped out/in of the output/input asset. initially set to 0 | ||||||
int256 amountCalculated = 0; | ||||||
// the global fee growth of the input token. updated in storage at the end of swap | ||||||
uint256 feeGrowthGlobalX128 = zeroForOne ? self.feeGrowthGlobal0X128 : self.feeGrowthGlobal1X128; | ||||||
// initialize to the current sqrt(price) | ||||||
result.sqrtPriceX96 = slot0Start.sqrtPriceX96(); | ||||||
// initialize to the current tick | ||||||
result.tick = slot0Start.tick(); | ||||||
// initialize to the current liquidity | ||||||
result.liquidity = liquidityStart; | ||||||
result.liquidity = self.liquidity; | ||||||
|
||||||
// if the beforeSwap hook returned a valid fee override, use that as the LP fee, otherwise load from storage | ||||||
// lpFee, swapFee, and protocolFee are all in pips | ||||||
|
@@ -308,11 +307,10 @@ library Pool { | |||||
swapFee = protocolFee == 0 ? lpFee : uint16(protocolFee).calculateSwapFee(lpFee); | ||||||
} | ||||||
|
||||||
bool exactInput = params.amountSpecified < 0; | ||||||
|
||||||
// a swap fee totaling MAX_SWAP_FEE (100%) makes exact output swaps impossible since the input is entirely consumed by the fee | ||||||
if (swapFee >= SwapMath.MAX_SWAP_FEE) { | ||||||
if (!exactInput) { | ||||||
// if not exactInput | ||||||
if (!(params.amountSpecified < 0)) { | ||||||
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.
Suggested change
might be better? |
||||||
InvalidFeeForExactOut.selector.revertWith(); | ||||||
} | ||||||
} | ||||||
|
@@ -340,6 +338,7 @@ library Pool { | |||||
} | ||||||
|
||||||
StepComputations memory step; | ||||||
step.feeGrowthGlobalX128 = zeroForOne ? self.feeGrowthGlobal0X128 : self.feeGrowthGlobal1X128; | ||||||
|
||||||
// continue swapping as long as we haven't used the entire input/output and haven't reached the price limit | ||||||
while (!(amountSpecifiedRemaining == 0 || result.sqrtPriceX96 == params.sqrtPriceLimitX96)) { | ||||||
|
@@ -368,7 +367,8 @@ library Pool { | |||||
swapFee | ||||||
); | ||||||
|
||||||
if (!exactInput) { | ||||||
// if not exactInput | ||||||
if (!(params.amountSpecified < 0)) { | ||||||
unchecked { | ||||||
amountSpecifiedRemaining -= step.amountOut.toInt256(); | ||||||
} | ||||||
|
@@ -398,7 +398,8 @@ library Pool { | |||||
if (result.liquidity > 0) { | ||||||
unchecked { | ||||||
// FullMath.mulDiv isn't needed as the numerator can't overflow uint256 since tokens have a max supply of type(uint128).max | ||||||
feeGrowthGlobalX128 += UnsafeMath.simpleMulDiv(step.feeAmount, FixedPoint128.Q128, result.liquidity); | ||||||
step.feeGrowthGlobalX128 += | ||||||
UnsafeMath.simpleMulDiv(step.feeAmount, FixedPoint128.Q128, result.liquidity); | ||||||
} | ||||||
} | ||||||
|
||||||
|
@@ -410,8 +411,8 @@ library Pool { | |||||
// if the tick is initialized, run the tick transition | ||||||
if (step.initialized) { | ||||||
(uint256 feeGrowthGlobal0X128, uint256 feeGrowthGlobal1X128) = zeroForOne | ||||||
? (feeGrowthGlobalX128, self.feeGrowthGlobal1X128) | ||||||
: (self.feeGrowthGlobal0X128, feeGrowthGlobalX128); | ||||||
? (step.feeGrowthGlobalX128, self.feeGrowthGlobal1X128) | ||||||
: (self.feeGrowthGlobal0X128, step.feeGrowthGlobalX128); | ||||||
int128 liquidityNet = | ||||||
Pool.crossTick(self, step.tickNext, feeGrowthGlobal0X128, feeGrowthGlobal1X128); | ||||||
// if we're moving leftward, we interpret liquidityNet as the opposite sign | ||||||
|
@@ -435,17 +436,18 @@ library Pool { | |||||
self.slot0 = slot0Start.setTick(result.tick).setSqrtPriceX96(result.sqrtPriceX96); | ||||||
|
||||||
// update liquidity if it changed | ||||||
if (liquidityStart != result.liquidity) self.liquidity = result.liquidity; | ||||||
if (self.liquidity != result.liquidity) self.liquidity = result.liquidity; | ||||||
|
||||||
// update fee growth global | ||||||
if (!zeroForOne) { | ||||||
self.feeGrowthGlobal1X128 = feeGrowthGlobalX128; | ||||||
self.feeGrowthGlobal1X128 = step.feeGrowthGlobalX128; | ||||||
} else { | ||||||
self.feeGrowthGlobal0X128 = feeGrowthGlobalX128; | ||||||
self.feeGrowthGlobal0X128 = step.feeGrowthGlobalX128; | ||||||
} | ||||||
|
||||||
unchecked { | ||||||
if (zeroForOne != exactInput) { | ||||||
// "if currency1 is specified" | ||||||
if (zeroForOne != (params.amountSpecified < 0)) { | ||||||
swapDelta = toBalanceDelta( | ||||||
amountCalculated.toInt128(), (params.amountSpecified - amountSpecifiedRemaining).toInt128() | ||||||
); | ||||||
|
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.
does this ordering change matter?
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.
we removed the cached
exactInput = params.amountSpecified < 0
, and are now usingparams.amountSpecified < 0
in place of the cached variablemaking it the first element in the struct avoids the need of offset calculations