Skip to content
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
14 changes: 10 additions & 4 deletions packages/web/src/hooks/pool/data/use-increase-handle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ export const useIncreaseHandle = () => {
: null,
});

const sqrtPriceX96 = useMemo(() => {
return selectPool?.poolInfo?.chainData?.sqrtPriceX96 ?? null;
}, [selectPool]);

useEffect(() => {
if (!selectedPosition?.tickLower || !selectedPosition?.tickUpper || !selectedPosition?.pool.fee || !selectPool)
return;
Expand Down Expand Up @@ -233,12 +237,13 @@ export const useIncreaseHandle = () => {
return;
}

if (!selectPool || !tokenA || !tokenB) {
if (!selectPool || !tokenA || !tokenB || !sqrtPriceX96) {
return;
}
const amountAAmountRaw = makeRawTokenAmount(tokenA, amount) || "0";
const { amountB } = getDepositAmountsByAmountA(
selectPool.currentPrice,
sqrtPriceX96,
minPrice,
maxPrice,
BigInt(amountAAmountRaw),
Expand All @@ -247,7 +252,7 @@ export const useIncreaseHandle = () => {
const tokenBAmount = makeDisplayTokenAmount(tokenB, amountB) || "0";
tokenBAmountInput.changeAmount(tokenBAmount.toString());
},
[tokenAAmountInput, selectPool.currentPrice, tokenA, tokenB, minPrice, maxPrice],
[tokenAAmountInput, sqrtPriceX96, selectPool.currentPrice, tokenA, tokenB, minPrice, maxPrice],
);

const changeTokenBAmount = useCallback(
Expand All @@ -259,13 +264,14 @@ export const useIncreaseHandle = () => {
return;
}

if (!selectPool || !tokenA || !tokenB) {
if (!selectPool || !tokenA || !tokenB || !sqrtPriceX96) {
return;
}

const amountBAmountRaw = makeRawTokenAmount(tokenB, amount) || "0";
const { amountA } = getDepositAmountsByAmountB(
selectPool.currentPrice,
sqrtPriceX96,
minPrice,
maxPrice,
BigInt(amountBAmountRaw),
Expand All @@ -274,7 +280,7 @@ export const useIncreaseHandle = () => {
const tokenAAmount = makeDisplayTokenAmount(tokenA, amountA) || "0";
tokenAAmountInput.changeAmount(tokenAAmount.toString());
},
[tokenBAmountInput, selectPool.currentPrice, tokenA, tokenB, minPrice, maxPrice],
[tokenBAmountInput, sqrtPriceX96, selectPool.currentPrice, tokenA, tokenB, minPrice, maxPrice],
);

const buttonType: INCREASE_BUTTON_TYPE = useMemo(() => {
Expand Down
11 changes: 10 additions & 1 deletion packages/web/src/hooks/pool/data/use-reposition-handle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ export const useRepositionHandle = () => {
feeTier: `FEE_${fee}` as SwapFeeTierType,
});

const sqrtPriceX96 = useMemo(() => {
return selectPool?.poolInfo?.chainData?.sqrtPriceX96 ?? null;
}, [selectPool]);

const inRange = useMemo(() => {
if (!selectedPosition) return false;
const { pool } = selectedPosition;
Expand Down Expand Up @@ -280,6 +284,7 @@ export const useRepositionHandle = () => {
!selectedPosition ||
!selectPool.minPrice ||
!selectPool.maxPrice ||
!sqrtPriceX96 ||
!selectPool.compareToken ||
!tokenA ||
!tokenB
Expand All @@ -290,6 +295,7 @@ export const useRepositionHandle = () => {

const repositionAmountsByNewPriceRange = getRepositionAmountsByPriceRange(
ordered ? selectPool.currentPrice : 1 / selectPool.currentPrice,
sqrtPriceX96,
selectPool.minPrice,
selectPool.maxPrice,
tickToPrice(ordered ? selectedPosition.tickLower : selectedPosition.tickUpper * -1),
Expand Down Expand Up @@ -360,7 +366,8 @@ export const useRepositionHandle = () => {
!initialEstimatedRepositionAmounts ||
!selectedPosition ||
selectPool.minPrice === null ||
selectPool.maxPrice === null
selectPool.maxPrice === null ||
!sqrtPriceX96
) {
return null;
}
Expand All @@ -378,6 +385,7 @@ export const useRepositionHandle = () => {

return getRepositionAmountsWithSwapSimulation(
selectPool.currentPrice,
sqrtPriceX96,
selectPool.minPrice,
selectPool.maxPrice,
selectedPosition.pool.tokenA,
Expand All @@ -394,6 +402,7 @@ export const useRepositionHandle = () => {
isEstimatedRemainSwapLoading,
initialEstimatedRepositionAmounts,
selectPool.currentPrice,
sqrtPriceX96,
selectPool.maxPrice,
selectPool.minPrice,
selectedPosition,
Expand Down
21 changes: 19 additions & 2 deletions packages/web/src/hooks/pool/data/use-select-pool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,10 @@ export const useSelectPool = ({
return poolInfo.chainData?.price;
}, [poolInfo]);

const sqrtPriceX96 = useMemo(() => {
return poolInfo?.chainData?.sqrtPriceX96 ?? null;
}, [poolInfo]);

const minPrice = useMemo(() => {
if (fullRange) {
return swapFeeTierMaxPriceRangeMap?.minPrice;
Expand All @@ -374,7 +378,7 @@ export const useSelectPool = ({
}, [fullRange, maxPosition, swapFeeTierMaxPriceRangeMap?.maxPrice]);

const depositRatio = useMemo(() => {
if (!tokenA || !tokenB || minPrice === null || maxPrice === null || !compareToken) {
if (!tokenA || !tokenB || minPrice === null || maxPrice === null || !compareToken || !sqrtPriceX96) {
return null;
}

Expand All @@ -400,6 +404,7 @@ export const useSelectPool = ({
const decimals = tokenB.decimals - tokenA.decimals;
const { amountA, amountB } = getDepositAmountsByAmountA(
BigNumber(currentPrice).shiftedBy(decimals).toNumber(),
sqrtPriceX96,
BigNumber(currentMinPrice).shiftedBy(decimals).toNumber(),
BigNumber(currentMaxPrice).shiftedBy(decimals).toNumber(),
adjustAmountA,
Expand All @@ -410,7 +415,19 @@ export const useSelectPool = ({

const sumOfAmounts = tokenAAmount + tokenBAmount;
return BigNumber(tokenAAmount.toString()).dividedBy(sumOfAmounts.toString()).multipliedBy(100).toNumber();
}, [tokenA, tokenB, minPrice, maxPrice, swapFeeTierMaxPriceRangeMap, isCreate, startPrice, price, fullRange]);
}, [
sqrtPriceX96,
tokenA,
tokenB,
compareToken,
minPrice,
maxPrice,
swapFeeTierMaxPriceRangeMap,
isCreate,
startPrice,
price,
fullRange,
]);

const feeBoost = useMemo(() => {
if (minPrice === null || maxPrice === null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ const EarnAddLiquidityContainer: React.FC = () => {

const { isLoading: isLoadingCommon } = useLoading();

const sqrtPriceX96 = useMemo(() => {
return selectPool?.poolInfo?.chainData?.sqrtPriceX96 ?? null;
}, [selectPool]);

const priceRangeSummary: PriceRangeSummary = useMemo(() => {
let depositRatio = "-";
let feeBoost: string = "-";
Expand Down Expand Up @@ -336,7 +340,7 @@ const EarnAddLiquidityContainer: React.FC = () => {
return;
}

if (!selectPool.currentPrice) {
if (!selectPool.currentPrice || !sqrtPriceX96) {
return;
}

Expand All @@ -352,6 +356,7 @@ const EarnAddLiquidityContainer: React.FC = () => {
const amountRaw = makeRawTokenAmount(tokenA, amount) || 0;
const { amountB } = getDepositAmountsByAmountA(
BigNumber(selectPool.currentPrice).shiftedBy(decimals).toNumber(),
sqrtPriceX96,
BigNumber(selectPool.minPrice).shiftedBy(decimals).toNumber(),
BigNumber(selectPool.maxPrice).shiftedBy(decimals).toNumber(),
BigInt(amountRaw),
Expand All @@ -361,6 +366,7 @@ const EarnAddLiquidityContainer: React.FC = () => {
},
[
selectPool.currentPrice,
sqrtPriceX96,
selectPool.compareToken?.symbol,
selectPool.minPrice,
selectPool.maxPrice,
Expand All @@ -375,7 +381,7 @@ const EarnAddLiquidityContainer: React.FC = () => {
return;
}

if (!selectPool.currentPrice) {
if (!selectPool.currentPrice || !sqrtPriceX96) {
return;
}

Expand All @@ -391,14 +397,23 @@ const EarnAddLiquidityContainer: React.FC = () => {
const amountRaw = makeRawTokenAmount(tokenB, amount) || 0;
const { amountA } = getDepositAmountsByAmountB(
BigNumber(selectPool.currentPrice).shiftedBy(decimals).toNumber(),
sqrtPriceX96,
BigNumber(selectPool.minPrice).shiftedBy(decimals).toNumber(),
BigNumber(selectPool.maxPrice).shiftedBy(decimals).toNumber(),
BigInt(amountRaw),
);
const expectedTokenAmount = makeDisplayTokenAmount(tokenA, amountA) || "0";
tokenAAmountInput.changeAmount(expectedTokenAmount.toString());
},
[selectPool.currentPrice, selectPool.minPrice, selectPool.maxPrice, tokenA, tokenB, tokenAAmountInput],
[
selectPool.currentPrice,
sqrtPriceX96,
selectPool.minPrice,
selectPool.maxPrice,
tokenA,
tokenB,
tokenAAmountInput,
],
);

const changeTokenAAmount = useCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ const PoolAddLiquidityContainer: React.FC = () => {
});
const { isLoading: isLoadingCommon } = useLoading();

const sqrtPriceX96 = useMemo(() => {
return selectPool?.poolInfo?.chainData?.sqrtPriceX96 ?? null;
}, [selectPool]);

const priceRangeSummary: PriceRangeSummary = useMemo(() => {
let depositRatio = "-";
let feeBoost: string = "-";
Expand Down Expand Up @@ -273,7 +277,7 @@ const PoolAddLiquidityContainer: React.FC = () => {
if (BigNumber(amount).isNaN() || !BigNumber(amount).isFinite()) {
return;
}
if (!selectPool.currentPrice) {
if (!selectPool.currentPrice || !sqrtPriceX96) {
return;
}

Expand All @@ -294,6 +298,7 @@ const PoolAddLiquidityContainer: React.FC = () => {
const amountRaw = makeRawTokenAmount(tokenA, amount) || 0;
const { amountB } = getDepositAmountsByAmountA(
BigNumber(selectPool.currentPrice).shiftedBy(decimals).toNumber(),
sqrtPriceX96,
BigNumber(selectPool.minPrice).shiftedBy(decimals).toNumber(),
BigNumber(selectPool.maxPrice).shiftedBy(decimals).toNumber(),
BigInt(amountRaw),
Expand All @@ -303,6 +308,7 @@ const PoolAddLiquidityContainer: React.FC = () => {
},
[
selectPool.currentPrice,
sqrtPriceX96,
selectPool.compareToken?.symbol,
selectPool.minPrice,
selectPool.maxPrice,
Expand All @@ -316,7 +322,7 @@ const PoolAddLiquidityContainer: React.FC = () => {
return;
}

if (!selectPool.currentPrice) {
if (!selectPool.currentPrice || !sqrtPriceX96) {
return;
}

Expand All @@ -332,6 +338,7 @@ const PoolAddLiquidityContainer: React.FC = () => {
const amountRaw = makeRawTokenAmount(tokenB, amount) || 0;
const { amountA } = getDepositAmountsByAmountB(
BigNumber(selectPool.currentPrice).shiftedBy(decimals).toNumber(),
sqrtPriceX96,
BigNumber(selectPool.minPrice).shiftedBy(decimals).toNumber(),
BigNumber(selectPool.maxPrice).shiftedBy(decimals).toNumber(),
BigInt(amountRaw),
Expand All @@ -341,6 +348,7 @@ const PoolAddLiquidityContainer: React.FC = () => {
},
[
selectPool.currentPrice,
sqrtPriceX96,
selectPool.compareToken?.symbol,
selectPool.minPrice,
selectPool.maxPrice,
Expand Down
26 changes: 22 additions & 4 deletions packages/web/src/utils/reposition-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const DEPOSIT_AMOUNT_10_POW_8 = 100_000_000n;

export function getRepositionAmountsByPriceRange(
currentPrice: number,
sqrtPriceX96: bigint,
repositionMinPrice: number,
repositionMaxPrice: number,
originMinPrice: number,
Expand All @@ -21,13 +22,25 @@ export function getRepositionAmountsByPriceRange(
} {
const originDepositAmounts =
currentPrice <= originMaxPrice
? getDepositAmountsByAmountA(currentPrice, originMinPrice, originMaxPrice, DEPOSIT_AMOUNT_10_POW_8)
: getDepositAmountsByAmountB(currentPrice, originMinPrice, originMaxPrice, DEPOSIT_AMOUNT_10_POW_8);
? getDepositAmountsByAmountA(currentPrice, sqrtPriceX96, originMinPrice, originMaxPrice, DEPOSIT_AMOUNT_10_POW_8)
: getDepositAmountsByAmountB(currentPrice, sqrtPriceX96, originMinPrice, originMaxPrice, DEPOSIT_AMOUNT_10_POW_8);

const newDepositAmounts =
currentPrice <= repositionMaxPrice
? getDepositAmountsByAmountA(currentPrice, repositionMinPrice, repositionMaxPrice, DEPOSIT_AMOUNT_10_POW_8)
: getDepositAmountsByAmountB(currentPrice, repositionMinPrice, repositionMaxPrice, DEPOSIT_AMOUNT_10_POW_8);
? getDepositAmountsByAmountA(
currentPrice,
sqrtPriceX96,
repositionMinPrice,
repositionMaxPrice,
DEPOSIT_AMOUNT_10_POW_8,
)
: getDepositAmountsByAmountB(
currentPrice,
sqrtPriceX96,
repositionMinPrice,
repositionMaxPrice,
DEPOSIT_AMOUNT_10_POW_8,
);

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

export function getRepositionAmountsWithSwapSimulation(
currentPrice: number,
sqrtPriceX96: bigint,
repositionMinPrice: number,
repositionMaxPrice: number,
tokenA: TokenModel,
Expand Down Expand Up @@ -109,6 +123,7 @@ export function getRepositionAmountsWithSwapSimulation(
if (isInsufficientQuantity) {
const depositAmounts = getDepositAmountsByAmountB(
currentPrice,
sqrtPriceX96,
repositionMinPrice || 1,
repositionMaxPrice || 1,
toShiftBitInt(estimatedAmountB || 0, tokenB.decimals),
Expand All @@ -121,6 +136,7 @@ export function getRepositionAmountsWithSwapSimulation(

const depositAmounts = getDepositAmountsByAmountA(
currentPrice,
sqrtPriceX96,
repositionMinPrice || 1,
repositionMaxPrice || 1,
toShiftBitInt(estimatedAmountA, tokenA.decimals),
Expand All @@ -146,6 +162,7 @@ export function getRepositionAmountsWithSwapSimulation(
if (isInsufficientQuantity) {
const depositAmounts = getDepositAmountsByAmountA(
currentPrice,
sqrtPriceX96,
repositionMinPrice || 1,
repositionMaxPrice || 1,
toShiftBitInt(estimatedAmountA || 0, tokenA.decimals),
Expand All @@ -158,6 +175,7 @@ export function getRepositionAmountsWithSwapSimulation(

const depositAmounts = getDepositAmountsByAmountB(
currentPrice,
sqrtPriceX96,
repositionMinPrice || 1,
repositionMaxPrice || 1,
toShiftBitInt(estimatedAmountB || 0, tokenB.decimals),
Expand Down
Loading
Loading