Create ERC20 Token with Openzeppelin

Create custom ERC20 tokens with openzeppelin without much coding !

erc20

ganesh deshpande

@theblockmatrix
1069 2 2

Setting Up Files

We will be using remix for the deployment purpose. Create new file named your token.sol...

 

Import openzeppeline erc20 solidity file

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

 

Declare contract and create constructor

contract MyToken is ERC20 {
    constructor(uint256 initialSupply) ERC20("MyTokenname", "MyTokenSymbol") {
        _mint(msg.sender, initialSupply);
    }
}

Give contract your token name and also in ERC20("", "") put your token name and symbol parameters.

Also constructor takes one more parameter - initialSupply, this will decide how much tokens you want to supply. note that the default decimals in openzeppeline in 18. you can change it if you want by calling their decimals() function. You will need to pass the intialSupply number along with decimals.

 

Change decimals

function decimals() public view virtual override returns (uint8) {
  return 16;
}

Note that you can use more functionality to this like minting new tokens, burning tokens, transfer tokens, etc

 

The final file will look like this :

// contracts/GLDToken.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

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

contract MyToken is ERC20 {

    constructor(uint256 initialSupply) ERC20("MyTokenname", "MyTokenSymbol") {
        _mint(msg.sender, initialSupply);
    }

   function decimals() public view virtual override returns (uint8) {
     return decimalnumber;  // eg. return 12;
   }
}

you can always add some custom functions in the contract and call the declared functions in openzeppelin erc20 sol file.


More SmartBooks like this