Skip to content
Open
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
10 changes: 7 additions & 3 deletions scripts/error-codes.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export const errorMappings = {
16: "InvalidTarget",
17: "InvalidTreasury",
19: "ContractPaused",
20: "SelfPayout"
20: "SelfPayout",
21: "ArithmeticOverflow"
},

// Milestones contract errors
Expand All @@ -48,7 +49,8 @@ export const errorMappings = {
17: "IssueNotAllocatedForDeallocate",
18: "ContractPaused",
19: "ContributionNotFound",
20: "SelfPayout"
20: "SelfPayout",
21: "ArithmeticOverflow"
},

// Maintenance-pool contract errors
Expand All @@ -66,7 +68,9 @@ export const errorMappings = {
11: "NotDepositSponsor",
12: "DepositNotFound",
13: "ContractPaused",
14: "DepositCountOverflow"
14: "DepositCountOverflow",
15: "TooManyDeposits",
16: "ArithmeticOverflow"
}
};

Expand Down
60 changes: 60 additions & 0 deletions scripts/error-codes.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { test, describe } from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { errorMappings } from "./error-codes.mjs";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const repoRoot = path.resolve(__dirname, "..");

function parseRustErrorEnum(filePath) {
const content = fs.readFileSync(filePath, "utf8");
const mapping = {};
const regex = /^\s*([A-Za-z0-9_]+)\s*=\s*(\d+)\s*,/gm;
let match;
while ((match = regex.exec(content)) !== null) {
const [, name, code] = match;
mapping[parseInt(code, 10)] = name;
}
return mapping;
}

describe("scripts/error-codes.mjs error mapping synchronization", () => {
test("escrow errorMappings matches contracts/escrow/src/error.rs", () => {
const rustPath = path.join(repoRoot, "contracts", "escrow", "src", "error.rs");
const expected = parseRustErrorEnum(rustPath);

const actual = {};
for (const [k, v] of Object.entries(errorMappings.escrow)) {
actual[parseInt(k, 10)] = v;
}

assert.deepStrictEqual(actual, expected);
});

test("milestones errorMappings matches contracts/milestones/src/error.rs", () => {
const rustPath = path.join(repoRoot, "contracts", "milestones", "src", "error.rs");
const expected = parseRustErrorEnum(rustPath);

const actual = {};
for (const [k, v] of Object.entries(errorMappings.milestones)) {
actual[parseInt(k, 10)] = v;
}

assert.deepStrictEqual(actual, expected);
});

test("maintenancePool errorMappings matches contracts/maintenance-pool/src/error.rs", () => {
const rustPath = path.join(repoRoot, "contracts", "maintenance-pool", "src", "error.rs");
const expected = parseRustErrorEnum(rustPath);

const actual = {};
for (const [k, v] of Object.entries(errorMappings.maintenancePool)) {
actual[parseInt(k, 10)] = v;
}

assert.deepStrictEqual(actual, expected);
});
});