diff --git a/integration/ocean/ConsumeAsset.test.ts b/integration/ocean/ConsumeAsset.test.ts index 5a6a289..c8f61d2 100644 --- a/integration/ocean/ConsumeAsset.test.ts +++ b/integration/ocean/ConsumeAsset.test.ts @@ -26,7 +26,7 @@ describe("Consume Asset", () => { }) // Accounts - const instanceConfig = (ocean).instanceConfig + const instanceConfig = (ocean as any).instanceConfig publisher = new Account("0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e", instanceConfig) publisher.setPassword("node0") consumer = new Account("0x068Ed00cF0441e4829D9784fCBe7b9e26D4BD8d0", instanceConfig) diff --git a/integration/ocean/ConsumeAssetBrizo.test.ts b/integration/ocean/ConsumeAssetBrizo.test.ts index f663864..a12fe71 100644 --- a/integration/ocean/ConsumeAssetBrizo.test.ts +++ b/integration/ocean/ConsumeAssetBrizo.test.ts @@ -26,7 +26,7 @@ describe("Consume Asset (Brizo)", () => { }) // Accounts - const instanceConfig = (ocean).instanceConfig + const instanceConfig = (ocean as any).instanceConfig publisher = new Account("0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e", instanceConfig) publisher.setPassword("node0") consumer = new Account("0x068Ed00cF0441e4829D9784fCBe7b9e26D4BD8d0", instanceConfig) diff --git a/package.json b/package.json index 99061f4..d2c9275 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "build:watch": "tsc -w", "doc": "typedoc --mode modules --exclude \"**/examples/**\" --out ./doc/ ./src/", "merge-coverages": "npx lcov-result-merger \"coverage/*/lcov.info\" coverage/lcov.info", - "report-coverage": "cat ./coverage/unit/lcov.info | codacy-coverage --token 71ef0d15f6f04ac29b31d704b28f866a", + "report-coverage": "cat ./coverage/lcov.info | codacy-coverage --token 71ef0d15f6f04ac29b31d704b28f866a", "run": "ts-node", "release": "./node_modules/release-it/bin/release-it.js --src.tagName='v%s' --github.release --npm.publish --non-interactive", "release-minor": "./node_modules/release-it/bin/release-it.js minor --src.tagName='v%s' --github.release --npm.publish --non-interactive", diff --git a/src/Instantiable.abstract.ts b/src/Instantiable.abstract.ts index 3d850f8..78b4914 100644 --- a/src/Instantiable.abstract.ts +++ b/src/Instantiable.abstract.ts @@ -23,10 +23,6 @@ export function generateIntantiableConfigFromConfig(config: Config): Partial + public static async getInstance(config: InstantiableConfig): Promise { + LoggerInstance.warn("getInstance() methods has needs to be added to child class.") + } + protected static setInstanceConfig(instance: T, {ocean, config, web3, logger}: InstantiableConfig) { instance._ocean = ocean instance._config = config @@ -72,10 +73,16 @@ export abstract class Instantiable { instance._logger = logger } + // tslint:disable-next-line + private _ocean: Ocean + // tslint:disable-next-line + private _web3: Web3 + // tslint:disable-next-line + private _config: Config + // tslint:disable-next-line + private _logger: Logger + protected setInstanceConfig(config: InstantiableConfig) { Instantiable.setInstanceConfig(this, config) } - - public static async getInstance(...args: any[]): Promise - public static async getInstance(config: InstantiableConfig): Promise { } } diff --git a/src/keeper/ContractEvent.ts b/src/keeper/ContractEvent.ts index 2993265..32b787c 100644 --- a/src/keeper/ContractEvent.ts +++ b/src/keeper/ContractEvent.ts @@ -1,12 +1,14 @@ import ContractBase from "./contracts/ContractBase" interface EventEmitter { + // tslint:disable-next-line subscribe: Function + // tslint:disable-next-line unsubscribe: Function } export interface ContractEventSubscription { - unsubscribe: Function + unsubscribe: () => void } export class ContractEvent { @@ -17,8 +19,8 @@ export class ContractEvent { private filter: {[key: string]: any}, ) { } - subscribe(callback: (events: any[]) => void): ContractEventSubscription { - const onEvent = async blockNumber => { + public subscribe(callback: (events: any[]) => void): ContractEventSubscription { + const onEvent = async (blockNumber) => { const events = await this.contract.getEventData(this.eventName, { filter: this.filter, fromBlock: blockNumber, @@ -35,9 +37,9 @@ export class ContractEvent { } } - once(callback?: (events: any[]) => void) { - return new Promise(resolve => { - const subscription = this.subscribe(events => { + public once(callback?: (events: any[]) => void) { + return new Promise((resolve) => { + const subscription = this.subscribe((events) => { subscription.unsubscribe() if (callback) { callback(events) diff --git a/src/keeper/ContractHandler.ts b/src/keeper/ContractHandler.ts index 4b54834..6dc3040 100644 --- a/src/keeper/ContractHandler.ts +++ b/src/keeper/ContractHandler.ts @@ -3,17 +3,6 @@ import { Instantiable, InstantiableConfig } from "../Instantiable.abstract" export default class ContractHandler extends Instantiable { - constructor(config: InstantiableConfig) { - super() - this.setInstanceConfig(config) - } - - private static contracts: Map = new Map() - - private static getHash(what: string, networkId: number): string { - return `${what}/#${networkId}` - } - protected static getContract(what: string, networkId: number) { return ContractHandler.contracts.get(this.getHash(what, networkId)) } @@ -26,6 +15,16 @@ export default class ContractHandler extends Instantiable { return ContractHandler.contracts.has(this.getHash(what, networkId)) } + private static contracts: Map = new Map() + + private static getHash(what: string, networkId: number): string { + return `${what}/#${networkId}` + } + + constructor(config: InstantiableConfig) { + super() + this.setInstanceConfig(config) + } public async get(what: string): Promise { const where = (await this.ocean.keeper.getNetworkName()).toLowerCase() diff --git a/src/keeper/EventHandler.ts b/src/keeper/EventHandler.ts index 0c700de..86c4874 100644 --- a/src/keeper/EventHandler.ts +++ b/src/keeper/EventHandler.ts @@ -4,21 +4,43 @@ import ContractBase from "./contracts/ContractBase" import { Instantiable, InstantiableConfig } from "../Instantiable.abstract" export class EventHandler extends Instantiable { + + get count() { + return this.events.size + } private events = new Set<(blockNumber) => void>() private lastBlock: number private interval = 200 private polling: boolean = false private lastTimeout: NodeJS.Timeout - get count() { - return this.events.size - } - constructor(config: InstantiableConfig) { super() this.setInstanceConfig(config) } + public subscribe(callback: (blockNumber: number) => void) { + this.events.add(callback) + this.checkBlock() + + return { + unsubscribe: () => this.unsubscribe(callback), + } + } + + public unsubscribe(callback: (blockNumber: number) => void) { + this.events.delete(callback) + if (!this.count) { + clearTimeout(this.lastTimeout) + delete this.lastBlock + this.polling = false + } + } + + public getEvent(contract: ContractBase, eventName: string, filter: {[key: string]: any}) { + return new ContractEvent(this, contract, eventName, filter) + } + private async checkBlock(isInterval?: boolean, n = 0) { const blockNumber = await this.web3.eth.getBlockNumber() @@ -32,31 +54,9 @@ export class EventHandler extends Instantiable { } if (this.lastBlock !== blockNumber) { - this.events.forEach(fn => fn(this.lastBlock + 1)) + this.events.forEach((fn) => fn(this.lastBlock + 1)) this.lastBlock = blockNumber } this.lastTimeout = setTimeout(() => this.checkBlock(true, n++), this.interval) } - - public subscribe(callback: (number) => void) { - this.events.add(callback) - this.checkBlock() - - return { - unsubscribe: () => this.unsubscribe(callback), - } - } - - public unsubscribe(callback: (number) => void) { - this.events.delete(callback) - if (!this.count) { - clearTimeout(this.lastTimeout) - delete this.lastBlock - this.polling = false - } - } - - public getEvent(contract: ContractBase, eventName: string, filter: {[key: string]: any}) { - return new ContractEvent(this, contract, eventName, filter) - } } diff --git a/src/keeper/Keeper.ts b/src/keeper/Keeper.ts index 1157f90..dabdd96 100644 --- a/src/keeper/Keeper.ts +++ b/src/keeper/Keeper.ts @@ -115,7 +115,7 @@ export class Keeper extends Instantiable { * Helpers for contracts. */ public utils: { - eventHandler: EventHandler + eventHandler: EventHandler, } /** diff --git a/src/keeper/contracts/ContractBase.ts b/src/keeper/contracts/ContractBase.ts index 77cc6ca..597f2ad 100644 --- a/src/keeper/contracts/ContractBase.ts +++ b/src/keeper/contracts/ContractBase.ts @@ -102,6 +102,13 @@ export default abstract class ContractBase extends Instantiable { } } + protected getEvent(eventName: string, filter: {[key: string]: any}) { + if (!this.contract.events[eventName]) { + throw new Error(`Event ${eventName} is not part of contract ${this.contractName}`) + } + return this.ocean.keeper.utils.eventHandler.getEvent(this, eventName, filter) + } + private searchMethod(methodName: string, args: any[] = []) { const methods = this.contract.options.jsonInterface .map((method) => ({...method, signature: (method as any).signature})) @@ -112,11 +119,4 @@ export default abstract class ContractBase extends Instantiable { } return foundMethod } - - protected getEvent(eventName: string, filter: {[key: string]: any}) { - if (!this.contract.events[eventName]) { - throw new Error(`Event ${eventName} is not part of contract ${this.contractName}`) - } - return this.ocean.keeper.utils.eventHandler.getEvent(this, eventName, filter) - } } diff --git a/src/keeper/contracts/GenericContract.ts b/src/keeper/contracts/GenericContract.ts index 2171647..286813d 100644 --- a/src/keeper/contracts/GenericContract.ts +++ b/src/keeper/contracts/GenericContract.ts @@ -1,7 +1,6 @@ import ContractBase from "./ContractBase" import { InstantiableConfig } from "../../Instantiable.abstract" - export default class GenericContract extends ContractBase { public static async getInstance(config: InstantiableConfig, contractName: string): Promise { diff --git a/src/keeper/contracts/managers/TemplateStoreManager.ts b/src/keeper/contracts/managers/TemplateStoreManager.ts index 782e519..0530f4a 100644 --- a/src/keeper/contracts/managers/TemplateStoreManager.ts +++ b/src/keeper/contracts/managers/TemplateStoreManager.ts @@ -2,7 +2,6 @@ import ContractBase from "../ContractBase" import { zeroX } from "../../../utils" import { InstantiableConfig } from "../../../Instantiable.abstract" - export enum TemplateState { Uninitialized = 0, Proposed = 1, diff --git a/src/keeper/contracts/templates/AgreementTemplate.abstract.ts b/src/keeper/contracts/templates/AgreementTemplate.abstract.ts index 276b0a9..604e3af 100644 --- a/src/keeper/contracts/templates/AgreementTemplate.abstract.ts +++ b/src/keeper/contracts/templates/AgreementTemplate.abstract.ts @@ -7,7 +7,11 @@ import { InstantiableConfig } from "../../../Instantiable.abstract" export abstract class AgreementTemplate extends ContractBase { - public static async getInstance(config: InstantiableConfig, conditionName: string, templateClass: any): Promise { + 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 diff --git a/src/ocean/OceanAssets.ts b/src/ocean/OceanAssets.ts index e032aec..d41e15d 100644 --- a/src/ocean/OceanAssets.ts +++ b/src/ocean/OceanAssets.ts @@ -252,7 +252,7 @@ export class OceanAssets extends Instantiable { try { await paymentFlow - } catch(e) { + } catch (e) { throw new Error("Error paying the asset.") } diff --git a/src/squid.ts b/src/squid.ts index 77a7245..1580a0c 100644 --- a/src/squid.ts +++ b/src/squid.ts @@ -5,7 +5,6 @@ import { Ocean } from "./ocean/Ocean" import { LoggerInstance as Logger} from "./utils/Logger" import WebServiceConnectorProvider from "./utils/WebServiceConnectorProvider" import Keeper from "./keeper/Keeper" -import EventListener from "./keeper/EventListener" import * as templates from "./keeper/contracts/templates" import * as conditions from "./keeper/contracts/conditions" diff --git a/src/utils/SignatureHelpers.ts b/src/utils/SignatureHelpers.ts index ea2ff10..d2010b2 100644 --- a/src/utils/SignatureHelpers.ts +++ b/src/utils/SignatureHelpers.ts @@ -1,7 +1,6 @@ import * as Web3 from "web3" import LoggerInstance from "./Logger" - export async function signText(web3: Web3, text: string, publicKey: string, password?: string): Promise { try { return await web3.eth.personal.sign(text, publicKey, password) diff --git a/test/aquarius/Aquarius.test.ts b/test/aquarius/Aquarius.test.ts index 04f6c7c..403283c 100644 --- a/test/aquarius/Aquarius.test.ts +++ b/test/aquarius/Aquarius.test.ts @@ -9,7 +9,7 @@ import WebServiceConnectorMock from "../mocks/WebServiceConnector.mock" describe("Aquarius", () => { - const aquarius: Aquarius = new Aquarius({config}) + const aquarius: Aquarius = new Aquarius({config} as any) describe("#queryMetadata()", () => { diff --git a/test/ddo/DDO.test.ts b/test/ddo/DDO.test.ts index a1bbeea..5de2dea 100644 --- a/test/ddo/DDO.test.ts +++ b/test/ddo/DDO.test.ts @@ -7,6 +7,7 @@ import { Service } from "../../src/ddo/Service" import * as signatureHelpers from "../../src/utils/SignatureHelpers" import { Ocean } from "../../src/ocean/Ocean" import config from "../config" +import TestContractHandler from "../keeper/TestContractHandler" import * as jsonDDO from "../testdata/ddo.json" @@ -166,6 +167,7 @@ describe("DDO", () => { let web3: Web3 beforeEach(async () => { + await TestContractHandler.prepareContracts() web3 = (await Ocean.getInstance(config) as any).web3 }) diff --git a/test/keeper/ContractBase.test.ts b/test/keeper/ContractBase.test.ts index 88d3907..84f4251 100644 --- a/test/keeper/ContractBase.test.ts +++ b/test/keeper/ContractBase.test.ts @@ -14,7 +14,7 @@ describe("ContractWrapperBase", () => { await TestContractHandler.prepareContracts() const ocean: Ocean = await Ocean.getInstance(config) accounts = await ocean.accounts.list() - await wrappedContract.initMock((ocean).instanceConfig) + await wrappedContract.initMock((ocean as any).instanceConfig) }) describe("#call()", () => { diff --git a/test/keeper/ContractEvent.test.ts b/test/keeper/ContractEvent.test.ts index f4bdf60..40b5a06 100644 --- a/test/keeper/ContractEvent.test.ts +++ b/test/keeper/ContractEvent.test.ts @@ -15,7 +15,7 @@ describe("ContractEvent", () => { beforeEach(async () => { await TestContractHandler.prepareContracts() ocean = await Ocean.getInstance(config) - eventHandler = new EventHandler((ocean).instanceConfig) + eventHandler = new EventHandler((ocean as any).instanceConfig) account = (await ocean.accounts.list())[0].getId() executeTransaction = () => ocean.keeper.dispenser.requestTokens(10, account) @@ -27,8 +27,8 @@ describe("ContractEvent", () => { let validResolve = false let subscription: ContractEventSubscription - const waitUntilEvent = new Promise(resolve => { - subscription = event.subscribe(events => { + const waitUntilEvent = new Promise((resolve) => { + subscription = event.subscribe((events) => { assert.isDefined(events) assert.lengthOf(events, 2) if (validResolve) { @@ -42,7 +42,7 @@ describe("ContractEvent", () => { executeTransaction(), ]) - await new Promise(_ => setTimeout(_, 2000)) + await new Promise((_) => setTimeout(_, 2000)) validResolve = true await Promise.all([ @@ -63,7 +63,7 @@ describe("ContractEvent", () => { let canBeRejected = false const waitUntilEvent = new Promise((resolve, reject) => { - event.once(events => { + event.once((events) => { if (canBeRejected) { reject() } @@ -73,7 +73,7 @@ describe("ContractEvent", () => { await executeTransaction() - await new Promise(_ => setTimeout(_, 2000)) + await new Promise((_) => setTimeout(_, 2000)) canBeRejected = true await executeTransaction() @@ -87,7 +87,7 @@ describe("ContractEvent", () => { const waitUntilEvent = event.once() - await new Promise(_ => setTimeout(_, 400)) + await new Promise((_) => setTimeout(_, 400)) await executeTransaction() diff --git a/test/keeper/ContractHandler.test.ts b/test/keeper/ContractHandler.test.ts index 391efeb..0d0b4c4 100644 --- a/test/keeper/ContractHandler.test.ts +++ b/test/keeper/ContractHandler.test.ts @@ -3,12 +3,11 @@ import ContractHandler from "../../src/keeper/ContractHandler" import { Ocean } from "../../src/ocean/Ocean" import config from "../config" - describe("ContractHandler", () => { let contractHandler: ContractHandler before(async () => { - const instanceConfig = (await Ocean.getInstance(config)).instanceConfig + const instanceConfig = (await Ocean.getInstance(config) as any).instanceConfig contractHandler = new ContractHandler(instanceConfig) }) diff --git a/test/keeper/EventHandler.test.ts b/test/keeper/EventHandler.test.ts index ec3e7c0..5535d43 100644 --- a/test/keeper/EventHandler.test.ts +++ b/test/keeper/EventHandler.test.ts @@ -13,7 +13,7 @@ describe("EventHandler", () => { before(async () => { ocean = await Ocean.getInstance(config) - eventHandler = new EventHandler((ocean).instanceConfig) + eventHandler = new EventHandler((ocean as any).instanceConfig) }) afterEach(() => { @@ -24,22 +24,19 @@ describe("EventHandler", () => { it("should subscribe to an event", async () => { const countBefore = eventHandler.count - const subscription = eventHandler.subscribe(() => {}) + const subscription = eventHandler.subscribe(() => null) assert.isDefined(subscription) const countAfter = eventHandler.count assert.equal(countBefore + 1, countAfter, "The event seems not added.") - try { - // Not important in this test - subscription.unsubscribe() - } catch(e) { } + subscription.unsubscribe() }) it("should unsubscribe using the subscription", async () => { const countBefore = eventHandler.count - const subscription = eventHandler.subscribe(() => {}) + const subscription = eventHandler.subscribe(() => null) assert.isDefined(subscription) subscription.unsubscribe() @@ -52,7 +49,7 @@ describe("EventHandler", () => { describe("#unsubscribe()", () => { it("should unsubscribe from an event", async () => { const countBefore = eventHandler.count - const callback = () => {} + const callback = () => null eventHandler.subscribe(callback) eventHandler.unsubscribe(callback) @@ -71,19 +68,16 @@ describe("EventHandler", () => { const subscription = eventHandler.subscribe(callbackSpy) - await new Promise(_ => setTimeout(_, 300)) + await new Promise((_) => setTimeout(_, 300)) expect(callbackSpy).not.to.has.been.called() blockNumber++ - await new Promise(_ => setTimeout(_, 300)) + await new Promise((_) => setTimeout(_, 300)) expect(callbackSpy).to.has.been.called.with(blockNumber) - try { - // Not important in this test - subscription.unsubscribe() - } catch(e) { } + subscription.unsubscribe() }) }) }) diff --git a/test/keeper/TestContractHandler.ts b/test/keeper/TestContractHandler.ts index aa6750f..0acc6f7 100644 --- a/test/keeper/TestContractHandler.ts +++ b/test/keeper/TestContractHandler.ts @@ -6,8 +6,6 @@ import config from "../config" export default class TestContractHandler extends ContractHandler { - private static networkId: number - public static async prepareContracts() { const web3 = Web3Provider.getWeb3(config) const deployerAddress = (await web3.eth.getAccounts())[0] @@ -17,6 +15,8 @@ export default class TestContractHandler extends ContractHandler { await TestContractHandler.deployContracts(deployerAddress) } + private static networkId: number + private static async deployContracts(deployerAddress: string) { Logger.log("Trying to deploy contracts") diff --git a/test/keeper/templates/EscrowAccessSecretStoreTemplate.test.ts b/test/keeper/templates/EscrowAccessSecretStoreTemplate.test.ts index 7349a29..90baaed 100644 --- a/test/keeper/templates/EscrowAccessSecretStoreTemplate.test.ts +++ b/test/keeper/templates/EscrowAccessSecretStoreTemplate.test.ts @@ -4,7 +4,6 @@ import config from "../../config" import TestContractHandler from "../TestContractHandler" import { Ocean } from "../../../src/ocean/Ocean" - let condition: EscrowAccessSecretStoreTemplate describe("EscrowAccessSecretStoreTemplate", () => { diff --git a/test/utils/SignatureHelpers.test.ts b/test/utils/SignatureHelpers.test.ts index 76fc9ed..2aa7a48 100644 --- a/test/utils/SignatureHelpers.test.ts +++ b/test/utils/SignatureHelpers.test.ts @@ -42,7 +42,6 @@ describe("SignatureHelpers", () => { describe("#verifyText", () => { it("should recover the privateKey of a signed message", async () => { - const web3 = Web3Provider.getWeb3() const personalRecoverSpy = spy.on(web3.eth.personal, "ecRecover", () => publicKey) const verifiedPublicKey = await verifyText(web3, text, signature)