import type { EventEmitter } from "events"; import type { EventLog } from "web3-core"; import type { BaseContract, Callback, ContractEventLog, EventOptions, } from "../../build/types/types"; /** * Contract event function type. */ export type ContractEvent = { (cb?: Callback>): EventEmitter; (options?: EventOptions, cb?: Callback>): EventEmitter; }; /** * Event data type specified by name. */ export type Event< C extends BaseContract, T extends Exclude > = EventValues; /** * Concrete event type with known properties based on the event name. * * @remarks * This type definition allows the TypeScript compiler to determine the type of * the `returnValues` property based on `event` property checks. For example: * ``` * const eventData: AnyEvent = ... * switch (eventData.event) { * case "Token": // ERR: 2678: Type '"Token"' is not comparable to type * // '"OrderPlacement" | "TokenListing" | ...' * break * case "OrderPlacement": * eventData.returnValues.buyToken = "asdf" // OK * break * case "Withdraw": * eventData.returnValues.buyToken = "asdf" // ERR: 2339: Property 'buyToken' does not exist on type * // '{ user: string; token: string; amount: string; ... }'. * break * } * ``` */ export type AnyEvent = EventMetadata & EventDiscriminant>; export type EventMetadata = Omit; export type EventName = Exclude< keyof C["events"], "allEvents" >; export type EventValues = T extends ContractEvent ? U : never; export type EventDiscriminant< C extends BaseContract, T extends EventName > = T extends unknown ? { event: T; returnValues: EventValues; } : never;