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:
parent
a5c4884f18
commit
08a79305dc
6
package-lock.json
generated
6
package-lock.json
generated
@ -1521,9 +1521,9 @@
|
||||
"integrity": "sha512-p0oOHXr60hXZuLNsQ/PsOQtCfia79thm7MjPxTrnnBvD+csJoHzARYMB0IFj/KTw6U5vLXODgjJAn8x6QksLwg=="
|
||||
},
|
||||
"@oceanprotocol/lib": {
|
||||
"version": "0.9.5",
|
||||
"resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-0.9.5.tgz",
|
||||
"integrity": "sha512-r+mcLwTgB+A42Q1lX0Ni+Yv3xgaujU3uOrSrKs+uNfVt/VCitublgZYnOhZws2odinJt2Ca7cjDPK4QtNzw7MQ==",
|
||||
"version": "0.9.7",
|
||||
"resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-0.9.7.tgz",
|
||||
"integrity": "sha512-C47Bv1d9IEvWkI2cOAYUE115jc6iN87ddDdl/BTCAiHKVllhl3TvoSMD+kdwfTivnsiW6HytpqL56IGtA/F4zQ==",
|
||||
"requires": {
|
||||
"@ethereum-navigator/navigator": "^0.5.0",
|
||||
"@oceanprotocol/contracts": "^0.5.7",
|
||||
|
@ -25,7 +25,7 @@
|
||||
"dist/"
|
||||
],
|
||||
"dependencies": {
|
||||
"@oceanprotocol/lib": "^0.9.6",
|
||||
"@oceanprotocol/lib": "^0.9.7",
|
||||
"axios": "^0.21.0",
|
||||
"decimal.js": "^10.2.1",
|
||||
"web3": "^1.3.0",
|
||||
|
@ -19,6 +19,7 @@ interface UseMetadata {
|
||||
price: BestPrice | undefined
|
||||
isLoaded: boolean
|
||||
getPrice: (dataTokenAddress: string) => Promise<BestPrice | void>
|
||||
refreshPrice: () => void
|
||||
}
|
||||
|
||||
function useMetadata(asset?: DID | string | DDO): UseMetadata {
|
||||
@ -42,8 +43,15 @@ function useMetadata(asset?: DID | string | DDO): UseMetadata {
|
||||
)
|
||||
|
||||
const getPrice = useCallback(
|
||||
async (dataTokenAddress: string): Promise<BestPrice> => {
|
||||
const price = await getBestDataTokenPrice(ocean, dataTokenAddress)
|
||||
async (
|
||||
dataTokenAddress: string,
|
||||
poolAddress?: string
|
||||
): Promise<BestPrice> => {
|
||||
const price = await getBestDataTokenPrice(
|
||||
ocean,
|
||||
dataTokenAddress,
|
||||
poolAddress
|
||||
)
|
||||
return price
|
||||
},
|
||||
[ocean]
|
||||
@ -74,7 +82,14 @@ function useMetadata(asset?: DID | string | DDO): UseMetadata {
|
||||
}
|
||||
init()
|
||||
}, [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
|
||||
//
|
||||
@ -98,7 +113,10 @@ function useMetadata(asset?: DID | string | DDO): UseMetadata {
|
||||
return
|
||||
|
||||
// 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)
|
||||
}
|
||||
init()
|
||||
@ -111,8 +129,8 @@ function useMetadata(asset?: DID | string | DDO): UseMetadata {
|
||||
// )
|
||||
// return
|
||||
|
||||
// const priceLive = await getPrice(internalDdo.dataToken)
|
||||
// priceLive && setPrice(priceLive)
|
||||
// await refreshPrice
|
||||
|
||||
// }, 10000)
|
||||
|
||||
// return () => {
|
||||
@ -127,7 +145,8 @@ function useMetadata(asset?: DID | string | DDO): UseMetadata {
|
||||
title,
|
||||
price,
|
||||
isLoaded,
|
||||
getPrice
|
||||
getPrice,
|
||||
refreshPrice
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -85,8 +85,11 @@ export async function getCheapestExchange(
|
||||
|
||||
export async function getFirstPool(
|
||||
ocean: Ocean,
|
||||
dataTokenAddress: string
|
||||
dataTokenAddress: string,
|
||||
poolAddress?: string
|
||||
): Promise<Pool> {
|
||||
let firstPoolAddress = poolAddress
|
||||
if (!poolAddress) {
|
||||
const tokenPools = await ocean.pool.searchPoolforDT(dataTokenAddress)
|
||||
|
||||
if (tokenPools === undefined || tokenPools.length === 0) {
|
||||
@ -95,9 +98,19 @@ export async function getFirstPool(
|
||||
price: 0
|
||||
}
|
||||
}
|
||||
const firstPoolAddress = tokenPools[0]
|
||||
firstPoolAddress = tokenPools[0]
|
||||
}
|
||||
if (!firstPoolAddress) {
|
||||
return {
|
||||
address: '',
|
||||
price: 0
|
||||
}
|
||||
}
|
||||
|
||||
const firstPoolPrice = await ocean.pool.getOceanNeeded(firstPoolAddress, '1')
|
||||
|
||||
const oceanReserve = await ocean.pool.getOceanReserve(firstPoolAddress)
|
||||
|
||||
const dtReserve = await ocean.pool.getDTReserve(firstPoolAddress)
|
||||
|
||||
return {
|
||||
@ -110,9 +123,10 @@ export async function getFirstPool(
|
||||
|
||||
export async function getBestDataTokenPrice(
|
||||
ocean: Ocean,
|
||||
dataTokenAddress: string
|
||||
dataTokenAddress: string,
|
||||
poolAddress?: string
|
||||
): Promise<BestPrice> {
|
||||
const cheapestPool = await getFirstPool(ocean, dataTokenAddress)
|
||||
const cheapestPool = await getFirstPool(ocean, dataTokenAddress, poolAddress)
|
||||
const cheapestExchange = await getCheapestExchange(ocean, dataTokenAddress)
|
||||
Decimal.set({ precision: 5 })
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user