Deploy and Verify Contract on EVM with Foundry


Back to All Posts

This guide provides step-by-step instructions on manually deploying and verifying an ERC-20 contract on the Sepolia network using Foundry.


Initial Setup

  1. Install Foundry (if not installed): Foundry is the tool we will use to compile and deploy the contract.

    curl -L https://foundry.paradigm.xyz | bash
    foundryup
    
  2. Install OpenZeppelin Contracts: To avoid errors when using the OpenZeppelin library, clone the OpenZeppelin Contracts repository into your project:

    git clone https://github.com/OpenZeppelin/openzeppelin-contracts.git lib/openzeppelin-contracts
    
  3. Configure foundry.toml: Create a foundry.toml file in your project directory to set source and output locations and add the RPC endpoint for the Sepolia network:

    [profile.default]
    src = "src"
    out = "out"
    libs = ["lib"]
    
    [rpc_endpoints]
    unichain = "https://sepolia.unichain.org"
    

1. Create the ERC-20 Contract

Create a Solidity file in the src/ directory named MyToken.sol for your ERC-20 contract, and use the following template:

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

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

contract MyToken is ERC20 {
    constructor() ERC20("MyToken", "MTK") {
        _mint(msg.sender, 100000 * (10 ** decimals()));
    }
}
  • Token Name: "MyToken"
  • Token Symbol: "MTK"
  • Mint Amount: 100,000 tokens

2. Compile the Contract

After writing your contract code, compile it with the following command:

forge build

If there are no errors, the contract is compiled, and you can proceed to the deployment step.


3. Deploy the Contract

To deploy the contract on the Sepolia network, use this command:

forge create src/MyToken.sol:MyToken --rpc-url https://sepolia.unichain.org --private-key <PRIVATE_KEY>

Replace <PRIVATE_KEY> with your wallet's private key.

  • If deployment is successful, you will see the address of the newly deployed contract. Copy this address, as you will need it for verification.

4. Verify the Contract

Once deployed, verify the contract on Etherscan. You can do this by using the Etherscan API:

  1. Create an account on Etherscan and obtain an API key.

  2. Run the following command to verify the contract:

    forge verify-contract <CONTRACT_ADDRESS> src/MyToken.sol:MyToken <ETHERSCAN_API_KEY> --chain-id 11155111
    

    Replace:

    • <CONTRACT_ADDRESS> with the deployed contract's address.
    • <ETHERSCAN_API_KEY> with your Etherscan API key.
    • --chain-id 11155111 with the chain ID for Sepolia.

If the verification is successful, your contract will appear as "Verified" on Sepolia Etherscan.


By following these steps, you can manually deploy and verify an ERC-20 contract on the Sepolia network.