Skip to content

Commit

Permalink
Fixed custom error decoding (#3785).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Feb 16, 2023
1 parent e1e0929 commit 4d9b29d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
10 changes: 7 additions & 3 deletions src.ts/abi/abi-coder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ function getBuiltinCallException(action: CallExceptionAction, tx: { to?: null |
const bytes = getBytes(data);
data = hexlify(data);

if (bytes.length % 32 !== 4) {
if (bytes.length === 0) {
message += " (no data present; likely require(false) occurred";
reason = "require(false)";

} else if (bytes.length % 32 !== 4) {
message += " (could not decode reason; invalid data length)";

} else if (hexlify(bytes.slice(0, 4)) === "0x08c379a0") {
Expand All @@ -83,7 +87,7 @@ function getBuiltinCallException(action: CallExceptionAction, tx: { to?: null |
message += `: ${ JSON.stringify(reason) }`;

} catch (error) {
message += " (could not decode reason; invalid data)";
message += " (could not decode reason; invalid string data)";
}

} else if (hexlify(bytes.slice(0, 4)) === "0x4e487b71") {
Expand All @@ -98,7 +102,7 @@ function getBuiltinCallException(action: CallExceptionAction, tx: { to?: null |
reason = `Panic due to ${ PanicReasons.get(code) || "UNKNOWN" }(${ code })`;
message += `: ${ reason }`;
} catch (error) {
message += " (could not decode panic reason)";
message += " (could not decode panic code)";
}
} else {
message += " (unknown custom error)";
Expand Down
9 changes: 4 additions & 5 deletions src.ts/abi/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -777,17 +777,16 @@ export class Interface {
const error = AbiCoder.getBuiltinCallException("call", tx, data);

// Not a built-in error; try finding a custom error
if (!error.message.match(/could not decode/)) {
const customPrefix = "execution reverted (unknown custom error)";
if (error.message.startsWith(customPrefix)) {
const selector = hexlify(data.slice(0, 4));

error.message = "execution reverted (unknown custom error)";
const ef = this.getError(selector);
if (ef) {
try {
const args = this.#abiCoder.decode(ef.inputs, data.slice(4));
error.revert = {
name: ef.name,
signature: ef.format(),
args: this.#abiCoder.decode(ef.inputs, data.slice(4))
name: ef.name, signature: ef.format(), args
};
error.reason = error.revert.signature;
error.message = `execution reverted: ${ error.reason }`
Expand Down
2 changes: 1 addition & 1 deletion src.ts/utils/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ export function makeError<K extends ErrorCode, T extends CodedEthersError<K>>(me

defineProperties<EthersError>(<EthersError>error, { code });

if (info) { defineProperties<any>(error, info); }
if (info) { Object.assign(error, info); }

return <T>error;
}
Expand Down

0 comments on commit 4d9b29d

Please sign in to comment.