Skip to content

Commit fa2a955

Browse files
committed
fix: [GSW-2397] replace price with sqrtPriceX96 for accurate calculation
1 parent a88f763 commit fa2a955

File tree

7 files changed

+107
-24
lines changed

7 files changed

+107
-24
lines changed

packages/web/src/hooks/pool/data/use-increase-handle.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ export const useIncreaseHandle = () => {
145145
: null,
146146
});
147147

148+
const sqrtPriceX96 = useMemo(() => {
149+
return selectPool?.poolInfo?.chainData?.sqrtPriceX96 ?? null;
150+
}, [selectPool]);
151+
148152
useEffect(() => {
149153
if (!selectedPosition?.tickLower || !selectedPosition?.tickUpper || !selectedPosition?.pool.fee || !selectPool)
150154
return;
@@ -233,12 +237,13 @@ export const useIncreaseHandle = () => {
233237
return;
234238
}
235239

236-
if (!selectPool || !tokenA || !tokenB) {
240+
if (!selectPool || !tokenA || !tokenB || !sqrtPriceX96) {
237241
return;
238242
}
239243
const amountAAmountRaw = makeRawTokenAmount(tokenA, amount) || "0";
240244
const { amountB } = getDepositAmountsByAmountA(
241245
selectPool.currentPrice,
246+
sqrtPriceX96,
242247
minPrice,
243248
maxPrice,
244249
BigInt(amountAAmountRaw),
@@ -247,7 +252,7 @@ export const useIncreaseHandle = () => {
247252
const tokenBAmount = makeDisplayTokenAmount(tokenB, amountB) || "0";
248253
tokenBAmountInput.changeAmount(tokenBAmount.toString());
249254
},
250-
[tokenAAmountInput, selectPool.currentPrice, tokenA, tokenB, minPrice, maxPrice],
255+
[tokenAAmountInput, sqrtPriceX96, selectPool.currentPrice, tokenA, tokenB, minPrice, maxPrice],
251256
);
252257

253258
const changeTokenBAmount = useCallback(
@@ -259,13 +264,14 @@ export const useIncreaseHandle = () => {
259264
return;
260265
}
261266

262-
if (!selectPool || !tokenA || !tokenB) {
267+
if (!selectPool || !tokenA || !tokenB || !sqrtPriceX96) {
263268
return;
264269
}
265270

266271
const amountBAmountRaw = makeRawTokenAmount(tokenB, amount) || "0";
267272
const { amountA } = getDepositAmountsByAmountB(
268273
selectPool.currentPrice,
274+
sqrtPriceX96,
269275
minPrice,
270276
maxPrice,
271277
BigInt(amountBAmountRaw),
@@ -274,7 +280,7 @@ export const useIncreaseHandle = () => {
274280
const tokenAAmount = makeDisplayTokenAmount(tokenA, amountA) || "0";
275281
tokenAAmountInput.changeAmount(tokenAAmount.toString());
276282
},
277-
[tokenBAmountInput, selectPool.currentPrice, tokenA, tokenB, minPrice, maxPrice],
283+
[tokenBAmountInput, sqrtPriceX96, selectPool.currentPrice, tokenA, tokenB, minPrice, maxPrice],
278284
);
279285

280286
const buttonType: INCREASE_BUTTON_TYPE = useMemo(() => {

packages/web/src/hooks/pool/data/use-reposition-handle.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ export const useRepositionHandle = () => {
161161
feeTier: `FEE_${fee}` as SwapFeeTierType,
162162
});
163163

164+
const sqrtPriceX96 = useMemo(() => {
165+
return selectPool?.poolInfo?.chainData?.sqrtPriceX96 ?? null;
166+
}, [selectPool]);
167+
164168
const inRange = useMemo(() => {
165169
if (!selectedPosition) return false;
166170
const { pool } = selectedPosition;
@@ -280,6 +284,7 @@ export const useRepositionHandle = () => {
280284
!selectedPosition ||
281285
!selectPool.minPrice ||
282286
!selectPool.maxPrice ||
287+
!sqrtPriceX96 ||
283288
!selectPool.compareToken ||
284289
!tokenA ||
285290
!tokenB
@@ -290,6 +295,7 @@ export const useRepositionHandle = () => {
290295

291296
const repositionAmountsByNewPriceRange = getRepositionAmountsByPriceRange(
292297
ordered ? selectPool.currentPrice : 1 / selectPool.currentPrice,
298+
sqrtPriceX96,
293299
selectPool.minPrice,
294300
selectPool.maxPrice,
295301
tickToPrice(ordered ? selectedPosition.tickLower : selectedPosition.tickUpper * -1),
@@ -360,7 +366,8 @@ export const useRepositionHandle = () => {
360366
!initialEstimatedRepositionAmounts ||
361367
!selectedPosition ||
362368
selectPool.minPrice === null ||
363-
selectPool.maxPrice === null
369+
selectPool.maxPrice === null ||
370+
!sqrtPriceX96
364371
) {
365372
return null;
366373
}
@@ -378,6 +385,7 @@ export const useRepositionHandle = () => {
378385

379386
return getRepositionAmountsWithSwapSimulation(
380387
selectPool.currentPrice,
388+
sqrtPriceX96,
381389
selectPool.minPrice,
382390
selectPool.maxPrice,
383391
selectedPosition.pool.tokenA,
@@ -394,6 +402,7 @@ export const useRepositionHandle = () => {
394402
isEstimatedRemainSwapLoading,
395403
initialEstimatedRepositionAmounts,
396404
selectPool.currentPrice,
405+
sqrtPriceX96,
397406
selectPool.maxPrice,
398407
selectPool.minPrice,
399408
selectedPosition,

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,10 @@ export const useSelectPool = ({
359359
return poolInfo.chainData?.price;
360360
}, [poolInfo]);
361361

362+
const sqrtPriceX96 = useMemo(() => {
363+
return poolInfo?.chainData?.sqrtPriceX96 ?? null;
364+
}, [poolInfo]);
365+
362366
const minPrice = useMemo(() => {
363367
if (fullRange) {
364368
return swapFeeTierMaxPriceRangeMap?.minPrice;
@@ -374,7 +378,7 @@ export const useSelectPool = ({
374378
}, [fullRange, maxPosition, swapFeeTierMaxPriceRangeMap?.maxPrice]);
375379

376380
const depositRatio = useMemo(() => {
377-
if (!tokenA || !tokenB || minPrice === null || maxPrice === null || !compareToken) {
381+
if (!tokenA || !tokenB || minPrice === null || maxPrice === null || !compareToken || !sqrtPriceX96) {
378382
return null;
379383
}
380384

@@ -400,6 +404,7 @@ export const useSelectPool = ({
400404
const decimals = tokenB.decimals - tokenA.decimals;
401405
const { amountA, amountB } = getDepositAmountsByAmountA(
402406
BigNumber(currentPrice).shiftedBy(decimals).toNumber(),
407+
sqrtPriceX96,
403408
BigNumber(currentMinPrice).shiftedBy(decimals).toNumber(),
404409
BigNumber(currentMaxPrice).shiftedBy(decimals).toNumber(),
405410
adjustAmountA,
@@ -410,7 +415,19 @@ export const useSelectPool = ({
410415

411416
const sumOfAmounts = tokenAAmount + tokenBAmount;
412417
return BigNumber(tokenAAmount.toString()).dividedBy(sumOfAmounts.toString()).multipliedBy(100).toNumber();
413-
}, [tokenA, tokenB, minPrice, maxPrice, swapFeeTierMaxPriceRangeMap, isCreate, startPrice, price, fullRange]);
418+
}, [
419+
sqrtPriceX96,
420+
tokenA,
421+
tokenB,
422+
compareToken,
423+
minPrice,
424+
maxPrice,
425+
swapFeeTierMaxPriceRangeMap,
426+
isCreate,
427+
startPrice,
428+
price,
429+
fullRange,
430+
]);
414431

415432
const feeBoost = useMemo(() => {
416433
if (minPrice === null || maxPrice === null) {

packages/web/src/layouts/pool/pool-add/containers/earn-add-liquidity-container/EarnAddLiquidityContainer.tsx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ const EarnAddLiquidityContainer: React.FC = () => {
110110

111111
const { isLoading: isLoadingCommon } = useLoading();
112112

113+
const sqrtPriceX96 = useMemo(() => {
114+
return selectPool?.poolInfo?.chainData?.sqrtPriceX96 ?? null;
115+
}, [selectPool]);
116+
113117
const priceRangeSummary: PriceRangeSummary = useMemo(() => {
114118
let depositRatio = "-";
115119
let feeBoost: string = "-";
@@ -336,7 +340,7 @@ const EarnAddLiquidityContainer: React.FC = () => {
336340
return;
337341
}
338342

339-
if (!selectPool.currentPrice) {
343+
if (!selectPool.currentPrice || !sqrtPriceX96) {
340344
return;
341345
}
342346

@@ -352,6 +356,7 @@ const EarnAddLiquidityContainer: React.FC = () => {
352356
const amountRaw = makeRawTokenAmount(tokenA, amount) || 0;
353357
const { amountB } = getDepositAmountsByAmountA(
354358
BigNumber(selectPool.currentPrice).shiftedBy(decimals).toNumber(),
359+
sqrtPriceX96,
355360
BigNumber(selectPool.minPrice).shiftedBy(decimals).toNumber(),
356361
BigNumber(selectPool.maxPrice).shiftedBy(decimals).toNumber(),
357362
BigInt(amountRaw),
@@ -361,6 +366,7 @@ const EarnAddLiquidityContainer: React.FC = () => {
361366
},
362367
[
363368
selectPool.currentPrice,
369+
sqrtPriceX96,
364370
selectPool.compareToken?.symbol,
365371
selectPool.minPrice,
366372
selectPool.maxPrice,
@@ -375,7 +381,7 @@ const EarnAddLiquidityContainer: React.FC = () => {
375381
return;
376382
}
377383

378-
if (!selectPool.currentPrice) {
384+
if (!selectPool.currentPrice || !sqrtPriceX96) {
379385
return;
380386
}
381387

@@ -391,14 +397,23 @@ const EarnAddLiquidityContainer: React.FC = () => {
391397
const amountRaw = makeRawTokenAmount(tokenB, amount) || 0;
392398
const { amountA } = getDepositAmountsByAmountB(
393399
BigNumber(selectPool.currentPrice).shiftedBy(decimals).toNumber(),
400+
sqrtPriceX96,
394401
BigNumber(selectPool.minPrice).shiftedBy(decimals).toNumber(),
395402
BigNumber(selectPool.maxPrice).shiftedBy(decimals).toNumber(),
396403
BigInt(amountRaw),
397404
);
398405
const expectedTokenAmount = makeDisplayTokenAmount(tokenA, amountA) || "0";
399406
tokenAAmountInput.changeAmount(expectedTokenAmount.toString());
400407
},
401-
[selectPool.currentPrice, selectPool.minPrice, selectPool.maxPrice, tokenA, tokenB, tokenAAmountInput],
408+
[
409+
selectPool.currentPrice,
410+
sqrtPriceX96,
411+
selectPool.minPrice,
412+
selectPool.maxPrice,
413+
tokenA,
414+
tokenB,
415+
tokenAAmountInput,
416+
],
402417
);
403418

404419
const changeTokenAAmount = useCallback(

packages/web/src/layouts/pool/pool-add/containers/pool-add-liquidity-container/PoolAddLiquidityContainer.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ const PoolAddLiquidityContainer: React.FC = () => {
105105
});
106106
const { isLoading: isLoadingCommon } = useLoading();
107107

108+
const sqrtPriceX96 = useMemo(() => {
109+
return selectPool?.poolInfo?.chainData?.sqrtPriceX96 ?? null;
110+
}, [selectPool]);
111+
108112
const priceRangeSummary: PriceRangeSummary = useMemo(() => {
109113
let depositRatio = "-";
110114
let feeBoost: string = "-";
@@ -273,7 +277,7 @@ const PoolAddLiquidityContainer: React.FC = () => {
273277
if (BigNumber(amount).isNaN() || !BigNumber(amount).isFinite()) {
274278
return;
275279
}
276-
if (!selectPool.currentPrice) {
280+
if (!selectPool.currentPrice || !sqrtPriceX96) {
277281
return;
278282
}
279283

@@ -294,6 +298,7 @@ const PoolAddLiquidityContainer: React.FC = () => {
294298
const amountRaw = makeRawTokenAmount(tokenA, amount) || 0;
295299
const { amountB } = getDepositAmountsByAmountA(
296300
BigNumber(selectPool.currentPrice).shiftedBy(decimals).toNumber(),
301+
sqrtPriceX96,
297302
BigNumber(selectPool.minPrice).shiftedBy(decimals).toNumber(),
298303
BigNumber(selectPool.maxPrice).shiftedBy(decimals).toNumber(),
299304
BigInt(amountRaw),
@@ -303,6 +308,7 @@ const PoolAddLiquidityContainer: React.FC = () => {
303308
},
304309
[
305310
selectPool.currentPrice,
311+
sqrtPriceX96,
306312
selectPool.compareToken?.symbol,
307313
selectPool.minPrice,
308314
selectPool.maxPrice,
@@ -316,7 +322,7 @@ const PoolAddLiquidityContainer: React.FC = () => {
316322
return;
317323
}
318324

319-
if (!selectPool.currentPrice) {
325+
if (!selectPool.currentPrice || !sqrtPriceX96) {
320326
return;
321327
}
322328

@@ -332,6 +338,7 @@ const PoolAddLiquidityContainer: React.FC = () => {
332338
const amountRaw = makeRawTokenAmount(tokenB, amount) || 0;
333339
const { amountA } = getDepositAmountsByAmountB(
334340
BigNumber(selectPool.currentPrice).shiftedBy(decimals).toNumber(),
341+
sqrtPriceX96,
335342
BigNumber(selectPool.minPrice).shiftedBy(decimals).toNumber(),
336343
BigNumber(selectPool.maxPrice).shiftedBy(decimals).toNumber(),
337344
BigInt(amountRaw),
@@ -341,6 +348,7 @@ const PoolAddLiquidityContainer: React.FC = () => {
341348
},
342349
[
343350
selectPool.currentPrice,
351+
sqrtPriceX96,
344352
selectPool.compareToken?.symbol,
345353
selectPool.minPrice,
346354
selectPool.maxPrice,

packages/web/src/utils/reposition-utils.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const DEPOSIT_AMOUNT_10_POW_8 = 100_000_000n;
99

1010
export function getRepositionAmountsByPriceRange(
1111
currentPrice: number,
12+
sqrtPriceX96: bigint,
1213
repositionMinPrice: number,
1314
repositionMaxPrice: number,
1415
originMinPrice: number,
@@ -21,13 +22,25 @@ export function getRepositionAmountsByPriceRange(
2122
} {
2223
const originDepositAmounts =
2324
currentPrice <= originMaxPrice
24-
? getDepositAmountsByAmountA(currentPrice, originMinPrice, originMaxPrice, DEPOSIT_AMOUNT_10_POW_8)
25-
: getDepositAmountsByAmountB(currentPrice, originMinPrice, originMaxPrice, DEPOSIT_AMOUNT_10_POW_8);
25+
? getDepositAmountsByAmountA(currentPrice, sqrtPriceX96, originMinPrice, originMaxPrice, DEPOSIT_AMOUNT_10_POW_8)
26+
: getDepositAmountsByAmountB(currentPrice, sqrtPriceX96, originMinPrice, originMaxPrice, DEPOSIT_AMOUNT_10_POW_8);
2627

2728
const newDepositAmounts =
2829
currentPrice <= repositionMaxPrice
29-
? getDepositAmountsByAmountA(currentPrice, repositionMinPrice, repositionMaxPrice, DEPOSIT_AMOUNT_10_POW_8)
30-
: getDepositAmountsByAmountB(currentPrice, repositionMinPrice, repositionMaxPrice, DEPOSIT_AMOUNT_10_POW_8);
30+
? getDepositAmountsByAmountA(
31+
currentPrice,
32+
sqrtPriceX96,
33+
repositionMinPrice,
34+
repositionMaxPrice,
35+
DEPOSIT_AMOUNT_10_POW_8,
36+
)
37+
: getDepositAmountsByAmountB(
38+
currentPrice,
39+
sqrtPriceX96,
40+
repositionMinPrice,
41+
repositionMaxPrice,
42+
DEPOSIT_AMOUNT_10_POW_8,
43+
);
3144

3245
const originDepositRatioBN = BigNumber(originDepositAmounts.amountA.toString()).dividedBy(
3346
Number(originDepositAmounts.amountA.toString()) + Number(originDepositAmounts.amountB.toString()),
@@ -70,6 +83,7 @@ export function getRepositionAmountsByPriceRange(
7083

7184
export function getRepositionAmountsWithSwapSimulation(
7285
currentPrice: number,
86+
sqrtPriceX96: bigint,
7387
repositionMinPrice: number,
7488
repositionMaxPrice: number,
7589
tokenA: TokenModel,
@@ -109,6 +123,7 @@ export function getRepositionAmountsWithSwapSimulation(
109123
if (isInsufficientQuantity) {
110124
const depositAmounts = getDepositAmountsByAmountB(
111125
currentPrice,
126+
sqrtPriceX96,
112127
repositionMinPrice || 1,
113128
repositionMaxPrice || 1,
114129
toShiftBitInt(estimatedAmountB || 0, tokenB.decimals),
@@ -121,6 +136,7 @@ export function getRepositionAmountsWithSwapSimulation(
121136

122137
const depositAmounts = getDepositAmountsByAmountA(
123138
currentPrice,
139+
sqrtPriceX96,
124140
repositionMinPrice || 1,
125141
repositionMaxPrice || 1,
126142
toShiftBitInt(estimatedAmountA, tokenA.decimals),
@@ -146,6 +162,7 @@ export function getRepositionAmountsWithSwapSimulation(
146162
if (isInsufficientQuantity) {
147163
const depositAmounts = getDepositAmountsByAmountA(
148164
currentPrice,
165+
sqrtPriceX96,
149166
repositionMinPrice || 1,
150167
repositionMaxPrice || 1,
151168
toShiftBitInt(estimatedAmountA || 0, tokenA.decimals),
@@ -158,6 +175,7 @@ export function getRepositionAmountsWithSwapSimulation(
158175

159176
const depositAmounts = getDepositAmountsByAmountB(
160177
currentPrice,
178+
sqrtPriceX96,
161179
repositionMinPrice || 1,
162180
repositionMaxPrice || 1,
163181
toShiftBitInt(estimatedAmountB || 0, tokenB.decimals),

0 commit comments

Comments
 (0)