Skip to content

Commit

Permalink
fix: Initialize Pool Graph range
Browse files Browse the repository at this point in the history
  • Loading branch information
jinoosss committed Dec 5, 2023
1 parent a232205 commit 56e7620
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,13 @@ const SelectPriceRangeCustom: React.FC<SelectPriceRangeCustomProps> = ({

function getPriceRange() {
const currentPrice = selectPool.currentPrice || 1;
if (selectPool.liquidityOfTickPoints.length === 0) {
const priceGap = currentPrice * 0.5;
return [currentPrice - priceGap, currentPrice + priceGap];
if (!selectPool.feeTier || !priceRangeType) {
return [0, currentPrice * 2];
}
const [minX, maxX] = d3.extent(selectPool.liquidityOfTickPoints.map(point => point[0]));

const minPriceGap = (typeof selectPool.currentPrice === "number" && typeof minX === "number") ? Math.abs(selectPool.currentPrice - minX) : 0;
const maxPriceGap = (typeof selectPool.currentPrice === "number" && typeof maxX === "number") ? Math.abs(selectPool.currentPrice - maxX) : 0;
const priceGap = Math.min(maxPriceGap, minPriceGap);
return [currentPrice - priceGap, currentPrice + priceGap];
const visibleRate = SwapFeeTierPriceRange[selectPool.feeTier][priceRangeType].max / 100;
const range = currentPrice * visibleRate;
return [currentPrice - range, currentPrice + range];
}

function getHeightRange() {
Expand Down
5 changes: 4 additions & 1 deletion packages/web/src/hooks/pool/use-select-pool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export const useSelectPool = ({
}

const logMin = minPrice <= 0 ?
Math.log(currentPrice / Number(MIN_PRICE_X96)) :
Math.log(currentPrice / Number(0.0000000001)) :
Math.log(currentPrice / minPrice);
const logMax = Math.log(maxPrice / currentPrice);
return logMin * 100 / (logMin + logMax);
Expand Down Expand Up @@ -201,6 +201,9 @@ export const useSelectPool = ({
if (!poolInfo || !minPosition) {
return;
}
if (minPosition === 0) {
return;
}
setInteractionType("INTERACTION");
const tickSpacing = poolInfo.tickSpacing;
const nearTick = priceToNearTick(minPosition, tickSpacing);
Expand Down
14 changes: 12 additions & 2 deletions packages/web/src/utils/swap-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import {
SwapFeeTierInfoMap,
SwapFeeTierType,
} from "@constants/option.constant";
import { MAX_TICK, MIN_TICK, Q96 } from "@constants/swap.constant";
import {
MAX_TICK,
MIN_PRICE_X96,
MIN_TICK,
Q96,
} from "@constants/swap.constant";
import BigNumber from "bignumber.js";
import { tickToSqrtPriceX96 } from "./math.utils";

Expand All @@ -18,7 +23,12 @@ export function makeSwapFeeTier(value: string | number): SwapFeeTierType {
}

export function priceToTick(price: number | bigint) {
if (price === 0) {
const priceX96 = BigInt(
BigNumber(Math.sqrt(Number(price)).toString())
.multipliedBy(Q96.toString())
.toFixed(0),
);
if (priceX96 <= MIN_PRICE_X96) {
return MIN_TICK;
}
const logPrice = Math.log(Number(price));
Expand Down

0 comments on commit 56e7620

Please sign in to comment.