Skip to content
This repository has been archived by the owner on Aug 3, 2021. It is now read-only.

Commit

Permalink
refresh price
Browse files Browse the repository at this point in the history
Signed-off-by: mihaisc <[email protected]>
  • Loading branch information
mihaisc committed Oct 29, 2020
1 parent a5c4884 commit 08a7930
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 17 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
33 changes: 26 additions & 7 deletions src/hooks/useMetadata/useMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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]
Expand Down Expand Up @@ -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
//
Expand All @@ -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()
Expand All @@ -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 () => {
Expand All @@ -127,7 +145,8 @@ function useMetadata(asset?: DID | string | DDO): UseMetadata {
title,
price,
isLoaded,
getPrice
getPrice,
refreshPrice
}
}

Expand Down
26 changes: 20 additions & 6 deletions src/utils/dtUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,32 @@ export async function getCheapestExchange(

export async function getFirstPool(
ocean: Ocean,
dataTokenAddress: string
dataTokenAddress: string,
poolAddress?: string
): Promise<Pool> {
const tokenPools = await ocean.pool.searchPoolforDT(dataTokenAddress)
let firstPoolAddress = poolAddress
if (!poolAddress) {
const tokenPools = await ocean.pool.searchPoolforDT(dataTokenAddress)

if (tokenPools === undefined || tokenPools.length === 0) {
if (tokenPools === undefined || tokenPools.length === 0) {
return {
address: '',
price: 0
}
}
firstPoolAddress = tokenPools[0]
}
if (!firstPoolAddress) {
return {
address: '',
price: 0
}
}
const firstPoolAddress = tokenPools[0]

const firstPoolPrice = await ocean.pool.getOceanNeeded(firstPoolAddress, '1')

const oceanReserve = await ocean.pool.getOceanReserve(firstPoolAddress)

const dtReserve = await ocean.pool.getDTReserve(firstPoolAddress)

return {
Expand All @@ -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 })

Expand Down

0 comments on commit 08a7930

Please sign in to comment.