mirror of
https://github.com/oceanprotocol/react.git
synced 2025-02-14 21:10:38 +01:00
update oceanlib, improve price calculation
Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro>
This commit is contained in:
parent
e7e83e4197
commit
0f62406a58
3094
package-lock.json
generated
3094
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -25,7 +25,7 @@
|
|||||||
"dist/"
|
"dist/"
|
||||||
],
|
],
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@oceanprotocol/lib": "^0.9.8",
|
"@oceanprotocol/lib": "^0.9.9",
|
||||||
"axios": "^0.21.0",
|
"axios": "^0.21.0",
|
||||||
"decimal.js": "^10.2.1",
|
"decimal.js": "^10.2.1",
|
||||||
"web3": "^1.3.0",
|
"web3": "^1.3.0",
|
||||||
|
@ -8,7 +8,7 @@ import {
|
|||||||
MetadataCache
|
MetadataCache
|
||||||
} from '@oceanprotocol/lib'
|
} from '@oceanprotocol/lib'
|
||||||
import { useOcean } from 'providers'
|
import { useOcean } from 'providers'
|
||||||
import { isDDO, getBestDataTokenPrice, getDataTokenPrice } from 'utils'
|
import { isDDO, 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 {
|
||||||
@ -16,6 +16,7 @@ interface UseMetadata {
|
|||||||
did: DID | string | undefined
|
did: DID | string | undefined
|
||||||
metadata: Metadata | undefined
|
metadata: Metadata | undefined
|
||||||
title: string | undefined
|
title: string | undefined
|
||||||
|
owner: string | undefined
|
||||||
price: BestPrice | undefined
|
price: BestPrice | undefined
|
||||||
isLoaded: boolean
|
isLoaded: boolean
|
||||||
refreshPrice: () => void
|
refreshPrice: () => void
|
||||||
@ -29,6 +30,7 @@ function useMetadata(asset?: DID | string | DDO): UseMetadata {
|
|||||||
const [title, setTitle] = useState<string>()
|
const [title, setTitle] = useState<string>()
|
||||||
const [isLoaded, setIsLoaded] = useState(false)
|
const [isLoaded, setIsLoaded] = useState(false)
|
||||||
const [price, setPrice] = useState<BestPrice>()
|
const [price, setPrice] = useState<BestPrice>()
|
||||||
|
const [owner, setOwner] = useState<string>()
|
||||||
|
|
||||||
const getDDO = useCallback(
|
const getDDO = useCallback(
|
||||||
async (did: DID | string): Promise<DDO | undefined> => {
|
async (did: DID | string): Promise<DDO | undefined> => {
|
||||||
@ -102,6 +104,7 @@ function useMetadata(asset?: DID | string | DDO): UseMetadata {
|
|||||||
const metadata = await getMetadata(internalDdo)
|
const metadata = await getMetadata(internalDdo)
|
||||||
setMetadata(metadata)
|
setMetadata(metadata)
|
||||||
setTitle(metadata.main.name)
|
setTitle(metadata.main.name)
|
||||||
|
setOwner(internalDdo.publicKey[0].owner)
|
||||||
setIsLoaded(true)
|
setIsLoaded(true)
|
||||||
|
|
||||||
// Stop here and do not start fetching from chain, when not connected properly.
|
// 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,
|
did: internalDid,
|
||||||
metadata,
|
metadata,
|
||||||
title,
|
title,
|
||||||
|
owner,
|
||||||
price,
|
price,
|
||||||
isLoaded,
|
isLoaded,
|
||||||
refreshPrice
|
refreshPrice
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
import { Logger, Ocean, BestPrice } from '@oceanprotocol/lib'
|
import { Ocean, BestPrice, Logger } from '@oceanprotocol/lib'
|
||||||
import { Decimal } from 'decimal.js'
|
import { Decimal } from 'decimal.js'
|
||||||
import Pool from 'hooks/useMetadata/Pool'
|
import Pool from 'hooks/useMetadata/Pool'
|
||||||
import Web3 from 'web3'
|
|
||||||
|
|
||||||
export async function getCheapestPool(
|
export async function getCheapestPool(
|
||||||
ocean: Ocean,
|
ocean: Ocean,
|
||||||
@ -60,9 +59,7 @@ export async function getCheapestExchange(
|
|||||||
let cheapestExchangePrice = new Decimal(tokenExchanges[0].fixedRate)
|
let cheapestExchangePrice = new Decimal(tokenExchanges[0].fixedRate)
|
||||||
|
|
||||||
for (let i = 0; i < tokenExchanges.length; i++) {
|
for (let i = 0; i < tokenExchanges.length; i++) {
|
||||||
const decimalExchangePrice = new Decimal(
|
const decimalExchangePrice = new Decimal(tokenExchanges[i].fixedRate)
|
||||||
Web3.utils.fromWei(tokenExchanges[i].fixedRate)
|
|
||||||
)
|
|
||||||
|
|
||||||
if (decimalExchangePrice < cheapestExchangePrice) {
|
if (decimalExchangePrice < cheapestExchangePrice) {
|
||||||
cheapestExchangePrice = decimalExchangePrice
|
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(
|
export async function getFirstPool(
|
||||||
ocean: Ocean,
|
ocean: Ocean,
|
||||||
dataTokenAddress: string,
|
dataTokenAddress: string,
|
||||||
@ -185,10 +210,7 @@ export async function getDataTokenPrice(
|
|||||||
} as BestPrice
|
} as BestPrice
|
||||||
}
|
}
|
||||||
case 'exchange': {
|
case 'exchange': {
|
||||||
const cheapestExchange = await getCheapestExchange(
|
const cheapestExchange = await getFirstExchange(ocean, dataTokenAddress)
|
||||||
ocean,
|
|
||||||
dataTokenAddress
|
|
||||||
)
|
|
||||||
return {
|
return {
|
||||||
type: 'exchange',
|
type: 'exchange',
|
||||||
address: cheapestExchange?.address,
|
address: cheapestExchange?.address,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user