From afd1c2d9db2586affe04500a061c6b36febedd14 Mon Sep 17 00:00:00 2001 From: Bogdan Fazakas Date: Wed, 19 Jan 2022 09:12:59 +0200 Subject: [PATCH] add balance check and check is consumable --- src/@utils/ddo.ts | 23 ++++++++++++++++++- .../Compute/FormComputeDataset.tsx | 8 +++---- .../Asset/AssetActions/Compute/index.tsx | 23 +++++++++++-------- 3 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/@utils/ddo.ts b/src/@utils/ddo.ts index 14ccd87cc..c77df1097 100644 --- a/src/@utils/ddo.ts +++ b/src/@utils/ddo.ts @@ -1,8 +1,13 @@ import { Asset, DDO, Service } from '@oceanprotocol/lib' +interface Consumable { + status: number + message: string + result: boolean +} export function getServiceByName( ddo: Asset | DDO, - name: 'access' | 'compute' + name: 'access' | 'compute' | 'metadata' ): Service { if (!ddo) return @@ -58,3 +63,19 @@ export function secondsToString(numberOfSeconds: number): string { ? `${seconds} second${numberEnding(seconds)}` : 'less than a second' } + +export async function checkIfConsumable(ddo: DDO): Promise { + if (!ddo) throw new Error('ERROR: DDO does not exist') + const allowedConsume = { status: 0, message: 'All good', result: true } + const orderDisabled = { + status: 1, + message: + 'Ordering this asset has been temporarily disabled by the publisher.', + result: false + } + + const metadata = this.getServiceByName(ddo, 'metadata') + if (metadata.attributes.status?.isOrderDisabled) return orderDisabled + + return allowedConsume +} diff --git a/src/components/Asset/AssetActions/Compute/FormComputeDataset.tsx b/src/components/Asset/AssetActions/Compute/FormComputeDataset.tsx index 5bbccf36c..09d1d4f11 100644 --- a/src/components/Asset/AssetActions/Compute/FormComputeDataset.tsx +++ b/src/components/Asset/AssetActions/Compute/FormComputeDataset.tsx @@ -8,6 +8,7 @@ import ButtonBuy from '@shared/ButtonBuy' import PriceOutput from './PriceOutput' import { useAsset } from '@context/Asset' import { useWeb3 } from '@context/Web3' +import { checkIfConsumable } from '@utils/ddo' import content from '../../../../../content/pages/startComputeDataset.json' import { Asset } from '@oceanprotocol/lib' @@ -84,11 +85,8 @@ export default function FormStartCompute({ if (!accountId || !isConsumable) return async function checkIsConsumable() { - // const consumable = await ocean.assets.isConsumable( - // algorithmDDO as any, - // accountId.toLowerCase() - // ) - // if (consumable) setAlgorithmConsumableStatus(consumable.status) + const consumable = await checkIfConsumable(algorithmDDO as any) + if (consumable) setAlgorithmConsumableStatus(consumable.status) } checkIsConsumable() }, [values.algorithm, accountId, isConsumable]) diff --git a/src/components/Asset/AssetActions/Compute/index.tsx b/src/components/Asset/AssetActions/Compute/index.tsx index 773a5c05e..47d31f199 100644 --- a/src/components/Asset/AssetActions/Compute/index.tsx +++ b/src/components/Asset/AssetActions/Compute/index.tsx @@ -6,7 +6,8 @@ import { Asset, DDO, PublisherTrustedAlgorithm, - FileMetadata + FileMetadata, + Datatoken } from '@oceanprotocol/lib' import { toast } from 'react-toastify' import Price from '@shared/Price' @@ -36,6 +37,7 @@ import ComputeJobs from '../../../Profile/History/ComputeJobs' import { useCancelToken } from '@hooks/useCancelToken' import { useIsMounted } from '@hooks/useIsMounted' import { SortTermOptions } from '../../../../@types/aquarius/SearchQuery' +import { Decimal } from 'decimal.js' export default function Compute({ ddo, @@ -55,7 +57,7 @@ export default function Compute({ consumableFeedback?: string }): ReactElement { const { appConfig } = useSiteMetadata() - const { accountId } = useWeb3() + const { accountId, web3 } = useWeb3() const { buyDT, pricingError, pricingStepText } = usePricing() const [isJobStarting, setIsJobStarting] = useState(false) const [error, setError] = useState() @@ -69,7 +71,7 @@ export default function Compute({ const [previousDatasetOrderId, setPreviousDatasetOrderId] = useState() const [hasPreviousAlgorithmOrder, setHasPreviousAlgorithmOrder] = useState(false) - const [algorithmDTBalance, setalgorithmDTBalance] = useState() + const [algorithmDTBalance, setAlgorithmDTBalance] = useState() const [algorithmPrice, setAlgorithmPrice] = useState() const [previousAlgorithmOrderId, setPreviousAlgorithmOrderId] = useState() @@ -110,12 +112,13 @@ export default function Compute({ } async function checkAssetDTBalance(asset: DDO) { - // const AssetDtBalance = await ocean.datatokens.balance( - // asset.services[0].datatokenAddress, - // accountId - // ) - // setalgorithmDTBalance(AssetDtBalance) - // setHasAlgoAssetDatatoken(Number(AssetDtBalance) >= 1) + const datatokenInstance = new Datatoken(web3) + const AssetDtBalance = await datatokenInstance.balance( + asset.services[0].datatokenAddress, + accountId + ) + setAlgorithmDTBalance(new Decimal(AssetDtBalance).toString()) + setHasAlgoAssetDatatoken(Number(AssetDtBalance) >= 1) } function getQuerryString( @@ -230,7 +233,7 @@ export default function Compute({ checkPreviousOrders(selectedAlgorithmAsset) } } - // ocean && checkAssetDTBalance(selectedAlgorithmAsset) + checkAssetDTBalance(selectedAlgorithmAsset) }, [ddo, selectedAlgorithmAsset, accountId, hasPreviousAlgorithmOrder]) // Output errors in toast UI