Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
Merge pull request #2341 from 0xProject/fix/parity-revert-errors
Browse files Browse the repository at this point in the history
RevertError: Decode Parity revert errors
  • Loading branch information
dekz committed Nov 15, 2019
2 parents ecf1ad8 + 42dc112 commit 30f0168
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/utils/CHANGELOG.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
{
"note": "Removed exports AuthorizableRevertErrors, LibAddressArrayRevertErrors, LibBytesRevertErrors, OwnableRevertErrors, ReentrancyGuardRevertErrors and SafeMathRevertErrors",
"pr": 2321
},
{
"note": "Decode `Parity` revert errors",
"pr": 2341
}
]
},
Expand Down
27 changes: 26 additions & 1 deletion packages/utils/src/revert_error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ export abstract class RevertError extends Error {
}
}

const PARITY_TRANSACTION_REVERT_ERROR_MESSAGE = /^VM execution error/;
const GANACHE_TRANSACTION_REVERT_ERROR_MESSAGE = /^VM Exception while processing transaction: revert/;
const GETH_TRANSACTION_REVERT_ERROR_MESSAGE = /always failing transaction$/;

Expand All @@ -329,10 +330,18 @@ interface GanacheTransactionRevertError extends Error {
hashes: string[];
}

interface ParityTransactionRevertError extends Error {
code: number;
data: string;
message: string;
}

/**
* Try to extract the ecnoded revert error bytes from a thrown `Error`.
*/
export function getThrownErrorRevertErrorBytes(error: Error | GanacheTransactionRevertError): string {
export function getThrownErrorRevertErrorBytes(
error: Error | GanacheTransactionRevertError | ParityTransactionRevertError,
): string {
// Handle ganache transaction reverts.
if (isGanacheTransactionRevertError(error)) {
// Grab the first result attached.
Expand All @@ -344,6 +353,13 @@ export function getThrownErrorRevertErrorBytes(error: Error | GanacheTransaction
if (result.return !== undefined && result.return !== '0x') {
return result.return;
}
} else if (isParityTransactionRevertError(error)) {
// Parity returns { data: 'Reverted 0xa6bcde47...', ... }
const { data } = error;
const hexDataIndex = data.indexOf('0x');
if (hexDataIndex !== -1) {
return data.slice(hexDataIndex);
}
} else {
// Handle geth transaction reverts.
if (isGethTransactionRevertError(error)) {
Expand All @@ -354,6 +370,15 @@ export function getThrownErrorRevertErrorBytes(error: Error | GanacheTransaction
throw new Error(`Cannot decode thrown Error "${error.message}" as a RevertError`);
}

function isParityTransactionRevertError(
error: Error | ParityTransactionRevertError,
): error is ParityTransactionRevertError {
if (PARITY_TRANSACTION_REVERT_ERROR_MESSAGE.test(error.message) && 'code' in error && 'data' in error) {
return true;
}
return false;
}

function isGanacheTransactionRevertError(
error: Error | GanacheTransactionRevertError,
): error is GanacheTransactionRevertError {
Expand Down
16 changes: 15 additions & 1 deletion packages/utils/test/revert_error_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import * as chai from 'chai';
import * as _ from 'lodash';

import { AnyRevertError, RawRevertError, RevertError, StringRevertError } from '../src/revert_error';
import {
AnyRevertError,
getThrownErrorRevertErrorBytes,
RawRevertError,
RevertError,
StringRevertError,
} from '../src/revert_error';

import { chaiSetup } from './utils/chai_setup';

Expand Down Expand Up @@ -154,6 +160,14 @@ describe('RevertError', () => {
expect(decode).to.throw();
});
});
describe('getThrownErrorRevertErrorBytes', () => {
it('should decode Parity revert errors', () => {
const revertAbi = '0x1234';
const parityError = { code: 1234, message: 'VM execution error.', data: `Reverted ${revertAbi}`, name: '' };
const revertError = getThrownErrorRevertErrorBytes(parityError);
expect(revertError).to.be.eq(revertAbi);
});
});
describe('encoding', () => {
const message = 'foobar';
it('should be able to encode', () => {
Expand Down

0 comments on commit 30f0168

Please sign in to comment.