Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dispute escalated event #33

Merged
merged 3 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions packages/automated-dispute/src/eboActor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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":
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<EboEventName>,
): event is EboEvent<"DisputeStatusChanged"> {
return event.name === "DisputeStatusChanged";
}

run(): void {
if (this.wasRun) throw new CommandAlreadyRun(UpdateDisputeStatus.name);

Expand Down
16 changes: 12 additions & 4 deletions packages/automated-dispute/src/types/events.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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;
Expand All @@ -60,9 +66,11 @@ export type EboEventData<E extends EboEventName> = 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<T extends EboEventName> = {
name: T;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down