From eb2c75cb3a424d3c90496f8e6f82b4bcfeff2216 Mon Sep 17 00:00:00 2001 From: Hoang Trinh Date: Mon, 17 Apr 2023 11:14:33 +0700 Subject: [PATCH] fix: correct initialized ticks (#8) --- entities/ticklist.go | 10 ++++++++-- utils/sqrtprice_math.go | 6 ++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/entities/ticklist.go b/entities/ticklist.go index 82953e1..7d65047 100644 --- a/entities/ticklist.go +++ b/entities/ticklist.go @@ -4,6 +4,8 @@ import ( "errors" "math" "math/big" + + "github.com/daoleno/uniswapv3-sdk/constants" ) const ( @@ -192,8 +194,12 @@ func NextInitializedTickIndex(ticks []Tick, tick int, lte bool) (int, bool, erro return ZeroValueTickIndex, ZeroValueTickInitialized, err } - // The found tick is surely initialized - return nextInitializedTick.Index, true, nil + var isInitialized bool + if nextInitializedTick.LiquidityGross.Cmp(constants.Zero) != 0 { + isInitialized = true + } + + return nextInitializedTick.Index, isInitialized, nil } // utils diff --git a/utils/sqrtprice_math.go b/utils/sqrtprice_math.go index 35f0459..9c3739c 100644 --- a/utils/sqrtprice_math.go +++ b/utils/sqrtprice_math.go @@ -26,7 +26,8 @@ func addIn256(x, y *big.Int) *big.Int { } func GetAmount0Delta(sqrtRatioAX96, sqrtRatioBX96, liquidity *big.Int, roundUp bool) *big.Int { - if sqrtRatioAX96.Cmp(sqrtRatioBX96) >= 0 { + // https://github.com/Uniswap/v3-core/blob/d8b1c635c275d2a9450bd6a78f3fa2484fef73eb/contracts/libraries/SqrtPriceMath.sol#L159 + if sqrtRatioAX96.Cmp(sqrtRatioBX96) > 0 { sqrtRatioAX96, sqrtRatioBX96 = sqrtRatioBX96, sqrtRatioAX96 } @@ -40,7 +41,8 @@ func GetAmount0Delta(sqrtRatioAX96, sqrtRatioBX96, liquidity *big.Int, roundUp b } func GetAmount1Delta(sqrtRatioAX96, sqrtRatioBX96, liquidity *big.Int, roundUp bool) *big.Int { - if sqrtRatioAX96.Cmp(sqrtRatioBX96) >= 0 { + // https://github.com/Uniswap/v3-core/blob/d8b1c635c275d2a9450bd6a78f3fa2484fef73eb/contracts/libraries/SqrtPriceMath.sol#L188 + if sqrtRatioAX96.Cmp(sqrtRatioBX96) > 0 { sqrtRatioAX96, sqrtRatioBX96 = sqrtRatioBX96, sqrtRatioAX96 }