diff --git a/src/tokens/ERC20.sol b/src/tokens/ERC20.sol index 96570446..b760d87d 100644 --- a/src/tokens/ERC20.sol +++ b/src/tokens/ERC20.sol @@ -73,20 +73,23 @@ abstract contract ERC20 { return true; } - function transfer(address to, uint256 amount) public virtual returns (bool) { - balanceOf[msg.sender] -= amount; - - // Cannot overflow because the sum of all user - // balances can't exceed the max uint256 value. - unchecked { - balanceOf[to] += amount; - } + error InsufficientBalance(); + error TransferToZeroAddress(); - emit Transfer(msg.sender, to, amount); + function transfer(address to, uint256 amount) public returns (bool) { + if (balanceOf[msg.sender] < amount) revert InsufficientBalance(); + if (to == address(0)) revert TransferToZeroAddress(); - return true; + unchecked { + balanceOf[msg.sender] -= amount; + balanceOf[to] += amount; } + emit Transfer(msg.sender, to, amount); + return true; +} + + function transferFrom( address from, address to,