1
0
mirror of https://github.com/oceanprotocol/react.git synced 2024-12-22 17:23:32 +01:00
Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro>
This commit is contained in:
mihaisc 2020-11-19 12:07:19 +02:00 committed by GitHub
parent 20178b78d7
commit bf3528754a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 28 additions and 238 deletions

View File

@ -19,7 +19,6 @@ export function Trade() {
} = usePricing(new DDO())
const [datatoken, setDatatoken] = useState<string | undefined>()
const handleBuy = async () => {
const tx = await buyDT('1')
console.log(tx)

6
package-lock.json generated
View File

@ -1521,9 +1521,9 @@
"integrity": "sha512-p0oOHXr60hXZuLNsQ/PsOQtCfia79thm7MjPxTrnnBvD+csJoHzARYMB0IFj/KTw6U5vLXODgjJAn8x6QksLwg=="
},
"@oceanprotocol/lib": {
"version": "0.9.12",
"resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-0.9.12.tgz",
"integrity": "sha512-R52kWSwwpKNzNHfnNbF6seFPvXEtExK3bWIi4V4eIkgmAf272sa6PVza4mJrtEpTAS1WcJv5ihF7cczIDecxbg==",
"version": "0.9.16",
"resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-0.9.16.tgz",
"integrity": "sha512-7uHnVaykabT3cEPUGRq3ITpiw9+mhS29Pq6rDTi5mtW4dD8DX7jdA7ddkxyHsZe/uQY9I4FLEuCmeTPcaFXPJw==",
"requires": {
"@ethereum-navigator/navigator": "^0.5.0",
"@oceanprotocol/contracts": "^0.5.7",

View File

@ -1,15 +1,8 @@
import { useState, useEffect, useCallback } from 'react'
import {
DID,
DDO,
Metadata,
Logger,
BestPrice,
MetadataCache
} from '@oceanprotocol/lib'
import { DID, DDO, Metadata, Logger, BestPrice } from '@oceanprotocol/lib'
import { useOcean } from 'providers'
import { isDDO, getDataTokenPrice } from 'utils'
import { ConfigHelperConfig } from '@oceanprotocol/lib/dist/node/utils/ConfigHelper'
import axios, { CancelToken } from 'axios'
interface UseMetadata {
ddo: DDO | undefined
@ -18,8 +11,7 @@ interface UseMetadata {
title: string | undefined
owner: string | undefined
price: BestPrice | undefined
isLoaded: boolean
refreshPrice: () => void
getLivePrice: () => Promise<BestPrice>
}
function useMetadata(asset?: DID | string | DDO): UseMetadata {
@ -28,22 +20,27 @@ function useMetadata(asset?: DID | string | DDO): UseMetadata {
const [internalDid, setDID] = useState<DID | string>()
const [metadata, setMetadata] = useState<Metadata>()
const [title, setTitle] = useState<string>()
const [isLoaded, setIsLoaded] = useState(false)
const [price, setPrice] = useState<BestPrice>()
const [owner, setOwner] = useState<string>()
const getDDO = useCallback(
async (did: DID | string): Promise<DDO | undefined> => {
async (
did: DID | string,
cancelToken: CancelToken
): Promise<DDO | undefined> => {
if (!config.metadataCacheUri) return
const metadataCache = new MetadataCache(config.metadataCacheUri, Logger)
const ddo = await metadataCache.retrieveDDO(did)
const request = await axios.get(
`${config.metadataCacheUri}/api/v1/aquarius/assets/ddo/${did}`,
{ cancelToken }
)
const ddo = request.data as DDO
return ddo
},
[config.metadataCacheUri]
)
const getPrice = useCallback(async (): Promise<BestPrice> => {
const getLivePrice = useCallback(async (): Promise<BestPrice> => {
if (!internalDdo)
return {
type: '',
@ -73,24 +70,29 @@ function useMetadata(asset?: DID | string | DDO): UseMetadata {
useEffect(() => {
if (!asset) return
const source = axios.CancelToken.source()
let isMounted = true
async function init(): Promise<void> {
if (isDDO(asset as string | DDO | DID)) {
setDDO(asset as DDO)
setDID((asset as DDO).id)
} else {
// asset is a DID
const ddo = await getDDO(asset as DID)
const ddo = await getDDO(asset as DID, source.token)
if (!isMounted) return
Logger.debug('DDO', ddo)
setDDO(ddo)
setDID(asset as DID)
}
}
init()
return () => {
isMounted = false
source.cancel()
}
}, [asset, getDDO])
async function refreshPrice(): Promise<void> {
const livePrice = await getPrice()
setPrice(livePrice)
}
//
// Get metadata & price for stored DDO
//
@ -105,37 +107,9 @@ function useMetadata(asset?: DID | string | DDO): UseMetadata {
setMetadata(metadata)
setTitle(metadata.main.name)
setOwner(internalDdo.publicKey[0].owner)
setIsLoaded(true)
// 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()
priceLive && internalDdo.price !== priceLive && setPrice(priceLive)
}
init()
// const interval = setInterval(async () => {
// if (
// !internalDdo ||
// status !== 1 ||
// networkId !== (config as ConfigHelperConfig).networkId
// )
// return
// await refreshPrice
// }, 10000)
// return () => {
// clearInterval(interval)
// }
}, [status, networkId, config, internalDdo, getMetadata, getPrice])
}, [status, networkId, config, internalDdo, getMetadata])
return {
ddo: internalDdo,
@ -144,8 +118,7 @@ function useMetadata(asset?: DID | string | DDO): UseMetadata {
title,
owner,
price,
isLoaded,
refreshPrice
getLivePrice
}
}

View File

@ -1,171 +0,0 @@
import React, {
useContext,
useState,
useEffect,
createContext,
ReactElement,
useCallback,
ReactNode
} from 'react'
import {
Logger,
DDO,
Metadata,
BestPrice,
MetadataCache
} from '@oceanprotocol/lib'
import { PurgatoryData } from '@oceanprotocol/lib/dist/node/ddo/interfaces/PurgatoryData'
import { useOcean } from '../OceanProvider'
import { isDDO, getDataTokenPrice } from 'utils'
import { getAssetPurgatoryData } from '../../utils/getPurgatoryData'
import { ConfigHelperConfig } from '@oceanprotocol/lib/dist/node/utils/ConfigHelper'
interface AssetProviderValue {
isInPurgatory: boolean
purgatoryData: PurgatoryData
ddo: DDO | undefined
did: string | undefined
metadata: Metadata | undefined
title: string | undefined
owner: string | undefined
price: BestPrice | undefined
refreshPrice: () => void
}
const AssetContext = createContext({} as AssetProviderValue)
function AssetProvider({
asset,
children
}: {
asset: string | DDO
children: ReactNode
}): ReactElement {
const { ocean, status, config, networkId } = useOcean()
const [isInPurgatory, setIsInPurgatory] = useState(false)
const [purgatoryData, setPurgatoryData] = useState<PurgatoryData>()
const [internalDdo, setDDO] = useState<DDO>()
const [internalDid, setDID] = useState<string>()
const [metadata, setMetadata] = useState<Metadata>()
const [title, setTitle] = useState<string>()
const [price, setPrice] = useState<BestPrice>()
const [owner, setOwner] = useState<string>()
const init = useCallback(async () => {
Logger.log('Asset Provider init')
if (!config.metadataCacheUri) return
if (isDDO(asset as string | DDO)) {
setDDO(asset as DDO)
setDID((asset as DDO).id)
} else {
// asset is a DID
const metadataCache = new MetadataCache(config.metadataCacheUri, Logger)
const ddo = await metadataCache.retrieveDDO(asset as string)
Logger.debug('DDO asset', ddo)
setDDO(ddo)
setDID(asset as string)
}
}, [asset, config.metadataCacheUri])
const getPrice = useCallback(async (): Promise<BestPrice> => {
if (!internalDdo)
return {
type: '',
address: '',
value: 0,
ocean: 0,
datatoken: 0
} as BestPrice
const price = await getDataTokenPrice(
ocean,
internalDdo.dataToken,
internalDdo?.price?.type,
internalDdo.price.pools[0]
)
return price
}, [ocean, internalDdo])
useEffect(() => {
// removed until we can properly test and refactor market
// init()
}, [init, asset, ocean, status])
const setPurgatory = useCallback(async (did: string): Promise<void> => {
if (!did) return
try {
const result = await getAssetPurgatoryData(did)
if (result?.did !== undefined) {
setIsInPurgatory(true)
setPurgatoryData(result)
} else {
setIsInPurgatory(false)
}
setPurgatoryData(result)
} catch (error) {
Logger.error(error)
}
}, [])
const initMetadata = useCallback(async (): Promise<void> => {
// remove until we can properly implement this
// if (!internalDdo) return
// setPrice(internalDdo.price)
// const metadata = internalDdo.findServiceByType('metadata').attributes
// setMetadata(metadata)
// setTitle(metadata.main.name)
// setOwner(internalDdo.publicKey[0].owner)
// setIsInPurgatory(internalDdo.isInPurgatory)
// setPurgatoryData(internalDdo.purgatoryData)
setMetadata(undefined)
setTitle('not_implemented')
setOwner('not_implemented')
if (!asset) return
await setPurgatory(asset as string)
// 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()
setPrice(priceLive)
}, [asset, getPrice])
useEffect(() => {
if (!asset) return
initMetadata()
}, [status, asset, initMetadata])
async function refreshPrice(): Promise<void> {
const livePrice = await getPrice()
setPrice(livePrice)
}
return (
<AssetContext.Provider
value={
{
ddo: internalDdo,
did: internalDid,
metadata,
title,
owner,
price,
isInPurgatory,
purgatoryData,
refreshPrice
} as AssetProviderValue
}
>
{children}
</AssetContext.Provider>
)
}
// Helper hook to access the provider values
const useAsset = (): AssetProviderValue => useContext(AssetContext)
export { AssetProvider, useAsset, AssetProviderValue, AssetContext }
export default AssetProvider

View File

@ -1 +0,0 @@
export * from './AssetProvider'

View File

@ -1,2 +1 @@
export * from './OceanProvider'
export * from './AssetProvider'

View File

@ -1,4 +1,3 @@
import { PurgatoryData } from '@oceanprotocol/lib'
import axios from 'axios'
const purgatoryUrl = 'https://market-purgatory.oceanprotocol.com/api/'
@ -7,14 +6,6 @@ export interface AccountPurgatoryData {
reason: string
}
export async function getAssetPurgatoryData(
did: string
): Promise<PurgatoryData> {
const response = await axios(`${purgatoryUrl}asset?did=${did}`)
const responseJson = await response.data[0]
return { did: responseJson?.did, reason: responseJson?.reason }
}
export async function getAccountPurgatoryData(
address: string
): Promise<AccountPurgatoryData> {