diff --git a/packages/math/src/pool/concentrated/tick.ts b/packages/math/src/pool/concentrated/tick.ts index 333268ba10..41940f4897 100644 --- a/packages/math/src/pool/concentrated/tick.ts +++ b/packages/math/src/pool/concentrated/tick.ts @@ -20,48 +20,7 @@ const nine = new Dec(9); If tickIndex is zero, the function returns new Dec(1). */ export function tickToSqrtPrice(tickIndex: Int): Dec { - if (tickIndex.isZero()) { - return new Dec(1); - } - - const geometricExponentIncrementDistanceInTicks = nine.mul( - powTenBigDec(new Int(exponentAtPriceOne).neg()).toDec() - ); - - if (tickIndex.lt(minTick) || tickIndex.gt(maxTick)) { - throw new Error( - `tickIndex is out of range: ${tickIndex.toString()}, min: ${minTick.toString()}, max: ${maxTick.toString()}` - ); - } - - const geometricExponentDelta = new Dec(tickIndex) - .quoTruncate(new Dec(geometricExponentIncrementDistanceInTicks.truncate())) - .truncate(); - - let exponentAtCurTick = new Int(exponentAtPriceOne).add( - geometricExponentDelta - ); - if (tickIndex.lt(new Int(0))) { - exponentAtCurTick = exponentAtCurTick.sub(new Int(1)); - } - - const currentAdditiveIncrementInTicks = powTenBigDec(exponentAtCurTick); - - const numAdditiveTicks = tickIndex.sub( - geometricExponentDelta.mul( - geometricExponentIncrementDistanceInTicks.truncate() - ) - ); - - const price = powTenBigDec(geometricExponentDelta) - .add(new BigDec(numAdditiveTicks).mul(currentAdditiveIncrementInTicks)) - .toDec(); - - if (price.gt(maxSpotPrice) || price.lt(minSpotPrice)) { - throw new Error( - `price is out of range: ${price.toString()}, min: ${minSpotPrice.toString()}, max: ${maxSpotPrice.toString()}` - ); - } + const price = tickToPrice(tickIndex); return approxSqrt(price); } diff --git a/packages/server/src/queries/complex/concentrated-liquidity/index.ts b/packages/server/src/queries/complex/concentrated-liquidity/index.ts index c305248a13..67fe721b71 100644 --- a/packages/server/src/queries/complex/concentrated-liquidity/index.ts +++ b/packages/server/src/queries/complex/concentrated-liquidity/index.ts @@ -1,4 +1,4 @@ -import { BigDec, maxTick, minTick, tickToSqrtPrice } from "@osmosis-labs/math"; +import { BigDec, maxTick, minTick, tickToPrice } from "@osmosis-labs/math"; import { AssetList, Chain } from "@osmosis-labs/types"; import { CoinPretty, @@ -154,6 +154,22 @@ export function getPriceFromSqrtPrice({ return price; } +export function getDisplayPriceFromPrice({ + price, + baseCoin, + quoteCoin, +}: { + baseCoin: CoinPretty; + quoteCoin: CoinPretty; + price: Dec; +}) { + const multiplicationQuoteOverBase = DecUtils.getTenExponentN( + baseCoin.currency.coinDecimals - quoteCoin.currency.coinDecimals + ); + const displayPrice = price.mul(multiplicationQuoteOverBase); + return displayPrice; +} + export function getTickPrice({ tick, baseCoin, @@ -163,11 +179,11 @@ export function getTickPrice({ baseCoin: CoinPretty; quoteCoin: CoinPretty; }) { - const sqrtPrice = tickToSqrtPrice(tick); - return getPriceFromSqrtPrice({ + const price = tickToPrice(tick); + return getDisplayPriceFromPrice({ baseCoin, quoteCoin, - sqrtPrice, + price, }); }