Ethernity Layer 2
  • Introduction
    • ๐ŸŒŽIntroducing Ethernity Chain
    • โ„น๏ธGeneral Information
    • ๐Ÿ’ณConnect your wallet
    • ๐Ÿ‘ฉโ€๐Ÿ’ปNetwork Information
  • ERN TOKEN
    • ๐Ÿช™ERN Tokenomics
    • ๐Ÿฅ‡Privileges for ERN Holders
  • DEFI PRODUCTS
    • ๐ŸŽฏFixed Staking
    • ๐Ÿค‘Satoshi Staking
    • ๐Ÿ’ŽStones Farming
  • GOVERNANCE
    • ๐Ÿ›๏ธDAO
  • DRM
    • ๐Ÿง Introduction
    • โ“How it works
    • ๐Ÿ’ฏAuthenticity Confidence
    • โ„ข๏ธProtected brands/licenses
    • โžก๏ธAdd your Collection
  • DEVELOPER TOOLS
    • ๐Ÿ’ปNetwork Information
    • ๐Ÿ“”Addresses
    • ๐Ÿ—‚๏ธIndexers
    • ๐Ÿ’ณWallets
  • TUTORIALS
    • Deploy a Smart Contract
      • With Hardhat
      • With Foundry
      • With Remix
    • โœ…Verify a Smart Contract
    • ๐Ÿ’ฐCreate an ERC-20 token
    • ๐Ÿ‘ทCreate an NFT Collection
  • SECURITY
    • ๐Ÿ”ŽAudits
    • ๐ŸŽBug Bounty
  • OTHER
    • ๐Ÿง‘โ€๐Ÿคโ€๐Ÿง‘Community
    • ๐Ÿ“šMedia Kit
    • ๐Ÿ”Privacy Policy
    • ๐Ÿ“„Terms of Service
Powered by GitBook
On this page
  • 1. Initialize a Hardhat TypeScript project
  • 2. Write the Token Smart Contract
  • 3. Configure Harhdat for Ethernity
  • 4. Compile
  • 5. Deploy
  • 6. Verify
  • 7. Check

Was this helpful?

  1. TUTORIALS

Create an ERC-20 token

PreviousVerify a Smart ContractNextCreate an NFT Collection

Last updated 11 months ago

Was this helpful?

Creating an ERC-20 token steps are pretty much the same steps as deploying a smart contract, the only difference is the contract code. You will be able to create an ERC-20 token that can be a memecoin, an utility cryptocurrency, a social token, etc. Let's go over the steps with the Hardhat example and using battle-tested :

Requirements

  • Node.js and npm: download and install both packages

  • Ethereum Wallet: or any other non-custodial Ethereum wallet, since you will need the

  • ETH: You can get some and then bridge it to Ethernity

  • Solidity and CLI knowledge

1. Initialize a Hardhat TypeScript project

Open your terminal and create a new directory for your project, then navigate into it:

mkdir token && cd token

Initialize an npm project:

npm init -y

Install the necessary packages for Hardhat, TypeScript and Open Zeppelin:

npm install --save-dev hardhat ts-node typescript @nomiclabs/hardhat-ethers ethers @openzeppelin/contracts

Start a new Hardhat project with TypeScript:

npx hardhat init

When prompted, make the following selections:

  • Choose "Create a TypeScript project".

  • For the .gitignore prompt, select "Yes" (or y).

  • For installing the projects dependencies select "Yes" (or y).

npx hardhat

888    888                      888 888               888
888    888                      888 888               888
888    888                      888 888               888
8888888888  8888b.  888d888 .d88888 88888b.   8888b.  888888
888    888      88b 888P   d88  888 888  88b      88b 888
888    888 .d888888 888    888  888 888  888 .d888888 888
888    888 888  888 888    Y88b 888 888  888 888  888 Y88b.
888    888  Y888888 888      Y88888 888  888  Y888888  Y888

๐Ÿ‘ท Welcome to Hardhat v2.18.2 ๐Ÿ‘ทโ€

โœ” What do you want to do? ยท Create a TypeScript project
โœ” Hardhat project root: ยท /Users/Ethernity/token
โœ” Do you want to add a .gitignore? (Y/n) ยท y
โœ” Do you want to install this sample project's dependencies with npm (@nomicfoundation/hardhat-toolbox)? (Y/n) ยท y

2. Write the Token Smart Contract

In the contracts directory, delete the sample smart contract Lock.sol and then create a new file named Token.sol.

Fixed supply

For a fixed supply, standard ERC-20 token that will mint the entire initial supply to your wallet, use this code for the Token.sol file:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

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

contract Token is ERC20 {
    constructor(uint256 initialSupply) ERC20("My token", "TOK") {
        _mint(msg.sender, initialSupply);
    }
}

Decimals

The standard is 18 decimal places - if you want to create a token with a different decimals setting, add this function above contract Token ...:

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

and replace the 16 with whatever decimal places you want (not recommended though, better to have the standard of 18).

Variable supply

Although not recommended, you can deploy a mintable token by exposing the standard _mint function:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/ownership/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20Mintable.sol";

contract Token is ERC20, ERC20Mintable {
    constructor(uint256 initialSupply) ERC20("My token", "TOK") {
        _mint(msg.sender, initialSupply);
    }

    function mint(address account, uint256 amount) onlyOwner returns (bool) {
        return super.mint(account, amount);
    }
}

Then only the owner (the wallet that deployed the contract) will get the initial supply, and will be able to mint more.

3. Configure Harhdat for Ethernity

Edit the hardhat.config.ts file to include Ethernity Chain Testnet settings:

import "@nomiclabs/hardhat-ethers";

import { HardhatUserConfig } from "hardhat/config";

const config: HardhatUserConfig = {
  networks: {
    ethernity-testnet: {
      url: "https://testnet.ethernitychain.io",
      chainId: 233,
      accounts: ["PRIVATE_KEY"] // DO NOT SHARE THIS!
    }
  },
  solidity: "0.8.0",
};

export default config;

Replace PRIVATE_KEY with your Ethereum wallet private key.

IMPORTANT: Do not push your hardhat.config.ts file to github or share your private key with anyone.

4. Compile

Compile the smart contract:

npx hardhat compile

5. Deploy

In the scripts directory, create a new file named deploy.ts:

import { ethers } from "hardhat";

async function main() {
    const Token = await ethers.getContractFactory("Token");
    const token = await Token.deploy();
    await token.deployed();
    console.log("Token deployed to:", token.address);
}

main()
  .then(() => process.exit(0))
  .catch(error => {
    console.error(error);
    process.exit(1);
  });

Now you can deploy the smart contract to Ethernity Testnet:

npx hardhat run scripts/deploy.ts --network ethernity-testnet

6. Verify

7. Check

Follow to verify the token contract.

See your brand new Token deployed on the Ethernity Testnet block explorer (ERNScan): . Enter the contract address from the command line in the search bar to see the details.

๐Ÿ’ฐ
Open Zeppelin contracts
here
Metamask
private key
Sepolia ETH
these instructions
https://testnet.ernscan.io