From 81766284492ff951b8c3fc23176faa9d334a8ead Mon Sep 17 00:00:00 2001 From: Nguyen Van Viet Date: Tue, 26 Sep 2023 17:03:57 +0700 Subject: [PATCH] fix: farm is stale when switching network (#2269) * fix: farm is stale when switching network * switch to prod * Update pr.yaml --- src/state/farms/elastic/updaters/v1.tsx | 19 +++++----- src/state/farms/elasticv2/updater.tsx | 47 ++++++++++--------------- 2 files changed, 27 insertions(+), 39 deletions(-) diff --git a/src/state/farms/elastic/updaters/v1.tsx b/src/state/farms/elastic/updaters/v1.tsx index 6d1958b937..02135e84f1 100644 --- a/src/state/farms/elastic/updaters/v1.tsx +++ b/src/state/farms/elastic/updaters/v1.tsx @@ -151,26 +151,24 @@ const FarmUpdaterV1: React.FC = ({ interval }) => { }) useEffect(() => { - if (!elasticFarm.farms && !elasticFarm.loading) { - dispatch(setLoading({ chainId, loading: true })) + const getFarm = (withLoading = false) => { + withLoading && dispatch(setLoading({ chainId, loading: true })) try { getElasticFarms() } finally { - dispatch(setLoading({ chainId, loading: false })) + withLoading && dispatch(setLoading({ chainId, loading: false })) } } - }, [elasticFarm, getElasticFarms, dispatch, chainId]) - - useEffect(() => { + getFarm(true) const i = interval ? setInterval(() => { - getElasticFarms() + getFarm() }, 20_000) : undefined return () => { i && clearInterval(i) } - }, [interval, getElasticFarms]) + }, [interval, chainId, getElasticFarms, dispatch]) useEffect(() => { if (error && chainId) { @@ -179,8 +177,9 @@ const FarmUpdaterV1: React.FC = ({ interval }) => { } }, [error, dispatch, chainId]) + const hasFarm = elasticFarm?.farms?.length useEffect(() => { - if (data?.farms && chainId && !elasticFarm?.farms?.length) { + if (data?.farms && chainId && !hasFarm) { // transform farm data const formattedData: ElasticFarm[] = data.farms.map((farm: SubgraphFarm) => { return { @@ -268,7 +267,7 @@ const FarmUpdaterV1: React.FC = ({ interval }) => { }) dispatch(setFarms({ chainId, farms: formattedData })) } - }, [data, dispatch, chainId, elasticFarm]) + }, [data, dispatch, chainId, hasFarm]) return null } diff --git a/src/state/farms/elasticv2/updater.tsx b/src/state/farms/elasticv2/updater.tsx index f8b893e5ca..60b073ebe7 100644 --- a/src/state/farms/elasticv2/updater.tsx +++ b/src/state/farms/elasticv2/updater.tsx @@ -7,7 +7,7 @@ import { FeeAmount, Pool, Position } from '@kyberswap/ks-sdk-elastic' import { BigNumber } from 'ethers' import { Interface } from 'ethers/lib/utils' import { useEffect, useMemo, useRef } from 'react' -import { useLazyGetFarmV2Query } from 'services/knprotocol' +import knProtocolApi, { useLazyGetFarmV2Query } from 'services/knprotocol' import FarmV2QuoterABI from 'constants/abis/farmv2Quoter.json' import NFTPositionManagerABI from 'constants/abis/v2/ProAmmNFTPositionManager.json' @@ -17,11 +17,11 @@ import { NativeCurrencies } from 'constants/tokens' import { useActiveWeb3React } from 'hooks' import { useContract, useMulticallContract } from 'hooks/useContract' import { useKyberSwapConfig } from 'state/application/hooks' -import { useAppDispatch, useAppSelector } from 'state/hooks' +import { useAppDispatch } from 'state/hooks' import { useTokenPricesWithLoading } from 'state/tokenPrices/hooks' import { isAddressString } from 'utils' -import { defaultChainData, setFarms, setLoading, setUserFarmInfo } from '.' +import { setFarms, setLoading, setUserFarmInfo } from '.' import { ElasticFarmV2, SubgraphFarmV2, SubgraphToken, UserFarmV2Info } from './types' const positionManagerInterface = new Interface(NFTPositionManagerABI.abi) @@ -106,7 +106,6 @@ const queryFarms = gql` export default function ElasticFarmV2Updater({ interval = true }: { interval?: boolean }) { const dispatch = useAppDispatch() const { networkInfo, isEVM, chainId, account } = useActiveWeb3React() - const elasticFarm = useAppSelector(state => state.elasticFarmV2[chainId] || defaultChainData) const { elasticClient, isEnableKNProtocol } = useKyberSwapConfig() const multicallContract = useMulticallContract() @@ -122,12 +121,10 @@ export default function ElasticFarmV2Updater({ interval = true }: { interval?: b const [getElasticFarmV2FromKnProtocol, { data: knProtocolData, error: knProtocolError }] = useLazyGetFarmV2Query() - const latestKnProtocolData = useRef(knProtocolData) - const data = useMemo(() => { if (isEnableKNProtocol) { return { - farmV2S: knProtocolData?.data?.data || latestKnProtocolData.current?.data?.data || [], + farmV2S: knProtocolData?.data?.data || [], } } else return subgraphData }, [isEnableKNProtocol, knProtocolData, subgraphData]) @@ -136,38 +133,30 @@ export default function ElasticFarmV2Updater({ interval = true }: { interval?: b if (isEnableKNProtocol) return knProtocolError return subgraphError }, [isEnableKNProtocol, subgraphError, knProtocolError]) - const isLoadingElasticFarm = useRef(elasticFarm.loading) - isLoadingElasticFarm.current = elasticFarm.loading useEffect(() => { - if (isEVM && !elasticFarm?.farms && !isLoadingElasticFarm.current) { - dispatch(setLoading({ chainId, loading: true })) - if (isEnableKNProtocol) { + const getFarm = (chainId: number, withLoading = false) => { + if (withLoading) dispatch(setLoading({ chainId, loading: true })) + if (isEnableKNProtocol) getElasticFarmV2FromKnProtocol(chainId).finally(() => { dispatch(setLoading({ chainId, loading: false })) }) - } else + else { getElasticFarmV2().finally(() => { dispatch(setLoading({ chainId, loading: false })) }) + } } - }, [ - isEVM, - chainId, - dispatch, - getElasticFarmV2, - elasticFarm?.farms, - getElasticFarmV2FromKnProtocol, - isEnableKNProtocol, - ]) + Promise.resolve(dispatch(knProtocolApi.util.resetApiState())).then(() => { + dispatch(setFarms({ chainId, farms: [] })) + getFarm(chainId, true) + }) + // for chain which is not enable kn protocol + setTimeout(() => { + dispatch(setLoading({ chainId, loading: false })) + }, 3000) - useEffect(() => { - const i = interval - ? setInterval(() => { - if (isEnableKNProtocol) getElasticFarmV2FromKnProtocol(chainId) - else getElasticFarmV2() - }, 10_000) - : undefined + const i = interval ? setInterval(() => getFarm(chainId, false), 10_000) : undefined return () => { i && clearInterval(i) }