From fe7dd8e31211b96a21d1888a9b745a2c757c6106 Mon Sep 17 00:00:00 2001 From: Bogdan Fazakas Date: Wed, 5 Apr 2023 17:11:47 +0300 Subject: [PATCH] optimize c2d order logic and fix compute output price tooltip (#1896) * set price updates and tooltip fix * update order * cleanup --- src/@utils/order.ts | 36 +--- src/@utils/provider.ts | 1 - .../AssetActions/Compute/PriceOutput.tsx | 10 +- .../Asset/AssetActions/Compute/index.tsx | 191 +++++++++--------- 4 files changed, 100 insertions(+), 138 deletions(-) diff --git a/src/@utils/order.ts b/src/@utils/order.ts index e6ba5d4c4..62e624078 100644 --- a/src/@utils/order.ts +++ b/src/@utils/order.ts @@ -22,7 +22,6 @@ import { consumeMarketFixedSwapFee } from '../../app.config' import { toast } from 'react-toastify' -import { getEncryptedFiles, getFileInfo } from './provider' async function initializeProvider( asset: AssetExtended, @@ -64,12 +63,6 @@ export async function order( const datatoken = new Datatoken(web3) const config = getOceanConfig(asset.chainId) - const filesEncrypted = await getEncryptedFiles( - asset.services[0].files, - asset.chainId, - asset.services[0].serviceEndpoint - ) - const initializeData = await initializeProvider( asset, accountId, @@ -251,27 +244,6 @@ async function approveProviderFee( return txApproveWei } -async function startOrder( - web3: Web3, - asset: AssetExtended, - orderPriceAndFees: OrderPriceAndFees, - accountId: string, - hasDatatoken: boolean, - initializeData: ProviderComputeInitialize, - computeConsumerAddress?: string -): Promise { - const tx = await order( - web3, - asset, - orderPriceAndFees, - accountId, - initializeData.providerFee, - computeConsumerAddress - ) - LoggerInstance.log('[compute] Asset ordered:', tx) - return tx -} - /** * Handles order for compute assets for the following scenarios: * - have validOrder and no providerFees -> then order is valid, providerFees are valid, it returns the valid order value @@ -291,7 +263,6 @@ export async function handleComputeOrder( asset: AssetExtended, orderPriceAndFees: OrderPriceAndFees, accountId: string, - hasDatatoken: boolean, initializeData: ProviderComputeInitialize, computeConsumerAddress?: string ): Promise { @@ -342,15 +313,16 @@ export async function handleComputeOrder( } LoggerInstance.log('[compute] Calling order ...', initializeData) - const txStartOrder = await startOrder( + + const txStartOrder = await order( web3, asset, orderPriceAndFees, accountId, - hasDatatoken, - initializeData, + initializeData.providerFee, computeConsumerAddress ) + LoggerInstance.log('[compute] Order succeeded', txStartOrder) return txStartOrder?.transactionHash } catch (error) { diff --git a/src/@utils/provider.ts b/src/@utils/provider.ts index ec03ec330..61e0c48c0 100644 --- a/src/@utils/provider.ts +++ b/src/@utils/provider.ts @@ -13,7 +13,6 @@ import { ProviderInstance, UrlFile } from '@oceanprotocol/lib' -import { chainIds } from 'app.config' import { QueryHeader } from '@shared/FormInput/InputElement/Headers' import Web3 from 'web3' import { AbiItem } from 'web3-utils/types' diff --git a/src/components/Asset/AssetActions/Compute/PriceOutput.tsx b/src/components/Asset/AssetActions/Compute/PriceOutput.tsx index 52069e975..73bcc4881 100644 --- a/src/components/Asset/AssetActions/Compute/PriceOutput.tsx +++ b/src/components/Asset/AssetActions/Compute/PriceOutput.tsx @@ -42,21 +42,13 @@ function Row({ sign?: string type?: string }) { - const { isSupportedOceanNetwork } = useWeb3() - return (
{sign}
{type}
{ + if (asset.accessDetails.validProviderFees) { + providerData.datasets[0].providerFee.providerFeeAmount = '0' + } + + const providerFeeToken = + providerData?.datasets?.[0]?.providerFee?.providerFeeToken + const providerFeeAmount = asset.accessDetails.validProviderFees + ? '0' + : providerData?.datasets?.[0]?.providerFee?.providerFeeAmount + const feeValidity = providerData?.datasets?.[0]?.providerFee?.validUntil + + const feeAmount = await unitsToAmount( + !isSupportedOceanNetwork || !isAssetNetwork + ? await getDummyWeb3(asset?.chainId) + : web3, + providerFeeToken, + providerFeeAmount + ) + setProviderFeeAmount(feeAmount) + + const datatoken = new Datatoken( + await getDummyWeb3(asset?.chainId), + null, + null, + minAbi + ) + + setProviderFeesSymbol(await datatoken.getSymbol(providerFeeToken)) + + const computeDuration = asset.accessDetails.validProviderFees + ? asset.accessDetails.validProviderFees.validUntil + : (parseInt(feeValidity) - Math.floor(Date.now() / 1000)).toString() + setComputeValidUntil(computeDuration) + + return providerData + } + + async function setAlgoPrice(algoProviderFees: ProviderFees) { + if ( + selectedAlgorithmAsset?.accessDetails?.addressOrId !== ZERO_ADDRESS && + selectedAlgorithmAsset?.accessDetails?.type !== 'free' && + algoProviderFees + ) { + const algorithmOrderPriceAndFees = await getOrderPriceAndFees( + selectedAlgorithmAsset, + ZERO_ADDRESS, + algoProviderFees + ) + if (!algorithmOrderPriceAndFees) + throw new Error('Error setting algorithm price and fees!') + + setAlgoOrderPriceAndFees(algorithmOrderPriceAndFees) + } + } + + async function setDatasetPrice(datasetProviderFees: ProviderFees) { + if ( + asset?.accessDetails?.addressOrId !== ZERO_ADDRESS && + asset?.accessDetails?.type !== 'free' && + datasetProviderFees + ) { + const datasetPriceAndFees = await getOrderPriceAndFees( + asset, + ZERO_ADDRESS, + datasetProviderFees + ) + if (!datasetPriceAndFees) + throw new Error('Error setting dataset price and fees!') + + setDatasetOrderPriceAndFees(datasetPriceAndFees) + } + } + async function initPriceAndFees() { try { const computeEnv = await getComputeEnviroment(asset) @@ -150,101 +227,25 @@ export default function Compute({ ) throw new Error(`Error initializing provider for the compute job!`) - setInitializedProviderResponse(initializedProvider) - - if ( - asset.accessDetails?.validProviderFees && - initializedProvider?.datasets?.[0]?.providerFee?.providerFeeAmount - ) { - initializedProvider.datasets[0].providerFee = { - providerFeeAmount: '0', - ...asset.accessDetails?.validProviderFees - } - } - - if ( - selectedAlgorithmAsset?.accessDetails?.validProviderFees && - initializedProvider?.algorithm?.providerFee?.providerFeeAmount - ) { - initializedProvider.algorithm.providerFee = { - providerFeeAmount: '0', - ...selectedAlgorithmAsset.accessDetails.validProviderFees - } - } - - const feeAmount = await unitsToAmount( - !isSupportedOceanNetwork || !isAssetNetwork - ? await getDummyWeb3(asset?.chainId) - : web3, - initializedProvider?.datasets?.[0]?.providerFee?.providerFeeToken, - initializedProvider?.datasets?.[0]?.providerFee?.providerFeeAmount + setComputeStatusText( + getComputeFeedback( + asset.accessDetails?.baseToken?.symbol, + asset.accessDetails?.datatoken?.symbol, + asset.metadata.type + )[0] + ) + await setDatasetPrice(initializedProvider?.datasets?.[0]?.providerFee) + setComputeStatusText( + getComputeFeedback( + selectedAlgorithmAsset?.accessDetails?.baseToken?.symbol, + selectedAlgorithmAsset?.accessDetails?.datatoken?.symbol, + selectedAlgorithmAsset?.metadata?.type + )[0] ) - setProviderFeeAmount(feeAmount) - - const datatoken = new Datatoken( - await getDummyWeb3(asset?.chainId), - null, - null, - minAbi - ) - setProviderFeesSymbol( - await datatoken.getSymbol( - initializedProvider?.datasets?.[0]?.providerFee?.providerFeeToken - ) - ) - - const computeDuration = ( - parseInt(initializedProvider?.datasets?.[0]?.providerFee?.validUntil) - - Math.floor(Date.now() / 1000) - ).toString() - setComputeValidUntil(computeDuration) - - if ( - asset?.accessDetails?.addressOrId !== ZERO_ADDRESS && - asset?.accessDetails?.type !== 'free' && - initializedProvider?.datasets?.[0]?.providerFee - ) { - setComputeStatusText( - getComputeFeedback( - asset.accessDetails?.baseToken?.symbol, - asset.accessDetails?.datatoken?.symbol, - asset.metadata.type - )[0] - ) - const datasetPriceAndFees = await getOrderPriceAndFees( - asset, - ZERO_ADDRESS, - initializedProvider?.datasets?.[0]?.providerFee - ) - if (!datasetPriceAndFees) - throw new Error('Error setting dataset price and fees!') - - setDatasetOrderPriceAndFees(datasetPriceAndFees) - } - - if ( - selectedAlgorithmAsset?.accessDetails?.addressOrId !== ZERO_ADDRESS && - selectedAlgorithmAsset?.accessDetails?.type !== 'free' && - initializedProvider?.algorithm?.providerFee - ) { - setComputeStatusText( - getComputeFeedback( - selectedAlgorithmAsset?.accessDetails?.baseToken?.symbol, - selectedAlgorithmAsset?.accessDetails?.datatoken?.symbol, - selectedAlgorithmAsset?.metadata?.type - )[0] - ) - const algorithmOrderPriceAndFees = await getOrderPriceAndFees( - selectedAlgorithmAsset, - ZERO_ADDRESS, - initializedProvider.algorithm.providerFee - ) - if (!algorithmOrderPriceAndFees) - throw new Error('Error setting algorithm price and fees!') - - setAlgoOrderPriceAndFees(algorithmOrderPriceAndFees) - } + await setAlgoPrice(initializedProvider?.algorithm?.providerFee) + const sanitizedResponse = await setComputeFees(initializedProvider) + setInitializedProviderResponse(sanitizedResponse) } catch (error) { setError(error.message) LoggerInstance.error(`[compute] ${error.message} `) @@ -376,7 +377,6 @@ export default function Compute({ selectedAlgorithmAsset, algoOrderPriceAndFees, accountId, - hasAlgoAssetDatatoken, initializedProviderResponse.algorithm, computeEnv.consumerAddress ) @@ -395,7 +395,6 @@ export default function Compute({ asset, datasetOrderPriceAndFees, accountId, - hasDatatoken, initializedProviderResponse.datasets[0], computeEnv.consumerAddress )