Skip to content

Commit

Permalink
fix: farm is stale when switching network
Browse files Browse the repository at this point in the history
  • Loading branch information
viet-nv committed Sep 26, 2023
1 parent 560c179 commit a66181c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 40 deletions.
14 changes: 6 additions & 8 deletions src/state/farms/elastic/updaters/v1.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,26 +151,24 @@ const FarmUpdaterV1: React.FC<CommonProps> = ({ 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) {
Expand Down
46 changes: 14 additions & 32 deletions src/state/farms/elasticv2/updater.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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])
Expand All @@ -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)
}
Expand Down

0 comments on commit a66181c

Please sign in to comment.