From 3c8fda98a5d159d97b1eac09d69ab61f881c1176 Mon Sep 17 00:00:00 2001 From: Henry <11198460+godzillaba@users.noreply.github.com> Date: Wed, 23 Aug 2023 09:49:26 -0400 Subject: [PATCH 1/2] consolidate types --- src-ts/proposalMonitor.ts | 51 ++++++------------------- src-ts/proposalMonitorCli.ts | 5 +-- src-ts/proposalPipeline.ts | 74 ++++++++---------------------------- 3 files changed, 29 insertions(+), 101 deletions(-) diff --git a/src-ts/proposalMonitor.ts b/src-ts/proposalMonitor.ts index 548cf725..2266d023 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,24 +28,13 @@ 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) + listener: (args: GPMEvent) => void ): this { return super.on(eventName, listener); } @@ -63,31 +51,16 @@ export abstract class ProposalMonitor extends EventEmitter { this.pollingIntervalMs, this.writeMode ); + + AllTrackerEvents.forEach((ev) => { + tracker.on(ev, (args) => { + this.emit(ev, { + ...args, + originAddress: this.originAddress, + }); + }); + }); - 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, - }) - ); tracker.run(); } } diff --git a/src-ts/proposalMonitorCli.ts b/src-ts/proposalMonitorCli.ts index c08fb548..0b288834 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..b5417a41 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,73 +76,28 @@ 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) + 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, - }, + AllTrackerEvents.forEach((ev) => { + tracker.on(ev, (args) => { + this.emit(ev, { + ...args, + prevStage: args.prevStage || { + stage: this.stage.name, + identifier: this.stage.identifier, + }, + }); }); }); } From db4b21739846079c46f1ebb7577cfdb07e91ccdd Mon Sep 17 00:00:00 2001 From: Chris Buckland Date: Thu, 24 Aug 2023 15:38:49 +0200 Subject: [PATCH 2/2] Use forof instead of foreach --- src-ts/proposalMonitor.ts | 11 ++++------- src-ts/proposalPipeline.ts | 36 ++++++++++++++++-------------------- 2 files changed, 20 insertions(+), 27 deletions(-) diff --git a/src-ts/proposalMonitor.ts b/src-ts/proposalMonitor.ts index 2266d023..3c8e5c1b 100644 --- a/src-ts/proposalMonitor.ts +++ b/src-ts/proposalMonitor.ts @@ -32,10 +32,7 @@ export abstract class ProposalMonitor extends EventEmitter { return super.emit(eventName, args); } - public override on( - eventName: TrackerEventName, - listener: (args: GPMEvent) => void - ): this { + public override on(eventName: TrackerEventName, listener: (args: GPMEvent) => void): this { return super.on(eventName, listener); } @@ -51,15 +48,15 @@ export abstract class ProposalMonitor extends EventEmitter { this.pollingIntervalMs, this.writeMode ); - - AllTrackerEvents.forEach((ev) => { + + for (const ev of AllTrackerEvents) { tracker.on(ev, (args) => { this.emit(ev, { ...args, originAddress: this.originAddress, }); }); - }); + } tracker.run(); } diff --git a/src-ts/proposalPipeline.ts b/src-ts/proposalPipeline.ts index b5417a41..cb87cc09 100644 --- a/src-ts/proposalPipeline.ts +++ b/src-ts/proposalPipeline.ts @@ -63,7 +63,7 @@ export interface TrackerEvent { stage: string; prevStage?: Omit; publicExecutionUrl?: string; - error?: Error + error?: Error; } export class StageTracker extends EventEmitter { @@ -80,27 +80,11 @@ export class StageTracker extends EventEmitter { return super.emit(eventName, args); } - public override on( - eventName: TrackerEventName, - listener: (args: TrackerEvent) => 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 - AllTrackerEvents.forEach((ev) => { - tracker.on(ev, (args) => { - this.emit(ev, { - ...args, - prevStage: args.prevStage || { - stage: this.stage.name, - identifier: this.stage.identifier, - }, - }); - }); - }); - } + private propagateTrackerSubcriptions(tracker: StageTracker) {} public async run() { let polling = true; @@ -145,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(); }