Deploying the Contract

To deploy your ERC-20 token, do the following:

  1. In the project directory, create a contracts directory.

    mkdir contracts
    
  2. Create a new file in the contracts directory called MyToken.sol.

  3. Copy and paste the following code into MyToken.sol.

    //SPDX-License-Identifier: Unlicense
    pragma solidity ^0.8.0;
    
    import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
    
    contract WinterSchoolToken is ERC20 {
        uint constant _initial_supply = 100 * (10**18);
    
        /* ERC 20 constructor takes in two strings:
         1. The name of your token name
         2. A symbol for your token
        */
        constructor() ERC20("Winter School Token", "WST") {
            _mint(msg.sender, _initial_supply);
        }
    }
    
  4. Compile the contract by running the following command.

    npx hardhat compile
    

    You should see a message saying Compiled 1 Solidity file successfully.

  5. Create a directory called scripts

    mkdir scripts
    
  6. Create a file called deploy.js in the scripts directory with the following content.

    async function main() {
        const MyToken = await ethers.getContractFactory("WinterSchoolToken");
    
        // Start deployment, returning a promise that resolves to a contract object
        const my_token = await MyToken.deploy();   
        console.log("ERC-20 contract deployed to address:", my_token.address);
    }
    
    main()
    .then(() => process.exit(0))
    .catch(error => {
        console.error(error);
        process.exit(1);
    });
    
  7. Deploy the contract by running the following command.

    npx hardhat run scripts/deploy.js --network goerli
    

    You should see a message of the following form. The address will be different in your case.

    ERC-20 contract deployed to address: 0xbb8Ab9564596Ccbfe0C6eD49D7FdB056eE741CE5
    
  8. Go to https://goerli.etherscan.io/token/[Your Token Address] to see the token details. Notice that you have to enter the address of the newly created token in the URL.

Customize and Launch Your Token (optional)

  1. The Winter School Token launched in the previous section has three characteristics that can be customized.

    • Token name: Winter School Token
    • Token symbol: WST
    • Initial supply: 100 WST

    Note: The _initial_supply variable in MyToken.sol is set to 100 * (10**18) which equals \(100 \times 10^{18}\). This corresponds to 100 WST tokens because the default value for the number of decimals in OpenZeppelin's ERC20 implementation is 18. This is the same with Ether where 1 ETH = \(10^{18}\) wei.

  2. *Customize your token by changing the values in the constructor arguments and the initial supply. The following lines in MyToken.sol need to be changed.

    uint constant _initial_supply = ...;
    ...
    constructor() ERC20("Winter School Token", "WST") {
    
  3. Run the following commands to lauch your customized token contract.

    npx hardhat compile
    npx hardhat run scripts/deploy.js --network goerli
    

    Note: If you change the name of the contract from WinterSchoolToken to something else, remember to enter the new name as the argument to the ethers.getContractFactory in scripts/deploy.js.

  4. Go to https://goerli.etherscan.io/token/[Your Token Address] to see the token details.