-
Notifications
You must be signed in to change notification settings - Fork 607
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
fix(CL): tick iterator bugs and tests #5526
Changes from all commits
9abafa9
7913e5c
c9567b4
e22f040
834dd2a
4b3e80d
0633c3a
aea44c6
ef9549e
371753d
a5cedf7
525a1e1
8f3ed3e
54533ca
372968b
92e1ad9
2cb548b
c29225e
4aa41f8
7009f3e
b59fdc0
48a2ef9
57a9166
b638e7d
fb20f0c
22080a4
83cde3a
859f199
50fb3de
7e7f26b
a433b1c
04fc8f0
dc40325
b49576e
75483f6
bb881e1
398380e
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 |
---|---|---|
|
@@ -65,7 +65,7 @@ func newSwapState(specifiedAmount sdk.Int, p types.ConcentratedPoolExtension, st | |
amountSpecifiedRemaining: specifiedAmount.ToDec(), | ||
amountCalculated: sdk.ZeroDec(), | ||
sqrtPrice: p.GetCurrentSqrtPrice(), | ||
tick: strategy.InitializeTickValue(p.GetCurrentTick()), | ||
tick: p.GetCurrentTick(), | ||
liquidity: p.GetLiquidity(), | ||
globalSpreadRewardGrowthPerUnitLiquidity: sdk.ZeroDec(), | ||
globalSpreadRewardGrowth: sdk.ZeroDec(), | ||
|
@@ -362,13 +362,30 @@ func (k Keeper) computeOutAmtGivenIn( | |
if err != nil { | ||
return sdk.Coin{}, sdk.Coin{}, 0, sdk.Dec{}, sdk.Dec{}, sdk.Dec{}, err | ||
} | ||
// Update the swapState's tick with the tick we retrieved liquidity from | ||
swapState.tick = swapStrategy.UpdateTickAfterCrossing(nextTick) | ||
} else if !sqrtPriceStart.Equal(computedSqrtPrice) { | ||
// Otherwise if the sqrtPrice calculated from ComputeSwapWithinBucketOutGivenIn(...) does not equal the sqrtPriceStart we started with at the | ||
// beginning of this iteration, we set the swapState tick to the corresponding tick of the computedSqrtPrice calculated from ComputeSwapWithinBucketOutGivenIn(...) | ||
swapState.tick, err = math.SqrtPriceToTickRoundDownSpacing(computedSqrtPrice, p.GetTickSpacing()) | ||
newTick, err := math.CalculateSqrtPriceToTick(computedSqrtPrice) | ||
if err != nil { | ||
return sdk.Coin{}, sdk.Coin{}, 0, sdk.Dec{}, sdk.Dec{}, sdk.Dec{}, err | ||
} | ||
|
||
// TEMPORARY HACK: this is to fix tick rounding error where | ||
// the tick is off by 1 due to banker's rounding error in CalculatePriceToTick | ||
// TODO: if this is to remain in the codebase, consider abstracting this into a | ||
// method of swap strategy. | ||
isZeroForOne := getZeroForOne(tokenInMin.Denom, p.GetToken0()) | ||
if isZeroForOne { | ||
if newTick <= swapState.tick { | ||
swapState.tick = newTick | ||
} | ||
} else { | ||
if newTick > swapState.tick { | ||
swapState.tick = newTick | ||
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. Are there any cases where none of these branches would be hit? 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.
Prior to merging #5541 all branches were hit. Now, only "11111" is not being hit. I'm going to make an issue to investigate this separately. Merging for now |
||
} | ||
} | ||
} | ||
} | ||
|
||
|
@@ -552,8 +569,6 @@ func (k Keeper) swapCrossTickLogic(ctx sdk.Context, | |
newLiquidity := swapState.liquidity.AddMut(liquidityNet) | ||
swapState.liquidity = newLiquidity | ||
|
||
// Update the swapState's tick with the tick we retrieved liquidity from | ||
swapState.tick = nextInitializedTick | ||
return swapState, nil | ||
} | ||
|
||
|
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.
Note to reviewer: this is the key realization and fix to the original bug as opposed to tick initialization as originally thought
Once the general change is accepted, I will improve the abstraction by moving it inside the
swapCrossTickLogic
method. Currently, I cannot put there asswapStrategy
is not given as an argument toswapCrossTickLogic
I put it here to avoid large code diff and be able to point at the core fix.
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.
This makes sense per discussions!