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

fix price calculation

Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro>
This commit is contained in:
mihaisc 2020-10-29 20:04:19 +02:00
parent dbfa57ea61
commit 6757454ff2
No known key found for this signature in database
GPG Key ID: 4FB0C2329B4C6E29
3 changed files with 70 additions and 30 deletions

6
package-lock.json generated
View File

@ -1521,9 +1521,9 @@
"integrity": "sha512-p0oOHXr60hXZuLNsQ/PsOQtCfia79thm7MjPxTrnnBvD+csJoHzARYMB0IFj/KTw6U5vLXODgjJAn8x6QksLwg=="
},
"@oceanprotocol/lib": {
"version": "0.9.7",
"resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-0.9.7.tgz",
"integrity": "sha512-C47Bv1d9IEvWkI2cOAYUE115jc6iN87ddDdl/BTCAiHKVllhl3TvoSMD+kdwfTivnsiW6HytpqL56IGtA/F4zQ==",
"version": "0.9.8",
"resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-0.9.8.tgz",
"integrity": "sha512-jV7lobTXskqqmv23Vl3iQ4SZk+0FVFxLSVkXniOKOOFP0pJ0vllSNnM+wHkG4inF3HWHHyeh8AyFNTSxOUuirA==",
"requires": {
"@ethereum-navigator/navigator": "^0.5.0",
"@oceanprotocol/contracts": "^0.5.7",

View File

@ -8,7 +8,7 @@ import {
MetadataCache
} from '@oceanprotocol/lib'
import { useOcean } from 'providers'
import { isDDO, getBestDataTokenPrice } from 'utils'
import { isDDO, getBestDataTokenPrice, getDataTokenPrice } from 'utils'
import { ConfigHelperConfig } from '@oceanprotocol/lib/dist/node/utils/ConfigHelper'
interface UseMetadata {
@ -18,7 +18,6 @@ interface UseMetadata {
title: string | undefined
price: BestPrice | undefined
isLoaded: boolean
getPrice: (dataTokenAddress: string) => Promise<BestPrice | void>
refreshPrice: () => void
}
@ -42,20 +41,24 @@ function useMetadata(asset?: DID | string | DDO): UseMetadata {
[config.metadataCacheUri]
)
const getPrice = useCallback(
async (
dataTokenAddress: string,
poolAddress?: string
): Promise<BestPrice> => {
const price = await getBestDataTokenPrice(
ocean,
dataTokenAddress,
poolAddress
)
return price
},
[ocean]
)
const getPrice = useCallback(async (): Promise<BestPrice> => {
if (!internalDdo)
return {
type: '',
address: '',
value: 0,
ocean: 0,
datatoken: 0
} as BestPrice
const price = await getDataTokenPrice(
ocean,
internalDdo.dataToken,
internalDdo?.price?.type,
internalDdo.price.pools[0]
)
return price
}, [ocean, internalDdo])
const getMetadata = useCallback(async (ddo: DDO): Promise<Metadata> => {
const metadata = ddo.findServiceByType('metadata')
@ -83,11 +86,7 @@ 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]
)
const livePrice = await getPrice()
setPrice(livePrice)
}
//
@ -113,10 +112,7 @@ function useMetadata(asset?: DID | string | DDO): UseMetadata {
return
// Set price again, but from chain
const priceLive = await getPrice(
internalDdo.dataToken,
internalDdo.price.pools[0]
)
const priceLive = await getPrice()
priceLive && internalDdo.price !== priceLive && setPrice(priceLive)
}
init()
@ -145,7 +141,6 @@ function useMetadata(asset?: DID | string | DDO): UseMetadata {
title,
price,
isLoaded,
getPrice,
refreshPrice
}
}

View File

@ -107,12 +107,19 @@ export async function getFirstPool(
}
}
const firstPoolPrice = await ocean.pool.getOceanNeeded(firstPoolAddress, '1')
let firstPoolPrice = await ocean.pool.getOceanNeeded(firstPoolAddress, '1')
const oceanReserve = await ocean.pool.getOceanReserve(firstPoolAddress)
const dtReserve = await ocean.pool.getDTReserve(firstPoolAddress)
if (firstPoolPrice) {
const priceChars = firstPoolPrice.split('.')
const numberOfCharsOfPrice = priceChars[0].length
if (numberOfCharsOfPrice > 8) firstPoolPrice = '0'
}
return {
address: firstPoolAddress,
price: Number(firstPoolPrice),
@ -121,6 +128,44 @@ export async function getFirstPool(
}
}
export async function getDataTokenPrice(
ocean: Ocean,
dataTokenAddress: string,
type?: string,
poolAddress?: string
) {
switch (type) {
case 'pool': {
const cheapestPool = await getFirstPool(
ocean,
dataTokenAddress,
poolAddress
)
return {
type: 'pool',
address: cheapestPool?.address,
value: cheapestPool?.price,
ocean: cheapestPool?.ocean,
datatoken: cheapestPool?.datatoken
} as BestPrice
}
case 'exchange': {
const cheapestExchange = await getCheapestExchange(
ocean,
dataTokenAddress
)
return {
type: 'exchange',
address: cheapestExchange?.address,
value: Number(cheapestExchange?.price)
} as BestPrice
}
default: {
return await getBestDataTokenPrice(ocean, dataTokenAddress, poolAddress)
}
}
}
export async function getBestDataTokenPrice(
ocean: Ocean,
dataTokenAddress: string,