From da934f789fd547591560110a6e14253082d502fb Mon Sep 17 00:00:00 2001 From: Enzo Vezzaro Date: Fri, 17 Jun 2022 08:51:16 -0400 Subject: [PATCH] adding dummyweb3 and zero_address to calculate price on unsupported network / not connected user --- src/@utils/assetConvertor.ts | 3 +-- src/components/@shared/ButtonBuy/index.tsx | 19 +++++++++++++------ .../Compute/FormComputeDataset.tsx | 15 +++++++++------ .../Asset/AssetActions/Compute/index.tsx | 17 ++++++++++------- 4 files changed, 33 insertions(+), 21 deletions(-) diff --git a/src/@utils/assetConvertor.ts b/src/@utils/assetConvertor.ts index 2536060d2..224ff0fd7 100644 --- a/src/@utils/assetConvertor.ts +++ b/src/@utils/assetConvertor.ts @@ -16,9 +16,8 @@ export async function transformAssetToAssetSelection( for (const asset of extendedAssets) { const algoComputeService = getServiceByName(asset, 'compute') - if ( - asset?.accessDetails.price && + asset?.accessDetails?.price && algoComputeService?.serviceEndpoint === datasetProviderEndpoint ) { let selected = false diff --git a/src/components/@shared/ButtonBuy/index.tsx b/src/components/@shared/ButtonBuy/index.tsx index de9bee420..cc0250aff 100644 --- a/src/components/@shared/ButtonBuy/index.tsx +++ b/src/components/@shared/ButtonBuy/index.tsx @@ -2,6 +2,7 @@ import React, { FormEvent, ReactElement } from 'react' import Button from '../atoms/Button' import styles from './index.module.css' import Loader from '../atoms/Loader' +import { useWeb3 } from '@context/Web3' interface ButtonBuyProps { action: 'download' | 'compute' @@ -43,12 +44,13 @@ function getConsumeHelpText( isConsumable: boolean, isBalanceSufficient: boolean, consumableFeedback: string, - isSupportedOceanNetwork: boolean + isSupportedOceanNetwork: boolean, + web3: any ) { const text = isConsumable === false ? consumableFeedback - : hasPreviousOrder + : hasPreviousOrder && web3 ? `You bought this ${assetType} already allowing you to use it without paying again.` : hasDatatoken ? `You own ${dtBalance} ${dtSymbol} allowing you to use this data set by spending 1 ${dtSymbol}, but without paying OCEAN again.` @@ -106,7 +108,8 @@ function getComputeAssetHelpText( selectedComputeAssettLowPoolLiquidity?: boolean, selectedComputeAssetType?: string, isAlgorithmConsumable?: boolean, - isSupportedOceanNetwork?: boolean + isSupportedOceanNetwork?: boolean, + web3?: any ) { const computeAssetHelpText = getConsumeHelpText( dtBalance, @@ -118,7 +121,8 @@ function getComputeAssetHelpText( isConsumable, isBalanceSufficient, consumableFeedback, - isSupportedOceanNetwork + isSupportedOceanNetwork, + web3 ) const computeAlgoHelpText = getAlgoHelpText( @@ -170,6 +174,7 @@ export default function ButtonBuy({ isAlgorithmConsumable, isSupportedOceanNetwork }: ButtonBuyProps): ReactElement { + const { web3 } = useWeb3() const buttonText = action === 'download' ? hasPreviousOrder @@ -210,7 +215,8 @@ export default function ButtonBuy({ isConsumable, isBalanceSufficient, consumableFeedback, - isSupportedOceanNetwork + isSupportedOceanNetwork, + web3 ) : getComputeAssetHelpText( hasPreviousOrder, @@ -229,7 +235,8 @@ export default function ButtonBuy({ selectedComputeAssetLowPoolLiquidity, selectedComputeAssetType, isAlgorithmConsumable, - isSupportedOceanNetwork + isSupportedOceanNetwork, + web3 )} diff --git a/src/components/Asset/AssetActions/Compute/FormComputeDataset.tsx b/src/components/Asset/AssetActions/Compute/FormComputeDataset.tsx index 48596bcd5..edf55fac4 100644 --- a/src/components/Asset/AssetActions/Compute/FormComputeDataset.tsx +++ b/src/components/Asset/AssetActions/Compute/FormComputeDataset.tsx @@ -9,7 +9,7 @@ import PriceOutput from './PriceOutput' import { useAsset } from '@context/Asset' import { useWeb3 } from '@context/Web3' import content from '../../../../../content/pages/startComputeDataset.json' -import { Asset } from '@oceanprotocol/lib' +import { Asset, ZERO_ADDRESS } from '@oceanprotocol/lib' import { AccessDetails, OrderPriceAndFees } from 'src/@types/Price' import { getAccessDetailsForAssets, @@ -18,6 +18,7 @@ import { import { AssetExtended } from 'src/@types/AssetExtended' import Decimal from 'decimal.js' import { MAX_DECIMALS } from '@utils/constants' +import { getDummyWeb3 } from '@utils/web3' export default function FormStartCompute({ algorithms, @@ -94,14 +95,14 @@ export default function FormStartCompute({ } useEffect(() => { - if (!values.algorithm || !accountId || !isConsumable) return + if (!values.algorithm || !isConsumable) return async function fetchAlgorithmAssetExtended() { const algorithmAsset = getAlgorithmAsset(values.algorithm) const accessDetails = await getAccessDetails( algorithmAsset.chainId, algorithmAsset.services[0].datatokenAddress, algorithmAsset.services[0].timeout, - accountId + accountId || ZERO_ADDRESS // if user is not connected, use ZERO_ADDRESS as accountId ) const extendedAlgoAsset: AssetExtended = { ...algorithmAsset, @@ -121,6 +122,7 @@ export default function FormStartCompute({ setDatasetOrderPrice( datasetOrderPriceAndFees?.price || asset.accessDetails.price ) + setAlgoOrderPrice( algoOrderPriceAndFees?.price || selectedAlgorithmAsset?.accessDetails.price @@ -142,16 +144,17 @@ export default function FormStartCompute({ .plus(priceAlgo) .toDecimalPlaces(MAX_DECIMALS) .toString() + setTotalPrice(totalPrice) }, [ - asset?.accessDetails, - selectedAlgorithmAsset?.accessDetails, + asset, hasPreviousOrder, hasDatatoken, hasPreviousOrderSelectedComputeAsset, hasDatatokenSelectedComputeAsset, datasetOrderPriceAndFees, - algoOrderPriceAndFees + algoOrderPriceAndFees, + selectedAlgorithmAsset ]) useEffect(() => { diff --git a/src/components/Asset/AssetActions/Compute/index.tsx b/src/components/Asset/AssetActions/Compute/index.tsx index 842287dc2..345114339 100644 --- a/src/components/Asset/AssetActions/Compute/index.tsx +++ b/src/components/Asset/AssetActions/Compute/index.tsx @@ -65,6 +65,7 @@ export default function Compute({ consumableFeedback?: string }): ReactElement { const { accountId, web3 } = useWeb3() + const [isJobStarting, setIsJobStarting] = useState(false) const [error, setError] = useState() const newAbortController = useAbortController() @@ -116,7 +117,7 @@ export default function Compute({ const datatokenInstance = new Datatoken(web3) const dtBalance = await datatokenInstance.balance( asset?.services[0].datatokenAddress, - accountId + accountId || ZERO_ADDRESS // if the user is not connected, we use ZERO_ADDRESS as accountId ) setAlgorithmDTBalance(new Decimal(dtBalance).toString()) const hasAlgoDt = Number(dtBalance) >= 1 @@ -134,7 +135,7 @@ export default function Compute({ const initializedProvider = await initializeProviderForCompute( asset, selectedAlgorithmAsset, - accountId, + accountId || ZERO_ADDRESS, // if the user is not connected, we use ZERO_ADDRESS as accountId computeEnv ) if ( @@ -176,12 +177,13 @@ export default function Compute({ } : null const datasetPriceAndFees = await getOrderPriceAndFees( - web3, + web3 || (await getDummyWeb3(asset?.chainId)), // if the user is not connected, we need to use a dummy web3 asset, - ZERO_ADDRESS, + accountId || ZERO_ADDRESS, // if the user is not connected, we need to use ZERO_ADDRESS as accountId poolParams, initializedProvider?.datasets?.[0]?.providerFee ) + if (!datasetPriceAndFees) { setError('Error setting dataset price and fees!') return @@ -224,12 +226,13 @@ export default function Compute({ } } const algorithmOrderPriceAndFees = await getOrderPriceAndFees( - web3, + web3 || (await getDummyWeb3(asset?.chainId)), selectedAlgorithmAsset, - ZERO_ADDRESS, + accountId || ZERO_ADDRESS, algoPoolParams, initializedProvider.algorithm.providerFee ) + if (!algorithmOrderPriceAndFees) { setError('Error setting algorithm price and fees!') return @@ -247,7 +250,7 @@ export default function Compute({ }, [asset?.accessDetails]) useEffect(() => { - if (!selectedAlgorithmAsset?.accessDetails || !accountId) return + if (!selectedAlgorithmAsset?.accessDetails) return setIsRequestingAlgoOrderPrice(true) setIsConsumablePrice(selectedAlgorithmAsset?.accessDetails?.isPurchasable)