mirror of
https://github.com/oceanprotocol/market.git
synced 2024-12-02 05:57:29 +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:
parent
85a956a8f2
commit
a8aebf2fac
@ -24,17 +24,18 @@ export default function Price({
|
|||||||
conversion={conversion}
|
conversion={conversion}
|
||||||
type={price.type}
|
type={price.type}
|
||||||
/>
|
/>
|
||||||
) : !price || !price.address || price.address === '' ? (
|
) : !price || price?.type === '' ? (
|
||||||
<div className={styles.empty}>
|
<div className={styles.empty}>
|
||||||
No price set{' '}
|
No price set{' '}
|
||||||
<Tooltip content="No pricing mechanism has been set on this asset yet." />
|
<Tooltip content="No pricing mechanism has been set on this asset yet." />
|
||||||
</div>
|
</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..." />
|
<Loader message="Retrieving price..." />
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -2,12 +2,10 @@ import { DDO, Logger, BestPrice } from '@oceanprotocol/lib'
|
|||||||
import { useEffect, useState } from 'react'
|
import { useEffect, useState } from 'react'
|
||||||
import { TransactionReceipt } from 'web3-core'
|
import { TransactionReceipt } from 'web3-core'
|
||||||
import { Decimal } from 'decimal.js'
|
import { Decimal } from 'decimal.js'
|
||||||
import { getFirstPoolPrice } from '../utils/dtUtils'
|
|
||||||
import {
|
import {
|
||||||
getCreatePricingPoolFeedback,
|
getCreatePricingPoolFeedback,
|
||||||
getCreatePricingExchangeFeedback,
|
getCreatePricingExchangeFeedback,
|
||||||
getBuyDTFeedback,
|
getBuyDTFeedback
|
||||||
getSellDTFeedback
|
|
||||||
} from '../utils/feedback'
|
} from '../utils/feedback'
|
||||||
import { sleep } from '../utils'
|
import { sleep } from '../utils'
|
||||||
|
|
||||||
@ -29,7 +27,6 @@ interface UsePricing {
|
|||||||
createPricing: (
|
createPricing: (
|
||||||
priceOptions: PriceOptions
|
priceOptions: PriceOptions
|
||||||
) => Promise<TransactionReceipt | string | void>
|
) => Promise<TransactionReceipt | string | void>
|
||||||
sellDT: (dtAmount: number | string) => Promise<TransactionReceipt | void>
|
|
||||||
mint: (tokensToMint: string) => Promise<TransactionReceipt | void>
|
mint: (tokensToMint: string) => Promise<TransactionReceipt | void>
|
||||||
buyDT: (
|
buyDT: (
|
||||||
dtAmount: number | string,
|
dtAmount: number | string,
|
||||||
@ -72,7 +69,7 @@ function usePricing(ddo: DDO): UsePricing {
|
|||||||
}, [ocean, dataToken, dataTokenInfo])
|
}, [ocean, dataToken, dataTokenInfo])
|
||||||
|
|
||||||
// Helper for setting steps & feedback for all flows
|
// 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)
|
setPricingStep(index)
|
||||||
if (!dtSymbol) return
|
if (!dtSymbol) return
|
||||||
|
|
||||||
@ -88,9 +85,6 @@ function usePricing(ddo: DDO): UsePricing {
|
|||||||
case 'buy':
|
case 'buy':
|
||||||
messages = getBuyDTFeedback(dtSymbol)
|
messages = getBuyDTFeedback(dtSymbol)
|
||||||
break
|
break
|
||||||
case 'sell':
|
|
||||||
messages = getSellDTFeedback(dtSymbol)
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setPricingStepText(messages[index])
|
setPricingStepText(messages[index])
|
||||||
@ -193,45 +187,6 @@ function usePricing(ddo: DDO): UsePricing {
|
|||||||
return tx
|
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(
|
async function createPricing(
|
||||||
priceOptions: PriceOptions
|
priceOptions: PriceOptions
|
||||||
): Promise<TransactionReceipt | void> {
|
): Promise<TransactionReceipt | void> {
|
||||||
@ -295,7 +250,6 @@ function usePricing(ddo: DDO): UsePricing {
|
|||||||
dtName,
|
dtName,
|
||||||
createPricing,
|
createPricing,
|
||||||
buyDT,
|
buyDT,
|
||||||
sellDT,
|
|
||||||
mint,
|
mint,
|
||||||
pricingStep,
|
pricingStep,
|
||||||
pricingStepText,
|
pricingStepText,
|
||||||
|
@ -193,7 +193,8 @@ function AssetProvider({
|
|||||||
if (!ddo) return
|
if (!ddo) return
|
||||||
|
|
||||||
// Set price & metadata from DDO first
|
// 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() })
|
setVariables({ datatoken: ddo?.dataToken.toLowerCase() })
|
||||||
|
|
||||||
// Get metadata from DDO
|
// Get metadata from DDO
|
||||||
|
@ -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
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user