Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit b1699ce

Browse files
committed
Interpret undecodeable data as custom error only when length is 4 mod 32
1 parent 591d3ac commit b1699ce

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

packages/contract/lib/reason.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,22 @@ const reason = {
8181
return undefined;
8282
}
8383
} else {
84-
//we can't reasonably handle custom errors here
85-
//(but we can probably assume it is one?)
86-
return "Custom error (could not decode)";
84+
const bytesLength = (rawData.length - 2) / 2; //length of raw data in bytes
85+
if (bytesLength % 32 === 4) {
86+
//we can't reasonably handle custom errors here at present, sorry
87+
return "Custom error (could not decode)";
88+
} else {
89+
//if the length isn't 4 mod 32, just give up and return undefined.
90+
//the reason for this is that sometimes this function can accidentally get
91+
//called on a return value rather than an error (because the tx ran out of
92+
//gas or failed for a reason other than a revert, e.g., getting refused by
93+
//the user in MetaMask), meaning the eth_call rerun will *succeed*, potentially
94+
//resulting in a return value. We don't want to attach an additional
95+
//error message in that case, so we return undefined.
96+
//(What if e.g. the tx is refused by the user in MetaMask, but the rerun yields
97+
//a revert string...? Well, that's a problem for another time...)
98+
return undefined;
99+
}
87100
}
88101
},
89102

0 commit comments

Comments
 (0)