@nomicfoundation/hardhat-utils 3.0.3 → 3.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,15 @@
1
+ /**
2
+ * Converts a Solidity panic error code into a human-readable revert message.
3
+ *
4
+ * Solidity defines a set of standardized panic codes (0x01, 0x11, etc.)
5
+ * that represent specific runtime errors (e.g. arithmetic overflow).
6
+ * This function looks up the corresponding reason string and formats it
7
+ * into a message similar to what clients like Hardhat or ethers.js display.
8
+ *
9
+ * @param errorCode The panic error code returned by the EVM as a bigint.
10
+ * @returns A formatted message string:
11
+ * - `"reverted with panic code <hex> (<reason>)"` if the code is recognized.
12
+ * - `"reverted with unknown panic code <hex>"` if the code is not recognized.
13
+ */
1
14
  export declare function panicErrorCodeToMessage(errorCode: bigint): string;
2
15
  //# sourceMappingURL=panic-errors.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"panic-errors.d.ts","sourceRoot":"","sources":["../../src/panic-errors.ts"],"names":[],"mappings":"AAEA,wBAAgB,uBAAuB,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAQjE"}
1
+ {"version":3,"file":"panic-errors.d.ts","sourceRoot":"","sources":["../../src/panic-errors.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;GAYG;AACH,wBAAgB,uBAAuB,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAQjE"}
@@ -1,4 +1,18 @@
1
1
  import { numberToHexString } from "./hex.js";
2
+ import { panicErrorCodeToReason } from "./internal/panic-errors.js";
3
+ /**
4
+ * Converts a Solidity panic error code into a human-readable revert message.
5
+ *
6
+ * Solidity defines a set of standardized panic codes (0x01, 0x11, etc.)
7
+ * that represent specific runtime errors (e.g. arithmetic overflow).
8
+ * This function looks up the corresponding reason string and formats it
9
+ * into a message similar to what clients like Hardhat or ethers.js display.
10
+ *
11
+ * @param errorCode The panic error code returned by the EVM as a bigint.
12
+ * @returns A formatted message string:
13
+ * - `"reverted with panic code <hex> (<reason>)"` if the code is recognized.
14
+ * - `"reverted with unknown panic code <hex>"` if the code is not recognized.
15
+ */
2
16
  export function panicErrorCodeToMessage(errorCode) {
3
17
  const reason = panicErrorCodeToReason(errorCode);
4
18
  if (reason !== undefined) {
@@ -6,27 +20,4 @@ export function panicErrorCodeToMessage(errorCode) {
6
20
  }
7
21
  return `reverted with unknown panic code ${numberToHexString(errorCode)}`;
8
22
  }
9
- function panicErrorCodeToReason(errorCode) {
10
- // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- we are only covering some of the integer range
11
- switch (errorCode) {
12
- case 0x1n:
13
- return "Assertion error";
14
- case 0x11n:
15
- return "Arithmetic operation overflowed outside of an unchecked block";
16
- case 0x12n:
17
- return "Division or modulo division by zero";
18
- case 0x21n:
19
- return "Tried to convert a value into an enum, but the value was too big or negative";
20
- case 0x22n:
21
- return "Incorrectly encoded storage byte array";
22
- case 0x31n:
23
- return ".pop() was called on an empty array";
24
- case 0x32n:
25
- return "Array accessed at an out-of-bounds or negative index";
26
- case 0x41n:
27
- return "Too much memory was allocated, or an array was created that is too large";
28
- case 0x51n:
29
- return "Called a zero-initialized variable of internal function type";
30
- }
31
- }
32
23
  //# sourceMappingURL=panic-errors.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"panic-errors.js","sourceRoot":"","sources":["../../src/panic-errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAE7C,MAAM,UAAU,uBAAuB,CAAC,SAAiB;IACvD,MAAM,MAAM,GAAG,sBAAsB,CAAC,SAAS,CAAC,CAAC;IAEjD,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;QACzB,OAAO,4BAA4B,iBAAiB,CAAC,SAAS,CAAC,KAAK,MAAM,GAAG,CAAC;IAChF,CAAC;IAED,OAAO,oCAAoC,iBAAiB,CAAC,SAAS,CAAC,EAAE,CAAC;AAC5E,CAAC;AAED,SAAS,sBAAsB,CAAC,SAAiB;IAC/C,4HAA4H;IAC5H,QAAQ,SAAS,EAAE,CAAC;QAClB,KAAK,IAAI;YACP,OAAO,iBAAiB,CAAC;QAC3B,KAAK,KAAK;YACR,OAAO,+DAA+D,CAAC;QACzE,KAAK,KAAK;YACR,OAAO,qCAAqC,CAAC;QAC/C,KAAK,KAAK;YACR,OAAO,8EAA8E,CAAC;QACxF,KAAK,KAAK;YACR,OAAO,wCAAwC,CAAC;QAClD,KAAK,KAAK;YACR,OAAO,qCAAqC,CAAC;QAC/C,KAAK,KAAK;YACR,OAAO,sDAAsD,CAAC;QAChE,KAAK,KAAK;YACR,OAAO,0EAA0E,CAAC;QACpF,KAAK,KAAK;YACR,OAAO,8DAA8D,CAAC;IAC1E,CAAC;AACH,CAAC"}
1
+ {"version":3,"file":"panic-errors.js","sourceRoot":"","sources":["../../src/panic-errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAC7C,OAAO,EAAE,sBAAsB,EAAE,MAAM,4BAA4B,CAAC;AAEpE;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,uBAAuB,CAAC,SAAiB;IACvD,MAAM,MAAM,GAAG,sBAAsB,CAAC,SAAS,CAAC,CAAC;IAEjD,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;QACzB,OAAO,4BAA4B,iBAAiB,CAAC,SAAS,CAAC,KAAK,MAAM,GAAG,CAAC;IAChF,CAAC;IAED,OAAO,oCAAoC,iBAAiB,CAAC,SAAS,CAAC,EAAE,CAAC;AAC5E,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nomicfoundation/hardhat-utils",
3
- "version": "3.0.3",
3
+ "version": "3.0.4",
4
4
  "description": "Utilities for Hardhat and its plugins",
5
5
  "homepage": "https://github.com/nomicfoundation/hardhat/tree/v-next/v-next/hardhat-utils",
6
6
  "repository": {
@@ -23,6 +23,7 @@
23
23
  "./env": "./dist/src/env.js",
24
24
  "./error": "./dist/src/error.js",
25
25
  "./eth": "./dist/src/eth.js",
26
+ "./format": "./dist/src/format.js",
26
27
  "./fs": "./dist/src/fs.js",
27
28
  "./global-dir": "./dist/src/global-dir.js",
28
29
  "./hex": "./dist/src/hex.js",
package/src/format.ts ADDED
@@ -0,0 +1,267 @@
1
+ import {
2
+ getColumnWidths,
3
+ getContentWidth,
4
+ getHeadingWidth,
5
+ getStringWidth,
6
+ renderContentLine,
7
+ renderHeaderOpen,
8
+ renderRowSeparator,
9
+ renderSectionClose,
10
+ } from "./internal/format.js";
11
+
12
+ export type TableRow = string[];
13
+ export interface TableDivider {
14
+ type: "divider";
15
+ }
16
+ export type TableItem = TableRow | TableDivider;
17
+
18
+ export const divider: TableDivider = { type: "divider" };
19
+
20
+ /**
21
+ * Formats an array of rows and dividers into a table string.
22
+ *
23
+ * @param items An array of table rows (string arrays) and dividers.
24
+ * Dividers are objects with type: "divider" and will be rendered as table dividers.
25
+ * @returns The formatted table as a string, ready to be rendered.
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * formatTable([
30
+ * ["Name", "Age"],
31
+ * divider,
32
+ * ["Alice", "30"],
33
+ * ["Bob", "25"],
34
+ * divider,
35
+ * ["Average", "27.5"]
36
+ * ]);
37
+ *
38
+ * // =>
39
+ * // | Name | Age |
40
+ * // | ------- | ---- |
41
+ * // | Alice | 30 |
42
+ * // | Bob | 25 |
43
+ * // | ------- | ---- |
44
+ * // | Average | 27.5 |
45
+ * ```
46
+ */
47
+ export function formatTable(items: TableItem[]): string {
48
+ const widths: number[] = [];
49
+ const dataRows: string[][] = [];
50
+
51
+ for (const item of items) {
52
+ if (Array.isArray(item)) {
53
+ dataRows.push([...item]);
54
+ }
55
+ }
56
+
57
+ // Calculate maximum width for each column
58
+ for (const row of dataRows) {
59
+ for (let i = 0; i < row.length; i++) {
60
+ while (i >= widths.length) {
61
+ widths.push(0);
62
+ }
63
+ widths[i] = Math.max(widths[i], getStringWidth(row[i]));
64
+ }
65
+ }
66
+
67
+ const dividerRow = widths.map((width) => "-".repeat(width));
68
+ const outputRows: string[][] = [];
69
+
70
+ for (const item of items) {
71
+ if (Array.isArray(item)) {
72
+ const row = [...item];
73
+ // Pad the row to match the number of columns
74
+ while (row.length < widths.length) {
75
+ row.push("");
76
+ }
77
+ outputRows.push(row);
78
+ } else {
79
+ outputRows.push([...dividerRow]);
80
+ }
81
+ }
82
+
83
+ outputRows.forEach((row) => {
84
+ for (let i = 0; i < row.length; i++) {
85
+ const displayWidth = getStringWidth(row[i]);
86
+ const actualLength = row[i].length;
87
+ // Adjust padding to account for difference between display width and actual length
88
+ row[i] = row[i].padEnd(widths[i] + actualLength - displayWidth);
89
+ }
90
+ });
91
+
92
+ return outputRows.map((row) => `| ${row.join(" | ")} |`).join("\n");
93
+ }
94
+
95
+ export interface TableTitleV2 {
96
+ type: "title";
97
+ text: string;
98
+ }
99
+
100
+ export interface TableSectionHeaderV2 {
101
+ type: "section-header";
102
+ text: string;
103
+ }
104
+
105
+ export interface TableHeaderV2 {
106
+ type: "header";
107
+ cells: string[];
108
+ }
109
+
110
+ export interface TableRowV2 {
111
+ type: "row";
112
+ cells: string[];
113
+ }
114
+
115
+ export type TableItemV2 =
116
+ | TableTitleV2
117
+ | TableSectionHeaderV2
118
+ | TableHeaderV2
119
+ | TableRowV2;
120
+
121
+ /**
122
+ * Formats an array of titles, section headers, headers, and rows into a table
123
+ * string with box-drawing characters.
124
+ *
125
+ * Features:
126
+ * - Titles are centered in a standalone box with double borders (╔═╗)
127
+ * - Section headers group related content with automatic closing
128
+ * - Headers and rows can have different numbers of cells
129
+ * - Rows with fewer cells than max columns are handled with special rendering
130
+ *
131
+ * @param items An array of table items (titles, section headers, headers, and rows).
132
+ * Sections are automatically closed when a new section-header or title appears, or
133
+ * at the end of the table.
134
+ * @returns The formatted table as a string, ready to be rendered.
135
+ *
136
+ * @example
137
+ * ```ts
138
+ * formatTableV2([
139
+ * { type: "title", text: "My Table" },
140
+ * { type: "section-header", text: "User Data" },
141
+ * { type: "header", cells: ["Name", "Age", "City"] },
142
+ * { type: "row", cells: ["Alice", "30", "NYC"] },
143
+ * { type: "row", cells: ["Bob", "25", "LA"] },
144
+ * { type: "section-header", text: "Summary" },
145
+ * { type: "header", cells: ["Total", "Count"] },
146
+ * { type: "row", cells: ["55", "2"] }
147
+ * ]);
148
+ *
149
+ * // =>
150
+ * // ╔═══════════════════╗
151
+ * // ║ My Table ║
152
+ * // ╚═══════════════════╝
153
+ * // ╔═══════════════════╗
154
+ * // ║ User Data ║
155
+ * // ╟───────┬─────┬─────╢
156
+ * // ║ Name │ Age │ City║
157
+ * // ╟───────┼─────┼─────╢
158
+ * // ║ Alice │ 30 │ NYC ║
159
+ * // ╟───────┼─────┼─────╢
160
+ * // ║ Bob │ 25 │ LA ║
161
+ * // ╚═══════╧═════╧═════╝
162
+ * // ╔═══════════════════╗
163
+ * // ║ Summary ║
164
+ * // ╟───────┬───────────╢
165
+ * // ║ Total │ Count ║
166
+ * // ╟───────┼───────────╢
167
+ * // ║ 55 │ 2 ║
168
+ * // ╚═══════╧═══════════╝
169
+ * ```
170
+ */
171
+ export function formatTableV2(items: TableItemV2[]): string {
172
+ if (items.length === 0) {
173
+ return "";
174
+ }
175
+
176
+ const columnWidths = getColumnWidths(items);
177
+ const contentWidth = getContentWidth(columnWidths);
178
+ const headingWidth = getHeadingWidth(items);
179
+
180
+ // If heading is wider than content, expand last column to fit
181
+ if (headingWidth > contentWidth && columnWidths.length > 0) {
182
+ const extraSpace = headingWidth - contentWidth;
183
+ columnWidths[columnWidths.length - 1] += extraSpace;
184
+ }
185
+
186
+ const tableWidth = Math.max(contentWidth, headingWidth);
187
+
188
+ const lines: string[] = [];
189
+ let previousCellCount = 0; // Keep track of previous row/header cell count
190
+ let inSection = false;
191
+
192
+ for (let i = 0; i < items.length; i++) {
193
+ const [previous, current] = [items[i - 1], items[i]];
194
+
195
+ if (current.type === "title") {
196
+ if (inSection) {
197
+ lines.push(renderSectionClose(columnWidths, previousCellCount));
198
+ inSection = false;
199
+ }
200
+
201
+ lines.push("╔" + "═".repeat(tableWidth) + "╗");
202
+ const titleDisplayWidth = getStringWidth(current.text);
203
+ const titleActualLength = current.text.length;
204
+ const centeredTitle = current.text
205
+ .padStart(
206
+ (tableWidth + titleDisplayWidth) / 2 +
207
+ (titleActualLength - titleDisplayWidth),
208
+ )
209
+ .padEnd(tableWidth + (titleActualLength - titleDisplayWidth));
210
+ lines.push("║" + centeredTitle + "║");
211
+ lines.push("╚" + "═".repeat(tableWidth) + "╝");
212
+ } else if (current.type === "section-header") {
213
+ if (inSection) {
214
+ lines.push(renderSectionClose(columnWidths, previousCellCount));
215
+ }
216
+
217
+ lines.push("╔" + "═".repeat(tableWidth) + "╗");
218
+ const headerDisplayWidth = getStringWidth(current.text);
219
+ const headerActualLength = current.text.length;
220
+ const paddedHeader = current.text.padEnd(
221
+ tableWidth - 2 + (headerActualLength - headerDisplayWidth),
222
+ );
223
+ lines.push("║ " + paddedHeader + " ║");
224
+ inSection = true;
225
+ } else if (current.type === "header") {
226
+ const currentCellCount = current.cells.length;
227
+ const innerJoiner =
228
+ previous !== undefined && previous.type === "section-header"
229
+ ? "┬"
230
+ : "┼";
231
+ const needsTransition =
232
+ previous !== undefined &&
233
+ previous.type !== "section-header" &&
234
+ currentCellCount < previousCellCount;
235
+
236
+ lines.push(
237
+ renderHeaderOpen(
238
+ columnWidths,
239
+ currentCellCount,
240
+ innerJoiner,
241
+ needsTransition,
242
+ ),
243
+ );
244
+ lines.push(
245
+ renderContentLine(current.cells, columnWidths, currentCellCount),
246
+ );
247
+ previousCellCount = currentCellCount;
248
+ } else if (current.type === "row") {
249
+ const currentCellCount = current.cells.length;
250
+
251
+ // Only add separator if previous wasn't a row
252
+ if (previous === undefined || previous.type !== "row") {
253
+ lines.push(renderRowSeparator(columnWidths, currentCellCount));
254
+ }
255
+ lines.push(
256
+ renderContentLine(current.cells, columnWidths, currentCellCount),
257
+ );
258
+ previousCellCount = currentCellCount;
259
+ }
260
+ }
261
+
262
+ if (inSection) {
263
+ lines.push(renderSectionClose(columnWidths, previousCellCount));
264
+ }
265
+
266
+ return lines.join("\n");
267
+ }
@@ -0,0 +1,260 @@
1
+ import type { TableItemV2 } from "../format.js";
2
+
3
+ /**
4
+ * Calculate the display width of a string by removing ANSI escape codes.
5
+ *
6
+ * NOTE: This implementation only removes basic ANSI color/style codes and may
7
+ * not handle all escape sequences (e.g., cursor movement, complex control
8
+ * sequences).
9
+ */
10
+ export function getStringWidth(str: string): number {
11
+ // Remove ANSI escape codes if present
12
+ const stripped = str.replace(/\u001b\[[0-9;]*m/g, "");
13
+ return stripped.length;
14
+ }
15
+
16
+ /**
17
+ * Calculates the minimum width needed by each column in the table
18
+ * to fit its content (accounting for ANSI color codes).
19
+ */
20
+ export function getColumnWidths(items: TableItemV2[]): number[] {
21
+ const columnWidths: number[] = [];
22
+
23
+ for (const item of items) {
24
+ if (item.type === "row" || item.type === "header") {
25
+ item.cells.forEach((cell, i) => {
26
+ columnWidths[i] = Math.max(columnWidths[i] ?? 0, getStringWidth(cell));
27
+ });
28
+ }
29
+ }
30
+
31
+ return columnWidths;
32
+ }
33
+
34
+ /**
35
+ * Calculates the inner width needed to fit the rows and headers
36
+ * (excludes borders, which are added during rendering).
37
+ *
38
+ * Each column is padded by 1 space on each side, and columns are
39
+ * separated by " │ " (3 spaces).
40
+ */
41
+ export function getContentWidth(columnWidths: number[]): number {
42
+ return (
43
+ columnWidths.reduce((sum, w) => sum + w, 0) +
44
+ (columnWidths.length - 1) * 3 +
45
+ 2
46
+ );
47
+ }
48
+
49
+ /**
50
+ * Calculates the inner width needed to fit titles and section headers
51
+ * (excludes borders, which are added during rendering).
52
+ *
53
+ * Each title/header is padded by 1 space on each side.
54
+ * Accounts for ANSI color codes.
55
+ */
56
+ export function getHeadingWidth(items: TableItemV2[]): number {
57
+ let headingWidth = 0;
58
+ for (const item of items) {
59
+ if (item.type === "section-header" || item.type === "title") {
60
+ headingWidth = Math.max(headingWidth, getStringWidth(item.text) + 2);
61
+ }
62
+ }
63
+ return headingWidth;
64
+ }
65
+
66
+ /**
67
+ * Calculates the width needed for unused columns when a row/header has fewer
68
+ * cells than the total column count (e.g., if table has 6 columns but row
69
+ * only has 2 cells, calculates space for the remaining 4 columns).
70
+ */
71
+ export function getUnusedColumnsWidth(
72
+ columnWidths: number[],
73
+ previousCellCount: number,
74
+ ): number {
75
+ const remainingWidths = columnWidths.slice(previousCellCount);
76
+ return remainingWidths.reduce((sum, w) => sum + w + 3, 0) - 3;
77
+ }
78
+
79
+ /**
80
+ * Renders a horizontal rule segment by repeating a character for each column
81
+ * with padding, joined by a separator (e.g., "─────┼─────┼─────").
82
+ */
83
+ export function renderRuleSegment(
84
+ columnWidths: number[],
85
+ char: string,
86
+ joiner: string,
87
+ ): string {
88
+ return columnWidths.map((w) => char.repeat(w + 2)).join(joiner);
89
+ }
90
+
91
+ /**
92
+ * Renders a complete horizontal rule with left and right borders
93
+ * (e.g., "╟─────┼─────┼─────╢").
94
+ */
95
+ export function renderHorizontalRule(
96
+ leftBorder: string,
97
+ columnWidths: number[],
98
+ char: string,
99
+ joiner: string,
100
+ rightBorder: string,
101
+ ): string {
102
+ return (
103
+ leftBorder + renderRuleSegment(columnWidths, char, joiner) + rightBorder
104
+ );
105
+ }
106
+
107
+ /**
108
+ * Renders a content line containing cells from either a header or row.
109
+ *
110
+ * Handles two cases:
111
+ * - Full width: When all columns are used, cells are separated by " │ " and
112
+ * line ends with " ║" (e.g., "║ cell1 │ cell2 │ cell3 ║")
113
+ * - Short line: When fewer columns are used, active cells are followed by
114
+ * " │ " and empty space, ending with "║" (e.g., "║ cell1 │ cell2 │ ║")
115
+ *
116
+ * Accounts for ANSI color codes when padding cells.
117
+ */
118
+ export function renderContentLine(
119
+ cells: string[],
120
+ columnWidths: number[],
121
+ currentCellCount: number,
122
+ ): string {
123
+ if (currentCellCount === columnWidths.length) {
124
+ return (
125
+ "║ " +
126
+ cells
127
+ .map((cell, j) => {
128
+ const displayWidth = getStringWidth(cell);
129
+ const actualLength = cell.length;
130
+ // Adjust padding to account for ANSI escape codes
131
+ return cell.padEnd(columnWidths[j] + actualLength - displayWidth);
132
+ })
133
+ .join(" │ ") +
134
+ " ║"
135
+ );
136
+ } else {
137
+ const usedWidths = columnWidths.slice(0, currentCellCount);
138
+ const remainingWidth = getUnusedColumnsWidth(
139
+ columnWidths,
140
+ currentCellCount,
141
+ );
142
+ return (
143
+ "║ " +
144
+ cells
145
+ .map((cell, j) => {
146
+ const displayWidth = getStringWidth(cell);
147
+ const actualLength = cell.length;
148
+ // Adjust padding to account for ANSI escape codes
149
+ return cell.padEnd(usedWidths[j] + actualLength - displayWidth);
150
+ })
151
+ .join(" │ ") +
152
+ " │ " +
153
+ " ".repeat(remainingWidth + 1) +
154
+ "║"
155
+ );
156
+ }
157
+ }
158
+
159
+ /**
160
+ * Renders the horizontal rule that appears above a header row.
161
+ *
162
+ * Handles three cases:
163
+ * - Transition rule: When going from more columns to fewer, shows ┴ marks
164
+ * where columns collapse (e.g., "╟───┼───┼───┴───┴───╢")
165
+ * - Full width: When header uses all columns (e.g., "╟───┬───┬───╢" or "╟───┼───┼───╢")
166
+ * - Short header: When header uses fewer columns than max (e.g., "╟───┬─────────╢")
167
+ *
168
+ * The innerJoiner determines the separator character: ┬ after section-header, ┼ otherwise.
169
+ */
170
+ export function renderHeaderOpen(
171
+ columnWidths: number[],
172
+ currentCellCount: number,
173
+ innerJoiner: string,
174
+ needsTransition: boolean,
175
+ ): string {
176
+ if (needsTransition) {
177
+ const usedWidths = columnWidths.slice(0, currentCellCount);
178
+ const collapsingWidths = columnWidths.slice(currentCellCount);
179
+ return (
180
+ "╟" +
181
+ renderRuleSegment(usedWidths, "─", "┼") +
182
+ "┼" +
183
+ renderRuleSegment(collapsingWidths, "─", "┴") +
184
+ "╢"
185
+ );
186
+ } else if (currentCellCount === columnWidths.length) {
187
+ return renderHorizontalRule("╟", columnWidths, "─", innerJoiner, "╢");
188
+ } else {
189
+ const usedWidths = columnWidths.slice(0, currentCellCount);
190
+ const remainingWidth = getUnusedColumnsWidth(
191
+ columnWidths,
192
+ currentCellCount,
193
+ );
194
+ return (
195
+ "╟" +
196
+ renderRuleSegment(usedWidths, "─", innerJoiner) +
197
+ innerJoiner +
198
+ "─".repeat(remainingWidth + 2) +
199
+ "╢"
200
+ );
201
+ }
202
+ }
203
+
204
+ /**
205
+ * Renders the horizontal rule that appears above a row.
206
+ *
207
+ * Handles two cases:
208
+ * - Full width: When row uses all columns, renders with ┼ joiners and
209
+ * ends with ╢ (e.g., "╟───┼───┼───╢")
210
+ * - Short row: When row uses fewer columns, renders active columns with
211
+ * ┼ joiners, ends with ┤, then fills remaining space and ends with ║
212
+ * (e.g., "╟───┼───┤ ║")
213
+ */
214
+ export function renderRowSeparator(
215
+ columnWidths: number[],
216
+ currentCellCount: number,
217
+ ): string {
218
+ if (currentCellCount === columnWidths.length) {
219
+ return renderHorizontalRule("╟", columnWidths, "─", "┼", "╢");
220
+ } else {
221
+ // Short row - ends with ┤ instead of ╢
222
+ const usedWidths = columnWidths.slice(0, currentCellCount);
223
+ const remainingWidth = getUnusedColumnsWidth(
224
+ columnWidths,
225
+ currentCellCount,
226
+ );
227
+ return (
228
+ "╟" +
229
+ renderRuleSegment(usedWidths, "─", "┼") +
230
+ "┤" +
231
+ " ".repeat(remainingWidth + 2) +
232
+ "║"
233
+ );
234
+ }
235
+ }
236
+
237
+ /**
238
+ * Renders the section's bottom border, placing ╧ marks under column
239
+ * separators where the last row/header had cells (e.g., if the last row
240
+ * looked like "║ a │ b │ ║", the bottom border would be
241
+ * "╚═══╧═══╧═══════╝").
242
+ */
243
+ export function renderSectionClose(
244
+ columnWidths: number[],
245
+ previousCellCount: number,
246
+ ): string {
247
+ if (previousCellCount === columnWidths.length) {
248
+ return renderHorizontalRule("╚", columnWidths, "═", "╧", "╝");
249
+ } else {
250
+ const usedWidths = columnWidths.slice(0, previousCellCount);
251
+ const unusedWidth = getUnusedColumnsWidth(columnWidths, previousCellCount);
252
+ return (
253
+ "╚" +
254
+ renderRuleSegment(usedWidths, "═", "╧") +
255
+ "╧" +
256
+ renderRuleSegment([unusedWidth], "═", "") +
257
+ "╝"
258
+ );
259
+ }
260
+ }
@@ -0,0 +1,23 @@
1
+ export function panicErrorCodeToReason(errorCode: bigint): string | undefined {
2
+ // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- we are only covering some of the integer range
3
+ switch (errorCode) {
4
+ case 0x1n:
5
+ return "Assertion error";
6
+ case 0x11n:
7
+ return "Arithmetic operation overflowed outside of an unchecked block";
8
+ case 0x12n:
9
+ return "Division or modulo division by zero";
10
+ case 0x21n:
11
+ return "Tried to convert a value into an enum, but the value was too big or negative";
12
+ case 0x22n:
13
+ return "Incorrectly encoded storage byte array";
14
+ case 0x31n:
15
+ return ".pop() was called on an empty array";
16
+ case 0x32n:
17
+ return "Array accessed at an out-of-bounds or negative index";
18
+ case 0x41n:
19
+ return "Too much memory was allocated, or an array was created that is too large";
20
+ case 0x51n:
21
+ return "Called a zero-initialized variable of internal function type";
22
+ }
23
+ }
@@ -1,5 +1,19 @@
1
1
  import { numberToHexString } from "./hex.js";
2
+ import { panicErrorCodeToReason } from "./internal/panic-errors.js";
2
3
 
4
+ /**
5
+ * Converts a Solidity panic error code into a human-readable revert message.
6
+ *
7
+ * Solidity defines a set of standardized panic codes (0x01, 0x11, etc.)
8
+ * that represent specific runtime errors (e.g. arithmetic overflow).
9
+ * This function looks up the corresponding reason string and formats it
10
+ * into a message similar to what clients like Hardhat or ethers.js display.
11
+ *
12
+ * @param errorCode The panic error code returned by the EVM as a bigint.
13
+ * @returns A formatted message string:
14
+ * - `"reverted with panic code <hex> (<reason>)"` if the code is recognized.
15
+ * - `"reverted with unknown panic code <hex>"` if the code is not recognized.
16
+ */
3
17
  export function panicErrorCodeToMessage(errorCode: bigint): string {
4
18
  const reason = panicErrorCodeToReason(errorCode);
5
19
 
@@ -9,27 +23,3 @@ export function panicErrorCodeToMessage(errorCode: bigint): string {
9
23
 
10
24
  return `reverted with unknown panic code ${numberToHexString(errorCode)}`;
11
25
  }
12
-
13
- function panicErrorCodeToReason(errorCode: bigint): string | undefined {
14
- // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- we are only covering some of the integer range
15
- switch (errorCode) {
16
- case 0x1n:
17
- return "Assertion error";
18
- case 0x11n:
19
- return "Arithmetic operation overflowed outside of an unchecked block";
20
- case 0x12n:
21
- return "Division or modulo division by zero";
22
- case 0x21n:
23
- return "Tried to convert a value into an enum, but the value was too big or negative";
24
- case 0x22n:
25
- return "Incorrectly encoded storage byte array";
26
- case 0x31n:
27
- return ".pop() was called on an empty array";
28
- case 0x32n:
29
- return "Array accessed at an out-of-bounds or negative index";
30
- case 0x41n:
31
- return "Too much memory was allocated, or an array was created that is too large";
32
- case 0x51n:
33
- return "Called a zero-initialized variable of internal function type";
34
- }
35
- }