Skip to content

Commit

Permalink
🌭 Improve hardhat error catching (#707)
Browse files Browse the repository at this point in the history
  • Loading branch information
yivlad authored Apr 19, 2022
1 parent bbedb87 commit aa18bfa
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-queens-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ethereum-waffle/chai": patch
---

Improve hardhat error catching
2 changes: 1 addition & 1 deletion waffle-chai/src/matchers/reverted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function supportReverted(Assertion: Chai.AssertionStatic) {
);

const onSuccess = (value: any) => {
if ('wait' in value) {
if (value && 'wait' in value) {
// Sending the transaction succeeded, but we wait to see if it will revert on-chain.
return value.wait().then((newValue: any) => {
assertNotReverted();
Expand Down
28 changes: 22 additions & 6 deletions waffle-chai/src/matchers/revertedWith.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function supportRevertedWith(Assertion: Chai.AssertionStatic) {
);

const onSuccess = (value: any) => {
if ('wait' in value) {
if (value && 'wait' in value) {
// Sending the transaction succeeded, but we wait to see if it will revert on-chain.
return value.wait().then((newValue: any) => {
assertNotReverted();
Expand Down Expand Up @@ -80,12 +80,28 @@ export function supportRevertedWith(Assertion: Chai.AssertionStatic) {
const decodeHardhatError = (error: any) => {
const tryDecode = (error: any) => {
const errorString = String(error);
const regexp = new RegExp('VM Exception while processing transaction: reverted with reason string \'(.*)\'');
const matches = regexp.exec(errorString);
if (!matches) {
return undefined;
{
const regexp = new RegExp('VM Exception while processing transaction: reverted with reason string \'(.*)\'');
const matches = regexp.exec(errorString);
if (matches && matches.length >= 1) {
return matches[1];
}
}
{
const regexp = new RegExp('VM Exception while processing transaction: reverted with panic code ([a-zA-Z0-9]*)');
const matches = regexp.exec(errorString);
if (matches && matches.length >= 1) {
return 'panic code ' + matches[1];
}
}
{
const regexp = new RegExp('Error: Transaction reverted: (.*)');
const matches = regexp.exec(errorString);
if (matches && matches.length >= 1) {
return matches[1];
}
}
return matches[1];
return undefined;
};

return tryDecode(error) ?? tryDecode(error.error); // the error may be wrapped
Expand Down
9 changes: 9 additions & 0 deletions waffle-hardhat/contracts/Panic.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;

contract Panic {
uint[] private arr;
function panic() public {
uint a = arr[0];
}
}
14 changes: 13 additions & 1 deletion waffle-hardhat/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,17 @@ module.exports = {
initialDate: '2020-01-01T00:00:00',
allowUnlimitedContractSize: true,
},
}
},
paths: {
sources: './contracts',
artifacts: './build',
cache: './cache',
},
solidity: {
compilers: [
{
version: "0.8.10"
}
]
}
}
2 changes: 1 addition & 1 deletion waffle-hardhat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"module": "dist/esm/index.ts",
"types": "dist/esm/index.d.ts",
"scripts": {
"build": "true",
"build": "hardhat compile",
"test": "NODE_PATH=\"${PWD}/node_modules\":$NODE_PATH mocha",
"lint": "eslint '{src,test}/**/*.ts'",
"lint:fix": "eslint --fix '{src,test}/**/*.ts'"
Expand Down
12 changes: 12 additions & 0 deletions waffle-hardhat/test/reverted.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import {waffle} from 'hardhat';
import {expect} from 'chai';
import {MockProvider} from 'ethereum-waffle';
import {revertedTest, revertedWithTest} from '@ethereum-waffle/chai/test';
import {ContractFactory} from 'ethers';
import {abi, bytecode} from '../build/contracts/Panic.sol/Panic.json';

describe('INTEGRATION: Matchers: reverted', () => {
const provider = waffle.provider as MockProvider;
Expand All @@ -21,3 +24,12 @@ describe('INTEGRATION: Matchers: revertedWith', () => {

revertedWithTest(provider);
});

it('Panic code', async () => {
await waffle.provider.send('hardhat_reset', []);
const wallets = waffle.provider.getWallets();
const wallet = wallets[0];
const factory = new ContractFactory(abi, bytecode, wallet);
const panicContract = await factory.deploy();
await expect(panicContract.panic()).to.be.revertedWith('panic code 0x32');
});

0 comments on commit aa18bfa

Please sign in to comment.