Let's say that Ethereum solidity returns a number from one of its functions and I get that into Javascript.
Now ,because I write tests in my js application, I have to check if the returned value is equal to some number multiplied by some number.
Basically, let's look at the scenario.
In javascript,
Way 1)
web3.utils.toBN('100000000000000') * 30
Way 2)
web3.utils.toBN('100000000000000') * web3.utils.toBN('30')
Depending on the above 2 ones, can I use Way 1 ) ? reason is that if I use Way 2, my code just becomes bloated with toBN
functions all the way.
Best Answer
Neither. You cannot use
*
to multiply two BNs together. For that you should use themul
function (https://github.com/indutny/bn.js/#arithmetics)If what you are actually wanting to do is convert from/to wei/ether you can use
web3.utils.toWei
andweb3.utils.toEther
in combination withweb3.utils.toBN
, e.g.,web3.utils.toBN(web3.utils.toWei("30", "ether"))
.Note that you can use JavaScript's native
BigInt
for many calculations and comparisons.BigInt("0x100000000000000") * 30n
is valid JavaScript and will produce a valid BigInt. You'll just need to ensure your target platforms support it (https://caniuse.com/?search=bigint).