ERC20
Implement ERC20.
ERC20 is a commonly used token standard with 3 important functions, tranfser
, approve
and transferFrom
.
transfer
- Transfer token frommsg.sender
to another accountapprove
- Approve another account to spend your tokens.transferFrom
- Approved account can transfer tokens on your behalf
A common scenario to use approve
and transferFrom
is the following.
You approve
a contract to spend some of your tokens. Next the contract calls transferFrom
to transfer tokens from you into the contract.
By following the 2 steps above, you avoid the risk of accidentally sending tokens to the wrong address.
Here is the interface for ERC20
Transfer
This function will transfer tokens from msg.sender
to recipient
for the amount amount
specified in the input.
Balance of token is stored in
balanceOf
. Decrease balances formsg.sender
and increase forrecipient
.Emit the event
Transfer
.Return
true
to follow ERC20 token standards.
Approve
This function approves spender
to spend amount
of tokens owned by msg.sender
.
Mapping allowance[owner][spender]
stores the amount of tokens owner
has allowed spender
to spend.
Update
allowance
, heremsg.sender
is allowingspender
to spendamount
.Emit the event
Approval
.Return
true
to follow ERC20 token standards.
transferFrom
This function transfers tokens from sender
to recipient
. msg.sender
is approved to spend at least amount
amount of tokens from sender
.
Update
allowance
andbalanceOf
appropriately.Emit the event
Transfer
.Return
true
to follow ERC20 token standards.
mint
This function creates an additional amount
of token to msg.sender
.
Emit the event Transfer
from address(0)
, to msg.sender
for the amount amount
.
This function is not part of ERC20
standard but it is a common function present in many tokens.
Usually only an authorized account will be able to mint new tokens but for this exercise, we will skip the access control.
burn
This function deducts amount
of token from msg.sender
.
Emit the event Transfer
from msg.sender
, to address(0)
for the amount amount
.
burn
is not part of ERC20 standard but it is a common function present in many tokens.
Last updated
Was this helpful?