mirror of
https://github.com/discountry/ritmex-bot.git
synced 2026-09-10 08:48:07 +00:00
Refactor order status handling in NadoGateway and MakerEngine. Introduce isOrderActiveStatus utility to streamline order filtering logic. Add tests for error handling and order status utilities.
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { isUnknownOrderError } from "./errors";
|
||||
|
||||
describe("isUnknownOrderError", () => {
|
||||
it("matches Nado digest-not-found by code", () => {
|
||||
const err = Object.assign(
|
||||
new Error("2020: Order with the provided digest (0xdeadbeef) could not be found."),
|
||||
{ code: 2020 }
|
||||
);
|
||||
expect(isUnknownOrderError(err)).toBe(true);
|
||||
});
|
||||
|
||||
it("matches Nado digest-not-found by message", () => {
|
||||
const err = new Error("Order with the provided digest (0xdeadbeef) could not be found.");
|
||||
expect(isUnknownOrderError(err)).toBe(true);
|
||||
});
|
||||
|
||||
it("does not match unrelated errors", () => {
|
||||
expect(isUnknownOrderError(new Error("insufficient balance"))).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
+9
-1
@@ -1,4 +1,10 @@
|
||||
export function isUnknownOrderError(error: unknown): boolean {
|
||||
if (!error) return false;
|
||||
if (typeof error === "object" && error !== null && "code" in error) {
|
||||
const code = Number((error as { code?: unknown }).code);
|
||||
// Nado: 2020: Order with the provided digest ... could not be found.
|
||||
if (Number.isFinite(code) && code === 2020) return true;
|
||||
}
|
||||
const message = extractMessage(error);
|
||||
if (!message) return false;
|
||||
const upper = message.toUpperCase();
|
||||
@@ -7,7 +13,9 @@ export function isUnknownOrderError(error: unknown): boolean {
|
||||
upper.includes("CODE\":-2011") ||
|
||||
upper.includes("ORDER_ID_NOT_FOUND") ||
|
||||
upper.includes("ORDER_IS_CLOSED") ||
|
||||
upper.includes("COULD NOT FIND ORDER")
|
||||
upper.includes("COULD NOT FIND ORDER") ||
|
||||
upper.includes("ORDER WITH THE PROVIDED DIGEST") ||
|
||||
(upper.includes("COULD NOT BE FOUND") && upper.includes("ORDER"))
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { isOrderActiveStatus } from "./order-status";
|
||||
|
||||
describe("isOrderActiveStatus", () => {
|
||||
it("treats empty status as active", () => {
|
||||
expect(isOrderActiveStatus(undefined)).toBe(true);
|
||||
expect(isOrderActiveStatus("")).toBe(true);
|
||||
expect(isOrderActiveStatus(" ")).toBe(true);
|
||||
});
|
||||
|
||||
it("treats typical active statuses as active", () => {
|
||||
expect(isOrderActiveStatus("NEW")).toBe(true);
|
||||
expect(isOrderActiveStatus("PARTIALLY_FILLED")).toBe(true);
|
||||
expect(isOrderActiveStatus("waiting_price")).toBe(true);
|
||||
});
|
||||
|
||||
it("treats final/non-open statuses as inactive", () => {
|
||||
expect(isOrderActiveStatus("FILLED")).toBe(false);
|
||||
expect(isOrderActiveStatus("CANCELED")).toBe(false);
|
||||
expect(isOrderActiveStatus("CANCELLED")).toBe(false);
|
||||
expect(isOrderActiveStatus("REJECTED")).toBe(false);
|
||||
expect(isOrderActiveStatus("EXPIRED")).toBe(false);
|
||||
expect(isOrderActiveStatus("TRIGGERED")).toBe(false);
|
||||
expect(isOrderActiveStatus("CLOSED")).toBe(false);
|
||||
expect(isOrderActiveStatus("closed_by_user")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
export function isOrderActiveStatus(status: string | undefined): boolean {
|
||||
if (!status) return true;
|
||||
const normalized = status.trim().toUpperCase();
|
||||
if (!normalized) return true;
|
||||
if (normalized.includes("CLOSED")) return false;
|
||||
if (normalized === "FILLED") return false;
|
||||
if (normalized === "CANCELED" || normalized === "CANCELLED") return false;
|
||||
if (normalized === "REJECTED" || normalized === "EXPIRED") return false;
|
||||
if (normalized === "TRIGGERED") return false;
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user