diff --git a/src-ts/proposalMonitor.ts b/src-ts/proposalMonitor.ts index 548cf725..3c8e5c1b 100644 --- a/src-ts/proposalMonitor.ts +++ b/src-ts/proposalMonitor.ts @@ -7,14 +7,13 @@ import { StageTracker, TrackerEventName, TrackerEvent, - TrackerErrorEvent, + AllTrackerEvents, } from "./proposalPipeline"; import { EventEmitter } from "events"; import { Interface } from "@ethersproject/abi"; import { BigNumber } from "ethers"; export type GPMEvent = TrackerEvent & { originAddress: string }; -export type GPMErrorEvent = TrackerErrorEvent & { originAddress: string }; export abstract class ProposalMonitor extends EventEmitter { constructor( @@ -29,25 +28,11 @@ export abstract class ProposalMonitor extends EventEmitter { super(); } - public emit(eventName: TrackerEventName.TRACKER_STARTED, args: GPMEvent): boolean; - public emit(eventName: TrackerEventName.TRACKER_ENDED, args: GPMEvent): boolean; - public emit(eventName: TrackerEventName.TRACKER_ERRORED, args: GPMErrorEvent): boolean; - public emit(eventName: TrackerEventName.TRACKER_STATUS, args: GPMEvent): boolean; - public override emit(eventName: TrackerEventName, args: GPMEvent | GPMErrorEvent) { + public override emit(eventName: TrackerEventName, args: GPMEvent) { return super.emit(eventName, args); } - public on(eventName: TrackerEventName.TRACKER_STARTED, listener: (args: GPMEvent) => void): this; - public on(eventName: TrackerEventName.TRACKER_ENDED, listener: (args: GPMEvent) => void): this; - public on( - eventName: TrackerEventName.TRACKER_ERRORED, - listener: (args: GPMErrorEvent) => void - ): this; - public on(eventName: TrackerEventName.TRACKER_STATUS, listener: (args: GPMEvent) => void): this; - public override on( - eventName: TrackerEventName, - listener: ((args: GPMEvent) => void) | ((args: GPMErrorEvent) => void) - ): this { + public override on(eventName: TrackerEventName, listener: (args: GPMEvent) => void): this { return super.on(eventName, listener); } @@ -64,30 +49,15 @@ export abstract class ProposalMonitor extends EventEmitter { this.writeMode ); - tracker.on(TrackerEventName.TRACKER_STATUS, (args) => - this.emit(TrackerEventName.TRACKER_STATUS, { - originAddress: this.originAddress, - ...args, - }) - ); - tracker.on(TrackerEventName.TRACKER_STARTED, (args) => - this.emit(TrackerEventName.TRACKER_STARTED, { - originAddress: this.originAddress, - ...args, - }) - ); - tracker.on(TrackerEventName.TRACKER_ERRORED, (args) => - this.emit(TrackerEventName.TRACKER_ERRORED, { - originAddress: this.originAddress, - ...args, - }) - ); - tracker.on(TrackerEventName.TRACKER_ENDED, (args) => - this.emit(TrackerEventName.TRACKER_ENDED, { - originAddress: this.originAddress, - ...args, - }) - ); + for (const ev of AllTrackerEvents) { + tracker.on(ev, (args) => { + this.emit(ev, { + ...args, + originAddress: this.originAddress, + }); + }); + } + tracker.run(); } } diff --git a/src-ts/proposalMonitorCli.ts b/src-ts/proposalMonitorCli.ts index 7347b384..af51805b 100644 --- a/src-ts/proposalMonitorCli.ts +++ b/src-ts/proposalMonitorCli.ts @@ -8,7 +8,6 @@ dotenv.config(); import yargs from "yargs"; import { wait } from "./utils"; import { - GPMErrorEvent, GPMEvent, GnosisSafeProposalMonitor, GovernorProposalMonitor, @@ -57,7 +56,7 @@ const attachConsole = (proposalMonitor: ProposalMonitor, monitorName: string) => console.log(`${monitorName} tracker ended: Gov:${e.originAddress}, Prop:${e.identifier}`); }); - proposalMonitor.on(TrackerEventName.TRACKER_ERRORED, (e: GPMErrorEvent) => { + proposalMonitor.on(TrackerEventName.TRACKER_ERRORED, (e: GPMEvent) => { console.log( `${monitorName} tracker errored: Gov:${e.originAddress}, Prop:${e.identifier} Stage:${ e.stage @@ -135,7 +134,7 @@ class JsonLogger { const emittedStages: Map = new Map(); const originKey = `${proposalMonitor.originAddress}::`; - proposalMonitor.on(TrackerEventName.TRACKER_ERRORED, (e: GPMErrorEvent) => { + proposalMonitor.on(TrackerEventName.TRACKER_ERRORED, (e: GPMEvent) => { console.log("Emitted error event"); console.error(e); }); diff --git a/src-ts/proposalPipeline.ts b/src-ts/proposalPipeline.ts index 7b59af7a..cb87cc09 100644 --- a/src-ts/proposalPipeline.ts +++ b/src-ts/proposalPipeline.ts @@ -54,15 +54,16 @@ export enum TrackerEventName { */ TRACKER_STATUS = "TRACKED_STATUS", } + +export const AllTrackerEvents = Object.values(TrackerEventName); + export interface TrackerEvent { identifier: string; status: ProposalStageStatus; stage: string; prevStage?: Omit; publicExecutionUrl?: string; -} -export interface TrackerErrorEvent extends TrackerEvent { - error: Error; + error?: Error; } export class StageTracker extends EventEmitter { @@ -75,76 +76,15 @@ export class StageTracker extends EventEmitter { super(); } - public emit(eventName: TrackerEventName.TRACKER_STARTED, args: TrackerEvent): boolean; - public emit(eventName: TrackerEventName.TRACKER_ENDED, args: TrackerEvent): boolean; - public emit(eventName: TrackerEventName.TRACKER_ERRORED, args: TrackerErrorEvent): boolean; - public emit(eventName: TrackerEventName.TRACKER_STATUS, args: TrackerEvent): boolean; - public override emit(eventName: TrackerEventName, args: TrackerEvent | TrackerErrorEvent) { + public override emit(eventName: TrackerEventName, args: TrackerEvent) { return super.emit(eventName, args); } - public on( - eventName: TrackerEventName.TRACKER_STARTED, - listener: (args: TrackerEvent) => void - ): this; - public on( - eventName: TrackerEventName.TRACKER_ENDED, - listener: (args: TrackerEvent) => void - ): this; - public on( - eventName: TrackerEventName.TRACKER_ERRORED, - listener: (args: TrackerErrorEvent) => void - ): this; - public on( - eventName: TrackerEventName.TRACKER_STATUS, - listener: (args: TrackerEvent) => void - ): this; - public override on( - eventName: TrackerEventName, - listener: ((args: TrackerEvent) => void) | ((args: TrackerErrorEvent) => void) - ): this { + public override on(eventName: TrackerEventName, listener: (args: TrackerEvent) => void): this { return super.on(eventName, listener); } - private propagateTrackerSubcriptions(tracker: StageTracker) { - // propagate events to the listener of this tracker - add some info about the previous stage - tracker.on(TrackerEventName.TRACKER_STATUS, (args) => { - this.emit(TrackerEventName.TRACKER_STATUS, { - ...args, - prevStage: args.prevStage || { - stage: this.stage.name, - identifier: this.stage.identifier, - }, - }); - }); - tracker.on(TrackerEventName.TRACKER_ENDED, (args) => { - this.emit(TrackerEventName.TRACKER_ENDED, { - ...args, - prevStage: args.prevStage || { - stage: this.stage.name, - identifier: this.stage.identifier, - }, - }); - }); - tracker.on(TrackerEventName.TRACKER_STARTED, (args) => { - this.emit(TrackerEventName.TRACKER_STARTED, { - ...args, - prevStage: args.prevStage || { - stage: this.stage.name, - identifier: this.stage.identifier, - }, - }); - }); - tracker.on(TrackerEventName.TRACKER_ERRORED, (args) => { - this.emit(TrackerEventName.TRACKER_ERRORED, { - ...args, - prevStage: args.prevStage || { - stage: this.stage.name, - identifier: this.stage.identifier, - }, - }); - }); - } + private propagateTrackerSubcriptions(tracker: StageTracker) {} public async run() { let polling = true; @@ -189,7 +129,19 @@ export class StageTracker extends EventEmitter { this.writeMode ); - this.propagateTrackerSubcriptions(tracker) + // propagate events to the listener of this tracker - add some info about the previous stage + for (const ev of AllTrackerEvents) { + tracker.on(ev, (args) => { + this.emit(ev, { + ...args, + prevStage: args.prevStage || { + stage: this.stage.name, + identifier: this.stage.identifier, + }, + }); + }); + } + // run but dont await tracker.run(); }