LCX Logo

    Deploy with Hardhat

    Hardhat is one of the most popular Ethereum development frameworks, offering a robust environment for compiling, testing, deploying, and verifying smart contracts. Because Liberty Chain is fully EVM-compatible, your existing Hardhat workflow works with minimal configuration changes — just point it at the Liberty Chain RPC endpoint.

    Prerequisites

    • Node.js 18+ — Download from nodejs.org.
    • npm — Comes bundled with Node.js.
    • Test ETH on Liberty Chain — Follow the Get Test ETH guide to fund your wallet.

    Step 1: Create a Hardhat Project

    Initialize a new Hardhat project with TypeScript support. When prompted, select "Create a TypeScript project" and accept the default settings.

    Initialize project
    bash
    mkdir my-liberty-project && cd my-liberty-project
    npx hardhat init

    Select "Create a TypeScript project" when the interactive prompt appears, and accept the defaults for the remaining options.

    Step 2: Install Dependencies

    Install the project dependencies, including any additional packages needed for deployment and environment variable management.

    Install dependencies
    bash
    npm install
    npm install dotenv

    Step 3: Configure the Network

    Open hardhat.config.ts and add Liberty Chain as a network target. This tells Hardhat how to connect to the Liberty Chain testnet when deploying or running scripts.

    hardhat.config.ts
    typescript
    import { HardhatUserConfig } from "hardhat/config";
    import "@nomicfoundation/hardhat-toolbox";
    import dotenv from "dotenv";
    
    dotenv.config();
    
    const config: HardhatUserConfig = {
      solidity: "0.8.24",
      networks: {
        libertyChain: {
          url: "https://testnet-rpc.lcx.com",
          chainId: 76847801,
          accounts: [process.env.PRIVATE_KEY!],
        },
      },
    };
    
    export default config;

    Use a .env File for Your Private Key

    Create a .env file in your project root with your private key: PRIVATE_KEY=0xYourPrivateKeyHere. Add .env to your .gitignore file to ensure it is never committed to version control.

    Step 4: Write a Smart Contract

    Create a simple contract to deploy. This example implements a basic storage contract that allows anyone to store and retrieve a value.

    contracts/SimpleStorage.sol
    solidity
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.24;
    
    contract SimpleStorage {
        uint256 private _value;
    
        event ValueChanged(uint256 newValue);
    
        function store(uint256 value) public {
            _value = value;
            emit ValueChanged(value);
        }
    
        function retrieve() public view returns (uint256) {
            return _value;
        }
    }

    Step 5: Write a Deploy Script

    Create a deployment script that compiles and deploys the contract to Liberty Chain.

    scripts/deploy.ts
    typescript
    import { ethers } from "hardhat";
    
    async function main() {
      const SimpleStorage = await ethers.getContractFactory("SimpleStorage");
      const simpleStorage = await SimpleStorage.deploy();
    
      await simpleStorage.waitForDeployment();
    
      const address = await simpleStorage.getAddress();
      console.log("SimpleStorage deployed to:", address);
    }
    
    main().catch((error) => {
      console.error(error);
      process.exitCode = 1;
    });

    Step 6: Deploy to Liberty Chain

    Run the deploy script targeting the Liberty Chain network. Hardhat will compile the contract, send the deployment transaction, and wait for confirmation.

    Deploy
    bash
    npx hardhat run scripts/deploy.ts --network libertyChain

    After a successful deployment, the console will print the contract address. You can view your deployed contract on the Liberty Chain Block Explorer.

    Step 7: Verify Your Contract

    Verifying your contract links the on-chain bytecode to its source code, making it readable on the explorer. First, add the Blockscout verification configuration to your Hardhat config.

    hardhat.config.ts — Blockscout verification
    typescript
    import { HardhatUserConfig } from "hardhat/config";
    import "@nomicfoundation/hardhat-toolbox";
    import dotenv from "dotenv";
    
    dotenv.config();
    
    const config: HardhatUserConfig = {
      solidity: "0.8.24",
      networks: {
        libertyChain: {
          url: "https://testnet-rpc.lcx.com",
          chainId: 76847801,
          accounts: [process.env.PRIVATE_KEY!],
        },
      },
      etherscan: {
        apiKey: {
          libertyChain: "abc",
        },
        customChains: [
          {
            network: "libertyChain",
            chainId: 76847801,
            urls: {
              apiURL: "https://testnet-explorer-api.lcx.com/api",
              browserURL: "https://testnet-explorer.lcx.com",
            },
          },
        ],
      },
    };
    
    export default config;

    Then run the verify command, replacing <address> with your deployed contract address:

    Verify contract
    bash
    npx hardhat verify --network libertyChain <address>

    Once verified, you can interact with your contract's read and write functions directly from the explorer. For more verification options, see the Verify Smart Contracts guide.

    Blockscout API Key

    The Blockscout explorer does not require a real API key for verification. The placeholder value "abc" is sufficient because Blockscout's verification endpoint does not enforce API key authentication. Hardhat requires the field to be present, so any non-empty string will work.