01
text
The transfer() function lets you send tokens directly from your wallet to another address. It's the most basic way to move tokens.
02
code
solidity
1function transfer(address to, uint256 amount) public returns (bool) {2address owner = msg.sender;3_transfer(owner, to, amount);4return true;5}67function _transfer(address from, address to, uint256 amount) internal {8require(from != address(0), "Transfer from zero address");9require(to != address(0), "Transfer to zero address");1011uint256 fromBalance = _balances[from];12require(fromBalance >= amount, "Transfer amount exceeds balance");1314_balances[from] = fromBalance - amount;15_balances[to] += amount;1617emit Transfer(from, to, amount);18}
03
text
When you call transfer(), the contract checks that you have enough tokens, subtracts from your balance, and adds to the recipient's balance. It's atomic - either the whole thing succeeds or nothing happens.
04
note
Key Insight
Important: transfer() uses msg.sender to identify YOU. This means only you can transfer your own tokens using this function. Nobody else can call transfer() and move your tokens.
05
text
The Transfer event is emitted every time tokens move. This is how block explorers and wallets track token movements - they listen for these events.
Complete
Connect your wallet to mark this lesson as complete
Earn 5 EFFORT tokens