Skip to content

Commit 1f8c2a2

Browse files
authored
fix: position status in zap widget (#2915)
1 parent 787e2ca commit 1f8c2a2

File tree

3 files changed

+45
-31
lines changed

3 files changed

+45
-31
lines changed

packages/liquidity-widgets/src/components/Header/index.tsx

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1+
import { useMemo } from 'react';
2+
13
import { Trans, t } from '@lingui/macro';
24

35
import { useCopy } from '@kyber/hooks';
4-
import {
5-
DEXES_INFO,
6-
NATIVE_TOKEN_ADDRESS,
7-
NETWORKS_INFO,
8-
defaultToken,
9-
univ3PoolNormalize,
10-
univ3Position,
11-
} from '@kyber/schema';
6+
import { DEXES_INFO, NATIVE_TOKEN_ADDRESS, NETWORKS_INFO, defaultToken, univ3Types } from '@kyber/schema';
127
import { InfoHelper, LoadingCounter, MouseoverTooltip, Skeleton, TokenLogo, TokenSymbol } from '@kyber/ui';
138
import { shortenAddress } from '@kyber/utils/crypto';
149

@@ -27,10 +22,18 @@ const Header = () => {
2722
'poolType',
2823
'positionId',
2924
]);
30-
const { pool } = usePoolStore(['pool']);
25+
const { pool, poolPrice } = usePoolStore(['pool', 'poolPrice']);
3126
const { position } = usePositionStore(['position']);
3227

33-
const { toggleSetting, uiState, loading: zapLoading, getZapRoute, zapRouteDisabled } = useZapState();
28+
const {
29+
toggleSetting,
30+
uiState,
31+
loading: zapLoading,
32+
getZapRoute,
33+
zapRouteDisabled,
34+
minPrice,
35+
maxPrice,
36+
} = useZapState();
3437

3538
const initializing = !pool;
3639
const poolAddress = initializing ? '' : pool.address;
@@ -52,14 +55,13 @@ const Header = () => {
5255

5356
const { icon: dexLogo, name: rawName } = DEXES_INFO[poolType];
5457
const dexName = typeof rawName === 'string' ? rawName : rawName[chainId];
58+
const isUniV3 = univ3Types.includes(poolType as any);
5559

56-
const { success, data } = univ3Position.safeParse(position);
57-
const { success: isUniV3, data: univ3Pool } = univ3PoolNormalize.safeParse(pool);
60+
const isOutOfRange = useMemo(() => {
61+
if (!positionId || !isUniV3 || !poolPrice || minPrice === null || maxPrice === null) return false;
62+
return poolPrice < +minPrice || poolPrice > +maxPrice;
63+
}, [isUniV3, maxPrice, minPrice, poolPrice, positionId]);
5864

59-
const isOutOfRange =
60-
!!positionId && position && success && isUniV3
61-
? univ3Pool.tick < data.tickLower || univ3Pool.tick >= data.tickUpper
62-
: false;
6365
const isClosed = position !== null && position.liquidity.toString() === '0';
6466

6567
const handleToggleSetting = (e: React.MouseEvent<HTMLDivElement>) => {

packages/zap-migration-widgets/src/components/PoolInfo.tsx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { useMemo } from 'react';
2+
13
import { Trans, t } from '@lingui/macro';
24

35
import { useCopy } from '@kyber/hooks';
@@ -11,6 +13,7 @@ import {
1113
} from '@kyber/schema';
1214
import { InfoHelper, Skeleton, TokenLogo, TokenSymbol } from '@kyber/ui';
1315
import { shortenAddress } from '@kyber/utils/crypto';
16+
import { sqrtToPrice, tickToPrice } from '@kyber/utils/uniswapv3';
1417

1518
import { usePoolStore } from '@/stores/usePoolStore';
1619
import { usePositionStore } from '@/stores/usePositionStore';
@@ -53,6 +56,17 @@ export function PoolInfo({ type }: { type: PoolInfoType }) {
5356
copyClassName: '!text-blue',
5457
});
5558

59+
const { success: isUniV3, data: uniV3Pool } = univ3PoolNormalize.safeParse(pool);
60+
61+
const isOutOfRange = useMemo(() => {
62+
if (!position || !isUniV3 || !uniV3Pool) return false;
63+
const poolPrice = +sqrtToPrice(BigInt(uniV3Pool.sqrtPriceX96), token0.decimals, token1.decimals);
64+
const lowerPrice = +tickToPrice((position as UniV3Position).tickLower, token0.decimals, token1.decimals);
65+
const upperPrice = +tickToPrice((position as UniV3Position).tickUpper, token0.decimals, token1.decimals);
66+
return poolPrice < lowerPrice || poolPrice > upperPrice;
67+
}, [isUniV3, position, token0.decimals, token1.decimals, uniV3Pool]);
68+
const isClosed = position && position.liquidity.toString() === '0';
69+
5670
if (!pool)
5771
return (
5872
<div className="ui-h-[62px] flex flex-col gap-2 rounded-md bg-[#ffffff0a] px-4 py-3 w-full">
@@ -61,14 +75,6 @@ export function PoolInfo({ type }: { type: PoolInfoType }) {
6175
</div>
6276
);
6377

64-
const { success: isUniV3, data: uniV3Pool } = univ3PoolNormalize.safeParse(pool);
65-
66-
const isOutOfRange =
67-
position && isUniV3
68-
? uniV3Pool.tick < (position as UniV3Position).tickLower || uniV3Pool.tick > (position as UniV3Position).tickUpper
69-
: false;
70-
const isClosed = position && position.liquidity.toString() === '0';
71-
7278
const dexName =
7379
typeof DEXES_INFO[pool.poolType].name === 'string'
7480
? (DEXES_INFO[pool.poolType].name as string)

packages/zap-out-widgets/src/components/Header.tsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import { useMemo } from 'react';
2+
13
import { Trans, t } from '@lingui/macro';
24

35
import { useCopy } from '@kyber/hooks';
4-
import { DEXES_INFO, NATIVE_TOKEN_ADDRESS, NETWORKS_INFO, UniV3Pool, UniV3Position, univ3Types } from '@kyber/schema';
6+
import { DEXES_INFO, NATIVE_TOKEN_ADDRESS, NETWORKS_INFO, UniV3Position, univ3Types } from '@kyber/schema';
57
import { InfoHelper, MouseoverTooltip, Skeleton, TokenLogo, TokenSymbol } from '@kyber/ui';
68
import { cn } from '@kyber/utils/tailwind-helpers';
9+
import { tickToPrice } from '@kyber/utils/uniswapv3';
710

811
import SettingIcon from '@/assets/svg/setting.svg';
912
import X from '@/assets/svg/x.svg';
@@ -13,7 +16,9 @@ import { useZapOutContext } from '@/stores';
1316
import { useZapOutUserState } from '@/stores/state';
1417

1518
export const Header = () => {
16-
const { poolAddress, onClose, poolType, pool, position, positionId, theme, chainId } = useZapOutContext(s => s);
19+
const { poolAddress, onClose, poolType, pool, position, positionId, theme, chainId, poolPrice } = useZapOutContext(
20+
s => s,
21+
);
1722
const isUniV3 = univ3Types.includes(poolType as any);
1823

1924
const { degenMode, toggleSetting, mode } = useZapOutUserState();
@@ -30,11 +35,12 @@ export const Header = () => {
3035
text: loading ? '' : pool.token1.address,
3136
});
3237

33-
const isOutOfRange =
34-
isUniV3 && !loading
35-
? (position as UniV3Position).tickLower > (pool as UniV3Pool).tick ||
36-
(pool as UniV3Pool).tick >= (position as UniV3Position).tickUpper
37-
: false;
38+
const isOutOfRange = useMemo(() => {
39+
if (loading || !isUniV3 || !poolPrice) return false;
40+
const lowerPrice = +tickToPrice((position as UniV3Position).tickLower, pool.token0.decimals, pool.token1.decimals);
41+
const upperPrice = +tickToPrice((position as UniV3Position).tickUpper, pool.token0.decimals, pool.token1.decimals);
42+
return poolPrice < lowerPrice || poolPrice > upperPrice;
43+
}, [isUniV3, loading, pool?.token0.decimals, pool?.token1.decimals, poolPrice, position]);
3844

3945
const { icon: dexLogo, name: rawName } = DEXES_INFO[poolType];
4046
const dexName = typeof rawName === 'string' ? rawName : rawName[chainId];

0 commit comments

Comments
 (0)