From d9f8be5ca540e1fc3481fa5c62b0f965ff488de3 Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Fri, 23 Oct 2020 02:02:25 -0700 Subject: [PATCH] usePreviousOrder --- src/hooks/useCompute/useCompute.ts | 66 +++++++++++++++--------------- src/hooks/useConsume/useConsume.ts | 48 ++++++++++++---------- 2 files changed, 60 insertions(+), 54 deletions(-) diff --git a/src/hooks/useCompute/useCompute.ts b/src/hooks/useCompute/useCompute.ts index 25bb1a6..d73c14e 100644 --- a/src/hooks/useCompute/useCompute.ts +++ b/src/hooks/useCompute/useCompute.ts @@ -56,47 +56,49 @@ function useCompute(): UseCompute { dataTokenAddress: string, algorithmRawCode: string, computeContainer: ComputeValue, - marketFeeAddress?: string + marketFeeAddress?: string, + orderId?: string ): Promise { if (!ocean || !account) return - setComputeError(undefined) - try { setIsLoading(true) setStep(0) - - const userOwnedTokens = await ocean.accounts.getTokenBalance( - dataTokenAddress, - account - ) - if (parseFloat(userOwnedTokens) < 1) { - setComputeError('Not enough datatokens') - } else { - rawAlgorithmMeta.container = computeContainer - rawAlgorithmMeta.rawcode = algorithmRawCode - const output = {} - Logger.log( - 'compute order', - accountId, - did, - computeService, - rawAlgorithmMeta, - marketFeeAddress + rawAlgorithmMeta.container = computeContainer + rawAlgorithmMeta.rawcode = algorithmRawCode + const output = {} + if (!orderId) { + const userOwnedTokens = await ocean.accounts.getTokenBalance( + dataTokenAddress, + account ) - const tokenTransfer = await ocean.compute.order( - accountId, - did, - computeService.index, - undefined, - rawAlgorithmMeta, - marketFeeAddress - ) - setStep(1) - setStep(2) + if (parseFloat(userOwnedTokens) < 1) { + setComputeError('Not enough datatokens') + } else { + Logger.log( + 'compute order', + accountId, + did, + computeService, + rawAlgorithmMeta, + marketFeeAddress + ) + orderId = await ocean.compute.order( + accountId, + did, + computeService.index, + undefined, + rawAlgorithmMeta, + marketFeeAddress + ) + setStep(1) + } + } + setStep(2) + if (orderId) { const response = await ocean.compute.start( did, - tokenTransfer, + orderId, dataTokenAddress, account, undefined, diff --git a/src/hooks/useConsume/useConsume.ts b/src/hooks/useConsume/useConsume.ts index f4021d2..3fca74a 100644 --- a/src/hooks/useConsume/useConsume.ts +++ b/src/hooks/useConsume/useConsume.ts @@ -32,7 +32,8 @@ function useConsume(): UseConsume { did: DID | string, dataTokenAddress: string, serviceType: ServiceType = 'access', - marketFeeAddress: string + marketFeeAddress: string, + orderId?: string ): Promise { if (!ocean || !account || !accountId) return @@ -41,34 +42,37 @@ function useConsume(): UseConsume { try { setStep(0) - const userOwnedTokens = await ocean.accounts.getTokenBalance( - dataTokenAddress, - account - ) - if (parseFloat(userOwnedTokens) < 1) { - setConsumeError('Not enough datatokens') - } else { - setStep(1) - ocean.datatokens.generateDtName() - const tokenTransfer = await ocean.assets.order( - did as string, - serviceType, - accountId, - undefined, - marketFeeAddress + if (!orderId) { + // if we don't have a previous valid order, get one + const userOwnedTokens = await ocean.accounts.getTokenBalance( + dataTokenAddress, + account ) - Logger.log('order created', tokenTransfer) - setStep(2) - setStep(3) + if (parseFloat(userOwnedTokens) < 1) { + setConsumeError('Not enough datatokens') + } else { + setStep(1) + orderId = await ocean.assets.order( + did as string, + serviceType, + accountId, + undefined, + marketFeeAddress + ) + Logger.log('order created', orderId) + setStep(2) + } + } + setStep(3) + if (orderId) await ocean.assets.download( did as string, - tokenTransfer, + orderId, dataTokenAddress, account, '' ) - setStep(4) - } + setStep(4) } catch (error) { setConsumeError(error.message) Logger.error(error)