From c780411b7475c6c132e7c38614971e914b7bccae Mon Sep 17 00:00:00 2001 From: Bogdan Fazakas Date: Wed, 2 Nov 2022 17:24:20 +0200 Subject: [PATCH] C2D fixes (#1715) --- src/@types/Compute.d.ts | 5 + src/@utils/dispenser.ts | 8 +- .../Compute/FormComputeDataset.tsx | 116 +++++++++++++----- .../AssetActions/Compute/PriceOutput.tsx | 33 +++-- .../Asset/AssetActions/Compute/index.tsx | 4 +- .../Asset/Edit/EditComputeDataset.tsx | 6 +- 6 files changed, 122 insertions(+), 50 deletions(-) diff --git a/src/@types/Compute.d.ts b/src/@types/Compute.d.ts index 5be008ed1..a34bd93cd 100644 --- a/src/@types/Compute.d.ts +++ b/src/@types/Compute.d.ts @@ -27,4 +27,9 @@ declare global { computeJobs: ComputeJobMetaData[] isLoaded: boolean } + + interface totalPriceMap { + value: string + symbol: string + } } diff --git a/src/@utils/dispenser.ts b/src/@utils/dispenser.ts index f6f74d112..068f4977d 100644 --- a/src/@utils/dispenser.ts +++ b/src/@utils/dispenser.ts @@ -1,18 +1,13 @@ -import { LoggerInstance, Dispenser, Datatoken } from '@oceanprotocol/lib' +import { LoggerInstance, Datatoken } from '@oceanprotocol/lib' import Web3 from 'web3' import { TransactionReceipt } from 'web3-core' export async function setMinterToPublisher( web3: Web3, - dispenserAddress: string, datatokenAddress: string, accountId: string, setError: (msg: string) => void ): Promise { - const dispenserInstance = new Dispenser(dispenserAddress, web3) - const status = await dispenserInstance.status(datatokenAddress) - if (!status?.active) return - const datatokenInstance = new Datatoken(web3) const response = await datatokenInstance.removeMinter( @@ -20,6 +15,7 @@ export async function setMinterToPublisher( accountId, accountId ) + if (!response) { setError('Updating DDO failed.') LoggerInstance.error('Failed at cancelMinter') diff --git a/src/components/Asset/AssetActions/Compute/FormComputeDataset.tsx b/src/components/Asset/AssetActions/Compute/FormComputeDataset.tsx index e9e8fa45b..fff5b1953 100644 --- a/src/components/Asset/AssetActions/Compute/FormComputeDataset.tsx +++ b/src/components/Asset/AssetActions/Compute/FormComputeDataset.tsx @@ -11,11 +11,11 @@ import { useWeb3 } from '@context/Web3' import content from '../../../../../content/pages/startComputeDataset.json' import { Asset } from '@oceanprotocol/lib' import { getAccessDetails } from '@utils/accessDetailsAndPricing' -import Decimal from 'decimal.js' -import { MAX_DECIMALS } from '@utils/constants' import { useMarketMetadata } from '@context/MarketMetadata' import Alert from '@shared/atoms/Alert' import { getTokenBalanceFromSymbol } from '@utils/web3' +import { MAX_DECIMALS } from '@utils/constants' +import Decimal from 'decimal.js' export default function FormStartCompute({ algorithms, @@ -31,7 +31,8 @@ export default function FormStartCompute({ assetTimeout, hasPreviousOrderSelectedComputeAsset, hasDatatokenSelectedComputeAsset, - oceanSymbol, + datasetSymbol, + algorithmSymbol, dtSymbolSelectedComputeAsset, dtBalanceSelectedComputeAsset, selectedComputeAssetType, @@ -57,7 +58,8 @@ export default function FormStartCompute({ assetTimeout: string hasPreviousOrderSelectedComputeAsset?: boolean hasDatatokenSelectedComputeAsset?: boolean - oceanSymbol?: string + datasetSymbol?: string + algorithmSymbol?: string dtSymbolSelectedComputeAsset?: string dtBalanceSelectedComputeAsset?: string selectedComputeAssetType?: string @@ -76,14 +78,14 @@ export default function FormStartCompute({ useFormikContext() const { asset, isAssetNetwork } = useAsset() - const [totalPrice, setTotalPrice] = useState('0') const [datasetOrderPrice, setDatasetOrderPrice] = useState( asset?.accessDetails?.price ) const [algoOrderPrice, setAlgoOrderPrice] = useState( selectedAlgorithmAsset?.accessDetails?.price ) - const [isBalanceSufficient, setIsBalanceSufficient] = useState(false) + const [totalPrices, setTotalPrices] = useState([]) + const [isBalanceSufficient, setIsBalanceSufficient] = useState(true) function getAlgorithmAsset(algorithmId: string): Asset { let assetDdo = null @@ -126,28 +128,73 @@ export default function FormStartCompute({ algoOrderPriceAndFees?.price || selectedAlgorithmAsset?.accessDetails.price ) + const totalPrices: totalPriceMap[] = [] const priceDataset = - hasPreviousOrder || hasDatatoken + !datasetOrderPrice || hasPreviousOrder || hasDatatoken ? new Decimal(0) - : new Decimal( - datasetOrderPriceAndFees?.price || asset.accessDetails.price - ).toDecimalPlaces(MAX_DECIMALS) + : new Decimal(datasetOrderPrice).toDecimalPlaces(MAX_DECIMALS) const priceAlgo = - hasPreviousOrderSelectedComputeAsset || hasDatatokenSelectedComputeAsset + !algoOrderPrice || + hasPreviousOrderSelectedComputeAsset || + hasDatatokenSelectedComputeAsset ? new Decimal(0) - : new Decimal( - algoOrderPriceAndFees?.price || - selectedAlgorithmAsset.accessDetails.price - ).toDecimalPlaces(MAX_DECIMALS) + : new Decimal(algoOrderPrice).toDecimalPlaces(MAX_DECIMALS) const providerFees = providerFeeAmount ? new Decimal(providerFeeAmount).toDecimalPlaces(MAX_DECIMALS) : new Decimal(0) - const totalPrice = priceDataset - .plus(priceAlgo) - .plus(providerFees) - .toDecimalPlaces(MAX_DECIMALS) - .toString() - setTotalPrice(totalPrice) + + if (algorithmSymbol === 'OCEAN') { + let sum = providerFees.add(priceAlgo) + totalPrices.push({ + value: sum.toDecimalPlaces(MAX_DECIMALS).toString(), + symbol: algorithmSymbol + }) + if (algorithmSymbol === datasetSymbol) { + sum = sum.add(priceDataset) + totalPrices[0].value = sum.toDecimalPlaces(MAX_DECIMALS).toString() + } else { + totalPrices.push({ + value: priceDataset.toDecimalPlaces(MAX_DECIMALS).toString(), + symbol: datasetSymbol + }) + } + } else { + if (datasetSymbol === 'OCEAN') { + const sum = providerFees.add(priceDataset) + totalPrices.push({ + value: sum.toDecimalPlaces(MAX_DECIMALS).toString(), + symbol: datasetSymbol + }) + totalPrices.push({ + value: priceAlgo.toDecimalPlaces(MAX_DECIMALS).toString(), + symbol: algorithmSymbol + }) + } else if (datasetSymbol === algorithmSymbol) { + const sum = priceAlgo.add(priceDataset) + totalPrices.push({ + value: sum.toDecimalPlaces(MAX_DECIMALS).toString(), + symbol: algorithmSymbol + }) + totalPrices.push({ + value: providerFees.toDecimalPlaces(MAX_DECIMALS).toString(), + symbol: 'OCEAN' + }) + } else { + totalPrices.push({ + value: priceDataset.toDecimalPlaces(MAX_DECIMALS).toString(), + symbol: datasetSymbol + }) + totalPrices.push({ + value: providerFees.toDecimalPlaces(MAX_DECIMALS).toString(), + symbol: 'OCEAN' + }) + totalPrices.push({ + value: priceAlgo.toDecimalPlaces(MAX_DECIMALS).toString(), + symbol: algorithmSymbol + }) + } + } + setTotalPrices(totalPrices) }, [ asset?.accessDetails, selectedAlgorithmAsset?.accessDetails, @@ -161,16 +208,18 @@ export default function FormStartCompute({ ]) useEffect(() => { - const baseTokenBalance = getTokenBalanceFromSymbol( - balance, - asset?.accessDetails?.baseToken?.symbol - ) - - if (!totalPrice || !baseTokenBalance || !dtBalance) return - setIsBalanceSufficient( - compareAsBN(baseTokenBalance, `${totalPrice}`) || Number(dtBalance) >= 1 - ) - }, [totalPrice, balance, dtBalance, asset?.accessDetails?.baseToken?.symbol]) + totalPrices.forEach((price) => { + const baseTokenBalance = getTokenBalanceFromSymbol(balance, price.symbol) + if (!baseTokenBalance) { + setIsBalanceSufficient(false) + return + } + // if one comparison of baseTokenBalance and token price comparison is false then the state will be false + setIsBalanceSufficient( + isBalanceSufficient && compareAsBN(baseTokenBalance, `${price.value}`) + ) + }) + }, [balance, dtBalance, datasetSymbol, algorithmSymbol]) return (
@@ -201,12 +250,13 @@ export default function FormStartCompute({ selectedComputeAssetTimeout={selectedComputeAssetTimeout} hasDatatokenSelectedComputeAsset={hasDatatokenSelectedComputeAsset} algorithmConsumeDetails={selectedAlgorithmAsset?.accessDetails} - symbol={oceanSymbol} - totalPrice={totalPrice} + symbol={datasetSymbol} + algorithmSymbol={algorithmSymbol} datasetOrderPrice={datasetOrderPrice} algoOrderPrice={algoOrderPrice} providerFeeAmount={providerFeeAmount} validUntil={validUntil} + totalPrices={totalPrices} /> You will pay{' '} - + {totalPrices.map((item, index) => ( +
+ +
+ ))} @@ -106,18 +118,25 @@ export default function PriceOutput({ .toDecimalPlaces(MAX_DECIMALS) .toString()} timeout={selectedComputeAssetTimeout} - symbol={symbol} + symbol={algorithmSymbol} sign="+" type="ALGORITHM" /> - + {totalPrices.map((item, index) => ( + + ))} } /> diff --git a/src/components/Asset/AssetActions/Compute/index.tsx b/src/components/Asset/AssetActions/Compute/index.tsx index cab80cfdb..72e7b0234 100644 --- a/src/components/Asset/AssetActions/Compute/index.tsx +++ b/src/components/Asset/AssetActions/Compute/index.tsx @@ -456,8 +456,8 @@ export default function Compute({ validAlgorithmOrderTx !== undefined } hasDatatokenSelectedComputeAsset={hasAlgoAssetDatatoken} - oceanSymbol={ - asset?.accessDetails?.baseToken?.symbol || + datasetSymbol={asset?.accessDetails?.baseToken?.symbol || 'OCEAN'} + algorithmSymbol={ selectedAlgorithmAsset?.accessDetails?.baseToken?.symbol || 'OCEAN' } diff --git a/src/components/Asset/Edit/EditComputeDataset.tsx b/src/components/Asset/Edit/EditComputeDataset.tsx index 8c86baa26..c7cfdf933 100644 --- a/src/components/Asset/Edit/EditComputeDataset.tsx +++ b/src/components/Asset/Edit/EditComputeDataset.tsx @@ -41,10 +41,12 @@ export default function EditComputeDataset({ async function handleSubmit(values: ComputeEditForm, resetForm: () => void) { try { - if (asset?.accessDetails?.type === 'free') { + if ( + asset?.accessDetails?.type === 'free' && + asset?.accessDetails?.isPurchasable + ) { const tx = await setMinterToPublisher( web3, - asset?.accessDetails?.addressOrId, asset?.accessDetails?.datatoken?.address, accountId, setError