Skip to content

Commit 2f609a7

Browse files
authored
[GSW-2380] PriceRange graph (#778)
* fix: [GSW-2380] currentPrice line * fix: [GSW-2380] Brush tick * fix: [GSW-2380] brush selection width becomes 0 in flip mode * refactor: replace hardcoded GNOT path with constant
1 parent eb25406 commit 2f609a7

File tree

2 files changed

+48
-15
lines changed

2 files changed

+48
-15
lines changed

packages/web/src/components/common/pool-selection-graph/PoolSelectionGraph.tsx

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,9 @@ const PoolSelectionGraph: React.FC<PoolSelectionGraphProps> = ({
215215
if (Number.isNaN(currentPrice)) {
216216
return 0;
217217
}
218-
return priceToTick(currentPrice);
219-
}, [currentPrice]);
218+
const tick = priceToTick(currentPrice);
219+
return flip ? -tick : tick;
220+
}, [currentPrice, flip]);
220221

221222
const tooltipPosition = useMemo((): FloatingPosition => {
222223
if (position) {
@@ -263,8 +264,16 @@ const PoolSelectionGraph: React.FC<PoolSelectionGraphProps> = ({
263264
const startPosition = selection[0] as number;
264265
const endPosition = selection[1] as number;
265266

266-
const startPrice = tickToPrice(Math.round(scaleX.invert(startPosition) + graphMinTick));
267-
const endPrice = tickToPrice(Math.round(scaleX.invert(endPosition) + graphMinTick));
267+
let startTick = Math.round(scaleX.invert(startPosition) + graphMinTick);
268+
let endTick = Math.round(scaleX.invert(endPosition) + graphMinTick);
269+
270+
if (flip) {
271+
startTick = -startTick;
272+
endTick = -endTick;
273+
}
274+
275+
const startPrice = tickToPrice(startTick);
276+
const endPrice = tickToPrice(endTick);
268277

269278
const startRate = currentPrice ? ((Number(startPrice) - currentPrice) / currentPrice) * 100 : 0;
270279
const endRate = currentPrice ? ((Number(endPrice) - currentPrice) / currentPrice) * 100 : 0;
@@ -386,7 +395,12 @@ const PoolSelectionGraph: React.FC<PoolSelectionGraphProps> = ({
386395
return 0;
387396
}
388397

389-
const tick = Math.round((tickWithOffset + graphMinTick) / tickSpacing) * tickSpacing;
398+
let tick = Math.round((tickWithOffset + graphMinTick) / tickSpacing) * tickSpacing;
399+
400+
if (flip) {
401+
tick = -tick;
402+
}
403+
390404
if (tick <= swapFeeTierMaxPriceRange.minTick) {
391405
return 0;
392406
}
@@ -398,8 +412,12 @@ const PoolSelectionGraph: React.FC<PoolSelectionGraphProps> = ({
398412
return tickToPrice(tick);
399413
}
400414

401-
const minPrice = getPriceBy(startPosition);
402-
const maxPrice = getPriceBy(endPosition);
415+
let minPrice = getPriceBy(startPosition);
416+
let maxPrice = getPriceBy(endPosition);
417+
418+
if (minPrice > maxPrice) {
419+
[minPrice, maxPrice] = [maxPrice, minPrice];
420+
}
403421

404422
setSelectionColor(selectionColor);
405423
setMinPrice(minPrice);
@@ -521,7 +539,15 @@ const PoolSelectionGraph: React.FC<PoolSelectionGraphProps> = ({
521539
const mouseXTick = scaleX.invert(event.offsetX) + graphMinTick;
522540

523541
if (minPrice && maxPrice) {
524-
if (priceToTick(minPrice) < mouseXTick && priceToTick(maxPrice) > mouseXTick) {
542+
let minTick = priceToTick(minPrice);
543+
let maxTick = priceToTick(maxPrice);
544+
545+
if (flip) {
546+
minTick = -minTick;
547+
maxTick = -maxTick;
548+
}
549+
550+
if (minTick < mouseXTick && maxTick > mouseXTick) {
525551
setTooltipInfo(null);
526552
setHoverBarIndex(null);
527553
return;
@@ -547,7 +573,6 @@ const PoolSelectionGraph: React.FC<PoolSelectionGraphProps> = ({
547573
return;
548574
}
549575

550-
// To reduce the computation of scaleY, the Y-axis condition check is done separately.
551576
if (mouseY < scaleY(bin.height)) {
552577
setPositionX(null);
553578
setPositionX(null);
@@ -672,12 +697,19 @@ const PoolSelectionGraph: React.FC<PoolSelectionGraphProps> = ({
672697
if (fullRange) {
673698
brush.move(brushElement, [0, boundsWidth]);
674699
} else {
675-
brush.move(brushElement, [
676-
scaleX(priceToTick(minPrice) - graphMinTick),
677-
scaleX(priceToTick(maxPrice) - graphMinTick),
678-
]);
700+
let minTick = flip ? -priceToTick(maxPrice) : priceToTick(minPrice);
701+
let maxTick = flip ? -priceToTick(minPrice) : priceToTick(maxPrice);
702+
703+
if (minTick > maxTick) {
704+
[minTick, maxTick] = [maxTick, minTick];
705+
}
706+
707+
const x0 = scaleX(minTick - graphMinTick);
708+
const x1 = scaleX(maxTick - graphMinTick);
709+
710+
brush.move(brushElement, [x0, x1]);
679711
}
680-
}, [minPrice, maxPrice, zoomLevel, shiftIndex, fullRange, displayBins]);
712+
}, [minPrice, maxPrice, zoomLevel, shiftIndex, fullRange, displayBins, flip]);
681713

682714
useEffect(() => {
683715
if (!brushRef.current) {

packages/web/src/hooks/pool/data/use-select-pool.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
} from "@utils/swap-utils";
2727
import { makeDisplayTokenAmount } from "@utils/token-utils";
2828
import { sortTokenPaths } from "@utils/sort-utils";
29+
import { GNOT_TOKEN } from "@common/values/token-constant";
2930

3031
type RenderState = "NONE" | "CREATE" | "LOADING" | "DONE";
3132

@@ -126,7 +127,7 @@ export const useSelectPool = ({
126127
return (
127128
tokenPair?.findIndex(path => {
128129
if (compareToken) {
129-
return isNativeToken(compareToken) || compareToken.path === "gnot"
130+
return isNativeToken(compareToken) || compareToken.path === GNOT_TOKEN.path
130131
? compareToken.wrappedPath === path
131132
: compareToken.path === path;
132133
}

0 commit comments

Comments
 (0)