Skip to content

Commit

Permalink
chore: dynamic array support
Browse files Browse the repository at this point in the history
  • Loading branch information
avimak committed Oct 22, 2023
1 parent 4bd99ac commit 231ae86
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 28 deletions.
22 changes: 2 additions & 20 deletions repository/errors-default.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,7 @@
]
},
{
"matcher": "/Execution was reverted; failure reason: \\[(.*)\\].\\n?/",
"message": "Execution was reverted; failure reason: {{0}}",
"extractors": [
{
"matcher": "/Execution was reverted; failure reason: \\[(.*)\\].\\n?/"
}
]
},
{
"matcher": "/Execution was reverted; failure reason: (.+).\\n?/",
"matcher": "/Execution was reverted; failure reason: .+.\\n?/",
"message": "Execution was reverted; failure reason: {{0}}",
"extractors": [
{
Expand All @@ -38,16 +29,7 @@
]
},
{
"matcher": "/Execution failed. Failure reason: \\[(.*)\\].\\n?/",
"message": "Execution failed. Failure reason: {{0}}",
"extractors": [
{
"matcher": "/Execution failed. Failure reason: \\[(.*)\\].\\n?/"
}
]
},
{
"matcher": "/Execution failed. Failure reason: (.+).\\n?/",
"matcher": "/Execution failed. Failure reason: .+.\\n?/",
"message": "Execution failed. Failure reason: {{0}}",
"extractors": [
{
Expand Down
17 changes: 17 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@ export const formatByType = (
default:
if (result) {
try {
if (/^\[0x[0-9a-fA-F]+(, 0x[0-9a-fA-F]+)*\]$/.test(value)) {
// do not parse via JSON.parse to avoid js precision/roundness issues
const asList = value
// remove brackets
.slice(1, -1)
.split(",")
.map(s => s.trim());
const isAsciiList = asList.every(s =>
shortString.isASCII(s)
);
if (isAsciiList) {
return asList
.map(s => shortString.decodeShortString(s))
.join(" ");
}
}

result = shortString.decodeShortString(value);
} catch {
// ignore, just return original value
Expand Down
36 changes: 28 additions & 8 deletions tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ describe("getErrorMessageFromMatcher", () => {

it("should return the formatted error message when the matcher matches and extractors are provided - reverted", () => {
const errorMatcher = {
matcher: "/Execution was reverted; failure reason: (.+).\\n?/",
matcher: "/Execution was reverted; failure reason: .+.\\n?/",
message: "Execution was reverted; failure reason: {{0}}",
extractors: [
{
Expand All @@ -185,13 +185,12 @@ describe("getErrorMessageFromMatcher", () => {

it("should return the formatted error message when the matcher matches and extractors are provided - reverted + ascii", () => {
const errorMatcher = {
matcher:
"/Execution was reverted; failure reason: \\[(.*)\\].\\n?/",
matcher: "/Execution was reverted; failure reason: .+.\\n?/",
message: "Execution was reverted; failure reason: {{0}}",
extractors: [
{
matcher:
"/Execution was reverted; failure reason: \\[(.*)\\].\\n?/",
"/Execution was reverted; failure reason: (.+).\\n?/",
},
],
};
Expand All @@ -206,9 +205,31 @@ describe("getErrorMessageFromMatcher", () => {
);
});

it("should return the formatted error message when the matcher matches and extractors are provided - reverted + ascii list", () => {
const errorMatcher = {
matcher: "/Execution was reverted; failure reason: .+.\\n?/",
message: "Execution was reverted; failure reason: {{0}}",
extractors: [
{
matcher:
"/Execution was reverted; failure reason: (.+).\\n?/",
},
],
};
const rawMessage =
"Error in the called contract (0xfff107e2403123c7df78d91728a7ee5cfd557aec0fa2d2bdc5891c286bbfff): Execution was reverted; failure reason: [0x616d6f756e743020736c697070616765, 0x616d6f756e743020736c697070616765, 0x616d6f756e743020736c697070616765].";
const errorMessage = getErrorMessageFromMatcher(
errorMatcher,
rawMessage
);
expect(errorMessage).toBe(
"Execution was reverted; failure reason: amount0 slippage amount0 slippage amount0 slippage"
);
});

it("should return the formatted error message when the matcher matches and extractors are provided - blockfier reverted", () => {
const errorMatcher = {
matcher: "/Execution failed. Failure reason: (.+).\\n?/",
matcher: "/Execution failed. Failure reason: .+.\\n?/",
message: "Execution failed. Failure reason: {{0}}",
extractors: [
{
Expand All @@ -229,12 +250,11 @@ describe("getErrorMessageFromMatcher", () => {

it("should return the formatted error message when the matcher matches and extractors are provided - blockfier reverted + ascii", () => {
const errorMatcher = {
matcher: "/Execution failed. Failure reason: \\[(.*)\\].\\n?/",
matcher: "/Execution failed. Failure reason: .+.\\n?/",
message: "Execution failed. Failure reason: {{0}}",
extractors: [
{
matcher:
"/Execution failed. Failure reason: \\[(.*)\\].\\n?/",
matcher: "/Execution failed. Failure reason: (.+).\\n?/",
},
],
};
Expand Down

0 comments on commit 231ae86

Please sign in to comment.