1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00

Add status method to agreements module.

This commit is contained in:
Pedro Gutiérrez 2019-04-15 14:28:17 +02:00 committed by Pedro Gutiérrez
parent 9545a95b7f
commit 398ec203c1
4 changed files with 87 additions and 11 deletions

View File

@ -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")

View File

@ -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<AgreementConditionsStatus | false> {
const agreementStore = this.ocean.keeper.agreementStoreManager
const conditionStore = this.ocean.keeper.conditionStoreManager

View File

@ -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<any>}
*/
// 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<AgreementConditionsStatus>
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
}
}

View File

@ -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,