0

I am using Open Zeppelin's Test Helpers in my Hardhat testing to check for events emitted using the librarys expectEvent. The code I have has worked fine for other tests. The code would look like the following:

const reciept= await marketplace.listNFT(
  nftContract.address, 
  tokenId, 
  { from: USER, value: ethers.utils.parseUnits("1","ether")}
);
expectEvent(reciept, 'Listed', { 
   owner: TOKEN_OWNER,
   user: USER,
   nftContract: nft.address.toString(),
   tokenId: tokenId.toString(),
   startDateUNIX: new BN(TOMORROW),
   endDateUNIX: new BN(IN_FIVE_DAYS),
   expires: new BN(TODAY_2),
   Fee: new BN(0)
});

The event emits properly and I am able to test and verify other events except for this test case. I am having trouble with Fee. Initially I put 0 I get the following error:

 expected event argument 'Fee' to have value 0 but got 1000000000000000000
      + expected - actual

      -1000000000000000000
      +0

This is fine as I was creating the unit test for the first time and made a typo. Instead of 0 it should be 1000000000000000000 as I expect the fee 1Eth. So I changed the line to correctly reflect the output like below so it would pass:

  Fee: new BN(1000000000000000000)

I reran the test expecting this to pass. Instead I arrive at a new vague error stating assertion failed. Im unsure why its failing. Is this because of an overflow? How can I test the fee? I dont understand why the error is occuring

Error: Assertion failed
      at assert (node_modules/bn.js/lib/bn.js:6:21)
      at BN._initNumber (node_modules/bn.js/lib/bn.js:128:7)
      at BN.init [as _init] (node_modules/bn.js/lib/bn.js:82:19)
      at new BN (node_modules/bn.js/lib/bn.js:39:12)
      at new BNwrapped (node_modules/web3-utils/lib/utils.js:480:16)
      at Context.<anonymous> (test/Marketplace.js:447:14)
      at processTicksAndRejections (node:internal/process/task_queues:95:5)

1 Answer 1

2

For big integer you should construct the value from string instead of literal value because uint32 or uint64 native types cannot handle that. The below should work:

Fee: new BN('1000000000000000000')
3
  • Ah! Thank you. I have other constant variables defined like TOMORROW IN_FIVE_DAYS which are big numbers too. ideally should I be converting them to string ? TOMORROW.toString()
    – Potion
    Dec 23, 2022 at 6:46
  • I'm not sure what that constant means, if it means the UNIX timestamp then it may not be necessary. Otherwise, the safest choice should be using string for BN literals
    – minhhn2910
    Dec 23, 2022 at 6:48
  • Good point. Yes theyre unix timestamps represented in seconds so its not necessary. Thank you. Answer accepted.
    – Potion
    Dec 23, 2022 at 6:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.