2

Reading from this post, one should not use toNumber() when working with uint256, so when writting the below test, I was expecting it to crash, but it does not, instead it runs to success! why ?

  it("lottery amount should reflect previous two entries", async () => {
    const result = await contract.totalAmount();
    assert.equal(result, web3.utils.toWei("0.2")); // why we can compare BigNumber to string?
  });

web3.utils.toWei("0.2") is string, where result is BigNumber. So how can we simply compare them in an assert?

Somjit
  • 265
  • 1
  • 9

1 Answers1

1

From the Web3 documentation:

Returns

String|BN: If a string is given it returns a number string, otherwise a BN.js instance.

You've passed in a string; you are being returned a string.

Hence you're comparing a string with a string.

Richard Horrocks
  • 37,835
  • 13
  • 87
  • 144
  • typeof web3.utils.toWei("0.2") gives String, whereas typeof result gives object. So I am not sure how I am comparing a String to String, hence this question in the first place – Somjit Nov 17 '20 at 17:15
  • Okay, but in the question you said result was a string. What is the value of result? – Richard Horrocks Nov 17 '20 at 18:07
  • My bad, i had swapped the types around in the question. Fixed. result is a BN, whose 'toString()' evaluates to a string, ".2" ether in wei – Somjit Nov 17 '20 at 18:13