Skip to content

Commit aa18bfa

Browse files
authoredApr 19, 2022
🌭 Improve hardhat error catching (#707)
1 parent bbedb87 commit aa18bfa

File tree

7 files changed

+63
-9
lines changed

7 files changed

+63
-9
lines changed
 

‎.changeset/fluffy-queens-cross.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ethereum-waffle/chai": patch
3+
---
4+
5+
Improve hardhat error catching

‎waffle-chai/src/matchers/reverted.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export function supportReverted(Assertion: Chai.AssertionStatic) {
2525
);
2626

2727
const onSuccess = (value: any) => {
28-
if ('wait' in value) {
28+
if (value && 'wait' in value) {
2929
// Sending the transaction succeeded, but we wait to see if it will revert on-chain.
3030
return value.wait().then((newValue: any) => {
3131
assertNotReverted();

‎waffle-chai/src/matchers/revertedWith.ts

+22-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function supportRevertedWith(Assertion: Chai.AssertionStatic) {
1313
);
1414

1515
const onSuccess = (value: any) => {
16-
if ('wait' in value) {
16+
if (value && 'wait' in value) {
1717
// Sending the transaction succeeded, but we wait to see if it will revert on-chain.
1818
return value.wait().then((newValue: any) => {
1919
assertNotReverted();
@@ -80,12 +80,28 @@ export function supportRevertedWith(Assertion: Chai.AssertionStatic) {
8080
const decodeHardhatError = (error: any) => {
8181
const tryDecode = (error: any) => {
8282
const errorString = String(error);
83-
const regexp = new RegExp('VM Exception while processing transaction: reverted with reason string \'(.*)\'');
84-
const matches = regexp.exec(errorString);
85-
if (!matches) {
86-
return undefined;
83+
{
84+
const regexp = new RegExp('VM Exception while processing transaction: reverted with reason string \'(.*)\'');
85+
const matches = regexp.exec(errorString);
86+
if (matches && matches.length >= 1) {
87+
return matches[1];
88+
}
89+
}
90+
{
91+
const regexp = new RegExp('VM Exception while processing transaction: reverted with panic code ([a-zA-Z0-9]*)');
92+
const matches = regexp.exec(errorString);
93+
if (matches && matches.length >= 1) {
94+
return 'panic code ' + matches[1];
95+
}
96+
}
97+
{
98+
const regexp = new RegExp('Error: Transaction reverted: (.*)');
99+
const matches = regexp.exec(errorString);
100+
if (matches && matches.length >= 1) {
101+
return matches[1];
102+
}
87103
}
88-
return matches[1];
104+
return undefined;
89105
};
90106

91107
return tryDecode(error) ?? tryDecode(error.error); // the error may be wrapped
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.10;
3+
4+
contract Panic {
5+
uint[] private arr;
6+
function panic() public {
7+
uint a = arr[0];
8+
}
9+
}

‎waffle-hardhat/hardhat.config.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,17 @@ module.exports = {
66
initialDate: '2020-01-01T00:00:00',
77
allowUnlimitedContractSize: true,
88
},
9-
}
9+
},
10+
paths: {
11+
sources: './contracts',
12+
artifacts: './build',
13+
cache: './cache',
14+
},
15+
solidity: {
16+
compilers: [
17+
{
18+
version: "0.8.10"
19+
}
20+
]
21+
}
1022
}

‎waffle-hardhat/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"module": "dist/esm/index.ts",
1111
"types": "dist/esm/index.d.ts",
1212
"scripts": {
13-
"build": "true",
13+
"build": "hardhat compile",
1414
"test": "NODE_PATH=\"${PWD}/node_modules\":$NODE_PATH mocha",
1515
"lint": "eslint '{src,test}/**/*.ts'",
1616
"lint:fix": "eslint --fix '{src,test}/**/*.ts'"

‎waffle-hardhat/test/reverted.test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import {waffle} from 'hardhat';
2+
import {expect} from 'chai';
23
import {MockProvider} from 'ethereum-waffle';
34
import {revertedTest, revertedWithTest} from '@ethereum-waffle/chai/test';
5+
import {ContractFactory} from 'ethers';
6+
import {abi, bytecode} from '../build/contracts/Panic.sol/Panic.json';
47

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

2225
revertedWithTest(provider);
2326
});
27+
28+
it('Panic code', async () => {
29+
await waffle.provider.send('hardhat_reset', []);
30+
const wallets = waffle.provider.getWallets();
31+
const wallet = wallets[0];
32+
const factory = new ContractFactory(abi, bytecode, wallet);
33+
const panicContract = await factory.deploy();
34+
await expect(panicContract.panic()).to.be.revertedWith('panic code 0x32');
35+
});

0 commit comments

Comments
 (0)
Please sign in to comment.