Skip to content

Commit

Permalink
Removes an unneeded sqrt call from CL logic (#3978)
Browse files Browse the repository at this point in the history
No idea if this is a bottleneck in TRPC/locally.

just noticed it as I was reading through code
  • Loading branch information
ValarDragon authored Nov 28, 2024
1 parent ed6aafb commit 076f5db
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 46 deletions.
43 changes: 1 addition & 42 deletions packages/math/src/pool/concentrated/tick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
});
}

Expand Down

0 comments on commit 076f5db

Please sign in to comment.