1
0
mirror of https://github.com/oceanprotocol/react.git synced 2024-11-22 17:50:15 +01:00

Merge pull request #159 from oceanprotocol/feature/usePreviousValidOrder

usePreviousOrder
This commit is contained in:
mihaisc 2020-10-23 13:55:59 +03:00 committed by GitHub
commit 6d68bf1db7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 54 deletions

View File

@ -56,47 +56,49 @@ function useCompute(): UseCompute {
dataTokenAddress: string, dataTokenAddress: string,
algorithmRawCode: string, algorithmRawCode: string,
computeContainer: ComputeValue, computeContainer: ComputeValue,
marketFeeAddress?: string marketFeeAddress?: string,
orderId?: string
): Promise<ComputeJob | void> { ): Promise<ComputeJob | void> {
if (!ocean || !account) return if (!ocean || !account) return
setComputeError(undefined) setComputeError(undefined)
try { try {
setIsLoading(true) setIsLoading(true)
setStep(0) setStep(0)
rawAlgorithmMeta.container = computeContainer
const userOwnedTokens = await ocean.accounts.getTokenBalance( rawAlgorithmMeta.rawcode = algorithmRawCode
dataTokenAddress, const output = {}
account if (!orderId) {
) const userOwnedTokens = await ocean.accounts.getTokenBalance(
if (parseFloat(userOwnedTokens) < 1) { dataTokenAddress,
setComputeError('Not enough datatokens') account
} else {
rawAlgorithmMeta.container = computeContainer
rawAlgorithmMeta.rawcode = algorithmRawCode
const output = {}
Logger.log(
'compute order',
accountId,
did,
computeService,
rawAlgorithmMeta,
marketFeeAddress
) )
const tokenTransfer = await ocean.compute.order( if (parseFloat(userOwnedTokens) < 1) {
accountId, setComputeError('Not enough datatokens')
did, } else {
computeService.index, Logger.log(
undefined, 'compute order',
rawAlgorithmMeta, accountId,
marketFeeAddress did,
) computeService,
setStep(1) rawAlgorithmMeta,
setStep(2) 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( const response = await ocean.compute.start(
did, did,
tokenTransfer, orderId,
dataTokenAddress, dataTokenAddress,
account, account,
undefined, undefined,

View File

@ -32,7 +32,8 @@ function useConsume(): UseConsume {
did: DID | string, did: DID | string,
dataTokenAddress: string, dataTokenAddress: string,
serviceType: ServiceType = 'access', serviceType: ServiceType = 'access',
marketFeeAddress: string marketFeeAddress: string,
orderId?: string
): Promise<void> { ): Promise<void> {
if (!ocean || !account || !accountId) return if (!ocean || !account || !accountId) return
@ -41,34 +42,37 @@ function useConsume(): UseConsume {
try { try {
setStep(0) setStep(0)
const userOwnedTokens = await ocean.accounts.getTokenBalance( if (!orderId) {
dataTokenAddress, // if we don't have a previous valid order, get one
account const userOwnedTokens = await ocean.accounts.getTokenBalance(
) dataTokenAddress,
if (parseFloat(userOwnedTokens) < 1) { account
setConsumeError('Not enough datatokens')
} else {
setStep(1)
ocean.datatokens.generateDtName()
const tokenTransfer = await ocean.assets.order(
did as string,
serviceType,
accountId,
undefined,
marketFeeAddress
) )
Logger.log('order created', tokenTransfer) if (parseFloat(userOwnedTokens) < 1) {
setStep(2) setConsumeError('Not enough datatokens')
setStep(3) } 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( await ocean.assets.download(
did as string, did as string,
tokenTransfer, orderId,
dataTokenAddress, dataTokenAddress,
account, account,
'' ''
) )
setStep(4) setStep(4)
}
} catch (error) { } catch (error) {
setConsumeError(error.message) setConsumeError(error.message)
Logger.error(error) Logger.error(error)