diff --git a/integration/ocean/ConsumeAsset.test.ts b/integration/ocean/ConsumeAsset.test.ts index 410e12f..4491e85 100644 --- a/integration/ocean/ConsumeAsset.test.ts +++ b/integration/ocean/ConsumeAsset.test.ts @@ -4,7 +4,7 @@ import * as fs from "fs" import { config } from "../config" import { getMetadata } from "../utils" -import { Ocean, DDO, Account } from "../../src" // @oceanprotocol/squid +import { Ocean, DDO, Account, ConditionState } from "../../src" // @oceanprotocol/squid describe("Consume Asset", () => { let ocean: Ocean @@ -66,6 +66,22 @@ describe("Consume Asset", () => { assert.isTrue(success) }) + it("should get the agreement conditions status not fulfilled", async () => { + const accessService = ddo.findServiceByType("Access") + + const status = await ocean.agreements.status( + ddo.id, + serviceAgreementSignatureResult.agreementId, + accessService.serviceDefinitionId, + ) + + assert.deepEqual(status, { + lockReward: ConditionState.Unfulfilled, + accessSecretStore: ConditionState.Unfulfilled, + escrowReward: ConditionState.Unfulfilled, + }) + }) + it("should lock the payment by the consumer", async () => { const paid = await ocean.agreements.conditions .lockReward( @@ -90,6 +106,22 @@ describe("Consume Asset", () => { assert.isTrue(accessGranted, "Consumer has been granted.") }) + it("should get the agreement conditions status fulfilled", async () => { + const accessService = ddo.findServiceByType("Access") + + const status = await ocean.agreements.status( + ddo.id, + serviceAgreementSignatureResult.agreementId, + accessService.serviceDefinitionId, + ) + + assert.deepEqual(status, { + lockReward: ConditionState.Fulfilled, + accessSecretStore: ConditionState.Fulfilled, + escrowReward: ConditionState.Unfulfilled, + }) + }) + it("should consume and store the assets", async () => { const accessService = ddo.findServiceByType("Access") diff --git a/src/keeper/contracts/templates/AgreementTemplate.abstract.ts b/src/keeper/contracts/templates/AgreementTemplate.abstract.ts index 604e3af..e23f2de 100644 --- a/src/keeper/contracts/templates/AgreementTemplate.abstract.ts +++ b/src/keeper/contracts/templates/AgreementTemplate.abstract.ts @@ -5,6 +5,16 @@ import { ServiceAgreementTemplate } from "../../../ddo/ServiceAgreementTemplate" import { zeroX } from "../../../utils" import { InstantiableConfig } from "../../../Instantiable.abstract" +export interface AgreementConditionsStatus { + [condition: string]: { + condition: string, + contractName: string, + state: ConditionState, + blocked: boolean, + blockedBy: string[], + }, +} + export abstract class AgreementTemplate extends ContractBase { public static async getInstance( @@ -108,15 +118,7 @@ export abstract class AgreementTemplate extends ContractBase { */ public async getAgreementStatus( agreementId: string, - ): Promise<{ - [condition: string]: { - condition: string, - contractName: string, - state: ConditionState, - blocked: boolean, - blockedBy: string[], - }, - } | false> { + ): Promise { const agreementStore = this.ocean.keeper.agreementStoreManager const conditionStore = this.ocean.keeper.conditionStoreManager diff --git a/src/ocean/OceanAgreements.ts b/src/ocean/OceanAgreements.ts index 5edcb38..a6062cb 100644 --- a/src/ocean/OceanAgreements.ts +++ b/src/ocean/OceanAgreements.ts @@ -3,6 +3,8 @@ import Account from "./Account" import DID from "./DID" import { zeroX, didPrefixed } from "../utils" import { Instantiable, InstantiableConfig } from "../Instantiable.abstract" +import { AgreementConditionsStatus } from "../keeper/contracts/templates/AgreementTemplate.abstract" +import { ConditionState } from "../keeper/contracts/conditions/Condition.abstract" import { OceanAgreementsConditions } from "./OceanAgreementsConditions" @@ -126,4 +128,44 @@ export class OceanAgreements extends Instantiable { return true } + + /** + * Get the status of a service agreement. + * @param {string} did Decentralized ID. + * @param {string} agreementId Service agreement ID. + * @param {string} serviceDefinitionId Service definition ID. + * @param {boolean} extended Returns a complete status with dependencies. + * @return {Promise} + */ + // tslint:disable-next-line + public async status(did: string, agreementId: string, serviceDefinitionId: string, extended?: false): Promise<{[condition: string]: ConditionState}> + // tslint:disable-next-line + public async status(did: string, agreementId: string, serviceDefinitionId: string, extended: true): Promise + public async status( + did: string, + agreementId: string, + serviceDefinitionId: string, + extended: boolean = false, + ) { + const d: DID = DID.parse(did) + const ddo = await this.ocean.aquarius.retrieveDDO(d) + + const templateName = ddo.findServiceById<"Access">(serviceDefinitionId).serviceAgreementTemplate.contractName + const fullStatus = await this.ocean.keeper + .getTemplateByName(templateName) + .getAgreementStatus(agreementId) + + if (!fullStatus) { + return + } + if (extended) { + return fullStatus + } + const simpleStatus = {} + Object.entries(fullStatus) + .forEach(([condition, {state}]) => { + simpleStatus[condition] = state + }) + return simpleStatus as any + } } diff --git a/src/squid.ts b/src/squid.ts index 1580a0c..4a168dd 100644 --- a/src/squid.ts +++ b/src/squid.ts @@ -15,7 +15,7 @@ export * from "./ddo/DDO" export * from "./ddo/MetaData" export { AgreementTemplate } from "./keeper/contracts/templates" -export { Condition } from "./keeper/contracts/conditions" +export { Condition, ConditionState } from "./keeper/contracts/conditions" export { Ocean,