From 21f044e91ed42eb28299fce80b1ea93ddc49326a Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Wed, 21 Oct 2020 13:48:59 +0200 Subject: [PATCH] refactor --- src/hooks/usePricing/usePricing.ts | 55 +++++++++++++------------- src/utils/dtUtils.ts | 62 +----------------------------- 2 files changed, 28 insertions(+), 89 deletions(-) diff --git a/src/hooks/usePricing/usePricing.ts b/src/hooks/usePricing/usePricing.ts index 69f1ed2..580243e 100644 --- a/src/hooks/usePricing/usePricing.ts +++ b/src/hooks/usePricing/usePricing.ts @@ -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() const [pricingStepText, setPricingStepText] = useState() @@ -90,7 +90,9 @@ function usePricing(ddo: DDO): UsePricing { async function buyDT( dtAmount: number | string ): Promise { - 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 { - 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 { - if (!ocean || !account || !accountId || !dtSymbol) return + if (!ocean || !accountId || !dtSymbol) return const { type, dtAmount, price, weightOnDataToken, swapFee } = priceOptions const isPool = type === 'dynamic' diff --git a/src/utils/dtUtils.ts b/src/utils/dtUtils.ts index 9c8b638..38ec580 100644 --- a/src/utils/dtUtils.ts +++ b/src/utils/dtUtils.ts @@ -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 { - 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 - } - } - } -}