From b7138924c0eabbf990cf8d80484eefa3e58d3fd7 Mon Sep 17 00:00:00 2001 From: Yaco 0x Date: Tue, 3 Sep 2024 18:27:19 -0300 Subject: [PATCH] feat: use DisputeEscalated instead of DisputeStatusChanged --- packages/automated-dispute/src/eboActor.ts | 14 ++++++++----- .../services/eboRegistry/commands/index.ts | 2 +- .../commands/updateDisputeStatus.ts | 15 ++++++++++--- .../automated-dispute/src/types/events.ts | 16 ++++++++++---- .../commands/updateDisputeStatus.spec.ts | 21 +++++++++++++++++++ 5 files changed, 55 insertions(+), 13 deletions(-) diff --git a/packages/automated-dispute/src/eboActor.ts b/packages/automated-dispute/src/eboActor.ts index 592b682..2a9ea4b 100644 --- a/packages/automated-dispute/src/eboActor.ts +++ b/packages/automated-dispute/src/eboActor.ts @@ -194,6 +194,12 @@ export class EboActor { this.registry, ); + case "DisputeEscalated": + return UpdateDisputeStatus.buildFromEvent( + event as EboEvent<"DisputeEscalated">, + this.registry, + ); + case "RequestFinalized": return Noop.buildFromEvent(); @@ -711,11 +717,7 @@ export class EboActor { case "Active": // Case handled by ResponseDisputed case "Lost": // Relevant during periodic request state checks case "Won": // Relevant during periodic request state checks - break; - - case "Escalated": - await this.onDisputeEscalated(disputeId, request); - + case "Escalated": // Case handled by DisputeEscalated break; case "NoResolution": @@ -731,6 +733,8 @@ export class EboActor { private async onDisputeEscalated(disputeId: string, request: Request) { // TODO: notify this.logger.info(`Dispute ${disputeId} for request ${request.id} has been escalated.`); + + await this.onDisputeEscalated(disputeId, request); } private async onDisputeWithNoResolution(disputeId: string, request: Request) { diff --git a/packages/automated-dispute/src/services/eboRegistry/commands/index.ts b/packages/automated-dispute/src/services/eboRegistry/commands/index.ts index 13e989e..c05c58f 100644 --- a/packages/automated-dispute/src/services/eboRegistry/commands/index.ts +++ b/packages/automated-dispute/src/services/eboRegistry/commands/index.ts @@ -1,5 +1,5 @@ +export * from "./addDispute.js"; export * from "./addRequest.js"; export * from "./addResponse.js"; -export * from "./addDispute.js"; export * from "./noop.js"; export * from "./updateDisputeStatus.js"; diff --git a/packages/automated-dispute/src/services/eboRegistry/commands/updateDisputeStatus.ts b/packages/automated-dispute/src/services/eboRegistry/commands/updateDisputeStatus.ts index dfd8fa1..7fcea96 100644 --- a/packages/automated-dispute/src/services/eboRegistry/commands/updateDisputeStatus.ts +++ b/packages/automated-dispute/src/services/eboRegistry/commands/updateDisputeStatus.ts @@ -1,6 +1,6 @@ import { CommandAlreadyRun, CommandNotRun, DisputeNotFound } from "../../../exceptions/index.js"; import { EboRegistry, EboRegistryCommand } from "../../../interfaces/index.js"; -import { DisputeStatus, EboEvent } from "../../../types/index.js"; +import { DisputeStatus, EboEvent, EboEventName } from "../../../types/index.js"; export class UpdateDisputeStatus implements EboRegistryCommand { private wasRun: boolean = false; @@ -13,15 +13,24 @@ export class UpdateDisputeStatus implements EboRegistryCommand { ) {} public static buildFromEvent( - event: EboEvent<"DisputeStatusChanged">, + event: EboEvent<"DisputeStatusChanged" | "DisputeEscalated">, registry: EboRegistry, ): UpdateDisputeStatus { const disputeId = event.metadata.disputeId; - const status = event.metadata.status; + + const status = this.isDisputeStatusChangedEvent(event) + ? event.metadata.status + : "Escalated"; return new UpdateDisputeStatus(registry, disputeId, status); } + private static isDisputeStatusChangedEvent( + event: EboEvent, + ): event is EboEvent<"DisputeStatusChanged"> { + return event.name === "DisputeStatusChanged"; + } + run(): void { if (this.wasRun) throw new CommandAlreadyRun(UpdateDisputeStatus.name); diff --git a/packages/automated-dispute/src/types/events.ts b/packages/automated-dispute/src/types/events.ts index 4bbb0c6..096b522 100644 --- a/packages/automated-dispute/src/types/events.ts +++ b/packages/automated-dispute/src/types/events.ts @@ -1,5 +1,5 @@ import { Caip2ChainId } from "@ebo-agent/blocknumber/dist/types.js"; -import { Log } from "viem"; +import { Address, Log } from "viem"; import { Dispute, DisputeStatus, Request, RequestId, Response } from "./prophet.js"; @@ -43,6 +43,12 @@ export interface DisputeStatusChanged { blockNumber: bigint; } +export interface DisputeEscalated { + caller: Address; + disputeId: string; + blockNumber: bigint; +} + export interface RequestFinalized { requestId: string; responseId: string; @@ -60,9 +66,11 @@ export type EboEventData = E extends "NewEpoch" ? ResponseDisputed : E extends "DisputeStatusChanged" ? DisputeStatusChanged - : E extends "RequestFinalized" - ? RequestFinalized - : never; + : E extends "DisputeEscalated" + ? DisputeEscalated + : E extends "RequestFinalized" + ? RequestFinalized + : never; export type EboEvent = { name: T; diff --git a/packages/automated-dispute/tests/services/eboMemoryRegistry/commands/updateDisputeStatus.spec.ts b/packages/automated-dispute/tests/services/eboMemoryRegistry/commands/updateDisputeStatus.spec.ts index 40e5c49..6f86c60 100644 --- a/packages/automated-dispute/tests/services/eboMemoryRegistry/commands/updateDisputeStatus.spec.ts +++ b/packages/automated-dispute/tests/services/eboMemoryRegistry/commands/updateDisputeStatus.spec.ts @@ -46,6 +46,27 @@ describe("UpdateDisputeStatus", () => { ); }); + it("escalates when the event is DisputeEscalated", () => { + const escalatedDisputeEvent: EboEvent<"DisputeEscalated"> = { + ...event, + name: "DisputeEscalated", + metadata: { + disputeId: "0x01", + blockNumber: event.blockNumber, + caller: "0x01", + }, + }; + + const command = UpdateDisputeStatus.buildFromEvent(escalatedDisputeEvent, registry); + + command.run(); + + expect(registry.updateDisputeStatus).toHaveBeenCalledWith( + event.metadata.disputeId, + "Escalated", + ); + }); + it("throws if the command was already run", () => { const command = UpdateDisputeStatus.buildFromEvent(event, registry);