Skip to content

ERC20 Smart Contract

The first smart contract is the Token. We don't invent it ourselves, we take the ERC20 Smart Contract from OpenZeppelin. In this version open-zeppelin v3, with Solidity 0.6 smart contracts:

View the Full Course Now

Installation

In the console type:

npm install --save @openzeppelin/contracts@v3.0.0

Update from the Video

Note we will be using the v3.0.0 of openzeppelin contracts, instead of v3.0.0-beta.0

Possible Scenario

Let's think about a possible work-scenario. We will create a Token which let's you redeem a coffee at your favorite Coffee Store: StarDucks Coffee from Duckburg. It will look slightly different from other ERC20 tokens, since a coffee is hardly divisible, but still transferrable. Let's create our Token.

Adding the Token

Add a "MyToken.sol" file to the "/contracts" folder:

pragma solidity >=0.6.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract MyToken is ERC20 {
    constructor(uint256 initialSupply) ERC20("StarDucks Capu-Token", "SCT") public {
        _mint(msg.sender, initialSupply);
    _setupDecimals(0);
    }
}

Note

The ERC20Detailed has been deprecated and combined into the ERC20 contract default. The new ERC20 contract has a constructor with arguments for the name and symbol of the token. It has a name "StarDucks Capu-Token", a symbol "SCT". The new ERC20 contract has a default decimal points of 18, we can change it to decimal points of 0 in the constructor by calling the setupDecimals(uint8 decimals) function in the ERC20 contract, with 0 as the argument.