In @0x/utils add encode() method to RevertError

This commit is contained in:
Lawrence Forman
2019-04-04 22:20:33 -04:00
committed by Amir Bandeali
parent 2e846159a8
commit 633c5d5938
2 changed files with 57 additions and 3 deletions

View File

@@ -125,7 +125,15 @@ export abstract class RevertError extends Error {
if (!_.isNil(this.abi)) {
return this.abi.name;
}
return '<AnyRevertError>';
return `<${this.typeName}>`;
}
/**
* Get the class name of this type.
*/
get typeName(): string {
// tslint:disable-next-line: no-string-literal
return this.constructor.name;
}
/**
@@ -201,6 +209,14 @@ export abstract class RevertError extends Error {
return true;
}
public encode(): string {
if (!this._hasAllArgumentValues) {
throw new Error(`Instance of ${this.typeName} does not have all its parameter values set.`);
}
const encoder = createEncoder(this.abi as RevertErrorAbi);
return encoder(this.values);
}
public toString(): string {
const values = _.omitBy(this.values, (v: any) => _.isNil(v));
const inner = _.isEmpty(values) ? '' : inspect(values);
@@ -218,6 +234,18 @@ export abstract class RevertError extends Error {
private get _isAnyType(): boolean {
return _.isNil(this.abi);
}
private get _hasAllArgumentValues(): boolean {
if (_.isNil(this.abi) || _.isNil(this.abi.arguments)) {
return false;
}
for (const arg of this.abi.arguments) {
if (_.isNil(this.values[arg.name])) {
return false;
}
}
return true;
}
}
/**
@@ -289,10 +317,17 @@ function normalizeBytes(bytes: string): string {
return ethUtil.addHexPrefix(bytes).toLowerCase();
}
function createEncoder(abi: RevertErrorAbi): (values: ObjectMap<any>) => string {
const encoder = AbiEncoder.createMethod(abi.name, abi.arguments || []);
return (values: ObjectMap<any>): string => {
const valuesArray = _.map(abi.arguments, (arg: DataItem) => values[arg.name]);
return encoder.encode(valuesArray);
};
}
function createDecoder(abi: RevertErrorAbi): (hex: string) => ValueMap {
const encoder = AbiEncoder.createMethod(abi.name, abi.arguments || []);
return (hex: string): ValueMap => {
// tslint:disable-next-line
return encoder.decode(hex) as ValueMap;
};
}

View File

@@ -1,4 +1,5 @@
import * as chai from 'chai';
import * as _ from 'lodash';
import { AnyRevertError, RevertError, StringRevertError } from '../src/revert_error';
@@ -68,7 +69,8 @@ describe('RevertError', () => {
'0x08c379a0' +
'0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000006' +
Buffer.from(message).toString('hex');
Buffer.from(message).toString('hex') +
_.repeat('00', 32 - 6);
it('should decode an ABI encoded revert error', () => {
const expected = new StringRevertError(message);
@@ -86,4 +88,21 @@ describe('RevertError', () => {
expect(decode).to.be.throw();
});
});
describe('encoding', () => {
const message = 'foobar';
it('should be able to encode', () => {
const expected =
'0x08c379a0' +
'0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000006' +
Buffer.from(message).toString('hex') +
_.repeat('00', 32 - 6);
const revert = new StringRevertError(message);
expect(revert.encode()).to.equal(expected);
});
it('should throw if missing parameter values', () => {
const revert = new StringRevertError();
expect(() => revert.encode()).to.throw();
});
});
});