hardhat
hardhat copied to clipboard
Overflow error in hardhat-chai-matchers -> changeTokenBalances
Hi!
When using packange hardhat-chai-matchers I've discovered issue with changeTokenBalances
function:
Error: overflow [ See: https://links.ethers.org/v5-errors-NUMERIC_FAULT-overflow ] (fault="overflow", operation="BigNumber.from", value=-99899990019000000000, code=NUMERIC_FAULT, version=bignumber/5.7.0)
at Logger.makeError (node_modules/ethers/node_modules/@ethersproject/logger/src.ts/index.ts:269:28)
at Logger.throwError (node_modules/ethers/node_modules/@ethersproject/logger/src.ts/index.ts:281:20)
at throwFault (node_modules/ethers/node_modules/@ethersproject/bignumber/src.ts/bignumber.ts:356:19)
at Function.BigNumber.from (node_modules/ethers/node_modules/@ethersproject/bignumber/src.ts/bignumber.ts:247:17)
at ${projectPath}/node_modules/@nomicfoundation/hardhat-chai-matchers/src/internal/changeTokenBalance.ts:101:40
at Array.every (<anonymous>)
at checkBalanceChanges (node_modules/@nomicfoundation/hardhat-chai-matchers/src/internal/changeTokenBalance.ts:100:25)
at async Context.<anonymous> (hhTest/test.js:619:9)
This matcher cannot check big values which is common for tokens with 18 decimals (for example - ten tokens with 18 decimals is 10000000000000000000
which leads to overflow).
Test which was runned:
await expect(
txPromise
).to.emit(
contract, event
).withArgs(
...eventArgs
).and.changeTokenBalances(
tokens[0],
[unknownUser.address, otherContract.address],
[-amountIn, amountOut]
).and.changeTokenBalances(
tokens[1],
[unknownUser.address, otherContract.address],
[expectedAmountOut, -expectedAmountOut]
)
Both amountIn
and expectedAmountOut
are BigNumber instances but in stacktrace values are displayed as Numbers.
This issue is also being tracked on Linear.
We use Linear to manage our development process, but we keep the conversations on Github.
LINEAR-ID: 24420fce-6148-4ddc-8976-a9dc5d6816d6
Update: Forgot to mention setup:
-
hardhat-chai-matchers
: 1.0.3 -
hardhat
: 2.10.2 -
node
: v18.6.0 -
environment
: WSL2 with Ubuntu 20.04.4 LTS
Update #2: For local tests it is sometimes possible to lower decimals to 6-12 to avoid this issue, but for real-world application it may not be possible (for example - when working with forked network)
This issue has been added to our internal backlog, meaning that we intend to work on it, but that it has not yet been scheduled.
Hi @feuGeneA ! Any updates on issue?
I am having the same issue :(
Hi folks. In these cases, you should use a BigInt value instead of a number. That is, instead of 10000000000000000000
, you should use 10000000000000000000n
(notice the trailing n
).
That being said, we should show a better error in these scenarios. I'm going to open a new issue for that, and close this one in favor of it.
Thanks for the information!