Back to Lessons
Lesson 02

Understanding transfer()

+5EFFORT
5 sections
transfer()
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) {
2 address owner = msg.sender;
3 _transfer(owner, to, amount);
4 return true;
5}
6
7function _transfer(address from, address to, uint256 amount) internal {
8 require(from != address(0), "Transfer from zero address");
9 require(to != address(0), "Transfer to zero address");
10
11 uint256 fromBalance = _balances[from];
12 require(fromBalance >= amount, "Transfer amount exceeds balance");
13
14 _balances[from] = fromBalance - amount;
15 _balances[to] += amount;
16
17 emit 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