mirror of
https://github.com/oceanprotocol/react.git
synced 2025-02-14 21:10:38 +01:00
Merge pull request #138 from oceanprotocol/feature/price-updates
BestPrice updates
This commit is contained in:
commit
0a45eb2a6c
6
package-lock.json
generated
6
package-lock.json
generated
@ -1521,9 +1521,9 @@
|
||||
"integrity": "sha512-gJ8qQACJgxOPIrPE0OFQ09iYXBAisOGg56EmelQlsMUgp0yY0DKgBntDP83S/Ho1yBjGygqfxCjQrPH63hh/PA=="
|
||||
},
|
||||
"@oceanprotocol/lib": {
|
||||
"version": "0.5.5",
|
||||
"resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-0.5.5.tgz",
|
||||
"integrity": "sha512-TJTehUaQnFfNp0dJmw0t15+mpYbTkeF+RX4oc6D5FN7hmlhdvC8kfGf8yeHiqxI2Nb703foFLSSs38MWu/XEpg==",
|
||||
"version": "0.5.6",
|
||||
"resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-0.5.6.tgz",
|
||||
"integrity": "sha512-S8OU/FYjDJCKkx098GDT9LfxmTTe/gA8zv5fVMy7lRG1k5WFDsHHMplqZkU9mUGXg1aDDtt7KbctwxNdt7ZGFg==",
|
||||
"requires": {
|
||||
"@ethereum-navigator/navigator": "^0.5.0",
|
||||
"@oceanprotocol/contracts": "^0.5.3",
|
||||
|
@ -25,7 +25,7 @@
|
||||
"dist/"
|
||||
],
|
||||
"dependencies": {
|
||||
"@oceanprotocol/lib": "^0.5.5",
|
||||
"@oceanprotocol/lib": "^0.5.6",
|
||||
"axios": "^0.20.0",
|
||||
"decimal.js": "^10.2.1",
|
||||
"web3": "^1.3.0",
|
||||
|
@ -1,5 +0,0 @@
|
||||
export default interface BestPrice {
|
||||
type: 'pool' | 'exchange'
|
||||
address: string
|
||||
value: string
|
||||
}
|
@ -1,4 +1,6 @@
|
||||
export default interface Pool {
|
||||
address: string
|
||||
price: string
|
||||
price: number
|
||||
ocean?: number
|
||||
datatoken?: number
|
||||
}
|
||||
|
@ -1,3 +1,2 @@
|
||||
export * from './useMetadata'
|
||||
export * from './Pool'
|
||||
export * from './BestPrice'
|
||||
|
@ -1,10 +1,15 @@
|
||||
import { useState, useEffect, useCallback } from 'react'
|
||||
import { DID, DDO, Metadata, Logger } from '@oceanprotocol/lib'
|
||||
import {
|
||||
DID,
|
||||
DDO,
|
||||
Metadata,
|
||||
Logger,
|
||||
BestPrice,
|
||||
MetadataStore
|
||||
} from '@oceanprotocol/lib'
|
||||
import { useOcean } from 'providers'
|
||||
import ProviderStatus from 'providers/OceanProvider/ProviderStatus'
|
||||
import { getBestDataTokenPrice } from 'utils/dtUtils'
|
||||
import { isDDO } from 'utils'
|
||||
import BestPrice from './BestPrice'
|
||||
|
||||
interface UseMetadata {
|
||||
ddo: DDO | undefined
|
||||
@ -17,7 +22,7 @@ interface UseMetadata {
|
||||
}
|
||||
|
||||
function useMetadata(asset?: DID | string | DDO): UseMetadata {
|
||||
const { ocean, status, accountId, networkId } = useOcean()
|
||||
const { ocean, accountId, networkId, config } = useOcean()
|
||||
const [internalDdo, setDDO] = useState<DDO>()
|
||||
const [internalDid, setDID] = useState<DID | string>()
|
||||
const [metadata, setMetadata] = useState<Metadata>()
|
||||
@ -26,23 +31,22 @@ function useMetadata(asset?: DID | string | DDO): UseMetadata {
|
||||
const [price, setPrice] = useState<BestPrice>()
|
||||
|
||||
const getDDO = useCallback(
|
||||
async (did: DID | string): Promise<DDO> => {
|
||||
const ddo = await ocean.metadatastore.retrieveDDO(did)
|
||||
async (did: DID | string): Promise<DDO | undefined> => {
|
||||
if (!config.metadataStoreUri) return
|
||||
|
||||
const metadataStore = new MetadataStore(config.metadataStoreUri, Logger)
|
||||
const ddo = await metadataStore.retrieveDDO(did)
|
||||
return ddo
|
||||
},
|
||||
[ocean?.metadatastore]
|
||||
[config.metadataStoreUri]
|
||||
)
|
||||
|
||||
const getPrice = useCallback(
|
||||
async (dataTokenAddress: string): Promise<BestPrice> => {
|
||||
const price = await getBestDataTokenPrice(
|
||||
ocean,
|
||||
dataTokenAddress,
|
||||
accountId
|
||||
)
|
||||
const price = await getBestDataTokenPrice(ocean, dataTokenAddress)
|
||||
return price
|
||||
},
|
||||
[ocean, accountId]
|
||||
[ocean]
|
||||
)
|
||||
|
||||
const getMetadata = useCallback(async (ddo: DDO): Promise<Metadata> => {
|
||||
@ -54,11 +58,9 @@ function useMetadata(asset?: DID | string | DDO): UseMetadata {
|
||||
// Get and set DDO based on passed DDO or DID
|
||||
//
|
||||
useEffect(() => {
|
||||
if (!asset || !ocean || status !== ProviderStatus.CONNECTED) return
|
||||
|
||||
async function init(): Promise<void> {
|
||||
if (!asset) return
|
||||
|
||||
async function init(): Promise<void> {
|
||||
if (isDDO(asset as string | DDO | DID)) {
|
||||
setDDO(asset as DDO)
|
||||
setDID((asset as DDO).id)
|
||||
@ -71,30 +73,34 @@ function useMetadata(asset?: DID | string | DDO): UseMetadata {
|
||||
}
|
||||
}
|
||||
init()
|
||||
}, [ocean, status, asset, getDDO])
|
||||
}, [asset, getDDO])
|
||||
|
||||
//
|
||||
// Get metadata for stored DDO
|
||||
// Get metadata & price for stored DDO
|
||||
//
|
||||
useEffect(() => {
|
||||
if (!accountId) return
|
||||
|
||||
async function init(): Promise<void> {
|
||||
if (!internalDdo) return
|
||||
|
||||
// Set price from DDO first
|
||||
setPrice(internalDdo.price)
|
||||
|
||||
const metadata = await getMetadata(internalDdo)
|
||||
setMetadata(metadata)
|
||||
setTitle(metadata.main.name)
|
||||
const price = await getPrice(internalDdo.dataToken)
|
||||
price && setPrice(price)
|
||||
setIsLoaded(true)
|
||||
|
||||
if (!accountId) return
|
||||
// Set price again, but from chain
|
||||
const priceLive = await getPrice(internalDdo.dataToken)
|
||||
priceLive && internalDdo.price !== priceLive && setPrice(priceLive)
|
||||
}
|
||||
init()
|
||||
|
||||
const interval = setInterval(async () => {
|
||||
if (!internalDdo) return
|
||||
const price = await getPrice(internalDdo.dataToken)
|
||||
price && setPrice(price)
|
||||
if (!internalDdo || !accountId) return
|
||||
const priceLive = await getPrice(internalDdo.dataToken)
|
||||
priceLive && setPrice(priceLive)
|
||||
}, 10000)
|
||||
|
||||
return () => {
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { Logger, Ocean, Account, Config } from '@oceanprotocol/lib'
|
||||
import { Logger, Ocean, Account, Config, BestPrice } from '@oceanprotocol/lib'
|
||||
import { TransactionReceipt } from 'web3-core'
|
||||
import { Decimal } from 'decimal.js'
|
||||
import Pool from 'hooks/useMetadata/Pool'
|
||||
import BestPrice from 'hooks/useMetadata/BestPrice'
|
||||
import Web3 from 'web3'
|
||||
|
||||
export async function getCheapestPool(
|
||||
@ -15,7 +15,7 @@ export async function getCheapestPool(
|
||||
if (tokenPools === undefined || tokenPools.length === 0) {
|
||||
return {
|
||||
address: '',
|
||||
price: ''
|
||||
price: 0
|
||||
}
|
||||
}
|
||||
let cheapestPoolAddress = tokenPools[0]
|
||||
@ -33,16 +33,21 @@ export async function getCheapestPool(
|
||||
}
|
||||
}
|
||||
|
||||
const oceanReserve = await ocean.pool.getOceanReserve(cheapestPoolAddress)
|
||||
const dtReserve = await ocean.pool.getDTReserve(cheapestPoolAddress)
|
||||
|
||||
return {
|
||||
address: cheapestPoolAddress,
|
||||
price: cheapestPoolPrice.toString()
|
||||
price: Number(cheapestPoolPrice),
|
||||
ocean: Number(oceanReserve),
|
||||
datatoken: Number(dtReserve)
|
||||
}
|
||||
}
|
||||
|
||||
export async function getCheapestExchange(
|
||||
ocean: Ocean,
|
||||
dataTokenAddress: string
|
||||
): Promise<{ address?: string; price: string }> {
|
||||
): Promise<Pool | undefined> {
|
||||
try {
|
||||
const tokenExchanges = await ocean.fixedRateExchange.searchforDT(
|
||||
dataTokenAddress,
|
||||
@ -51,7 +56,7 @@ export async function getCheapestExchange(
|
||||
if (tokenExchanges === undefined || tokenExchanges.length === 0) {
|
||||
return {
|
||||
address: '',
|
||||
price: ''
|
||||
price: 0
|
||||
}
|
||||
}
|
||||
let cheapestExchangeAddress = tokenExchanges[0].exchangeID
|
||||
@ -69,34 +74,31 @@ export async function getCheapestExchange(
|
||||
}
|
||||
|
||||
return {
|
||||
address: cheapestExchangeAddress,
|
||||
price: cheapestExchangePrice.toString()
|
||||
address: cheapestExchangeAddress || '',
|
||||
price: Number(cheapestExchangePrice)
|
||||
}
|
||||
} catch (err) {
|
||||
Logger.log(err)
|
||||
return {
|
||||
address: '',
|
||||
price: ''
|
||||
price: 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function getBestDataTokenPrice(
|
||||
ocean: Ocean,
|
||||
dataTokenAddress: string,
|
||||
accountId: string
|
||||
dataTokenAddress: string
|
||||
): Promise<BestPrice> {
|
||||
const cheapestPool = await getCheapestPool(ocean, dataTokenAddress)
|
||||
const cheapestExchange = await getCheapestExchange(ocean, dataTokenAddress)
|
||||
Decimal.set({ precision: 5 })
|
||||
|
||||
const cheapestPoolPrice = new Decimal(
|
||||
cheapestPool && cheapestPool.price !== ''
|
||||
? cheapestPool.price
|
||||
: 999999999999
|
||||
cheapestPool && cheapestPool.price !== 0 ? cheapestPool.price : 999999999999
|
||||
)
|
||||
const cheapestExchangePrice = new Decimal(
|
||||
cheapestExchange && cheapestExchange?.price !== ''
|
||||
cheapestExchange && cheapestExchange?.price !== 0
|
||||
? cheapestExchange.price
|
||||
: 999999999999
|
||||
)
|
||||
@ -105,13 +107,15 @@ export async function getBestDataTokenPrice(
|
||||
return {
|
||||
type: 'pool',
|
||||
address: cheapestPool?.address,
|
||||
value: cheapestPool?.price
|
||||
value: cheapestPool?.price,
|
||||
ocean: cheapestPool?.ocean,
|
||||
datatoken: cheapestPool?.datatoken
|
||||
} as BestPrice
|
||||
} else {
|
||||
return {
|
||||
type: 'exchange',
|
||||
address: cheapestExchange?.address,
|
||||
value: cheapestExchange?.price
|
||||
value: Number(cheapestExchange?.price)
|
||||
} as BestPrice
|
||||
}
|
||||
}
|
||||
@ -121,18 +125,14 @@ export async function checkAndBuyDT(
|
||||
dataTokenAddress: string,
|
||||
account: Account,
|
||||
config: Config
|
||||
) {
|
||||
): Promise<TransactionReceipt | undefined> {
|
||||
const userOwnedTokens = await ocean.accounts.getTokenBalance(
|
||||
dataTokenAddress,
|
||||
account
|
||||
)
|
||||
Logger.log(`User has ${userOwnedTokens} tokens`)
|
||||
if (userOwnedTokens === '0') {
|
||||
const bestPrice = await getBestDataTokenPrice(
|
||||
ocean,
|
||||
dataTokenAddress,
|
||||
account.getId()
|
||||
)
|
||||
const bestPrice = await getBestDataTokenPrice(ocean, dataTokenAddress)
|
||||
|
||||
switch (bestPrice?.type) {
|
||||
case 'pool': {
|
||||
@ -152,19 +152,19 @@ export async function checkAndBuyDT(
|
||||
case 'exchange': {
|
||||
if (!config.oceanTokenAddress) {
|
||||
Logger.error(`'oceanTokenAddress' not set in config`)
|
||||
return null
|
||||
return
|
||||
}
|
||||
|
||||
if (!config.fixedRateExchangeAddress) {
|
||||
Logger.error(`'fixedRateExchangeAddress' not set in config`)
|
||||
return null
|
||||
return
|
||||
}
|
||||
|
||||
Logger.log('Buying token from exchange', bestPrice, account.getId())
|
||||
await ocean.datatokens.approve(
|
||||
config.oceanTokenAddress,
|
||||
config.fixedRateExchangeAddress,
|
||||
bestPrice.value,
|
||||
bestPrice.value.toString(),
|
||||
account.getId()
|
||||
)
|
||||
const exchange = await ocean.fixedRateExchange.buyDT(
|
||||
|
Loading…
x
Reference in New Issue
Block a user