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

added pool, refactor

This commit is contained in:
mihaisc 2020-08-20 12:23:09 +03:00
parent af76d900cf
commit ef1dd37837
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' import { useMetadata } from '@oceanprotocol/react'
export function MetadataExample({ did }: { did: string }) { export function MetadataExample({ did }: { did: string }) {
const { title, bestPrice } = useMetadata(did) const { title, pool } = useMetadata(did)
return ( return (
<> <>
<div> <div>
{title} - {bestPrice} {title} - {pool && pool.price + ' - ' + pool.address}
</div> </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 './useMetadata'
export * from './Pool'

View File

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

View File

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