LCX Logo

    Deploy Your First Contract in 5 Minutes

    Liberty Chain is fully EVM-compatible, so you can use the same tools you already know from Ethereum development. This quickstart walks you through deploying a simple smart contract using Hardhat, with a brief Foundry alternative at the end.

    Prerequisites

    • Node.js 18+ — download from nodejs.org
    • A wallet with test ETH — follow the Get Test ETH guide if you have not done this yet.
    • Your wallet private key — exported from MetaMask or your preferred wallet. Never share this or commit it to version control.

    Private Key Safety

    Never commit your private key to Git or share it publicly. Use environment variables or a .env file (added to .gitignore) to store sensitive values.

    1. Initialize a Hardhat Project

    Create project
    bash
    mkdir my-liberty-project && cd my-liberty-project
    npm init -y
    npm install --save-dev hardhat @nomicfoundation/hardhat-toolbox dotenv
    npx hardhat init

    Select "Create a JavaScript project" when prompted and accept the defaults.

    2. Configure the Network

    Create a .env file in your project root:

    .env
    bash
    PRIVATE_KEY=your_wallet_private_key_here

    Update hardhat.config.js to add Liberty Chain Testnet:

    hardhat.config.js
    javascript
    require("@nomicfoundation/hardhat-toolbox");
    require("dotenv").config();
    
    module.exports = {
      solidity: "0.8.24",
      networks: {
        libertyTestnet: {
          url: "https://testnet-rpc.lcx.com",
          chainId: 76847801,
          accounts: [process.env.PRIVATE_KEY],
        },
      },
      etherscan: {
        apiKey: {
          libertyTestnet: "not-needed",
        },
        customChains: [
          {
            network: "libertyTestnet",
            chainId: 76847801,
            urls: {
              apiURL: "https://testnet-explorer.lcx.com/api",
              browserURL: "https://testnet-explorer.lcx.com",
            },
          },
        ],
      },
    };

    3. Write the Contract

    Create a simple storage contract at contracts/Storage.sol:

    contracts/Storage.sol
    solidity
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.24;
    
    contract Storage {
        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;
        }
    }

    4. Write the Deploy Script

    Create a deployment script at scripts/deploy.js:

    scripts/deploy.js
    javascript
    const hre = require("hardhat");
    
    async function main() {
      const Storage = await hre.ethers.getContractFactory("Storage");
      const storage = await Storage.deploy();
      await storage.waitForDeployment();
    
      const address = await storage.getAddress();
      console.log("Storage deployed to:", address);
    
      // Store a value
      const tx = await storage.store(42);
      await tx.wait();
      console.log("Stored value: 42");
    
      // Read it back
      const value = await storage.retrieve();
      console.log("Retrieved value:", value.toString());
    }
    
    main().catch((error) => {
      console.error(error);
      process.exitCode = 1;
    });

    5. Deploy to Liberty Chain

    Compile the contract and deploy it to Liberty Chain Testnet:

    Compile and deploy
    bash
    npx hardhat compile
    npx hardhat run scripts/deploy.js --network libertyTestnet

    You should see output similar to:

    Expected output
    text
    Storage deployed to: 0x5FbDB2315678afecb367f032d93F642f64180aa3
    Stored value: 42
    Retrieved value: 42

    Fast Confirmations

    Transactions on Liberty Chain confirm in approximately 2 seconds, so your deployment will complete almost instantly compared to Ethereum mainnet.

    6. Verify on Blockscout

    Verify your contract source code on the Liberty Chain block explorer so anyone can read and interact with it:

    Verify contract
    bash
    npx hardhat verify --network libertyTestnet <DEPLOYED_CONTRACT_ADDRESS>

    Replace <DEPLOYED_CONTRACT_ADDRESS> with the address printed during deployment. Once verified, you can view and interact with your contract on the Liberty Chain Explorer.

    Alternative: Foundry

    If you prefer Foundry, you can deploy to Liberty Chain with forge:

    Deploy with Foundry
    bash
    # Install Foundry (if not already installed)
    curl -L https://foundry.paradigm.xyz | bash
    foundryup
    
    # Create a new project
    forge init my-liberty-project && cd my-liberty-project
    
    # Deploy to Liberty Chain Testnet
    forge create --rpc-url https://testnet-rpc.lcx.com \
      --private-key $PRIVATE_KEY \
      src/Counter.sol:Counter
    
    # Verify on Blockscout
    forge verify-contract <ADDRESS> src/Counter.sol:Counter \
      --verifier blockscout \
      --verifier-url https://testnet-explorer.lcx.com/api

    Info

    For a more detailed guide on deploying with Foundry, see Deploy with Foundry.

    Next Steps