Skip to content

Commit

Permalink
Added EIP-838 error name as well as error signature (#1498).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Apr 26, 2021
1 parent 987bec8 commit 483d67f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
15 changes: 9 additions & 6 deletions packages/abi/src.ts/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ export class Indexed extends Description<Indexed> {
}
}

const BuiltinErrors: Record<string, { signature: string, inputs: Array<string>, reason?: boolean }> = {
"0x08c379a0": { signature: "Error(string)", inputs: [ "string" ], reason: true },
"0x4e487b71": { signature: "Panic(uint256)", inputs: [ "uint256" ] }
const BuiltinErrors: Record<string, { signature: string, inputs: Array<string>, name: string, reason?: boolean }> = {
"0x08c379a0": { signature: "Error(string)", name: "Error", inputs: [ "string" ], reason: true },
"0x4e487b71": { signature: "Panic(uint256)", name: "Panic", inputs: [ "uint256" ] }
}

function wrapAccessError(property: string, error: Error): Error {
Expand Down Expand Up @@ -350,6 +350,7 @@ export class Interface {

let reason: string = null;
let errorArgs: Result = null;
let errorName: string = null;
let errorSignature: string = null;
switch (bytes.length % this._abiCoder._getWordSize()) {
case 0:
Expand All @@ -362,14 +363,16 @@ export class Interface {
const selector = hexlify(bytes.slice(0, 4));
const builtin = BuiltinErrors[selector];
if (builtin) {
errorSignature = builtin.signature;
errorArgs = this._abiCoder.decode(builtin.inputs, bytes.slice(4));
errorName = builtin.name;
errorSignature = builtin.signature;
if (builtin.reason) { reason = errorArgs[0]; }
} else {
try {
const error = this.getError(selector);
errorSignature = error.format();
errorArgs = this._abiCoder.decode(error.inputs, bytes.slice(4));
errorName = error.name;
errorSignature = error.format();
} catch (error) {
console.log(error);
}
Expand All @@ -380,7 +383,7 @@ export class Interface {

return logger.throwError("call revert exception", Logger.errors.CALL_EXCEPTION, {
method: functionFragment.format(),
errorSignature, errorArgs, reason
errorArgs, errorName, errorSignature, reason
});
}

Expand Down
1 change: 1 addition & 0 deletions packages/tests/src.ts/test-contract-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,7 @@ describe('Test EIP-838 Error Codes', function() {
} catch (error) {
assert.equal(error.code, ethers.utils.Logger.errors.CALL_EXCEPTION, "error.code");
assert.equal(error.errorSignature, "TestError1(address,uint256)", "error.errorSignature");
assert.equal(error.errorName, "TestError1", "error.errorName");
assert.equal(error.errorArgs[0], addr, "error.errorArgs[0]");
assert.equal(error.errorArgs.addr, addr, "error.errorArgs.addr");
assert.equal(error.errorArgs[1], 42, "error.errorArgs[1]");
Expand Down

0 comments on commit 483d67f

Please sign in to comment.