Objectives
By the end of this lesson you should be able to:- Describe OpenZeppelin
- Import the OpenZeppelin ERC-20 implementation
- Describe the difference between the ERC-20 standard and OpenZeppelin’s ERC20.sol
- Build and deploy an ERC-20 compliant token
Setting Up the Contract
Create a new Solidity file, add the license and pragma, and import the ERC-20 implementation linked above. Add a contract calledMyERC20Token
that inherits from ERC20
.
Adding a Constructor
Review the constructor on line 53 of the OpenZeppelin implementation. It requires strings for the name and symbol you wish to use for your token. They’re using a slightly different naming convention by putting the_
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:
There is neither a governing body nor built-in programmatic rules preventing you, or anyone else, from using the same name and symbol as an already in-use token. Scammers often take advantage of this fact, and even well-meaning developers can cause confusion by not being careful here.

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.
ERC-20 Further Testing
Line 251 of the OpenZeppelin implementation contains a_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.
Minting in the Constructor
One method of using the_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
Reveal code
Testing the Transfer Function
Try using thetransfer
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.
Testing the Transfer From Function
You might have noticed that there’s another function calledtransferFrom
. 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.