Skip to content

Commit

Permalink
refactor: generic ebo events (#17)
Browse files Browse the repository at this point in the history
# 🤖 Linear

Closes GRT-79

## Description

Refactors EBO events for strict type checking.
  • Loading branch information
0xyaco authored Aug 5, 2024
1 parent be97948 commit 70dd4b6
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 59 deletions.
20 changes: 8 additions & 12 deletions packages/automated-dispute/src/eboActor.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { BlockNumberService } from "@ebo-agent/blocknumber";

import { ProtocolProvider } from "./protocolProvider.js";
import {
DisputeStatusChanged,
RequestCreated,
RequestFinalizable,
ResponseDisputed,
} from "./types/events.js";
import { EboEvent } from "./types/events.js";
import { Dispute, Response } from "./types/prophet.js";

export class EboActor {
Expand All @@ -20,17 +15,17 @@ export class EboActor {
this.requestActivity = [];
}

public async onRequestCreated(_event: RequestCreated): Promise<void> {
public async onRequestCreated(_event: EboEvent<"RequestCreated">): Promise<void> {
// TODO: implement
return;
}

public async onResponseProposed(_event: ResponseDisputed): Promise<void> {
public async onResponseProposed(_event: EboEvent<"ResponseDisputed">): Promise<void> {
// TODO: implement
return;
}

public async onResponseDisputed(_event: ResponseDisputed): Promise<void> {
public async onResponseDisputed(_event: EboEvent<"ResponseDisputed">): Promise<void> {
// TODO: implement
return;
}
Expand All @@ -50,17 +45,18 @@ export class EboActor {
return true;
}

public async onFinalizeRequest(_event: RequestFinalizable): Promise<void> {
public async onFinalizeRequest(_event: EboEvent<"RequestFinalizable">): Promise<void> {
// TODO: implement
return;
}

public async onDisputeStatusChanged(_event: DisputeStatusChanged): Promise<void> {
public async onDisputeStatusChanged(_event: EboEvent<"DisputeStatusChanged">): Promise<void> {
// TODO: implement
return;
}

public async onDisputeEscalated(_event: Dispute): Promise<void> {
public async onDisputeEscalated(_event: EboEvent<"DisputeEscalated">): Promise<void> {
// TODO: implement
return;
}
}
20 changes: 7 additions & 13 deletions packages/automated-dispute/src/protocolProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ import {
} from "viem";
import { arbitrum } from "viem/chains";

import type {
DisputeStatusChanged,
EboEvent,
RequestCreated,
ResponseDisputed,
ResponseProposed,
} from "./types/events.js";
import type { EboEvent, EboEventName } from "./types/events.js";
import type { Dispute, Request, Response } from "./types/prophet.js";
import { epochManagerAbi, oracleAbi } from "./abis/index.js";
import { RpcUrlsEmpty } from "./exceptions/rpcUrlsEmpty.exception.js";
Expand Down Expand Up @@ -73,7 +67,7 @@ export class ProtocolProvider {
};
}

async getEvents(_fromBlock: bigint, _toBlock: bigint): Promise<EboEvent[]> {
async getEvents(_fromBlock: bigint, _toBlock: bigint): Promise<EboEvent<EboEventName>[]> {
// TODO: implement actual method.
//
// We should decode events using the corresponding ABI and also "fabricate" new events
Expand All @@ -94,7 +88,7 @@ export class ProtocolProvider {
finalityModule: "0x12345678901234567890123456789012",
},
},
} as RequestCreated,
} as EboEvent<"RequestCreated">,
];

const oracleEvents = [
Expand All @@ -111,7 +105,7 @@ export class ProtocolProvider {
response: "0x01234",
},
},
} as ResponseProposed,
} as EboEvent<"ResponseProposed">,
{
name: "ResponseDisputed",
blockNumber: 3n,
Expand All @@ -127,13 +121,13 @@ export class ProtocolProvider {
requestId: "0x01",
},
},
} as ResponseDisputed,
} as EboEvent<"ResponseDisputed">,
{
name: "DisputeStatusChanged",
blockNumber: 4n,
logIndex: 20,
metadata: { disputeId: "0x03", status: "Won", blockNumber: 4n },
} as DisputeStatusChanged,
} as EboEvent<"DisputeStatusChanged">,
];

return this.mergeEventStreams(eboRequestCreatorEvents, oracleEvents);
Expand All @@ -147,7 +141,7 @@ export class ProtocolProvider {
* @returns the EboEvent[] arrays merged in a single array and sorted by ascending blockNumber
* and logIndex
*/
private mergeEventStreams(...streams: EboEvent[][]) {
private mergeEventStreams(...streams: EboEvent<EboEventName>[][]) {
return streams
.reduce((acc, curr) => acc.concat(curr), [])
.sort((a, b) => {
Expand Down
105 changes: 71 additions & 34 deletions packages/automated-dispute/src/types/events.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,83 @@
import { Log } from "viem";

import { Dispute, Request, Response } from "./prophet.js";
import { Dispute, Request } from "./prophet.js";

export type BaseEvent = {
name: string;
blockNumber: bigint;
logIndex: number;
rawLog?: Log;
metadata: unknown;
};
export type EboEventName =
| "NewEpoch"
| "RequestCreated"
| "ResponseProposed"
| "ResponseDisputed"
| "DisputeStatusChanged"
| "DisputeEscalated"
| "RequestFinalizable"
| "RequestFinalized";

export type NewEpoch = BaseEvent & { metadata: { epoch: bigint; epochBlockNumber: bigint } };
export interface NewEpoch {
epoch: bigint;
epochBlockNumber: bigint;
}

export type RequestCreated = BaseEvent & { metadata: { requestId: string; request: Request } };
export interface ResponseCreated {
requestId: string;
request: Request;
}

export type ResponseProposed = BaseEvent & {
metadata: { requestId: string; responseId: string; response: Response };
};
export interface RequestCreated {
requestId: string;
request: Request;
}

export type ResponseDisputed = BaseEvent & {
metadata: { requestId: string; responseId: string; dispute: Dispute };
};
export interface ResponseDisputed {
requestId: string;
responseId: string;
dispute: Dispute;
}

export type DisputeStatusChanged = BaseEvent & {
metadata: { disputeId: string; status: string; blockNumber: bigint };
};
export interface DisputeStatusChanged {
disputeId: string;
status: string;
blockNumber: bigint;
}

export type DisputeEscalated = BaseEvent & {
metadata: { caller: string; disputeId: string; blockNumber: bigint };
};
export interface DisputeEscalated {
caller: string;
disputeId: string;
blockNumber: bigint;
}

export type RequestFinalizable = BaseEvent & {
metadata: { requestId: string };
};
export interface RequestFinalizable {
requestId: string;
}

export type RequestFinalized = BaseEvent & {
metadata: { requestId: string; responseId: string; caller: string; blockNumber: bigint };
};
export interface RequestFinalized {
requestId: string;
responseId: string;
caller: string;
blockNumber: bigint;
}

export type EboEventData<E extends EboEventName> = E extends "NewEpoch"
? NewEpoch
: E extends "RequestCreated"
? RequestCreated
: E extends "ResponseCreated"
? ResponseCreated
: E extends "ResponseDisputed"
? ResponseDisputed
: E extends "DisputeStatusChanged"
? DisputeStatusChanged
: E extends "DisputeEscalated"
? DisputeEscalated
: E extends "RequestFinalizable"
? RequestFinalizable
: E extends "RequestFinalized"
? RequestFinalized
: never;

export type EboEvent =
| NewEpoch
| RequestCreated
| ResponseProposed
| ResponseDisputed
| DisputeStatusChanged;
export type EboEvent<T extends EboEventName> = {
name: T;
blockNumber: bigint;
logIndex: number;
rawLog?: Log;
metadata: EboEventData<T>;
};

0 comments on commit 70dd4b6

Please sign in to comment.