Skip to content

Commit

Permalink
add back TicksToSqrtPrices
Browse files Browse the repository at this point in the history
  • Loading branch information
czarcas7ic committed Jan 11, 2023
1 parent d82ff60 commit 50410c4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 26 deletions.
12 changes: 10 additions & 2 deletions x/concentrated-liquidity/internal/math/tick.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var sdkTenDec = sdk.NewDec(10)
// TicksToPrice returns the price for the lower and upper ticks.
// Returns error if fails to calculate price.
// TODO: spec and tests
func TicksToPrice(lowerTick, upperTick int64, exponentAtPriceOne sdk.Int) (sdk.Dec, sdk.Dec, error) {
func TicksToSqrtPrice(lowerTick, upperTick int64, exponentAtPriceOne sdk.Int) (sdk.Dec, sdk.Dec, error) {
priceUpperTick, err := TickToPrice(sdk.NewInt(upperTick), exponentAtPriceOne)
if err != nil {
return sdk.Dec{}, sdk.Dec{}, err
Expand All @@ -24,7 +24,15 @@ func TicksToPrice(lowerTick, upperTick int64, exponentAtPriceOne sdk.Int) (sdk.D
if err != nil {
return sdk.Dec{}, sdk.Dec{}, err
}
return priceLowerTick, priceUpperTick, nil
sqrtPriceUpperTick, err := priceLowerTick.ApproxSqrt()
if err != nil {
return sdk.Dec{}, sdk.Dec{}, err
}
sqrtPriceLowerTick, err := priceUpperTick.ApproxSqrt()
if err != nil {
return sdk.Dec{}, sdk.Dec{}, err
}
return sqrtPriceLowerTick, sqrtPriceUpperTick, nil
}

// TickToPrice returns the price given the following two arguments:
Expand Down
28 changes: 4 additions & 24 deletions x/concentrated-liquidity/lp.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,8 @@ func (k Keeper) createPosition(ctx sdk.Context, poolId uint64, owner sdk.AccAddr
return sdk.Int{}, sdk.Int{}, sdk.Dec{}, err
}

// Transform the provided ticks into their corresponding prices.
priceLowerTick, priceUpperTick, err := math.TicksToPrice(lowerTick, upperTick, pool.GetPrecisionFactorAtPriceOne())
if err != nil {
return sdk.Int{}, sdk.Int{}, sdk.Dec{}, err
}

// Transform the provided prices into their corresponding square root prices.
sqrtPriceLowerTick, err := priceLowerTick.ApproxSqrt()
if err != nil {
return sdk.Int{}, sdk.Int{}, sdk.Dec{}, err
}
sqrtPriceUpperTick, err := priceUpperTick.ApproxSqrt()
// Transform the provided ticks into their corresponding sqrt prices.
sqrtPriceLowerTick, sqrtPriceUpperTick, err := math.TicksToSqrtPrice(lowerTick, upperTick, pool.GetPrecisionFactorAtPriceOne())
if err != nil {
return sdk.Int{}, sdk.Int{}, sdk.Dec{}, err
}
Expand Down Expand Up @@ -180,18 +170,8 @@ func (k Keeper) updatePosition(ctx sdk.Context, poolId uint64, owner sdk.AccAddr
return sdk.Int{}, sdk.Int{}, err
}

// Transform the provided ticks into their corresponding prices.
priceLowerTick, priceUpperTick, err := math.TicksToPrice(lowerTick, upperTick, pool.GetPrecisionFactorAtPriceOne())
if err != nil {
return sdk.Int{}, sdk.Int{}, err
}

// Transform the provided prices into their corresponding square root prices.
sqrtPriceLowerTick, err := priceLowerTick.ApproxSqrt()
if err != nil {
return sdk.Int{}, sdk.Int{}, err
}
sqrtPriceUpperTick, err := priceUpperTick.ApproxSqrt()
// Transform the provided ticks into their corresponding sqrt prices.
sqrtPriceLowerTick, sqrtPriceUpperTick, err := math.TicksToSqrtPrice(lowerTick, upperTick, pool.GetPrecisionFactorAtPriceOne())
if err != nil {
return sdk.Int{}, sdk.Int{}, err
}
Expand Down

0 comments on commit 50410c4

Please sign in to comment.