1
0
mirror of https://github.com/oceanprotocol/react.git synced 2024-11-22 09:47:06 +01:00
This commit is contained in:
Matthias Kretschmann 2020-10-21 13:48:59 +02:00
parent a9e8c10c58
commit 21f044e91e
Signed by: m
GPG Key ID: 606EEEF3C479A91F
2 changed files with 28 additions and 89 deletions

View File

@ -28,7 +28,7 @@ interface UsePricing {
}
function usePricing(ddo: DDO): UsePricing {
const { ocean, account, accountId, config } = useOcean()
const { ocean, accountId, config } = useOcean()
const [pricingIsLoading, setPricingIsLoading] = useState(false)
const [pricingStep, setPricingStep] = useState<number>()
const [pricingStepText, setPricingStepText] = useState<string>()
@ -90,7 +90,9 @@ function usePricing(ddo: DDO): UsePricing {
async function buyDT(
dtAmount: number | string
): Promise<TransactionReceipt | void> {
if (!ocean || !account || !accountId) return
if (!ocean || !accountId) return
let tx
try {
setPricingIsLoading(true)
@ -103,22 +105,17 @@ function usePricing(ddo: DDO): UsePricing {
const price = new Decimal(bestPrice.value).times(1.05).toString()
const maxPrice = new Decimal(bestPrice.value).times(2).toString()
setStep(2, 'buy')
Logger.log(
'Buying token from pool',
bestPrice,
account.getId(),
price
)
const buyResponse = await ocean.pool.buyDT(
account.getId(),
Logger.log('Buying token from pool', bestPrice, accountId, price)
tx = await ocean.pool.buyDT(
accountId,
bestPrice.address,
String(dtAmount),
price,
maxPrice
)
setStep(3, 'buy')
Logger.log('DT buy response', buyResponse)
return buyResponse
Logger.log('DT buy response', tx)
break
}
case 'exchange': {
if (!config.oceanTokenAddress) {
@ -129,22 +126,22 @@ function usePricing(ddo: DDO): UsePricing {
Logger.error(`'fixedRateExchangeAddress' not set in config`)
return
}
Logger.log('Buying token from exchange', bestPrice, account.getId())
Logger.log('Buying token from exchange', bestPrice, accountId)
await ocean.datatokens.approve(
config.oceanTokenAddress,
config.fixedRateExchangeAddress,
bestPrice.value.toString(),
account.getId()
`${bestPrice.value}`,
accountId
)
setStep(2, 'buy')
const exchange = await ocean.fixedRateExchange.buyDT(
tx = await ocean.fixedRateExchange.buyDT(
bestPrice.address,
String(dtAmount),
account.getId()
`${dtAmount}`,
accountId
)
setStep(3, 'buy')
Logger.log('DT exchange buy response', exchange)
return exchange
Logger.log('DT exchange buy response', tx)
break
}
}
} catch (error) {
@ -155,12 +152,14 @@ function usePricing(ddo: DDO): UsePricing {
setPricingStepText(undefined)
setPricingIsLoading(false)
}
return tx
}
async function sellDT(
dtAmount: number | string
): Promise<TransactionReceipt | void> {
if (!ocean || !account || !accountId) return
if (!ocean || !accountId) return
if (!config.oceanTokenAddress) {
Logger.error(`'oceanTokenAddress' not set in config`)
@ -175,16 +174,16 @@ function usePricing(ddo: DDO): UsePricing {
if (!pool || pool.price === 0) return
const price = new Decimal(pool.price).times(0.95).toString()
setStep(2, 'sell')
Logger.log('Selling token to pool', pool, account.getId(), price)
const sellResponse = await ocean.pool.sellDT(
account.getId(),
Logger.log('Selling token to pool', pool, accountId, price)
const tx = await ocean.pool.sellDT(
accountId,
pool.address,
String(dtAmount),
`${dtAmount}`,
price
)
setStep(3, 'sell')
Logger.log('DT sell response', sellResponse)
return sellResponse
Logger.log('DT sell response', tx)
return tx
} catch (error) {
setPricingError(error.message)
Logger.error(error)
@ -198,7 +197,7 @@ function usePricing(ddo: DDO): UsePricing {
async function createPricing(
priceOptions: PriceOptions
): Promise<TransactionReceipt | string | void> {
if (!ocean || !account || !accountId || !dtSymbol) return
if (!ocean || !accountId || !dtSymbol) return
const { type, dtAmount, price, weightOnDataToken, swapFee } = priceOptions
const isPool = type === 'dynamic'

View File

@ -1,5 +1,4 @@
import { Logger, Ocean, Account, Config, BestPrice } from '@oceanprotocol/lib'
import { TransactionReceipt } from 'web3-core'
import { Logger, Ocean, BestPrice } from '@oceanprotocol/lib'
import { Decimal } from 'decimal.js'
import Pool from 'hooks/useMetadata/Pool'
import Web3 from 'web3'
@ -146,62 +145,3 @@ export async function getBestDataTokenPrice(
} as BestPrice
}
}
export async function checkAndBuyDT(
ocean: Ocean,
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)
switch (bestPrice?.type) {
case 'pool': {
const price = new Decimal(bestPrice.value).times(1.05).toString()
const maxPrice = new Decimal(bestPrice.value).times(2).toString()
Logger.log('Buying token from pool', bestPrice, account.getId(), price)
const buyResponse = await ocean.pool.buyDT(
account.getId(),
bestPrice.address,
'1',
price,
maxPrice
)
Logger.log('DT buy response', buyResponse)
return buyResponse
}
case 'exchange': {
if (!config.oceanTokenAddress) {
Logger.error(`'oceanTokenAddress' not set in config`)
return
}
if (!config.fixedRateExchangeAddress) {
Logger.error(`'fixedRateExchangeAddress' not set in config`)
return
}
Logger.log('Buying token from exchange', bestPrice, account.getId())
await ocean.datatokens.approve(
config.oceanTokenAddress,
config.fixedRateExchangeAddress,
bestPrice.value.toString(),
account.getId()
)
const exchange = await ocean.fixedRateExchange.buyDT(
bestPrice.address,
'1',
account.getId()
)
Logger.log('DT exchange buy response', exchange)
return exchange
}
}
}
}