1
0
mirror of https://github.com/oceanprotocol/react.git synced 2024-12-23 01:29:49 +01:00

update oceanlib, improve price calculation

Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro>
This commit is contained in:
mihaisc 2020-10-30 16:05:58 +02:00
parent e7e83e4197
commit 0f62406a58
No known key found for this signature in database
GPG Key ID: 4FB0C2329B4C6E29
4 changed files with 3128 additions and 14 deletions

3094
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

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

View File

@ -8,7 +8,7 @@ import {
MetadataCache
} from '@oceanprotocol/lib'
import { useOcean } from 'providers'
import { isDDO, getBestDataTokenPrice, getDataTokenPrice } from 'utils'
import { isDDO, getDataTokenPrice } from 'utils'
import { ConfigHelperConfig } from '@oceanprotocol/lib/dist/node/utils/ConfigHelper'
interface UseMetadata {
@ -16,6 +16,7 @@ interface UseMetadata {
did: DID | string | undefined
metadata: Metadata | undefined
title: string | undefined
owner: string | undefined
price: BestPrice | undefined
isLoaded: boolean
refreshPrice: () => void
@ -29,6 +30,7 @@ function useMetadata(asset?: DID | string | DDO): UseMetadata {
const [title, setTitle] = useState<string>()
const [isLoaded, setIsLoaded] = useState(false)
const [price, setPrice] = useState<BestPrice>()
const [owner, setOwner] = useState<string>()
const getDDO = useCallback(
async (did: DID | string): Promise<DDO | undefined> => {
@ -102,6 +104,7 @@ function useMetadata(asset?: DID | string | DDO): UseMetadata {
const metadata = await getMetadata(internalDdo)
setMetadata(metadata)
setTitle(metadata.main.name)
setOwner(internalDdo.publicKey[0].owner)
setIsLoaded(true)
// Stop here and do not start fetching from chain, when not connected properly.
@ -139,6 +142,7 @@ function useMetadata(asset?: DID | string | DDO): UseMetadata {
did: internalDid,
metadata,
title,
owner,
price,
isLoaded,
refreshPrice

View File

@ -1,7 +1,6 @@
import { Logger, Ocean, BestPrice } from '@oceanprotocol/lib'
import { Ocean, BestPrice, Logger } from '@oceanprotocol/lib'
import { Decimal } from 'decimal.js'
import Pool from 'hooks/useMetadata/Pool'
import Web3 from 'web3'
export async function getCheapestPool(
ocean: Ocean,
@ -60,9 +59,7 @@ export async function getCheapestExchange(
let cheapestExchangePrice = new Decimal(tokenExchanges[0].fixedRate)
for (let i = 0; i < tokenExchanges.length; i++) {
const decimalExchangePrice = new Decimal(
Web3.utils.fromWei(tokenExchanges[i].fixedRate)
)
const decimalExchangePrice = new Decimal(tokenExchanges[i].fixedRate)
if (decimalExchangePrice < cheapestExchangePrice) {
cheapestExchangePrice = decimalExchangePrice
@ -83,6 +80,34 @@ export async function getCheapestExchange(
}
}
export async function getFirstExchange(ocean: Ocean, dataTokenAddress: string) {
try {
const tokenExchanges = await ocean.fixedRateExchange.searchforDT(
dataTokenAddress,
'1'
)
Logger.log('Found exchanges', tokenExchanges)
if (tokenExchanges === undefined || tokenExchanges.length === 0) {
return {
address: '',
price: 0
}
}
const [tokenExchange] = tokenExchanges
return {
address: tokenExchange.exchangeID || '',
price: Number(tokenExchange.fixedRate)
}
} catch (err) {
Logger.log(err)
return {
address: '',
price: 0
}
}
}
export async function getFirstPool(
ocean: Ocean,
dataTokenAddress: string,
@ -185,10 +210,7 @@ export async function getDataTokenPrice(
} as BestPrice
}
case 'exchange': {
const cheapestExchange = await getCheapestExchange(
ocean,
dataTokenAddress
)
const cheapestExchange = await getFirstExchange(ocean, dataTokenAddress)
return {
type: 'exchange',
address: cheapestExchange?.address,