1
0
mirror of https://github.com/oceanprotocol/react.git synced 2025-01-03 18:35:18 +01:00

metadata refactor , example

This commit is contained in:
mihaisc 2020-08-19 15:21:04 +03:00
parent 2fee743f8d
commit 6809183b5d
4 changed files with 75 additions and 38 deletions

View File

@ -4,6 +4,7 @@ import { DDO } from '@oceanprotocol/lib'
import { useState } from 'react' import { useState } from 'react'
import { useEffect } from 'react' import { useEffect } from 'react'
import shortid from 'shortid' import shortid from 'shortid'
import { MetadataExample } from './MetadataExample'
export function AllDdos() { export function AllDdos() {
const { accountId, chainId, account, ocean } = useOcean() const { accountId, chainId, account, ocean } = useOcean()
@ -32,7 +33,7 @@ export function AllDdos() {
{ddos?.map((ddo) => { {ddos?.map((ddo) => {
return ( return (
<div key={shortid.generate()}> <div key={shortid.generate()}>
{ddo.id} <MetadataExample did={ddo.id} />
<br /> <br />
</div> </div>
) )

View File

@ -10,6 +10,7 @@ import { ConsumeDdo } from './ConsumeDdo'
import WalletConnectProvider from '@walletconnect/web3-provider' import WalletConnectProvider from '@walletconnect/web3-provider'
import Torus from '@toruslabs/torus-embed' import Torus from '@toruslabs/torus-embed'
import { NetworkMonitor } from './NetworkMonitor' import { NetworkMonitor } from './NetworkMonitor'
import { MetadataExample } from './MetadataExample'
// factory Address needs to be updated each time you deploy the contract on local network // factory Address needs to be updated each time you deploy the contract on local network
const config = { const config = {
@ -53,11 +54,7 @@ function App() {
}, []) }, [])
return ( return (
<OceanProvider <OceanProvider initialConfig={configRinkeby} web3ModalOpts={web3ModalOpts}>
initialConfig={configRinkeby}
web3ModalOpts={web3ModalOpts}
marketFeeAddress={'0x4D156A2ef69ffdDC55838176C6712C90f60a2285'}
>
<div className="container"> <div className="container">
<NetworkMonitor /> <NetworkMonitor />
<div> <div>

View File

@ -0,0 +1,25 @@
import React, { useEffect, useState } from 'react'
import { useMetadata } from '@oceanprotocol/react'
export function MetadataExample({ did }: { did: string }) {
const { title, getBestPrice } = useMetadata(did)
const [price, setPrice] = useState<string>()
useEffect(() => {
async function init(): Promise<void> {
if (title) {
const price = await getBestPrice()
setPrice(price)
}
}
init()
}, [title])
return (
<>
<div>
{title} - {price}
</div>
</>
)
}

View File

@ -1,81 +1,95 @@
import { useState, useEffect } from 'react' import { useState, useEffect } from 'react'
import { DID, DDO, Metadata, MetadataStore, Logger } from '@oceanprotocol/lib' import { DID, DDO, Metadata, Logger } from '@oceanprotocol/lib'
import { useOcean } from '../../providers' import { useOcean } from '../../providers'
import ProviderStatus from '../../providers/OceanProvider/ProviderStatus' import ProviderStatus from '../../providers/OceanProvider/ProviderStatus'
import { getBestDataTokenPrice, getCheapestPool } from '../../utils/dtUtils' import { getBestDataTokenPrice, getCheapestPool } from '../../utils/dtUtils'
interface UseMetadata { interface UseMetadata {
ddo: DDO ddo: DDO
did: DID | string
metadata: Metadata metadata: Metadata
title: string title: string
getDDO: (did: DID | string) => Promise<DDO> isLoaded: boolean
getMetadata: (did: DID | string) => Promise<Metadata> getBestPrice: (dataTokenAddress?: string) => Promise<string>
getTitle: (did: DID | string) => Promise<string>
getBestPrice: (dataTokenAddress: string) => Promise<string>
getBestPool: ( getBestPool: (
dataTokenAddress: string dataTokenAddress?: string
) => Promise<{ poolAddress: string; poolPrice: string }> ) => Promise<{ poolAddress: string; poolPrice: string }>
} }
function useMetadata(did?: DID | string): UseMetadata { function useMetadata(did?: DID | string, ddo?: DDO): UseMetadata {
const { ocean, status, config, accountId } = useOcean() const { ocean, status, config, accountId } = useOcean()
const [ddo, setDDO] = useState<DDO | undefined>() const [internalDdo, setDDO] = useState<DDO | undefined>()
const [internalDid, setDID] = useState<DID | string | undefined>()
const [metadata, setMetadata] = useState<Metadata | undefined>() const [metadata, setMetadata] = useState<Metadata | undefined>()
const [title, setTitle] = useState<string | undefined>() const [title, setTitle] = useState<string | undefined>()
const [isLoaded, setIsLoaded] = useState(false)
async function getDDO(did: DID | string): Promise<DDO> { async function getDDO(did: DID | string): Promise<DDO> {
if (status === ProviderStatus.CONNECTED) { if (status === ProviderStatus.CONNECTED) {
const ddo = await ocean.metadatastore.retrieveDDO(did) const ddo = await ocean.metadatastore.retrieveDDO(did)
return ddo return ddo
} }
// fallback hitting MetadataStore directly
const metadataStore = new MetadataStore(config.metadataStoreUri, Logger)
const ddo = await metadataStore.retrieveDDO(did)
return ddo
} }
async function getBestPrice(dataTokenAddress: string): Promise<string> { async function getBestPrice(dataTokenAddress?: string): Promise<string> {
if (!dataTokenAddress) dataTokenAddress = internalDdo.dataToken
return await getBestDataTokenPrice(ocean, accountId, dataTokenAddress) return await getBestDataTokenPrice(ocean, accountId, dataTokenAddress)
} }
async function getBestPool( async function getBestPool(
dataTokenAddress: string dataTokenAddress: string
): Promise<{ poolAddress: string; poolPrice: string }> { ): Promise<{ poolAddress: string; poolPrice: string }> {
if (!dataTokenAddress) dataTokenAddress = internalDdo.dataToken
return await getCheapestPool(ocean, accountId, dataTokenAddress) return await getCheapestPool(ocean, accountId, dataTokenAddress)
} }
async function getMetadata(did: DID | string): Promise<Metadata> { async function getMetadata(): Promise<Metadata> {
const ddo = await getDDO(did) if (!internalDdo) return
if (!ddo) return const metadata = internalDdo.findServiceByType('metadata')
const metadata = ddo.findServiceByType('metadata')
return metadata.attributes return metadata.attributes
} }
async function getTitle(did: DID | string): Promise<string> { async function getTitle(): Promise<string> {
const metadata = await getMetadata(did) const metadata = await getMetadata()
return metadata.main.name return metadata.main.name
} }
useEffect(() => { useEffect(() => {
async function init(): Promise<void> { async function init(): Promise<void> {
if (!did) return Logger.debug('meta init', status)
const ddo = await getDDO(did) if (ocean && status === ProviderStatus.CONNECTED) {
setDDO(ddo) if (ddo) {
setDDO(ddo)
const metadata = await getMetadata(did) setDID(ddo.id)
setMetadata(metadata) }
setTitle(metadata.main.name) Logger.debug('meta init', did)
if (did && !ddo) {
const ddo = await getDDO(did)
Logger.debug('DDO', ddo)
setDDO(ddo)
setDID(did)
}
}
} }
init() init()
}, [ocean]) }, [ocean, status])
useEffect(() => {
async function init(): Promise<void> {
if (internalDdo) {
const metadata = await getMetadata()
setMetadata(metadata)
setTitle(metadata.main.name)
setIsLoaded(true)
}
}
init()
}, [internalDdo])
return { return {
ddo, ddo: internalDdo,
did: internalDid,
metadata, metadata,
title, title,
getDDO, isLoaded,
getMetadata,
getTitle,
getBestPrice, getBestPrice,
getBestPool getBestPool
} }