1
0
mirror of https://github.com/oceanprotocol/react.git synced 2025-02-14 21:10:38 +01:00

refresh price

Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro>
This commit is contained in:
mihaisc 2020-10-29 14:16:59 +02:00
parent a5c4884f18
commit 08a79305dc
No known key found for this signature in database
GPG Key ID: 4FB0C2329B4C6E29
4 changed files with 50 additions and 17 deletions

6
package-lock.json generated
View File

@ -1521,9 +1521,9 @@
"integrity": "sha512-p0oOHXr60hXZuLNsQ/PsOQtCfia79thm7MjPxTrnnBvD+csJoHzARYMB0IFj/KTw6U5vLXODgjJAn8x6QksLwg==" "integrity": "sha512-p0oOHXr60hXZuLNsQ/PsOQtCfia79thm7MjPxTrnnBvD+csJoHzARYMB0IFj/KTw6U5vLXODgjJAn8x6QksLwg=="
}, },
"@oceanprotocol/lib": { "@oceanprotocol/lib": {
"version": "0.9.5", "version": "0.9.7",
"resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-0.9.5.tgz", "resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-0.9.7.tgz",
"integrity": "sha512-r+mcLwTgB+A42Q1lX0Ni+Yv3xgaujU3uOrSrKs+uNfVt/VCitublgZYnOhZws2odinJt2Ca7cjDPK4QtNzw7MQ==", "integrity": "sha512-C47Bv1d9IEvWkI2cOAYUE115jc6iN87ddDdl/BTCAiHKVllhl3TvoSMD+kdwfTivnsiW6HytpqL56IGtA/F4zQ==",
"requires": { "requires": {
"@ethereum-navigator/navigator": "^0.5.0", "@ethereum-navigator/navigator": "^0.5.0",
"@oceanprotocol/contracts": "^0.5.7", "@oceanprotocol/contracts": "^0.5.7",

View File

@ -25,7 +25,7 @@
"dist/" "dist/"
], ],
"dependencies": { "dependencies": {
"@oceanprotocol/lib": "^0.9.6", "@oceanprotocol/lib": "^0.9.7",
"axios": "^0.21.0", "axios": "^0.21.0",
"decimal.js": "^10.2.1", "decimal.js": "^10.2.1",
"web3": "^1.3.0", "web3": "^1.3.0",

View File

@ -19,6 +19,7 @@ interface UseMetadata {
price: BestPrice | undefined price: BestPrice | undefined
isLoaded: boolean isLoaded: boolean
getPrice: (dataTokenAddress: string) => Promise<BestPrice | void> getPrice: (dataTokenAddress: string) => Promise<BestPrice | void>
refreshPrice: () => void
} }
function useMetadata(asset?: DID | string | DDO): UseMetadata { function useMetadata(asset?: DID | string | DDO): UseMetadata {
@ -42,8 +43,15 @@ function useMetadata(asset?: DID | string | DDO): UseMetadata {
) )
const getPrice = useCallback( const getPrice = useCallback(
async (dataTokenAddress: string): Promise<BestPrice> => { async (
const price = await getBestDataTokenPrice(ocean, dataTokenAddress) dataTokenAddress: string,
poolAddress?: string
): Promise<BestPrice> => {
const price = await getBestDataTokenPrice(
ocean,
dataTokenAddress,
poolAddress
)
return price return price
}, },
[ocean] [ocean]
@ -74,7 +82,14 @@ function useMetadata(asset?: DID | string | DDO): UseMetadata {
} }
init() init()
}, [asset, getDDO]) }, [asset, getDDO])
async function refreshPrice(): Promise<void> {
if (!internalDdo) return
const livePrice = await getPrice(
internalDdo.dataToken,
internalDdo.price.pools[0]
)
setPrice(livePrice)
}
// //
// Get metadata & price for stored DDO // Get metadata & price for stored DDO
// //
@ -98,7 +113,10 @@ function useMetadata(asset?: DID | string | DDO): UseMetadata {
return return
// Set price again, but from chain // Set price again, but from chain
const priceLive = await getPrice(internalDdo.dataToken) const priceLive = await getPrice(
internalDdo.dataToken,
internalDdo.price.pools[0]
)
priceLive && internalDdo.price !== priceLive && setPrice(priceLive) priceLive && internalDdo.price !== priceLive && setPrice(priceLive)
} }
init() init()
@ -111,8 +129,8 @@ function useMetadata(asset?: DID | string | DDO): UseMetadata {
// ) // )
// return // return
// const priceLive = await getPrice(internalDdo.dataToken) // await refreshPrice
// priceLive && setPrice(priceLive)
// }, 10000) // }, 10000)
// return () => { // return () => {
@ -127,7 +145,8 @@ function useMetadata(asset?: DID | string | DDO): UseMetadata {
title, title,
price, price,
isLoaded, isLoaded,
getPrice getPrice,
refreshPrice
} }
} }

View File

@ -85,8 +85,11 @@ export async function getCheapestExchange(
export async function getFirstPool( export async function getFirstPool(
ocean: Ocean, ocean: Ocean,
dataTokenAddress: string dataTokenAddress: string,
poolAddress?: string
): Promise<Pool> { ): Promise<Pool> {
let firstPoolAddress = poolAddress
if (!poolAddress) {
const tokenPools = await ocean.pool.searchPoolforDT(dataTokenAddress) const tokenPools = await ocean.pool.searchPoolforDT(dataTokenAddress)
if (tokenPools === undefined || tokenPools.length === 0) { if (tokenPools === undefined || tokenPools.length === 0) {
@ -95,9 +98,19 @@ export async function getFirstPool(
price: 0 price: 0
} }
} }
const firstPoolAddress = tokenPools[0] firstPoolAddress = tokenPools[0]
}
if (!firstPoolAddress) {
return {
address: '',
price: 0
}
}
const firstPoolPrice = await ocean.pool.getOceanNeeded(firstPoolAddress, '1') const firstPoolPrice = await ocean.pool.getOceanNeeded(firstPoolAddress, '1')
const oceanReserve = await ocean.pool.getOceanReserve(firstPoolAddress) const oceanReserve = await ocean.pool.getOceanReserve(firstPoolAddress)
const dtReserve = await ocean.pool.getDTReserve(firstPoolAddress) const dtReserve = await ocean.pool.getDTReserve(firstPoolAddress)
return { return {
@ -110,9 +123,10 @@ export async function getFirstPool(
export async function getBestDataTokenPrice( export async function getBestDataTokenPrice(
ocean: Ocean, ocean: Ocean,
dataTokenAddress: string dataTokenAddress: string,
poolAddress?: string
): Promise<BestPrice> { ): Promise<BestPrice> {
const cheapestPool = await getFirstPool(ocean, dataTokenAddress) const cheapestPool = await getFirstPool(ocean, dataTokenAddress, poolAddress)
const cheapestExchange = await getCheapestExchange(ocean, dataTokenAddress) const cheapestExchange = await getCheapestExchange(ocean, dataTokenAddress)
Decimal.set({ precision: 5 }) Decimal.set({ precision: 5 })