This is the other essential part of any minimal token implementation, and allows a balance to be transferred from one account to another:
function transfer(address _to, uint256 _value)
public
returns (bool success)
{
require(balanceOf[msg.sender] >= _value);
balanceOf[msg.sender] -= _value;
balanceOf[_to] += _value;
emit Transfer(msg.sender, _to, _value);
return true;
}
Let's break down what's happening:
- The contract requires that the user making the transfer has a sufficient number of tokens to do so.
- The balance of the sender is reduced and the balance of the receiver is increased.
- The transfer event described earlier must be emitted.
- The function returns true to show a successful transfer. If the transfer isn't successfulthat is, if the sender has insufficient funds—then the function can either return false, or revert. In our implementation, the require() call will cause a revert.