Skip to content

Difference between transfer and transfer-from

The function declarations:

C
1
2
function transfer(address to, uint256 amount) external returns (bool);
function transferFrom( address from, address to, uint256 amount ) external returns (bool);

The common member variables:

C
1
2
3
4
5
6
7
8
mapping(address account => uint256) private balances; // easy to understanding

// uint256 amount = allowance[A][B], means that
// B can transfer `amount` of token from A's balance  to another one,
// will reduce the allowance[A][B] each time the same amount that transfered
mapping(address account => mapping(address spender => uint256)) private allowances;

address msg_sender; // just who call the contracts function.

Sample

Assuming I'm the B , want to transfer tokens to C.

  • If I call the contract.transfer(C, 1)
  • It means reduce my(A) balance and add it to C's balance:

```c address B = msg.sender; // me address C = to; balances[B] -=1; balance[C] +=1;

Text Only
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
- If I call the `contract.transferFrom(A, C, 1)`
- It means I will  use  the allowance that A approve to me, then direct transfer tokens from A to C:

```c
address B = msg.sender; // me
address C = to;
address A = from; 
allowance[A][B] -=1;
balance[A]-=1;
balacne[C]+=1;