Skip to content

Commit

Permalink
refactor: improve generic event type
Browse files Browse the repository at this point in the history
  • Loading branch information
0xyaco committed Aug 2, 2024
1 parent fdaa4bb commit c07b536
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
6 changes: 3 additions & 3 deletions packages/automated-dispute/src/protocolProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from "viem";
import { arbitrum } from "viem/chains";

import type { AnyEboEvent, EboEvent } 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 @@ -67,7 +67,7 @@ export class ProtocolProvider {
};
}

async getEvents(_fromBlock: bigint, _toBlock: bigint): Promise<AnyEboEvent[]> {
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 Down Expand Up @@ -141,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: AnyEboEvent[][]) {
private mergeEventStreams(...streams: EboEvent<EboEventName>[][]) {
return streams
.reduce((acc, curr) => acc.concat(curr), [])
.sort((a, b) => {
Expand Down
10 changes: 3 additions & 7 deletions packages/automated-dispute/src/types/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Log } from "viem";

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

type EventName =
export type EboEventName =
| "NewEpoch"
| "RequestCreated"
| "ResponseProposed"
Expand Down Expand Up @@ -56,7 +56,7 @@ export interface RequestFinalized {
blockNumber: bigint;
}

export type EboEventData<E extends EventName> = E extends "NewEpoch"
export type EboEventData<E extends EboEventName> = E extends "NewEpoch"
? NewEpoch
: E extends "RequestCreated"
? RequestCreated
Expand All @@ -74,14 +74,10 @@ export type EboEventData<E extends EventName> = E extends "NewEpoch"
? RequestFinalized
: never;

export type EboEvent<T extends EventName> = {
export type EboEvent<T extends EboEventName> = {
name: T;
blockNumber: bigint;
logIndex: number;
rawLog?: Log;
metadata: EboEventData<T>;
};

export type AnyEboEvent = {
[K in EventName]: EboEvent<K>;
}[EventName];

0 comments on commit c07b536

Please sign in to comment.