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:
commit
9d96936352
@ -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>
|
||||
</>
|
||||
)
|
||||
|
4
src/hooks/useMetadata/Pool.ts
Normal file
4
src/hooks/useMetadata/Pool.ts
Normal file
@ -0,0 +1,4 @@
|
||||
export default interface Pool {
|
||||
address: string
|
||||
price: string
|
||||
}
|
@ -1 +1,2 @@
|
||||
export * from './useMetadata'
|
||||
export * from './Pool'
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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(
|
||||
|
Loading…
Reference in New Issue
Block a user