Implement your own ERC-20 token.
MyERC20Token
that inherits from ERC20
.
_
after the name of the parameters. Like any other function, you can pass variables of any name as long as they’re the right type, so feel free to continue adding the _
in front in your contract’s constructor:
totalSupply
and all balances are zero.
By default, the decimal for the token will be 18, which is the most common choice. Remember, there aren’t decimal types yet, so 1.0 ETH is really a uint
holding 1 * 10**18, or 1000000000000000000.
_mint
function, but it’s internal. As a result, you’ll need to figure out a minting mechanism and add it via your own contract.
_mint
function is to create an initial supply of tokens in the constructor. Add a call to _mint
that awards 1 full token to the contract creator. Remember, the decimal is 18. Minting literally 1
is creating a tiny speck of dust.
totalSupply
is now 1000000000000000000, as is the balanceOf
the deploying address.
You can also use this to mint to other users. Go ahead and add the second and third accounts:
Reveal code
transfer
function to move tokens around.
What happens if you try to burn a token by sending it to the zero address? Give it a try!
You’ll get an error, because protecting from burning is built into the _transfer
function.
transferFrom
. What’s that for? Check the documentation in the contract to find out!
This function works with the allowance
function to give the owner of one wallet permission to spend up to a specified amount of tokens owned by another. Exchanges can make use of this to allow a user to post tokens for sale at a given price without needing to take possession of them.