diff --git a/src/@utils/accessDetailsAndPricing.ts b/src/@utils/accessDetailsAndPricing.ts index 5415a6ab5..7ee6bcf34 100644 --- a/src/@utils/accessDetailsAndPricing.ts +++ b/src/@utils/accessDetailsAndPricing.ts @@ -235,6 +235,8 @@ function getValidUntilTime() { */ export async function getOrderPriceAndFees( asset: AssetExtended, + computeEnv: string = null, + computeValidUntil: number = null, accountId?: string ): Promise { const orderPriceAndFee = { @@ -257,16 +259,6 @@ export async function getOrderPriceAndFees( // fetch consume market order fee orderPriceAndFee.consumeMarketOrderFee = appConfig.consumeMarketOrderFee // fetch provider fee - const computeEnvs = - asset.services[0].type === 'compute' - ? await ProviderInstance.getComputeEnvironments( - asset.services[0].serviceEndpoint - ) - : null - const computeEnviroment = - computeEnvs && computeEnvs[0] ? computeEnvs[0].id : null - const computeValidUntil = - asset.services[0].type === 'compute' ? getValidUntilTime() : null const initializeData = await ProviderInstance.initialize( asset.id, @@ -276,7 +268,7 @@ export async function getOrderPriceAndFees( asset.services[0].serviceEndpoint, null, null, - computeEnviroment, + computeEnv, computeValidUntil ) orderPriceAndFee.providerFee = initializeData.providerFee diff --git a/src/@utils/compute.ts b/src/@utils/compute.ts index 46c0decef..ee79a6f69 100644 --- a/src/@utils/compute.ts +++ b/src/@utils/compute.ts @@ -6,7 +6,9 @@ import { LoggerInstance, ComputeAlgorithm, DDO, - Service + Service, + ProviderInstance, + ComputeEnvironment } from '@oceanprotocol/lib' import { CancelToken } from 'axios' import { gql } from 'urql' @@ -116,6 +118,27 @@ export async function isOrderable( return true } +export function getValidUntilTime() { + const mytime = new Date() + mytime.setMinutes(mytime.getMinutes() + 19) + return Math.floor(mytime.getTime() / 1000) +} + +export async function getComputeEnviroment( + asset: Asset +): Promise { + if (asset?.services[0]?.type !== 'compute') return null + try { + const computeEnvs = await ProviderInstance.getComputeEnvironments( + asset.services[0].serviceEndpoint + ) + if (!computeEnvs[0]) return null + return computeEnvs[0] + } catch (e) { + LoggerInstance.error('[compute] Fetch compute enviroment: ', e.message) + } +} + export function getQuerryString( trustedAlgorithmList: PublisherTrustedAlgorithm[], chainId?: number diff --git a/src/@utils/order.ts b/src/@utils/order.ts index 00046bbaa..e62f5ef4f 100644 --- a/src/@utils/order.ts +++ b/src/@utils/order.ts @@ -23,29 +23,24 @@ function getValidUntilTime() { * @param web3 * @param asset * @param accountId + * @param computeEnv + * @param computeValidUntil + * @param computeConsumerAddress * @returns {TransactionReceipt} receipt of the order */ export async function order( web3: Web3, asset: AssetExtended, orderPriceAndFees: OrderPriceAndFees, - accountId: string + accountId: string, + computeEnv: string = null, + computeValidUntil: number = null, + computeConsumerAddress?: string ): Promise { const datatoken = new Datatoken(web3) const config = getOceanConfig(asset.chainId) const { appConfig } = getSiteMetadata() - const computeEnvs = - asset.services[0].type === 'compute' - ? await ProviderInstance.getComputeEnvironments( - asset.services[0].serviceEndpoint - ) - : null - const computeEnviroment = - computeEnvs && computeEnvs[0] ? computeEnvs[0].id : null - const computeValidUntil = - asset.services[0].type === 'compute' ? getValidUntilTime() : null - const initializeData = await ProviderInstance.initialize( asset.id, asset.services[0].id, @@ -54,12 +49,12 @@ export async function order( asset.services[0].serviceEndpoint, null, null, - computeEnviroment, + computeEnv, computeValidUntil ) const orderParams = { - consumer: accountId, + consumer: computeConsumerAddress || accountId, serviceIndex: 0, _providerFee: initializeData.providerFee, _consumeMarketFee: { @@ -102,7 +97,7 @@ export async function order( const tx = await datatoken.startOrder( asset.accessDetails.datatoken.address, accountId, - accountId, + computeConsumerAddress || accountId, 0, initializeData.providerFee ) diff --git a/src/components/Asset/AssetActions/Compute/index.tsx b/src/components/Asset/AssetActions/Compute/index.tsx index fd7e241e1..3d2f0f5d7 100644 --- a/src/components/Asset/AssetActions/Compute/index.tsx +++ b/src/components/Asset/AssetActions/Compute/index.tsx @@ -43,7 +43,10 @@ import { isOrderable, getAlgorithmAssetSelectionList, getAlgorithmsForAsset, - getQuerryString + getQuerryString, + getValidUntilTime, + getComputeEnviromen, + getComputeEnviroment } from '@utils/compute' import AssetSelection, { AssetSelectionAsset @@ -172,8 +175,14 @@ export default function Compute({ async function init() { if (asset?.accessDetails?.addressOrId === ZERO_ADDRESS) return - - const orderPriceAndFees = await getOrderPriceAndFees(asset, ZERO_ADDRESS) + const validUntil = getValidUntilTime() + const computeEnv = await getComputeEnviroment(asset) + const orderPriceAndFees = await getOrderPriceAndFees( + asset, + computeEnv.id, + validUntil, + ZERO_ADDRESS + ) setOrderPriceAndFees(orderPriceAndFees) } init() @@ -192,9 +201,13 @@ export default function Compute({ async function init() { if (selectedAlgorithmAsset?.accessDetails?.addressOrId === ZERO_ADDRESS) return + const validUntil = getValidUntilTime() + const computeEnv = await getComputeEnviroment(selectedAlgorithmAsset) const orderPriceAndFees = await getOrderPriceAndFees( selectedAlgorithmAsset, + computeEnv.id, + validUntil, ZERO_ADDRESS ) setOrderAlgorithmPriceAndFees(orderPriceAndFees) diff --git a/src/components/Asset/AssetActions/Download.tsx b/src/components/Asset/AssetActions/Download.tsx index fd023aaa6..dab916b2a 100644 --- a/src/components/Asset/AssetActions/Download.tsx +++ b/src/components/Asset/AssetActions/Download.tsx @@ -76,7 +76,12 @@ export default function Download({ if (asset?.accessDetails?.addressOrId === ZERO_ADDRESS) return setIsLoading(true) setStatusText('Calculating price including fees.') - const orderPriceAndFees = await getOrderPriceAndFees(asset, ZERO_ADDRESS) + const orderPriceAndFees = await getOrderPriceAndFees( + asset, + null, + null, + ZERO_ADDRESS + ) setOrderPriceAndFees(orderPriceAndFees) setIsLoading(false)