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?

2
  • On Truffle 5.x, both are BN objects as far as I know. Nov 17, 2020 at 16:46
  • @goodvibration I used typeof to check, web3.utils.toWei("0.2") evaluates to string
    – Somjit
    Nov 17, 2020 at 17:09

1 Answer 1

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.

3
  • 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, 2020 at 17:15
  • Okay, but in the question you said result was a string. What is the value of result? Nov 17, 2020 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, 2020 at 18:13

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.