LCX Logo

    Deploy with Foundry

    Foundry is a fast, portable, and modular toolkit for Ethereum application development written in Rust. It includes Forge (testing), Cast (CLI interactions), Anvil (local node), and Chisel (REPL). Because Liberty Chain is fully EVM-compatible, Foundry works out of the box — just point your commands at the Liberty Chain RPC endpoint.

    Prerequisites

    Install Foundry using the official installer. This will install forge, cast, anvil, and chisel.

    Install Foundry
    bash
    curl -L https://foundry.paradigm.xyz | bash
    foundryup

    After running foundryup, verify the installation by running forge --version. You will also need test ETH on Liberty Chain to deploy contracts — see the Get Test ETH guide.

    Step 1: Create a New Project

    Initialize a new Foundry project with the standard directory structure.

    Create project
    bash
    forge init my-project && cd my-project

    This creates a project with src/ for contracts, script/ for deploy scripts, test/ for tests, and a foundry.toml configuration file.

    Step 2: Write Your Contract

    Replace the default contract with your own. Here is a simple counter contract to get started.

    src/Counter.sol
    solidity
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.24;
    
    contract Counter {
        uint256 public number;
    
        event NumberSet(uint256 newNumber);
        event NumberIncremented(uint256 newNumber);
    
        function setNumber(uint256 newNumber) public {
            number = newNumber;
            emit NumberSet(newNumber);
        }
    
        function increment() public {
            number++;
            emit NumberIncremented(number);
        }
    }

    Step 3: Write a Deploy Script

    Foundry uses Solidity-based deploy scripts. Create a script that deploys the Counter contract.

    script/Deploy.s.sol
    solidity
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.24;
    
    import "forge-std/Script.sol";
    import "../src/Counter.sol";
    
    contract DeployScript is Script {
        function run() external {
            uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
    
            vm.startBroadcast(deployerPrivateKey);
    
            Counter counter = new Counter();
            console.log("Counter deployed to:", address(counter));
    
            vm.stopBroadcast();
        }
    }

    Step 4: Deploy to Liberty Chain

    Run the deploy script targeting the Liberty Chain testnet RPC. The --broadcast flag sends the transactions to the network (without it, the script runs a dry run).

    Deploy to Liberty Chain
    bash
    forge script script/Deploy.s.sol \
      --rpc-url https://testnet-rpc.lcx.com \
      --broadcast \
      --private-key $PRIVATE_KEY

    After deployment, Forge will output the contract address and transaction hash. You can view your contract on the Liberty Chain Block Explorer.

    Never Commit Private Keys

    Never hardcode private keys in your scripts or commit them to version control. Use environment variables or a .env file. For production deployments, consider using a hardware wallet with --ledger or a keystore file with --keystore.

    Step 5: Verify Your Contract

    Verify the deployed contract on the Blockscout explorer so that users and other developers can read the source code and interact with it directly.

    Verify on Blockscout
    bash
    forge verify-contract <address> src/Counter.sol:Counter \
      --chain-id 76847801 \
      --verifier blockscout \
      --verifier-url https://testnet-explorer-api.lcx.com/api

    Replace <address> with the address printed during deployment. Once verified, the contract's source code will be visible on the explorer and users can interact with its functions through the Read/Write tabs.

    Testing Your Contracts

    Foundry includes a powerful testing framework that runs tests written in Solidity. Tests execute locally using Forge's built-in EVM, so they are extremely fast and do not require a running network.

    Run tests
    bash
    forge test

    Add the -vvvv flag for detailed trace output, which is helpful when debugging failing tests:

    Verbose test output
    bash
    forge test -vvvv

    Since Liberty Chain is EVM-equivalent, contracts that pass local Forge tests will behave identically when deployed to the network. There is no need for special testing configurations or chain-specific test utilities.