Skip to content

Commit 475bbc7

Browse files
committed
fix(earn): stabilize add liquidity price preset initialization on pool detail
1 parent d31efb8 commit 475bbc7

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

apps/kyberswap-interface/src/pages/Earns/PoolDetail/AddLiquidity/components/PriceSection/RangePresetSelector.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import styled from 'styled-components'
66
import { HStack } from 'components/Stack'
77
import useTheme from 'hooks/useTheme'
88

9-
import { FULL_PRICE_RANGE, getRangePresetOptions } from './utils'
9+
import { FULL_PRICE_RANGE, getDefaultRangePreset, getRangePresetOptions } from './utils'
1010

1111
const RangeButton = styled.button<{ $active: boolean }>`
1212
flex: 1 1 0;
@@ -118,6 +118,19 @@ const RangePresetSelector = ({
118118
previousRangeSelected.current = rangeSelected
119119
}, [handleSelectPriceRange, rangeSelected, revertPrice])
120120

121+
useEffect(() => {
122+
if (lastSelected || tickLower === null || tickUpper === null) return
123+
124+
const matches = priceRanges.filter(item => item.tickLower === tickLower && item.tickUpper === tickUpper)
125+
if (matches.length <= 1) return
126+
127+
const defaultRangePreset = getDefaultRangePreset(pool.category)
128+
const matchedDefaultRange = matches.find(item => item.range === defaultRangePreset) || matches[0]
129+
if (!matchedDefaultRange) return
130+
131+
setLastSelected(matchedDefaultRange.range)
132+
}, [lastSelected, pool.category, priceRanges, tickLower, tickUpper])
133+
121134
if (!priceRanges.length) return null
122135

123136
return (

apps/kyberswap-interface/src/pages/Earns/PoolDetail/AddLiquidity/hooks/useZapPool.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export const useZapPool = ({ chainId, pool: rawPool, poolType }: UseZapPoolProps
122122

123123
const token0 = buildPoolToken(rawPool.tokens[0], tokenMetadataMap)
124124
const token1 = buildPoolToken(rawPool.tokens[1], tokenMetadataMap)
125-
const category = mapPoolCategory(pairCategoryData?.data?.category)
125+
const category = pairCategoryLoading ? undefined : mapPoolCategory(pairCategoryData?.data?.category)
126126
const stats = buildPoolStats(rawPool.poolStats)
127127
const isFarming = Boolean(rawPool.programs?.includes('eg') || rawPool.programs?.includes('lm'))
128128
const isFarmingLm = Boolean(rawPool.programs?.includes('lm'))
@@ -168,7 +168,7 @@ export const useZapPool = ({ chainId, pool: rawPool, poolType }: UseZapPoolProps
168168
})
169169

170170
return parsedPool.success ? parsedPool.data : null
171-
}, [pairCategoryData?.data?.category, poolType, rawPool, tokenMetadataMap])
171+
}, [pairCategoryData?.data?.category, pairCategoryLoading, poolType, rawPool, tokenMetadataMap])
172172

173173
const loading = (Boolean(tokenAddresses.length) && tokenMetadataLoading) || pairCategoryLoading
174174
const error = !loading && !normalizedPool ? 'Failed to prepare pool data' : ''

apps/kyberswap-interface/src/pages/Earns/PoolDetail/AddLiquidity/hooks/useZapState.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ export const useZapState = ({ chainId, pool, poolAddress, poolType, account, sou
167167

168168
useEffect(() => {
169169
if (!isUniV3 || !pool) return
170+
if (revertPrice !== defaultRevertPrice) return
170171
if (tickPriceState.tickLower !== null && tickPriceState.tickUpper !== null) return
171172
if (!defaultRangeOptions.length) return
172173

@@ -180,8 +181,10 @@ export const useZapState = ({ chainId, pool, poolAddress, poolType, account, sou
180181
setTickUpper(defaultRange.tickUpper)
181182
}, [
182183
defaultRangeOptions,
184+
defaultRevertPrice,
183185
isUniV3,
184186
pool,
187+
revertPrice,
185188
setTickLower,
186189
setTickUpper,
187190
tickPriceState.tickLower,

apps/kyberswap-interface/src/pages/Earns/PoolDetail/AddLiquidity/index.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -476,21 +476,21 @@ const AddLiquidity = ({ children }: AddLiquidityProps) => {
476476

477477
return (
478478
<AddLiquidityRuntimeProvider value={runtimeValue}>
479-
{!normalizedPool.data ? (
479+
{!normalizedPool.data || normalizedPool.loading ? (
480480
<HStack align="flex-start" gap={24} wrap="wrap" width="100%">
481-
<Stack flex="1 1 480px" maxWidth="480px" minWidth={0} gap={16}>
482-
<AddLiquidityWidgetSkeleton />
483-
</Stack>
484-
485-
<Stack flex="1 1 480px" gap={24} minWidth={0}>
481+
<PoolInformationColumn>
486482
<AddLiquidityRoutePreview
487483
inputTokens={state.tokenInput.tokens}
488484
inputAmounts={state.tokenInput.amounts}
489485
pool={null}
490486
zapRoute={state.route.data}
491487
/>
492488
{children}
493-
</Stack>
489+
</PoolInformationColumn>
490+
491+
<AddLiquidityColumn>
492+
<AddLiquidityWidgetSkeleton />
493+
</AddLiquidityColumn>
494494
</HStack>
495495
) : (
496496
<AddLiquidityBody

0 commit comments

Comments
 (0)