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==" "integrity": "sha512-p0oOHXr60hXZuLNsQ/PsOQtCfia79thm7MjPxTrnnBvD+csJoHzARYMB0IFj/KTw6U5vLXODgjJAn8x6QksLwg=="
}, },
"@oceanprotocol/lib": { "@oceanprotocol/lib": {
"version": "0.9.7", "version": "0.9.8",
"resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-0.9.7.tgz", "resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-0.9.8.tgz",
"integrity": "sha512-C47Bv1d9IEvWkI2cOAYUE115jc6iN87ddDdl/BTCAiHKVllhl3TvoSMD+kdwfTivnsiW6HytpqL56IGtA/F4zQ==", "integrity": "sha512-jV7lobTXskqqmv23Vl3iQ4SZk+0FVFxLSVkXniOKOOFP0pJ0vllSNnM+wHkG4inF3HWHHyeh8AyFNTSxOUuirA==",
"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

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