-
Notifications
You must be signed in to change notification settings - Fork 989
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
Flip the sign of deltas #481
Changes from 29 commits
d93b3c9
13642d8
cd92291
1f4961e
c0e2722
2237a48
df58a45
99243a1
514726d
9c8f549
250d74d
56f6044
6ce20c4
96975e7
115341e
c802859
67f79e8
dff0d82
47bea1a
ba1494d
0f19c28
7a5d41d
b746376
d8ebb60
f45c75a
0043fa4
1a76db2
4a1ccfa
9fcf92f
b02bda8
8ffcc42
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
2203 | ||
1947 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
3028 | ||
3238 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1963 | ||
2208 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
3028 | ||
3238 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
2193 | ||
1937 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
3181 | ||
2826 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1953 | ||
2198 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
3181 | ||
2826 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
312661 | ||
312478 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
192843 | ||
192657 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
192821 | ||
192638 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
184735 | ||
184850 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
138762 | ||
138877 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
131854 | ||
132225 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
176958 | ||
177402 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
247213 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
54090 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
23345 | ||
23287 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
99632 | ||
99211 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
201005 | ||
200584 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
197282 | ||
196861 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
187335 | ||
187447 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
195872 | ||
195987 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
166598 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
165126 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
117998 | ||
118106 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
105462 | ||
105573 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
125651 | ||
125700 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
121604 | ||
121653 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
189861 | ||
190005 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
206666 | ||
206814 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
183836 | ||
183951 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
105440 | ||
105551 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
190540 | ||
190655 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,10 +27,10 @@ library SwapMath { | |
) internal pure returns (uint160 sqrtRatioNextX96, uint256 amountIn, uint256 amountOut, uint256 feeAmount) { | ||
unchecked { | ||
bool zeroForOne = sqrtRatioCurrentX96 >= sqrtRatioTargetX96; | ||
bool exactIn = amountRemaining >= 0; | ||
bool exactIn = amountRemaining < 0; | ||
|
||
if (exactIn) { | ||
uint256 amountRemainingLessFee = FullMath.mulDiv(uint256(amountRemaining), 1e6 - feePips, 1e6); | ||
uint256 amountRemainingLessFee = FullMath.mulDiv(uint256(-amountRemaining), 1e6 - feePips, 1e6); | ||
amountIn = zeroForOne | ||
? SqrtPriceMath.getAmount0Delta(sqrtRatioTargetX96, sqrtRatioCurrentX96, liquidity, true) | ||
: SqrtPriceMath.getAmount1Delta(sqrtRatioCurrentX96, sqrtRatioTargetX96, liquidity, true); | ||
|
@@ -45,11 +45,11 @@ library SwapMath { | |
amountOut = zeroForOne | ||
? SqrtPriceMath.getAmount1Delta(sqrtRatioTargetX96, sqrtRatioCurrentX96, liquidity, false) | ||
: SqrtPriceMath.getAmount0Delta(sqrtRatioCurrentX96, sqrtRatioTargetX96, liquidity, false); | ||
if (uint256(-amountRemaining) >= amountOut) { | ||
if (uint256(amountRemaining) >= amountOut) { | ||
sqrtRatioNextX96 = sqrtRatioTargetX96; | ||
} else { | ||
sqrtRatioNextX96 = SqrtPriceMath.getNextSqrtPriceFromOutput( | ||
sqrtRatioCurrentX96, liquidity, uint256(-amountRemaining), zeroForOne | ||
sqrtRatioCurrentX96, liquidity, uint256(amountRemaining), zeroForOne | ||
); | ||
} | ||
} | ||
|
@@ -74,13 +74,13 @@ library SwapMath { | |
} | ||
|
||
// cap the output amount to not exceed the remaining output amount | ||
if (!exactIn && amountOut > uint256(-amountRemaining)) { | ||
amountOut = uint256(-amountRemaining); | ||
if (!exactIn && amountOut > uint256(amountRemaining)) { | ||
amountOut = uint256(amountRemaining); | ||
} | ||
|
||
if (exactIn && sqrtRatioNextX96 != sqrtRatioTargetX96) { | ||
// we didn't reach the target, so take the remainder of the maximum input as fee | ||
feeAmount = uint256(amountRemaining) - amountIn; | ||
feeAmount = uint256(-amountRemaining) - amountIn; | ||
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. I think I'm not 100% understanding this line. If we have't reached the target the feeAmount is the amount left to swap minus the amount already swapped? Also why does this case only happen in the exactIn case? 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.
If we havent reached the target (which is the essentially maximum slippage where the trade has to stop), that means that all of the
LP fees are taken on input tokens. For exactIn, the calculation makes sure that the final amountIn including fees is amountRemaining. For non-exactIn the fee amount can just be calculated as |
||
} else { | ||
feeAmount = FullMath.mulDivRoundingUp(amountIn, feePips, 1e6 - feePips); | ||
} | ||
|
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.
Are we sure we don't want to switch liquidityDelta sign if we are changing amountSpecified sign? Now they don't match right?
IE liquidityDelta is positive, we return negative delta because user owes $ to pool and if liquidityDelta is negative (user is taking liquidity out of the pool) we return a positive delta because the user can now claim it back from the pool...
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.
Right the argument is that
liquidityDelta
is the user's delta of liquidity units not token units. When they pass inliquidityDelta = 100
its because they want their liquidity balance to increase by 100 (which means sending tokens into the contract). SoliquidityDelta
is in line withamountSpecified
now... for both of them: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.
great, makes sense! i think im in agreement w that framing then