Learn ERC20 in Solidity | transferFrom Function

Solidity Programming Language
4 min readMar 2, 2022

--

Aim : To learn ERC20 and make your own token

Thanks: GrandZero, Deniz and Hamza_krb

Turkish: Hamza, çek analojisini kurarak olayı çözmemizi sağladı. Bayram ve Deniz hocam da, pratik uygulamasında çok yardımcı oldu. Sağolun.

Code:

Compile it !

Choose the ERC20 file from Contract Section and deploy with your datas.

0x5B38Da6a701c568545dCfcB03FcB875f56beddC4 is owner

……………………………………………………………………..

This owner account has 1000 token.

We are 4 persons.

Owner

Bob

Alice

Su

Owner sents the check to Alice [ approve ]

We use transferFrom function when we use approve mechanism. You can think approve mechanism as check.

Owner will give a check to Alice . Check’s value will be 100 token. Alice’s balance does not increase because this is only permission for spending.

We can see this reality from remix screen.

Owner approves the transaction.

Now we can check allowance and balanceOf

This is ERC20 transferFrom function.

https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol

Owner would like to sent 100 tokens (as check) to the Alice

Owner need to use the approve function. Owner approves that Alice can spent the owner token.

Alice sents 100 tokens to Bob[ transferFrom ]

Alice sents 100 tokens to Bob. Don’t forget that Alice can sent these tokens with transferFrom function. (Because she took these tokens with approve mechanism)

MORE IMPORTANT : We are in Alice ACCOUNT

and fill transferFrom function as the follows

It is important. You put the owner address to the from section because Alice spents owner’s token with owner permission.

Alice sents the owner tokens with owner permission to the Bob.

Bob is free . He can use directly transfer or use approve

Bob will give a check to Su . Check’s value will be 50 token. Su’s balance does not increase because this is only permission for spending

MORE IMPORTANT : We are in Bob ACCOUNT

Bob sent 50 tokens directly to other account

MORE IMPORTANT : We are in Bob ACCOUNT

This is result.

Dr. Engin YILMAZ

2022

Ankara

--

--

Solidity Programming Language

Solidity basics for beginners: Learn the fundamentals of smart contract development and build your first DApp! #Solidity #Foundry #Ethereum #Opcodes #DApps