Skip to content

Commit ba71ce4

Browse files
authoredMay 5, 2022
🪁 Hardhat support custom errors (#719)
1 parent 587ff49 commit ba71ce4

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed
 

Diff for: ‎.changeset/big-goats-guess.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ethereum-waffle/chai": patch
3+
---
4+
5+
Add support for custom errors in hardhat

Diff for: ‎waffle-chai/src/matchers/revertedWith.ts

+7
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ const decodeHardhatError = (error: any) => {
8787
return matches[1];
8888
}
8989
}
90+
{
91+
const regexp = new RegExp('VM Exception while processing transaction: reverted with custom error \'(.*)\'');
92+
const matches = regexp.exec(errorString);
93+
if (matches && matches.length >= 1) {
94+
return matches[1];
95+
}
96+
}
9097
{
9198
const regexp = new RegExp('VM Exception while processing transaction: reverted with panic code ([a-zA-Z0-9]*)');
9299
const matches = regexp.exec(errorString);

Diff for: ‎waffle-hardhat/contracts/CustomError.sol

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
pragma solidity ^0.8.0;
2+
3+
error CustomError(uint value);
4+
5+
contract Matchers {
6+
function doRevertWithCustomError() public pure {
7+
revert CustomError(0);
8+
}
9+
}

Diff for: ‎waffle-hardhat/test/reverted.test.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import {expect} from 'chai';
33
import {MockProvider} from 'ethereum-waffle';
44
import {revertedTest, revertedWithTest} from '@ethereum-waffle/chai/test';
55
import {ContractFactory} from 'ethers';
6-
import {abi, bytecode} from '../build/contracts/Panic.sol/Panic.json';
6+
import Panic from '../build/contracts/Panic.sol/Panic.json';
7+
import CustomError from '../build/contracts/CustomError.sol/Matchers.json';
78

89
describe('INTEGRATION: Matchers: reverted', () => {
910
const provider = waffle.provider as MockProvider;
@@ -29,7 +30,16 @@ it('Panic code', async () => {
2930
await waffle.provider.send('hardhat_reset', []);
3031
const wallets = waffle.provider.getWallets();
3132
const wallet = wallets[0];
32-
const factory = new ContractFactory(abi, bytecode, wallet);
33+
const factory = new ContractFactory(Panic.abi, Panic.bytecode, wallet);
3334
const panicContract = await factory.deploy();
3435
await expect(panicContract.panic()).to.be.revertedWith('panic code 0x32');
3536
});
37+
38+
it('Handle custom error', async () => {
39+
await waffle.provider.send('hardhat_reset', []);
40+
const wallets = waffle.provider.getWallets();
41+
const wallet = wallets[0];
42+
const factory = new ContractFactory(CustomError.abi, CustomError.bytecode, wallet);
43+
const matchers = await factory.deploy();
44+
await expect(matchers.doRevertWithCustomError()).to.be.revertedWith('CustomError(0)');
45+
});

0 commit comments

Comments
 (0)
Please sign in to comment.