Hardhat Installation & Deploy your First L1X EVM FT Contract

You can use Hardhat and Ethers to Compile, Deploy and Interact with Contracts on L1X EVM.

Step 1: Initialize a New Project

  1. Create a New Directory (if you're starting fresh):

    mkdir L1XEVMERC20
    cd L1XEVMERC20
    
  2. Initialize a new NPM project:

    npm init -y
    

Step 2: Install Hardhat and Set Up the Project

  1. Install Hardhat:

    npm install --save-dev hardhat
    
  2. Set up the Hardhat project: Run the setup command and choose to create a TypeScript project:

    npx hardhat
    

    When prompted, select to create a TypeScript project. Follow the prompts to add a .gitignore and install the project's dependencies.

Step 3: Install Necessary Plugins and Dependencies

  1. Install TypeScript-related dependencies:

    npm install --save-dev ts-node typescript @types/node @types/mocha
    
  2. Install OpenZeppelin Contracts:

    npm install @openzeppelin/contracts
    
  3. Install Ethers and Hardhat Ethers (ensure compatibility):

    npm install --save-dev ethers @nomicfoundation/hardhat-ethers
    

Step 4: Configure TypeScript

  1. Create a tsconfig.json file in your project root with the following content:

    {
      "compilerOptions": {
        "target": "ES2020",
        "module": "CommonJS",
        "strict": true,
        "esModuleInterop": true,
        "outDir": "dist",
        "noImplicitAny": true
      },
      "include": [
        "./scripts/**/*",
        "./test/**/*",
        "./hardhat.config.ts"
      ],
      "exclude": ["node_modules"]
    }
    

Step 5: Write Your Smart Contract

  1. Create an ERC20 token using OpenZeppelin: Create a contracts/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("MyToken", "MTK") {
            _mint(msg.sender, initialSupply);
        }
    }
    

Step 7: Compile Your Contracts

  1. Compile your project:

    npx hardhat compile
    
  2. Write deployment scripts or tests as needed, using the setup you've created. Example Provided below.

// scripts/deploy_myerc20.js
const { ethers } = require("hardhat");

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

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

  const Token = await ethers.getContractFactory("Token");
  const initialSupply = "1000000000000000000000"; // 1000 tokens
  const name = "L1XToken";
  const symbol = "XT";
  const decimals = 18;

  const token = await Token.deploy(
    initialSupply,
    // name,
    // symbol,
    // decimals
  );

  // await token.waitForDeployment();

  console.log("Token deployed to:",await token.getAddress());
}

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

Sample Hardhat Config TS File

import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";
import '@nomicfoundation/hardhat-ethers';

// Enter your Private Key here
const PRIVATE_KEY = 'xx'; 

const config: HardhatUserConfig = {
  solidity: {
    version: "0.8.20",
    settings: {
      optimizer: {
        enabled: true,
      },
    },
  },
  networks: {
    localhost: {
      url: 'localhost:8545',
    },
    l1xTestnet: {
      url: '<https://v2-testnet-rpc.l1x.foundation>',
      accounts: [PRIVATE_KEY], 
    },
  },
};

export default config;

-- Check Endpoint for L1X TestNet Faucet Before Deployment

Deployment Bash

npx hardhat run scripts/deploy.ts --network l1xTestnet

Last updated