diff --git a/src/keeper/contracts/templates/AgreementTemplate.abstract.ts b/src/keeper/contracts/templates/AgreementTemplate.abstract.ts deleted file mode 100644 index 2a2407f..0000000 --- a/src/keeper/contracts/templates/AgreementTemplate.abstract.ts +++ /dev/null @@ -1,203 +0,0 @@ -import ContractBase from '../ContractBase' -import { - Condition, - ConditionState, - conditionStateNames -} from '../conditions/Condition.abstract' -import { DDO } from '../../../ddo/DDO' -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( - config: InstantiableConfig, - conditionName: string, - templateClass: any - ): Promise { - const condition: AgreementTemplate = new (templateClass as any)(conditionName) - await condition.init(config) - return condition - } - - protected constructor(contractName: string) { - super(contractName) - } - - /** - * Conditions address list. - * @return {Promise} Conditions address. - */ - public getConditionTypes(): Promise { - return this.call('getConditionTypes', []) - } - - /** - * List of condition contracts. - * @return {Promise} Conditions contracts. - */ - public async getConditions(): Promise { - return (await this.getConditionTypes()).map(address => - this.ocean.keeper.getConditionByAddress(address) - ) - } - - /** - * Get agreement conditions IDs. - * @param {string} agreementId Agreement ID. - * @param {DDO} ddo DDO. - * @param {string} from Consumer address. - * @return {Promise} Condition IDs. - */ - public abstract getConditionIdsFromDDO( - agreementId: string, - ddo: DDO, - consumer: string, - from?: string - ): Promise - - /** - * Create a new agreement using the data of a DDO. - * @param {string} agreementId Agreement ID. - * @param {DDO} ddo DDO. - * @param {string} from Creator address. - * @return {Promise} Success. - */ - public abstract createAgreementFromDDO( - agreementId: string, - ddo: DDO, - consumer: string, - from?: string - ): Promise - - public abstract async getServiceAgreementTemplate(): Promise - - 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 { - const agreementStore = this.ocean.keeper.agreementStoreManager - const conditionStore = this.ocean.keeper.conditionStoreManager - - const dependencies = await this.getServiceAgreementTemplateDependencies() - const { conditionIds } = await agreementStore.getAgreement(agreementId) - - if (!conditionIds.length) { - this.logger.error(`Agreement not creeated yet: "${agreementId}"`) - return false - } - - const conditionIdByConddition = (await this.getConditions()).reduce( - (acc, { contractName }, i) => ({ - ...acc, - [contractName]: conditionIds[i] - }), - {} - ) - - const statesPromises = Object.keys(dependencies).map(async (ref, i) => { - const { contractName } = await this.getServiceAgreementTemplateConditionByRef( - ref - ) - return { - ref, - contractName, - state: ( - await conditionStore.getCondition( - conditionIdByConddition[contractName] - ) - ).state - } - }) - const states = await Promise.all(statesPromises) - - return states.reduce((acc, { contractName, ref, state }) => { - const blockers = dependencies[ref] - .map(dependency => states.find(_ => _.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) - - this.logger.bypass('-'.repeat(80)) - this.logger.bypass('Template:', this.contractName) - this.logger.bypass('Agreement ID:', agreementId) - this.logger.bypass('-'.repeat(40)) - if (!status) { - this.logger.bypass('Agreement not created yet!') - } - Object.values(status || []).forEach( - ({ condition, contractName, state, blocked, blockedBy }, i) => { - if (i) { - this.logger.bypass('-'.repeat(20)) - } - this.logger.bypass(`${condition} (${contractName})`) - this.logger.bypass(' Status:', state, `(${conditionStateNames[state]})`) - if (blocked) { - this.logger.bypass(' Blocked by:', blockedBy) - } - } - ) - this.logger.bypass('-'.repeat(80)) - } - - /** - * Generates and returns the agreement creation event. - * @param {string} agreementId Agreement ID. - * @return {Event} Agreement created event. - */ - public getAgreementCreatedEvent(agreementId: string) { - return this.getEvent('AgreementCreated', { - agreementId: zeroX(agreementId) - }) - } -} diff --git a/test/ddo/DDO.test.ts b/test/ddo/DDO.test.ts index c46afb3..3747834 100644 --- a/test/ddo/DDO.test.ts +++ b/test/ddo/DDO.test.ts @@ -1,6 +1,5 @@ import { assert, expect, spy, use } from 'chai' import spies from 'chai-spies' -import Web3 from 'web3' import { DDO } from '../../src/ddo/DDO' import { Service } from '../../src/ddo/Service' @@ -164,13 +163,11 @@ describe('DDO', () => { ] }) - let web3: Web3 let ocean: Ocean beforeEach(async () => { await TestContractHandler.prepareContracts() ocean = await Ocean.getInstance(config) - ;({ web3 } = ocean as any) }) afterEach(() => {