From a66181c4554bc3ca29b244990b4d27b6bff2658f Mon Sep 17 00:00:00 2001 From: viet-nv Date: Tue, 26 Sep 2023 11:40:25 +0700 Subject: [PATCH] fix: farm is stale when switching network --- src/state/farms/elastic/updaters/v1.tsx | 14 ++++---- src/state/farms/elasticv2/updater.tsx | 46 ++++++++----------------- 2 files changed, 20 insertions(+), 40 deletions(-) diff --git a/src/state/farms/elastic/updaters/v1.tsx b/src/state/farms/elastic/updaters/v1.tsx index 6d1958b937..368cc5b883 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) { diff --git a/src/state/farms/elasticv2/updater.tsx b/src/state/farms/elasticv2/updater.tsx index f8b893e5ca..1e780af31f 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,23 @@ 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 = (withLoading = false) => { + if (withLoading) dispatch(setLoading({ chainId, loading: true })) + if (isEnableKNProtocol) getElasticFarmV2FromKnProtocol(chainId).finally(() => { - dispatch(setLoading({ chainId, loading: false })) + if (withLoading) dispatch(setLoading({ chainId, loading: false })) }) - } else + else getElasticFarmV2().finally(() => { - dispatch(setLoading({ chainId, loading: false })) + if (withLoading) dispatch(setLoading({ chainId, loading: false })) }) } - }, [ - isEVM, - chainId, - dispatch, - getElasticFarmV2, - elasticFarm?.farms, - getElasticFarmV2FromKnProtocol, - isEnableKNProtocol, - ]) - - useEffect(() => { - const i = interval - ? setInterval(() => { - if (isEnableKNProtocol) getElasticFarmV2FromKnProtocol(chainId) - else getElasticFarmV2() - }, 10_000) - : undefined + dispatch(knProtocolApi.util.resetApiState()) + dispatch(setFarms({ chainId, farms: [] })) + getFarm(true) + const i = interval ? setInterval(() => getFarm(false), 10_000) : undefined return () => { i && clearInterval(i) }