0

I am using truffle, but that shouldn't make any difference here...

When using contract's transfer mehtod, I want to pass a parameter - 1000000000000000000000000, but since this is too big, it results in an error. That's why I pass web3.utils.toBN('1000000000000000000000000')

After that, I should check if one of the address in solidity holds this amount of tokens.

So I have this:

let balance = await instance.balanceOf(address);

This returns a bigNumber.

Way 1: To get an actual number so that I can test if it worked, I tried this: balance.toNumber() which didn't work.

Way 2: Then, I tried assert.equal(balance, web3.utils.toBN('1000000000000000000000000'));, but now, it shows an error - AssertionError: expected <BN: d3c21bcecceda1000000> to equal <BN: d3c21bcecceda1000000> even though they still seem equal.

My question would be what way do you choose to work with such cases ? Way 1 or Way 2? and if so, how do I fix my issue then ? Thanks..

1 Answer 1

1

Whenever you need to display the balance just do balance.toString(). If you need to do some mathematical operations you can do them while variable balance is still a BigNumber, then after you can display the outcome again with .toString().

1
  • One question Miroslav. I am writing tests in web3 for my contracts. If I work with the big numbers that returned from solidity, I have to also transform all other numbers to bignumbers too. basically I can't do the following - balance - 30. if balance is returned from solidity, then I have to transform 30 into bignumber to make it work. and it gets super awkward to transform all test numbers into bignumbers in my tests. makes sense ? Dec 8, 2020 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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