mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
new compute.order method
This commit is contained in:
parent
8c4465eeb0
commit
d0ab7f6c14
@ -365,7 +365,7 @@ export class OceanAssets extends Instantiable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the owner of a asset.
|
* Returns the owner of an asset.
|
||||||
* @param {string} did Decentralized ID.
|
* @param {string} did Decentralized ID.
|
||||||
* @return {Promise<string>} Returns Account ID
|
* @return {Promise<string>} Returns Account ID
|
||||||
*/
|
*/
|
||||||
|
@ -2,6 +2,8 @@ import { Instantiable, InstantiableConfig } from '../Instantiable.abstract'
|
|||||||
import { MetaData } from '../ddo/MetaData'
|
import { MetaData } from '../ddo/MetaData'
|
||||||
import Account from './Account'
|
import Account from './Account'
|
||||||
import { DDO } from '../ddo/DDO'
|
import { DDO } from '../ddo/DDO'
|
||||||
|
import { SubscribablePromise, generateId, zeroX } from '../utils'
|
||||||
|
import { OrderProgressStep } from './OceanAssets'
|
||||||
|
|
||||||
export interface ComputeJobStatus {
|
export interface ComputeJobStatus {
|
||||||
owner: string
|
owner: string
|
||||||
@ -38,19 +40,79 @@ export class OceanCompute extends Instantiable {
|
|||||||
* Starts an order of a compute service that is defined in an asset's services.
|
* Starts an order of a compute service that is defined in an asset's services.
|
||||||
* @param {Account} consumerAccount The account of the consumer ordering the service.
|
* @param {Account} consumerAccount The account of the consumer ordering the service.
|
||||||
* @param {string} datasetDid The DID of the dataset asset (of type `dataset`) to run the algorithm on.
|
* @param {string} datasetDid The DID of the dataset asset (of type `dataset`) to run the algorithm on.
|
||||||
* @return {Promise<string>} Returns The service agreement ID, representation of `bytes32` ID.
|
* @return {Promise<string>} Returns the Service Agreement ID, representation of `bytes32` ID.
|
||||||
*/
|
*/
|
||||||
public async order(consumerAccount: Account, datasetDid: string): Promise<string> {
|
public order(
|
||||||
const ddo: DDO = await this.ocean.assets.resolve(datasetDid)
|
consumerAccount: Account,
|
||||||
const { index } = ddo.findServiceByType('compute')
|
datasetDid: string
|
||||||
|
): SubscribablePromise<OrderProgressStep, string> {
|
||||||
|
return new SubscribablePromise(async observer => {
|
||||||
|
const { keeper, assets, agreements } = this.ocean
|
||||||
|
|
||||||
const agreementId = await this.ocean.assets.order(
|
const agreementId = zeroX(generateId())
|
||||||
datasetDid,
|
const ddo: DDO = await assets.resolve(datasetDid)
|
||||||
index,
|
const { index, attributes } = ddo.findServiceByType('compute')
|
||||||
consumerAccount
|
|
||||||
)
|
|
||||||
|
|
||||||
return agreementId
|
const templateName = attributes.main.serviceAgreementTemplate.contractName
|
||||||
|
const template = keeper.getTemplateByName(templateName)
|
||||||
|
const computeCondition = keeper.conditions.computeExecutionCondition
|
||||||
|
|
||||||
|
// eslint-disable-next-line no-async-promise-executor
|
||||||
|
const paymentFlow = new Promise(async (resolve, reject) => {
|
||||||
|
await template.getAgreementCreatedEvent(agreementId).once()
|
||||||
|
|
||||||
|
this.logger.log('Agreement initialized')
|
||||||
|
observer.next(OrderProgressStep.AgreementInitialized)
|
||||||
|
|
||||||
|
this.logger.log('Locking payment')
|
||||||
|
|
||||||
|
const computeGranted = computeCondition
|
||||||
|
.getConditionFulfilledEvent(agreementId)
|
||||||
|
.once()
|
||||||
|
|
||||||
|
observer.next(OrderProgressStep.LockingPayment)
|
||||||
|
const paid = await agreements.conditions.lockReward(
|
||||||
|
agreementId,
|
||||||
|
attributes.main.price,
|
||||||
|
consumerAccount
|
||||||
|
)
|
||||||
|
observer.next(OrderProgressStep.LockedPayment)
|
||||||
|
|
||||||
|
if (paid) {
|
||||||
|
this.logger.log('Payment was OK')
|
||||||
|
} else {
|
||||||
|
this.logger.error('Payment was KO')
|
||||||
|
this.logger.error('Agreement ID: ', agreementId)
|
||||||
|
this.logger.error('DID: ', ddo.id)
|
||||||
|
reject(new Error('Error on payment'))
|
||||||
|
}
|
||||||
|
|
||||||
|
await computeGranted
|
||||||
|
|
||||||
|
this.logger.log('Compute granted')
|
||||||
|
resolve()
|
||||||
|
})
|
||||||
|
|
||||||
|
observer.next(OrderProgressStep.CreatingAgreement)
|
||||||
|
this.logger.log('Creating agreement')
|
||||||
|
await agreements.create(
|
||||||
|
datasetDid,
|
||||||
|
agreementId,
|
||||||
|
index,
|
||||||
|
undefined,
|
||||||
|
consumerAccount,
|
||||||
|
consumerAccount
|
||||||
|
)
|
||||||
|
this.logger.log('Agreement created')
|
||||||
|
|
||||||
|
try {
|
||||||
|
await paymentFlow
|
||||||
|
} catch (e) {
|
||||||
|
throw new Error('Error paying the compute service.')
|
||||||
|
}
|
||||||
|
|
||||||
|
return agreementId
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user