Skip to content

Commit

Permalink
fix: correct initialized ticks (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
piavgh authored Apr 17, 2023
1 parent 606557f commit eb2c75c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
10 changes: 8 additions & 2 deletions entities/ticklist.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"errors"
"math"
"math/big"

"github.com/daoleno/uniswapv3-sdk/constants"
)

const (
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions utils/sqrtprice_math.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down

0 comments on commit eb2c75c

Please sign in to comment.