1

I'm implementing my own ERC20 token and trying to figure out best practices by looking at existing implementations. I found that DAI has addition and subtraction functions that appear to prevent integer overflow in the transfer and transferFrom methods.

function add(uint x, uint y) internal pure returns (uint z) {
    require((z = x + y) >= x);
}
function sub(uint x, uint y) internal pure returns (uint z) {
    require((z = x - y) <= x);
}

Whereas the Consensys reference implementation just subtracts and adds balances without performing any such check.

    balances[_to] += _value;
    balances[_from] -= _value;

My intuition is that my token doesn't need these methods, as it has a fixed supply far less than MAX_UINT256. Am I missing an attack vector? Is there a more authoritative source to copy?

1

That contract you mentioned is 3+ years old and an Integer overflow & underflow attack was discovered after it (https://github.com/SmartContractSecurity/SWC-registry/blob/master/entries/SWC-101.md), you shouldn't trust that repo, it is unmaintained. Use openzeppelin's SafeMath for arithmetic operations if you need it

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.