I need to multiple and divide a big number in a safe way - without overlow.
It won't have to result in an accurate amount to suit me; namely, if it gets round by some function to remain integer rather than more precise float or decimal, it'd still do for me.
Currently this code will result in overflow:
let amountToSendPercent = provider.getBalance(wallet.address);
//let amountToSendPercent = contract.balanceOf(wallet.address);
//minus gas.... - doesn't matter here....
let amountToSendPercent = 45;
// !!!
//sometimes - overflow
// !!
if (amountToSendPercent < 100) {
amountToSend = Math.round(amountToSend / 100 * amountToSendPercent);
}
I'm using ethers. I'd prefer to refrain from using any additional library, if possible.
I need this for both ERC20 tokens and ETH.