Int Overflow

One doesn’t always encounter overflows in actual development. Especially not in most sectors where numebrs are realistic (e.g. money, number of users, number of items, etc.).

It happens in crypto world because crypto goes by minimal divisible units (wei, satoshi). Imagine you spot an abnormal tx. It doesn’t fail, it’s just not giving the correct amount. E.g. the tx is supposed to send 3000 ETH, but it sents 0.000000000000000003 instead.

That smells like an overflow. With that judgement, tracing becomes relatively easy. Staring at below code, the problem is blatant:

function toWei(etherValue) {
	let n = etherValue * 1e18;
	return '0x' + parseInt(n).toString(16);
}

So:

> parseInt(1000 * 1e18)
1
> parseInt(100 * 1e18)
100000000000000000000

Use big.js or simply wrap it in float (appears in scientific notation) fixes it.

Written on December 11, 2019