BestPrice updates

This commit is contained in:
Matthias Kretschmann 2020-10-08 12:56:40 +02:00
parent c98a76cbe1
commit 55326352fa
Signed by: m
GPG Key ID: 606EEEF3C479A91F
7 changed files with 36 additions and 45 deletions

6
package-lock.json generated
View File

@ -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",

View File

@ -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",

View File

@ -1,5 +0,0 @@
export default interface BestPrice {
type: 'pool' | 'exchange'
address: string
value: string
}

View File

@ -1,4 +1,6 @@
export default interface Pool {
address: string
price: string
price: number
ocean?: number
datatoken?: number
}

View File

@ -1,3 +1,2 @@
export * from './useMetadata'
export * from './Pool'
export * from './BestPrice'

View File

@ -1,10 +1,9 @@
import { useState, useEffect, useCallback } from 'react'
import { DID, DDO, Metadata, Logger } from '@oceanprotocol/lib'
import { DID, DDO, Metadata, Logger, BestPrice } 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
@ -35,14 +34,10 @@ function useMetadata(asset?: DID | string | DDO): UseMetadata {
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> => {

View File

@ -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(