mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
Add a way to get the status of a agreement.
This commit is contained in:
parent
51fbbc887e
commit
f8b3d839eb
@ -8,6 +8,8 @@ export enum ConditionState {
|
||||
Aborted = 3,
|
||||
}
|
||||
|
||||
export const conditionStateNames = ["Uninitialized", "Unfulfilled", "Fulfilled", "Aborted"]
|
||||
|
||||
export abstract class Condition extends ContractBase {
|
||||
|
||||
protected constructor(contractName: string) {
|
||||
|
@ -1,9 +1,10 @@
|
||||
import ContractBase from "../ContractBase"
|
||||
import { Condition } from "../conditions/Condition.abstract"
|
||||
import { AgreementStoreManager, ConditionStoreManager } from "../managers"
|
||||
import { Condition, ConditionState, conditionStateNames } from "../conditions/Condition.abstract"
|
||||
import Keeper from "../../Keeper"
|
||||
import { DDO } from '../../../ddo/DDO'
|
||||
import { ServiceAgreementTemplate, ServiceAgreementTemplateCondition } from '../../../ddo/ServiceAgreementTemplate'
|
||||
import { zeroX } from "../../../utils"
|
||||
import { zeroX, Logger } from "../../../utils"
|
||||
|
||||
export abstract class AgreementTemplate extends ContractBase {
|
||||
|
||||
@ -56,8 +57,97 @@ export abstract class AgreementTemplate extends ContractBase {
|
||||
|
||||
abstract getServiceAgreementTemplate(): Promise<ServiceAgreementTemplate>
|
||||
|
||||
public async getServiceAgreementTemplateConditions(): Promise<ServiceAgreementTemplateCondition[]> {
|
||||
public async getServiceAgreementTemplateConditions() {
|
||||
const serviceAgreementTemplate = await this.getServiceAgreementTemplate()
|
||||
return serviceAgreementTemplate.conditions
|
||||
}
|
||||
|
||||
public async getServiceAgreementTemplateConditionByRef(ref: string) {
|
||||
const name = (await this.getServiceAgreementTemplateConditions())
|
||||
.find(({name: conditionRef}) => conditionRef === ref)
|
||||
.contractName
|
||||
return (await this.getConditions())
|
||||
.find(condition => condition.contractName === name)
|
||||
}
|
||||
|
||||
public async getServiceAgreementTemplateDependencies() {
|
||||
const serviceAgreementTemplate = await this.getServiceAgreementTemplate()
|
||||
return serviceAgreementTemplate.conditionDependency
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the status of the conditions.
|
||||
* @param {string} agreementId Agreement ID.
|
||||
* @return {Promise} Conditions status.
|
||||
*/
|
||||
public async getAgreementStatus(
|
||||
agreementId: string
|
||||
): Promise<{
|
||||
[condition: string]: {
|
||||
condition: string,
|
||||
contractName: string,
|
||||
state: ConditionState,
|
||||
blocked: boolean,
|
||||
blockedBy: string[]
|
||||
}
|
||||
}> {
|
||||
const agreementStore = await AgreementStoreManager.getInstance()
|
||||
const conditionStore = await ConditionStoreManager.getInstance()
|
||||
|
||||
const dependencies = await this.getServiceAgreementTemplateDependencies()
|
||||
const {conditionIds} = await agreementStore.getAgreement(agreementId)
|
||||
|
||||
const statesPromises = Object.keys(dependencies)
|
||||
.map(async (ref, i) => {
|
||||
const condition = await this.getServiceAgreementTemplateConditionByRef(ref)
|
||||
return {
|
||||
ref,
|
||||
contractName: condition.contractName,
|
||||
state: (await conditionStore.getCondition(conditionIds[i])).state
|
||||
}
|
||||
})
|
||||
const states = await Promise.all(statesPromises)
|
||||
|
||||
return states
|
||||
.reduce((acc, {contractName, ref, state}) => {
|
||||
const blockers = dependencies[ref]
|
||||
.map(dependency => states.find(({ref}) => ref === dependency))
|
||||
.filter(condition => condition.state !== ConditionState.Fulfilled)
|
||||
return {
|
||||
...acc,
|
||||
[ref]: {
|
||||
condition: ref,
|
||||
contractName,
|
||||
state,
|
||||
blocked: !!blockers.length,
|
||||
blockedBy: blockers.map(_ => _.ref),
|
||||
}
|
||||
}
|
||||
}, {})
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints the agreement status.
|
||||
* @param {string} agreementId Agreement ID.
|
||||
*/
|
||||
public async printAgreementStatus(agreementId: string) {
|
||||
const status = await this.getAgreementStatus(agreementId)
|
||||
|
||||
Logger.bypass("-".repeat(80))
|
||||
Logger.bypass("Template:", this.contractName)
|
||||
Logger.bypass("Agreement ID:", agreementId)
|
||||
Logger.bypass("-".repeat(40))
|
||||
Object.values(status)
|
||||
.forEach(({condition, contractName, state, blocked, blockedBy}, i) => {
|
||||
if (i) {
|
||||
Logger.bypass("-".repeat(20))
|
||||
}
|
||||
Logger.bypass(`${condition} (${contractName})`)
|
||||
Logger.bypass(" Status:", state, `(${conditionStateNames[state]})`)
|
||||
if (blocked) {
|
||||
Logger.bypass(" Blocked by:", blockedBy)
|
||||
}
|
||||
})
|
||||
Logger.bypass("-".repeat(80))
|
||||
}
|
||||
}
|
||||
|
@ -90,7 +90,6 @@ export class EscrowAccessSecretStoreTemplate extends AgreementTemplate {
|
||||
return agreementId
|
||||
}
|
||||
|
||||
|
||||
async getServiceAgreementTemplateValuesMap(ddo: DDO, agreementId: string, consumer: string): Promise<{[value: string]: string}> {
|
||||
const keeper = await Keeper.getInstance()
|
||||
const ddoOwner = ddo.proof && ddo.proof.creator
|
||||
|
@ -12,6 +12,10 @@ export class Logger {
|
||||
this.logLevel = level
|
||||
}
|
||||
|
||||
public static bypass(...args: any[]) {
|
||||
Logger.dispatch("log", <any>-Infinity, ...args)
|
||||
}
|
||||
|
||||
public static debug(...args: any[]) {
|
||||
Logger.dispatch("debug", LogLevel.Verbose, ...args)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user