1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-12-02 05:57:29 +01:00

Merge branch 'main' into feature/compute

This commit is contained in:
Matthias Kretschmann 2021-04-15 13:50:07 +02:00
commit 4a83732255
Signed by: m
GPG Key ID: 606EEEF3C479A91F
5 changed files with 17 additions and 156 deletions

View File

@ -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..." />
) )
} }

View File

@ -2,12 +2,10 @@ import { DDO, Logger, BestPrice } from '@oceanprotocol/lib'
import { useState } from 'react' import { 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'
@ -30,10 +28,6 @@ interface UsePricing {
priceOptions: PriceOptions, priceOptions: PriceOptions,
ddo: DDO ddo: DDO
) => Promise<TransactionReceipt | string | void> ) => Promise<TransactionReceipt | string | void>
sellDT: (
dtAmount: number | string,
ddo: DDO
) => Promise<TransactionReceipt | void>
mint: (tokensToMint: string, ddo: DDO) => Promise<TransactionReceipt | void> mint: (tokensToMint: string, ddo: DDO) => Promise<TransactionReceipt | void>
buyDT: ( buyDT: (
dtAmount: number | string, dtAmount: number | string,
@ -56,6 +50,7 @@ function usePricing(): UsePricing {
async function getDTSymbol(ddo: DDO): Promise<string> { async function getDTSymbol(ddo: DDO): Promise<string> {
if (!ocean || !accountId) return if (!ocean || !accountId) return
const { dataToken, dataTokenInfo } = ddo const { dataToken, dataTokenInfo } = ddo
return dataTokenInfo return dataTokenInfo
? dataTokenInfo.symbol ? dataTokenInfo.symbol
@ -73,7 +68,7 @@ function usePricing(): UsePricing {
// Helper for setting steps & feedback for all flows // Helper for setting steps & feedback for all flows
async function setStep( async function setStep(
index: number, index: number,
type: 'pool' | 'exchange' | 'buy' | 'sell', type: 'pool' | 'exchange' | 'buy',
ddo: DDO ddo: DDO
) { ) {
const dtSymbol = await getDTSymbol(ddo) const dtSymbol = await getDTSymbol(ddo)
@ -92,9 +87,6 @@ function usePricing(): 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])
@ -201,47 +193,6 @@ function usePricing(): UsePricing {
return tx return tx
} }
async function sellDT(
dtAmount: number | string,
ddo: DDO
): Promise<TransactionReceipt | void> {
if (!ocean || !accountId) return
Decimal.set({ precision: 18 })
if (!config.oceanTokenAddress) {
Logger.error(`'oceanTokenAddress' not set in config`)
return
}
try {
const { dataToken } = ddo
setPricingIsLoading(true)
setPricingError(undefined)
setStep(1, 'sell', ddo)
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', ddo)
Logger.log('Selling token to pool', pool, accountId, price)
const tx = await ocean.pool.sellDT(
accountId,
pool.address,
`${dtAmount}`,
price
)
setStep(3, 'sell', ddo)
Logger.log('DT sell response', tx)
return tx
} catch (error) {
setPricingError(error.message)
Logger.error(error)
} finally {
setStep(0, 'sell', ddo)
setPricingStepText(undefined)
setPricingIsLoading(false)
}
}
async function createPricing( async function createPricing(
priceOptions: PriceOptions, priceOptions: PriceOptions,
ddo: DDO ddo: DDO
@ -309,7 +260,6 @@ function usePricing(): UsePricing {
getDTName, getDTName,
createPricing, createPricing,
buyDT, buyDT,
sellDT,
mint, mint,
pricingStep, pricingStep,
pricingStepText, pricingStepText,

View File

@ -197,7 +197,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

View File

@ -112,11 +112,13 @@ function Web3Provider({ children }: { children: ReactNode }): ReactElement {
const [block, setBlock] = useState<number>() const [block, setBlock] = useState<number>()
const [isTestnet, setIsTestnet] = useState<boolean>() const [isTestnet, setIsTestnet] = useState<boolean>()
const [accountId, setAccountId] = useState<string>() const [accountId, setAccountId] = useState<string>()
const [web3Loading, setWeb3Loading] = useState<boolean>() const [web3Loading, setWeb3Loading] = useState<boolean>(true)
const connect = useCallback(async () => { const connect = useCallback(async () => {
if (!web3Modal) return if (!web3Modal) {
setWeb3Loading(false)
return
}
try { try {
setWeb3Loading(true) setWeb3Loading(true)
Logger.log('[web3] Connecting Web3...') Logger.log('[web3] Connecting Web3...')

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
}