Skip to content

Instantly share code, notes, and snippets.

@AnthonyAkentiev
Created December 20, 2016 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnthonyAkentiev/14791dee17996c852ecd510ff3cdc066 to your computer and use it in GitHub Desktop.
Save AnthonyAkentiev/14791dee17996c852ecd510ff3cdc066 to your computer and use it in GitHub Desktop.
// Mixing safe math (that throws) with return true/false is not a good coding style
// This is your version:
function transfer(address _to, uint _value) returns (bool success) {
if ((msg.sender == owner) || (tradeable == true)) {
balances[msg.sender] = safeSub(balances[msg.sender], _value);
balances[_to] = safeAdd(balances[_to], _value);
// calling an event
Transfer(msg.sender, _to, _value);
return true;
} else {
return false;
}
}
// This is how it should be rewritten:
function transfer(address _to, uint256 _value) returns (bool success) {
// checking for wraps (useful only if token count can be more than 2^256 - 1)
if (balances[msg.sender] >= _value && balances[_to] + _value > balances[_to]) {
balances[msg.sender] -= _value;
balances[_to] += _value;
Transfer(msg.sender, _to, _value);
return true;
} else { return false; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment