In Javascript, the maximum safe integer is 2^53-1.
So you should generally avoid using numbers like 100000000000000000000.
For example, in your code, you should change this:
myToken.transfer(acc[3], 100000000000000000000, {from:acc[0]})
To this:
myToken.transfer(acc[3], "100000000000000000000", {from:acc[0]})
In addition to that, the usage of toNumber()
is also unsafe, as you have noticed yourself.
When you call from the off-chain an on-chain function which returns uint256
, you get a BigNumber
object. If this object represents a number smaller than Number.MAX_SAFE_INTEGER
, then it is safe to use toNumber()
on it. But since you do not know that for sure, you should avoid using toNumber()
.
Also, please note that it is better to use toFixed()
than toString()
, because the latter may return the scientific notation of the number (e.g. 1+e18
), which may give you unexpected problems when you later use it in other contexts.
Best Answer
If you're on web3 v0.x, then:
web3.fromAscii
before you send the string to the smart contractweb3.toAscii
after you read the string from the smart contractIf you're on web3 v1.x, then:
web3.utils.asciiToHex
before you send the string to the smart contractweb3.utils.hexToAscii
after you read the string from the smart contract