Create ERC-20 Token

This tutorial guides you through the process of deploying an ERC-20 token on the Fiefdom Playground testnet blockchain.

ERC-20 tokens are a standard for creating fungible tokens on the Ethereum blockchain, and thanks to Fiefdom's EVM compatibility, the process is similar to deploying on Ethereum. By following these steps, you'll learn how to create and deploy your own ERC-20 token.

Prerequisites

  • Ensure you have Node.js and npm installed.

  • A text editor or IDE for writing and editing your smart contract code.

  • MetaMask or another Ethereum-compatible wallet, set up and configured for the Fiefdom network.

  • Some testnet FIEF in your wallet for deploying the contract.

Step 1: Setting Up Your Project

  1. Create a new directory for your project and navigate into it.

  2. Initialize a new Node.js project by running npm init -y in your terminal.

  3. Install Hardhat, a popular development environment for compiling, deploying, and testing Ethereum software. Install it using npm:

    npm install --save-dev hardhat

Step 2: Initializing Your Hardhat Project

  1. Run npx hardhat in your project directory. Select “Create an empty hardhat.config.js” when prompted.

  2. Install the OpenZeppelin Contracts library, which provides a secure and community-audited ERC-20 implementation:

    npm install @openzeppelin/contracts

Step 3: Writing Your ERC-20 Token Contract

  1. Create a contracts folder in your project root.

  2. Inside contracts, create a new file named MyToken.sol.

  3. Open MyToken.sol in your editor and define your ERC-20 token by extending OpenZeppelin’s ERC20 contract. Here is a simple example:

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

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

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

This contract creates an ERC-20 token named "MyToken" with the symbol "MTK". The constructor sets the initial token supply and assigns it to the deployer's address.

Step 4: Compiling Your Contract

  1. Create a hardhat.config.js file in your project root if it’s not already there from the Hardhat setup.

  2. Configure Hardhat to use the Solidity version installed by OpenZeppelin Contracts and set up the Fiefdom Playground network configuration:

require("@nomiclabs/hardhat-waffle");

module.exports = {
  solidity: "0.8.0",
  networks: {
    fiefdomPlayground: {
      url: "https://fiefdom-playground.calderachain.xyz/http",
      accounts: [/* Your private key here */],
      chainId: 712,
    }
  }
};

Replace /* Your private key here */ with your own private key. Be careful with your private key and consider using environment variables to keep it secure.

  1. Compile your contract by running npx hardhat compile.

Step 5: Deploying Your ERC-20 Token

  1. Create a scripts folder in your project root.

  2. Inside scripts, create a new file named deploy.js.

  3. Write a deployment script in deploy.js:

async function main() {
    const [deployer] = await ethers.getSigners();

    console.log("Deploying contracts with the account:", deployer.address);

    const MyToken = await ethers.getContractFactory("MyToken");
    const token = await MyToken.deploy(1000000); // Deploy with 1,000,000 initial supply

    console.log("Token address:", token.address);
}

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

This script deploys your MyToken contract with an initial supply of 1,000,000 tokens (adjust the supply as needed).

  1. Deploy your token to the Fiefdom network by running:

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

Last updated