Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removes an unneeded sqrt call from CL logic #3978

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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