hardhat icon indicating copy to clipboard operation
hardhat copied to clipboard

Overflow error in hardhat-chai-matchers -> changeTokenBalances

Open Pafaul opened this issue 2 years ago • 6 comments

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.

Pafaul avatar Aug 29 '22 10:08 Pafaul

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

github-actions[bot] avatar Aug 29 '22 10:08 github-actions[bot]

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

Pafaul avatar Aug 29 '22 10:08 Pafaul

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)

Pafaul avatar Aug 29 '22 11:08 Pafaul

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.

feuGeneA avatar Sep 05 '22 16:09 feuGeneA

Hi @feuGeneA ! Any updates on issue?

Pafaul avatar Sep 19 '22 12:09 Pafaul

I am having the same issue :(

heycryptobob avatar Sep 21 '22 15:09 heycryptobob

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.

fvictorio avatar Oct 31 '22 17:10 fvictorio

Thanks for the information!

Pafaul avatar Oct 31 '22 18:10 Pafaul