1
0
mirror of https://github.com/oceanprotocol/react.git synced 2025-02-14 21:10:38 +01:00

set price from DDO first, then from chain

This commit is contained in:
Matthias Kretschmann 2020-10-08 13:38:00 +02:00
parent 55326352fa
commit efb4b12d71
Signed by: m
GPG Key ID: 606EEEF3C479A91F

View File

@ -1,7 +1,13 @@
import { useState, useEffect, useCallback } from 'react' import { useState, useEffect, useCallback } from 'react'
import { DID, DDO, Metadata, Logger, BestPrice } from '@oceanprotocol/lib' import {
DID,
DDO,
Metadata,
Logger,
BestPrice,
MetadataStore
} from '@oceanprotocol/lib'
import { useOcean } from 'providers' import { useOcean } from 'providers'
import ProviderStatus from 'providers/OceanProvider/ProviderStatus'
import { getBestDataTokenPrice } from 'utils/dtUtils' import { getBestDataTokenPrice } from 'utils/dtUtils'
import { isDDO } from 'utils' import { isDDO } from 'utils'
@ -16,7 +22,7 @@ interface UseMetadata {
} }
function useMetadata(asset?: DID | string | DDO): UseMetadata { function useMetadata(asset?: DID | string | DDO): UseMetadata {
const { ocean, status, accountId, networkId } = useOcean() const { ocean, accountId, networkId, config } = useOcean()
const [internalDdo, setDDO] = useState<DDO>() const [internalDdo, setDDO] = useState<DDO>()
const [internalDid, setDID] = useState<DID | string>() const [internalDid, setDID] = useState<DID | string>()
const [metadata, setMetadata] = useState<Metadata>() const [metadata, setMetadata] = useState<Metadata>()
@ -25,11 +31,14 @@ function useMetadata(asset?: DID | string | DDO): UseMetadata {
const [price, setPrice] = useState<BestPrice>() const [price, setPrice] = useState<BestPrice>()
const getDDO = useCallback( const getDDO = useCallback(
async (did: DID | string): Promise<DDO> => { async (did: DID | string): Promise<DDO | undefined> => {
const ddo = await ocean.metadatastore.retrieveDDO(did) if (!config.metadataStoreUri) return
const metadataStore = new MetadataStore(config.metadataStoreUri, Logger)
const ddo = await metadataStore.retrieveDDO(did)
return ddo return ddo
}, },
[ocean?.metadatastore] [config.metadataStoreUri]
) )
const getPrice = useCallback( const getPrice = useCallback(
@ -49,11 +58,9 @@ function useMetadata(asset?: DID | string | DDO): UseMetadata {
// Get and set DDO based on passed DDO or DID // Get and set DDO based on passed DDO or DID
// //
useEffect(() => { useEffect(() => {
if (!asset || !ocean || status !== ProviderStatus.CONNECTED) return
async function init(): Promise<void> {
if (!asset) return if (!asset) return
async function init(): Promise<void> {
if (isDDO(asset as string | DDO | DID)) { if (isDDO(asset as string | DDO | DID)) {
setDDO(asset as DDO) setDDO(asset as DDO)
setDID((asset as DDO).id) setDID((asset as DDO).id)
@ -66,30 +73,34 @@ function useMetadata(asset?: DID | string | DDO): UseMetadata {
} }
} }
init() init()
}, [ocean, status, asset, getDDO]) }, [asset, getDDO])
// //
// Get metadata for stored DDO // Get metadata & price for stored DDO
// //
useEffect(() => { useEffect(() => {
if (!accountId) return
async function init(): Promise<void> { async function init(): Promise<void> {
if (!internalDdo) return if (!internalDdo) return
// Set price from DDO first
setPrice(internalDdo.price)
const metadata = await getMetadata(internalDdo) const metadata = await getMetadata(internalDdo)
setMetadata(metadata) setMetadata(metadata)
setTitle(metadata.main.name) setTitle(metadata.main.name)
const price = await getPrice(internalDdo.dataToken)
price && setPrice(price)
setIsLoaded(true) setIsLoaded(true)
if (!accountId) return
// Set price again, but from chain
const priceLive = await getPrice(internalDdo.dataToken)
priceLive && internalDdo.price !== priceLive && setPrice(priceLive)
} }
init() init()
const interval = setInterval(async () => { const interval = setInterval(async () => {
if (!internalDdo) return if (!internalDdo || !accountId) return
const price = await getPrice(internalDdo.dataToken) const priceLive = await getPrice(internalDdo.dataToken)
price && setPrice(price) priceLive && setPrice(priceLive)
}, 10000) }, 10000)
return () => { return () => {