Skip to content

Commit

Permalink
fix(CL Swaps): incorrect rounding; redundant computations (#3962)
Browse files Browse the repository at this point in the history
* fix(CL Swaps): incorrect rounding; redundant computatations

* strategy

* fix strategy tests
  • Loading branch information
p0mvn authored Jan 12, 2023
1 parent fb45d52 commit 9cc4599
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion x/concentrated-liquidity/internal/math/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func GetNextSqrtPriceFromAmount0RoundingUp(sqrtPriceCurrent, liquidity, amountRe
// getNextSqrtPriceFromAmount1RoundingDown utilizes the current squareRootPrice, liquidity of denom1, and amount of denom1 that still needs
// to be swapped in order to determine the next squareRootPrice
func GetNextSqrtPriceFromAmount1RoundingDown(sqrtPriceCurrent, liquidity, amountRemaining sdk.Dec) (sqrtPriceNext sdk.Dec) {
return sqrtPriceCurrent.Add(amountRemaining.Quo(liquidity))
return sqrtPriceCurrent.Add(amountRemaining.QuoTruncate(liquidity))
}

// getLiquidityFromAmounts takes the current sqrtPrice and the sqrtPrice for the upper and lower ticks as well as the amounts of asset0 and asset1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func (s oneForZeroStrategy) ComputeSwapStep(sqrtPriceCurrent, nextSqrtPrice, liq
amountIn := math.CalcAmount1Delta(liquidity, nextSqrtPrice, sqrtPriceCurrent, false)
if amountRemaining.LT(amountIn) {
nextSqrtPrice = math.GetNextSqrtPriceFromAmount1RoundingDown(sqrtPriceCurrent, liquidity, amountRemaining)
amountIn = math.CalcAmount1Delta(liquidity, nextSqrtPrice, sqrtPriceCurrent, false)
}
amountIn = math.CalcAmount1Delta(liquidity, nextSqrtPrice, sqrtPriceCurrent, false)
amountOut := math.CalcAmount0Delta(liquidity, nextSqrtPrice, sqrtPriceCurrent, false)

return nextSqrtPrice, amountIn, amountOut
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ func (suite *StrategyTestSuite) TestComputeSwapState() {
// TODO: test case where it does affect.
sqrtPriceLimit: sdk.MustNewDecFromStr("70.738956735309575810").Add(sdk.OneDec()), // 5003
zeroForOne: false,
expectedSqrtPriceNext: "70.738348247484497718",
expectedAmountIn: "42000000.000000000749226725",
expectedAmountOut: "8396.714242162445246671",
expectedSqrtPriceNext: "70.738348247484497717",
expectedAmountIn: "41999999.999999999231344381",
expectedAmountOut: "8396.714242162444943332",
},
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func (s zeroForOneStrategy) ComputeSwapStep(sqrtPriceCurrent, nextSqrtPrice, liq
amountIn := math.CalcAmount0Delta(liquidity, nextSqrtPrice, sqrtPriceCurrent, false)
if amountRemaining.LT(amountIn) {
nextSqrtPrice = math.GetNextSqrtPriceFromAmount0RoundingUp(sqrtPriceCurrent, liquidity, amountRemaining)
amountIn = math.CalcAmount0Delta(liquidity, nextSqrtPrice, sqrtPriceCurrent, false)
}
amountIn = math.CalcAmount0Delta(liquidity, nextSqrtPrice, sqrtPriceCurrent, false)
amountOut := math.CalcAmount1Delta(liquidity, nextSqrtPrice, sqrtPriceCurrent, false)

return nextSqrtPrice, amountIn, amountOut
Expand Down

0 comments on commit 9cc4599

Please sign in to comment.