Added EIP-838 error name as well as error signature (#1498).

This commit is contained in:
Richard Moore 2021-04-26 17:46:16 -04:00
parent 987bec87af
commit 483d67f55c
No known key found for this signature in database
GPG Key ID: 665176BE8E9DC651
2 changed files with 10 additions and 6 deletions

@ -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 {
@ -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:
@ -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);
}
@ -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
});
}

@ -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]");