1
0
mirror of https://github.com/oceanprotocol/react.git synced 2024-11-23 02:00:27 +01:00

Merge pull request #89 from oceanprotocol/feature/metaUpdate

added pool, refactor
This commit is contained in:
mihaisc 2020-08-20 14:21:03 +03:00 committed by GitHub
commit 9d96936352
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 25 deletions

View File

@ -2,12 +2,12 @@ import React, { useEffect, useState } from 'react'
import { useMetadata } from '@oceanprotocol/react'
export function MetadataExample({ did }: { did: string }) {
const { title, bestPrice } = useMetadata(did)
const { title, pool } = useMetadata(did)
return (
<>
<div>
{title} - {bestPrice}
{title} - {pool && pool.price + ' - ' + pool.address}
</div>
</>
)

View File

@ -0,0 +1,4 @@
export default interface Pool {
address: string
price: string
}

View File

@ -1 +1,2 @@
export * from './useMetadata'
export * from './Pool'

View File

@ -3,18 +3,18 @@ import { DID, DDO, Metadata, Logger } from '@oceanprotocol/lib'
import { useOcean } from '../../providers'
import ProviderStatus from '../../providers/OceanProvider/ProviderStatus'
import { getBestDataTokenPrice, getCheapestPool } from '../../utils/dtUtils'
import Pool from './Pool'
interface UseMetadata {
ddo: DDO
did: DID | string
metadata: Metadata
title: string
bestPrice: string
price: string
poolAddress: string
isLoaded: boolean
getBestPrice: (dataTokenAddress?: string) => Promise<string>
getBestPool: (
dataTokenAddress?: string
) => Promise<{ poolAddress: string; poolPrice: string }>
getPrice: (dataTokenAddress?: string) => Promise<string>
getPool: (dataTokenAddress?: string) => Promise<Pool>
}
function useMetadata(did?: DID | string, ddo?: DDO): UseMetadata {
@ -24,7 +24,8 @@ function useMetadata(did?: DID | string, ddo?: DDO): UseMetadata {
const [metadata, setMetadata] = useState<Metadata | undefined>()
const [title, setTitle] = useState<string | undefined>()
const [isLoaded, setIsLoaded] = useState(false)
const [bestPrice, setBestPrice] = useState<string | undefined>()
const [price, setPrice] = useState<string | undefined>()
const [poolAddress, setPoolAddress] = useState<string | undefined>()
async function getDDO(did: DID | string): Promise<DDO> {
if (status === ProviderStatus.CONNECTED) {
@ -33,13 +34,11 @@ function useMetadata(did?: DID | string, ddo?: DDO): UseMetadata {
}
}
async function getBestPrice(dataTokenAddress?: string): Promise<string> {
async function getPrice(dataTokenAddress?: string): Promise<string> {
if (!dataTokenAddress) dataTokenAddress = internalDdo.dataToken
return await getBestDataTokenPrice(ocean, accountId, dataTokenAddress)
}
async function getBestPool(
dataTokenAddress: string
): Promise<{ poolAddress: string; poolPrice: string }> {
async function getPool(dataTokenAddress?: string): Promise<Pool> {
if (!dataTokenAddress) dataTokenAddress = internalDdo.dataToken
return await getCheapestPool(ocean, accountId, dataTokenAddress)
}
@ -81,16 +80,18 @@ function useMetadata(did?: DID | string, ddo?: DDO): UseMetadata {
const metadata = await getMetadata()
setMetadata(metadata)
setTitle(metadata.main.name)
const price = await getBestPrice()
setBestPrice(price)
const pool = await getPool()
setPoolAddress(pool.address)
setPrice(pool.price)
setIsLoaded(true)
}
}
init()
const interval = setInterval(async () => {
const price = await getBestPrice()
setBestPrice(price)
const pool = await getPool()
setPoolAddress(pool.address)
setPrice(pool.price)
}, 10000)
return () => clearInterval(interval)
}, [internalDdo])
@ -100,10 +101,11 @@ function useMetadata(did?: DID | string, ddo?: DDO): UseMetadata {
did: internalDid,
metadata,
title,
bestPrice,
price,
poolAddress,
isLoaded,
getBestPrice,
getBestPool
getPrice,
getPool
}
}

View File

@ -1,10 +1,12 @@
import { Logger, Ocean, Account } from '@oceanprotocol/lib'
import { Decimal } from 'decimal.js'
import Pool from '../hooks/useMetadata/Pool'
export async function getCheapestPool(
ocean: Ocean,
accountId: string,
dataTokenAddress: string
): Promise<{ poolAddress: string; poolPrice: string }> {
): Promise<Pool> {
if (!ocean || !accountId || !dataTokenAddress) return
const tokenPools = await ocean.pool.searchPoolforDT(
@ -14,8 +16,8 @@ export async function getCheapestPool(
Logger.log('DT Pool found', tokenPools)
if (tokenPools === undefined || tokenPools.length === 0) {
return {
poolAddress: '',
poolPrice: ''
address: '',
price: ''
}
}
let cheapestPoolAddress
@ -38,8 +40,8 @@ export async function getCheapestPool(
}
return {
poolAddress: cheapestPoolAddress,
poolPrice: cheapestPoolPrice.toString()
address: cheapestPoolAddress,
price: cheapestPoolPrice.toString()
}
}
@ -50,7 +52,7 @@ export async function getBestDataTokenPrice(
): Promise<string> {
const bestPool = await getCheapestPool(ocean, accountId, dataTokenAddress)
return bestPool.poolPrice
return bestPool.price
}
export async function checkAndBuyDT(