1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-11-13 16:54:53 +01:00

set isConsumable to true by default (#523)

* set isConsumable to true by default

* kick out dtUtils, cleanup

* cleanup

* tweak AssetTeaser price check
This commit is contained in:
Matthias Kretschmann 2021-04-15 13:47:32 +02:00 committed by GitHub
parent 85a956a8f2
commit a8aebf2fac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 148 deletions

View File

@ -24,17 +24,18 @@ export default function Price({
conversion={conversion}
type={price.type}
/>
) : !price || !price.address || price.address === '' ? (
) : !price || price?.type === '' ? (
<div className={styles.empty}>
No price set{' '}
<Tooltip content="No pricing mechanism has been set on this asset yet." />
</div>
) : price.isConsumable !== 'true' ? (
<div className={styles.empty}>
Low liquidity{' '}
<Tooltip content="This pool does not have enough liquidity for using this data set." />
</div>
) : (
// TODO: Hacky hack, put back some check for low liquidity
// ) : price.isConsumable !== 'true' ? (
// <div className={styles.empty}>
// Low liquidity{' '}
// <Tooltip content="This pool does not have enough liquidity for using this data set." />
// </div>
<Loader message="Retrieving price..." />
)
}

View File

@ -2,12 +2,10 @@ import { DDO, Logger, BestPrice } from '@oceanprotocol/lib'
import { useEffect, useState } from 'react'
import { TransactionReceipt } from 'web3-core'
import { Decimal } from 'decimal.js'
import { getFirstPoolPrice } from '../utils/dtUtils'
import {
getCreatePricingPoolFeedback,
getCreatePricingExchangeFeedback,
getBuyDTFeedback,
getSellDTFeedback
getBuyDTFeedback
} from '../utils/feedback'
import { sleep } from '../utils'
@ -29,7 +27,6 @@ interface UsePricing {
createPricing: (
priceOptions: PriceOptions
) => Promise<TransactionReceipt | string | void>
sellDT: (dtAmount: number | string) => Promise<TransactionReceipt | void>
mint: (tokensToMint: string) => Promise<TransactionReceipt | void>
buyDT: (
dtAmount: number | string,
@ -72,7 +69,7 @@ function usePricing(ddo: DDO): UsePricing {
}, [ocean, dataToken, dataTokenInfo])
// Helper for setting steps & feedback for all flows
function setStep(index: number, type: 'pool' | 'exchange' | 'buy' | 'sell') {
function setStep(index: number, type: 'pool' | 'exchange' | 'buy') {
setPricingStep(index)
if (!dtSymbol) return
@ -88,9 +85,6 @@ function usePricing(ddo: DDO): UsePricing {
case 'buy':
messages = getBuyDTFeedback(dtSymbol)
break
case 'sell':
messages = getSellDTFeedback(dtSymbol)
break
}
setPricingStepText(messages[index])
@ -193,45 +187,6 @@ function usePricing(ddo: DDO): UsePricing {
return tx
}
async function sellDT(
dtAmount: number | string
): Promise<TransactionReceipt | void> {
if (!ocean || !accountId) return
Decimal.set({ precision: 18 })
if (!config.oceanTokenAddress) {
Logger.error(`'oceanTokenAddress' not set in config`)
return
}
try {
setPricingIsLoading(true)
setPricingError(undefined)
setStep(1, 'sell')
const pool = await getFirstPoolPrice(ocean, dataToken)
if (!pool || pool.value === 0) return
const price = new Decimal(pool.value).times(0.95).toString()
setStep(2, 'sell')
Logger.log('Selling token to pool', pool, accountId, price)
const tx = await ocean.pool.sellDT(
accountId,
pool.address,
`${dtAmount}`,
price
)
setStep(3, 'sell')
Logger.log('DT sell response', tx)
return tx
} catch (error) {
setPricingError(error.message)
Logger.error(error)
} finally {
setStep(0, 'sell')
setPricingStepText(undefined)
setPricingIsLoading(false)
}
}
async function createPricing(
priceOptions: PriceOptions
): Promise<TransactionReceipt | void> {
@ -295,7 +250,6 @@ function usePricing(ddo: DDO): UsePricing {
dtName,
createPricing,
buyDT,
sellDT,
mint,
pricingStep,
pricingStepText,

View File

@ -193,7 +193,8 @@ function AssetProvider({
if (!ddo) return
// Set price & metadata from DDO first
setPrice(ddo.price)
// TODO Hacky hack, temporary™: set isConsumable to true by default since Aquarius can't be trusted.
setPrice({ ...ddo.price, isConsumable: 'true' })
setVariables({ datatoken: ddo?.dataToken.toLowerCase() })
// Get metadata from DDO

View File

@ -1,93 +0,0 @@
import { Ocean, BestPrice, Logger } from '@oceanprotocol/lib'
const priceError: BestPrice = {
type: '',
address: '',
pools: [],
datatoken: 0,
value: 0,
isConsumable: ''
}
export async function getFirstExchangePrice(
ocean: Ocean,
dataTokenAddress: string
): Promise<BestPrice> {
try {
const tokenExchanges = await ocean.fixedRateExchange.searchforDT(
dataTokenAddress,
'1'
)
if (tokenExchanges === undefined || tokenExchanges.length === 0) {
return priceError
}
const [tokenExchange] = tokenExchanges
return {
type: 'exchange',
pools: [],
address: tokenExchange.exchangeID || '',
value: Number(tokenExchange.fixedRate),
ocean: 0,
datatoken: Number(tokenExchange.supply),
isConsumable: Number(tokenExchange.supply) > 0 ? 'true' : 'false'
}
} catch (err) {
Logger.log(err)
return priceError
}
}
export async function getFirstPoolPrice(
ocean: Ocean,
dataTokenAddress: string,
poolAddress?: string
): Promise<BestPrice> {
let firstPoolAddress = poolAddress
if (!poolAddress) {
const tokenPools = await ocean.pool.searchPoolforDT(dataTokenAddress)
if (tokenPools === undefined || tokenPools.length === 0) {
return priceError
}
;[firstPoolAddress] = tokenPools
}
if (!firstPoolAddress) return priceError
const firstPoolPrice = await ocean.pool.calcInGivenOut(
firstPoolAddress,
ocean.pool.oceanAddress,
dataTokenAddress,
'1'
)
const usePrice = await ocean.pool.getOceanNeeded(firstPoolAddress, '1')
const oceanReserve = await ocean.pool.getOceanReserve(firstPoolAddress)
const dtReserve = await ocean.pool.getDTReserve(firstPoolAddress)
return {
type: 'pool',
pools: [firstPoolAddress],
address: firstPoolAddress,
value: Number(firstPoolPrice),
ocean: Number(oceanReserve),
datatoken: Number(dtReserve),
isConsumable: Number(usePrice) > 0 ? 'true' : 'false'
}
}
export async function getDataTokenPrice(
ocean: Ocean,
dataTokenAddress: string,
type: string,
poolAddress?: string
): Promise<BestPrice> {
const price =
type === 'pool'
? await getFirstPoolPrice(ocean, dataTokenAddress, poolAddress)
: await getFirstExchangePrice(ocean, dataTokenAddress)
return price
}