Skip to content

Commit 9602243

Browse files
authoredOct 31, 2022
👔 Handle bignumber in revertedWith.withArgs() (#786)
1 parent 64707ae commit 9602243

File tree

6 files changed

+168
-67
lines changed

6 files changed

+168
-67
lines changed
 

‎.changeset/polite-fans-arrive.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@ethereum-waffle/chai": patch
3+
"@ethereum-waffle/hardhat": patch
4+
---
5+
6+
👔 revertedWith().withArgs no longer fails for uint values exceeding JavaScript's max int limit

‎pnpm-lock.yaml

+110-60
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎waffle-chai/package.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,17 @@
4343
},
4444
"dependencies": {
4545
"@ethereum-waffle/provider": "workspace:*",
46-
"debug": "^4.3.4"
46+
"debug": "^4.3.4",
47+
"json-bigint": "^1.0.0"
4748
},
4849
"devDependencies": {
49-
"ethers": "5.6.2",
5050
"@types/debug": "^4.1.7",
51+
"@types/json-bigint": "^1.0.1",
52+
"eslint": "^7.14.0",
53+
"ethers": "5.6.2",
5154
"mocha": "^8.2.1",
5255
"rimraf": "^3.0.2",
53-
"typescript": "^4.6.2",
54-
"eslint": "^7.14.0"
56+
"typescript": "^4.6.2"
5557
},
5658
"peerDependencies": {
5759
"ethers": "*"

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {decodeRevertString} from '@ethereum-waffle/provider';
22
import {callPromise} from '../call-promise';
3+
import JSONbig from 'json-bigint';
34

45
export function supportRevertedWith(Assertion: Chai.AssertionStatic) {
56
Assertion.addMethod('revertedWith', function (this: any, revertReason: string | RegExp) {
@@ -65,8 +66,15 @@ const decodeHardhatError = (error: any, context: any) => {
6566
const regexp = /VM Exception while processing transaction: reverted with custom error '([a-zA-Z0-9$_]+)\((.*)\)'/g;
6667
const matches = regexp.exec(errorString);
6768
if (matches && matches.length >= 1) {
68-
// needs to be wrapped in list to be consistent with the emit matcher
69-
context.args = [JSON.parse(`[${matches[2]}]`)];
69+
// Matches is in a format of string: "arg1, arg2, arg3, ..."
70+
// So it only makes sense in an array:
71+
const matchesList = `[${matches[2]}]`;
72+
// Next, it needs to be wrapped in a list to be consistent with the emit matcher:
73+
context.args = [
74+
// Additionally, we preserve numbers as strings,
75+
// otherwise we face an overflow of bignumber.
76+
JSONbig({storeAsString: true}).parse(matchesList)
77+
];
7078
const errorName = matches[1];
7179
context.txErrorName = errorName;
7280
return errorName;

‎waffle-hardhat/contracts/CustomError.sol

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ contract Matchers {
99
revert One(0, 'message', 0x00cFBbaF7DDB3a1476767101c12a0162e241fbAD2a0162e2410cFBbaF7162123);
1010
}
1111

12+
function doRevertWithBigNumber() public pure {
13+
revert One(9007199254740991000000, 'message', 0x00cFBbaF7DDB3a1476767101c12a0162e241fbAD2a0162e2410cFBbaF7162123);
14+
}
15+
1216
function doRevertWithTwo() public pure {
1317
revert Two(
1418
[

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

+32-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {waffle} from 'hardhat';
22
import {expect} from 'chai';
33
import {MockProvider} from 'ethereum-waffle';
44
import {revertedTest, revertedWithTest} from '@ethereum-waffle/chai/test';
5-
import {ContractFactory} from 'ethers';
5+
import {BigNumber, ContractFactory} from 'ethers';
66
import CustomError from '../build/contracts/CustomError.sol/Matchers.json';
77

88
describe('INTEGRATION: Matchers: reverted', () => {
@@ -58,6 +58,37 @@ describe('INTEGRATION: Matchers: revertedWith', () => {
5858
'message',
5959
'0x00cfbbaf7ddb3a1476767101c12a0162e241fbad2a0162e2410cfbbaf7162123'
6060
);
61+
62+
await expect(matchers.doRevertWithOne())
63+
.to.be.revertedWith('One')
64+
.withArgs(
65+
BigNumber.from('0'), // Check BigNumber instance as well.
66+
'message',
67+
'0x00cfbbaf7ddb3a1476767101c12a0162e241fbad2a0162e2410cfbbaf7162123'
68+
);
69+
});
70+
71+
it('With args and big number success', async () => {
72+
const matchers = await deploy();
73+
await expect(matchers.doRevertWithBigNumber())
74+
.to.be.revertedWith('One')
75+
.withArgs(
76+
BigNumber.from('9007199254740991000000'),
77+
'message',
78+
'0x00cfbbaf7ddb3a1476767101c12a0162e241fbad2a0162e2410cfbbaf7162123'
79+
);
80+
});
81+
82+
it('With args and big number failure', async () => {
83+
const matchers = await deploy();
84+
await expect(expect(matchers.doRevertWithBigNumber())
85+
.to.be.revertedWith('One')
86+
.withArgs(
87+
BigNumber.from('9007199254740991000001'), // different
88+
'message',
89+
'0x00cfbbaf7ddb3a1476767101c12a0162e241fbad2a0162e2410cfbbaf7162123'
90+
)
91+
).to.be.eventually.rejectedWith('Expected "9007199254740991000000" to be equal 9007199254740991000001');
6192
});
6293

6394
it('With args failure', async () => {

0 commit comments

Comments
 (0)
Please sign in to comment.