From bb7765a18c257dfd4ca5a8de612a516b1b58dce4 Mon Sep 17 00:00:00 2001 From: viet-nv Date: Wed, 16 Aug 2023 16:17:29 +0700 Subject: [PATCH] fix: addliquidity multiple pos for farm --- src/pages/AddLiquidityV2/index.tsx | 51 ++++++++++++------- .../ElasticFarmv2/components/FarmCard.tsx | 2 +- .../components/NewRangesNotiModal.tsx | 2 +- .../components/StakeWithNFTsModal.tsx | 4 +- 4 files changed, 38 insertions(+), 21 deletions(-) diff --git a/src/pages/AddLiquidityV2/index.tsx b/src/pages/AddLiquidityV2/index.tsx index f6c63843f8..a2baac7ae7 100644 --- a/src/pages/AddLiquidityV2/index.tsx +++ b/src/pages/AddLiquidityV2/index.tsx @@ -224,21 +224,24 @@ export default function AddLiquidity() { const poolAddress = useProAmmPoolInfo(baseCurrency, currencyB, feeAmount) const { farms } = useElasticFarmsV2() - const farmV2 = farms?.find( + + const farmV2S = farms?.filter( item => !item.isSettled && item.endTime > Date.now() / 1000 && item.poolAddress.toLowerCase() === poolAddress.toLowerCase(), ) - const activeRanges = farmV2?.ranges.filter(r => !r.isRemoved) || [] + const activeRanges = + farmV2S?.map(farm => farm.ranges.filter(item => !item.isRemoved).map(item => ({ ...item, farm }))).flat() || [] - const isFarmV2Available = !!farmV2 + const isFarmV2Available = !!farmV2S?.length const [showFarmRangeSelect, setShowFarmRangeSelect] = useState(() => isFarmV2Available) const [searchParams, setSearchParams] = useSearchParams() const activeRangeIndex = Number(searchParams.get('farmRange') || '0') - const range = activeRanges.find(i => i.index === activeRangeIndex) + const defaultFId = Number(searchParams.get('fId') || '0') + const range = activeRanges.find(i => i.index === activeRangeIndex && i.farm.fId === defaultFId) const canJoinFarm = isFarmV2Available && @@ -928,7 +931,7 @@ export default function AddLiquidity() { const [shownTooltip, setShownTooltip] = useState(null) const pairFactor = usePairFactor([tokenA, tokenB]) - const isReverseWithFarm = baseCurrency?.wrapped.address !== farmV2?.token0.wrapped.address + const isReverseWithFarm = baseCurrency?.wrapped.address !== farmV2S?.[0]?.token0.wrapped.address const chart = ( @@ -1001,34 +1004,35 @@ export default function AddLiquidity() { )} - {showFarmRangeSelect && !!farmV2 && ( + {showFarmRangeSelect && !!activeRanges.length && farmV2S?.[0] && ( - {farmV2.ranges.map(range => { + {activeRanges.map(range => { if (range.isRemoved) return null return ( { searchParams.set('farmRange', range.index.toString()) + searchParams.set('fId', range.farm.fId.toString()) setSearchParams(searchParams) onFarmRangeSelected(+range.tickLower, +range.tickUpper) }} - isSelected={activeRangeIndex === range.index} + isSelected={activeRangeIndex === range.index && defaultFId === range.farm.fId} > {convertTickToPrice( - isReverseWithFarm ? farmV2.token1 : farmV2.token0, - isReverseWithFarm ? farmV2.token0 : farmV2.token1, + isReverseWithFarm ? farmV2S[0].token1 : farmV2S[0].token0, + isReverseWithFarm ? farmV2S[0].token0 : farmV2S[0].token1, isReverseWithFarm ? range.tickUpper : range.tickLower, - farmV2.pool.fee, + farmV2S[0].pool.fee, )} {convertTickToPrice( - isReverseWithFarm ? farmV2.token1 : farmV2.token0, - isReverseWithFarm ? farmV2.token0 : farmV2.token1, + isReverseWithFarm ? farmV2S[0].token1 : farmV2S[0].token0, + isReverseWithFarm ? farmV2S[0].token0 : farmV2S[0].token1, isReverseWithFarm ? range.tickLower : range.tickUpper, - farmV2.pool.fee, + farmV2S[0].pool.fee, )} @@ -1314,11 +1318,22 @@ export default function AddLiquidity() { ) useEffect(() => { - if (isFarmV2Available && range?.tickUpper && range?.tickUpper) { + if (isFarmV2Available) { setShowFarmRangeSelect(true) - onFarmRangeSelected(range.tickLower, range.tickUpper) } else setShowFarmRangeSelect(false) - }, [isFarmV2Available, range?.tickUpper, range?.tickLower, onFarmRangeSelected]) + }, [isFarmV2Available]) + + useEffect(() => { + if ( + isFarmV2Available && + range?.tickUpper && + range?.tickUpper && + !positionsState[pIndex].leftRangeTypedValue && + !positionsState[pIndex].rightRangeTypedValue + ) { + onFarmRangeSelected(range.tickLower, range.tickUpper) + } + }, [isFarmV2Available, range?.tickUpper, range?.tickLower, onFarmRangeSelected, positionsState, pIndex]) if (!isEVM) return diff --git a/src/pages/Farm/ElasticFarmv2/components/FarmCard.tsx b/src/pages/Farm/ElasticFarmv2/components/FarmCard.tsx index 3ad2d138bc..a1defa6ae8 100644 --- a/src/pages/Farm/ElasticFarmv2/components/FarmCard.tsx +++ b/src/pages/Farm/ElasticFarmv2/components/FarmCard.tsx @@ -369,7 +369,7 @@ function FarmCard({ {range?.isRemoved ? ( Idle Range ) : ( - + Add Liquidity ↗ )} diff --git a/src/pages/Farm/ElasticFarmv2/components/NewRangesNotiModal.tsx b/src/pages/Farm/ElasticFarmv2/components/NewRangesNotiModal.tsx index d73e137ebb..d1e932ec84 100644 --- a/src/pages/Farm/ElasticFarmv2/components/NewRangesNotiModal.tsx +++ b/src/pages/Farm/ElasticFarmv2/components/NewRangesNotiModal.tsx @@ -160,7 +160,7 @@ export default function NewRangesNotiModal({ updatedFarms }: { updatedFarms: Ela {range.isRemoved ? ( Idle Range ) : ( - + Add Liquidity ↗ )} diff --git a/src/pages/Farm/ElasticFarmv2/components/StakeWithNFTsModal.tsx b/src/pages/Farm/ElasticFarmv2/components/StakeWithNFTsModal.tsx index 645fa4742c..c6a0e0be58 100644 --- a/src/pages/Farm/ElasticFarmv2/components/StakeWithNFTsModal.tsx +++ b/src/pages/Farm/ElasticFarmv2/components/StakeWithNFTsModal.tsx @@ -301,7 +301,9 @@ const StakeWithNFTsModal = ({ const addliquidityElasticPool = `/${networkInfo.route}${APP_PATHS.ELASTIC_CREATE_POOL}/${ farm.token0.isNative ? farm.token0.symbol : farm.token0.address - }/${farm.token1.isNative ? farm.token1.symbol : farm.token1.address}/${farm.pool.fee}?farmRange=${activeRange.index}` + }/${farm.token1.isNative ? farm.token1.symbol : farm.token1.address}/${farm.pool.fee}?farmRange=${ + activeRange.index + }&fId=${farm.fId}` const upToMedium = useMedia(`(max-width: ${MEDIA_WIDTHS.upToMedium}px)`)