Skip to content

Commit

Permalink
Merge pull request #204 from ArbitrumFoundation/propomon-ui-minor-change
Browse files Browse the repository at this point in the history
consolidate types for proposal monitor
  • Loading branch information
yahgwai authored Aug 24, 2023
2 parents cdb81e4 + db4b217 commit 19d7240
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 113 deletions.
54 changes: 12 additions & 42 deletions src-ts/proposalMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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);
}

Expand All @@ -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();
}
}
Expand Down
5 changes: 2 additions & 3 deletions src-ts/proposalMonitorCli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ dotenv.config();
import yargs from "yargs";
import { wait } from "./utils";
import {
GPMErrorEvent,
GPMEvent,
GnosisSafeProposalMonitor,
GovernorProposalMonitor,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -135,7 +134,7 @@ class JsonLogger {
const emittedStages: Map<string, PipelineStage> = 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);
});
Expand Down
88 changes: 20 additions & 68 deletions src-ts/proposalPipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TrackerEvent, "status">;
publicExecutionUrl?: string;
}
export interface TrackerErrorEvent extends TrackerEvent {
error: Error;
error?: Error;
}

export class StageTracker extends EventEmitter {
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 19d7240

Please sign in to comment.