Skip to content
This repository has been archived by the owner on Aug 3, 2021. It is now read-only.

Commit

Permalink
price fetching fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kremalicious committed Oct 9, 2020
1 parent afbf029 commit b438742
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/hooks/useMetadata/useMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { useOcean } from 'providers'
import { getBestDataTokenPrice } from 'utils/dtUtils'
import { isDDO } from 'utils'
import { ConfigHelperConfig } from '@oceanprotocol/lib/dist/node/utils/ConfigHelper'

interface UseMetadata {
ddo: DDO | undefined
Expand All @@ -22,7 +23,7 @@ interface UseMetadata {
}

function useMetadata(asset?: DID | string | DDO): UseMetadata {
const { ocean, accountId, networkId, config } = useOcean()
const { ocean, config, status, networkId } = useOcean()
const [internalDdo, setDDO] = useState<DDO>()
const [internalDid, setDID] = useState<DID | string>()
const [metadata, setMetadata] = useState<Metadata>()
Expand Down Expand Up @@ -90,23 +91,35 @@ function useMetadata(asset?: DID | string | DDO): UseMetadata {
setTitle(metadata.main.name)
setIsLoaded(true)

if (!accountId) return
// Stop here and do not start fetching from chain, when not connected properly.
if (
status !== 1 ||
networkId !== (config as ConfigHelperConfig).networkId
)
return

// Set price again, but from chain
const priceLive = await getPrice(internalDdo.dataToken)
priceLive && internalDdo.price !== priceLive && setPrice(priceLive)
}
init()

const interval = setInterval(async () => {
if (!internalDdo || !accountId) return
if (
!internalDdo ||
status !== 1 ||
networkId !== (config as ConfigHelperConfig).networkId
)
return

const priceLive = await getPrice(internalDdo.dataToken)
priceLive && setPrice(priceLive)
}, 10000)

return () => {
clearInterval(interval)
}
}, [accountId, networkId, internalDdo, getMetadata, getPrice])
}, [status, networkId, config, internalDdo, getMetadata, getPrice])

return {
ddo: internalDdo,
Expand Down

0 comments on commit b438742

Please sign in to comment.