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:
Installation
In the console type:
npm install --save @openzeppelin/contracts@v3.0.0
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);
}
}
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.