Skip to content

Commit

Permalink
Merge branch 'main' into kyberai-searchissing-logo
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaoYhun authored Sep 26, 2023
2 parents 2e81dbf + 8176628 commit 0a49bc6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 39 deletions.
19 changes: 9 additions & 10 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 All @@ -179,8 +177,9 @@ const FarmUpdaterV1: React.FC<CommonProps> = ({ 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 {
Expand Down Expand Up @@ -268,7 +267,7 @@ const FarmUpdaterV1: React.FC<CommonProps> = ({ interval }) => {
})
dispatch(setFarms({ chainId, farms: formattedData }))
}
}, [data, dispatch, chainId, elasticFarm])
}, [data, dispatch, chainId, hasFarm])

return null
}
Expand Down
47 changes: 18 additions & 29 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,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)
}
Expand Down

0 comments on commit 0a49bc6

Please sign in to comment.