Deploying with Foundry

What is Foundry

Foundry is a smart contract development toolchain.Foundry manages your dependencies, compiles your project, runs tests, deploys, and lets you interact with the chain from the command-line and via Solidity scripts.For more information please visit: Foundry Book

Setting up the development environment

  1. Install Foundry binary

  • curl -L https://foundry.paradigm.xyz | bash
  1. If you have installed foundry script or want to check

  • forge --version

Project Config

In root project it has foundry.toml

Next edit file as below

## foundry.toml
[profile.default]
libs = ['lib']
out = 'out'
src = 'src'

optimizer = true
optimizer_runs = 200
solc_version = "0.8.13"

# See more config options https://github.com/foundry-rs/foundry/tree/master/config

[rpc_endpoints]
sixnet = "${SIXNET_RPC_URL}"
fivenet = "${FIVENET_RPC_URL}"

[etherscan]
fivenet = {key = "ANY STRING HERE", chain = 150, url="https://fivenet.evm.sixscan.io/api"}
sixnet = {key = "ANY STRING HERE", chain = 98, url="https://evm.sixscan.io/api"}

Next set up .env file. Foundry required .env

  1. Create .env file by

  • touch .env
  1. Edit .env file as below

  • ## USER CONFIG
    PRIVATE_KEY=0xPrivateKey
    
    ## NODE CONFIG
    # MAINNET
    SIXET_RPC_URL=https://sixnet-rpc-evm.sixprotocol.net:443
    SIXNET_VERIFIER_URL=https://evm.sixscan.io/api
    
    #TESTNET
    FIVENET_RPC_URL=https://rpc-evm.fivenet.sixprotocol.net:443
    FIVENET_VERIFIER_URL=https://fivenet.evm.sixscan.io/api

Compiling your contract

Next, if you take a look at the src/ folder, you will see Counter.sol :

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

contract Counter {
    uint256 public number;

    function setNumber(uint256 newNumber) public {
        number = newNumber;
    }

    function increment() public {
        number++;
    }
}

To compile it, simply run forge compile

[⠊] Compiling...
[⠆] Compiling 27 files with Solc 0.8.23
[⠰] Solc 0.8.23 finished in 1.22s

Deploying your contract

In folder scripts/ there is file Counter.s.sol :

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import {Script, console} from "forge-std/Script.sol";
import {Counter} from "../src/Counter.sol";

contract CounterScript is Script {
    Counter public counter;

    function setUp() public {}

    function run() public {
        vm.startBroadcast();

        counter = new Counter();

        vm.stopBroadcast();
    }
}

counter = new Counter(); this line only is how create or deploy new contractto deploy run

  1. source .env to initialize our environment

  2. forge script script/Counter.s.sol:CounterScript --broadcast --optimize --rpc-url $FIVENET_RPC_URL --private-key $PRIVATE_KEY

Verify your contract

forge verify-contract <contract_address> ./src/Counter.sol:Counter --verifier blockscout --verifier-url $FIVENET_VERIFIER_URL --chain 150

Output will similar to

Submitting verification for [src/Counter.sol:Counter] 0x5586139954C5De6b48A3F0aA546AF6f700b51335.
Submitted contract for verification:
        Response: `OK`
        GUID: `5586139954c5de6b48a3f0aa546af6f700b5133566f66b0b`
        URL: https://evm.sixscan.io/address/0x5586139954c5de6b48a3f0aa546af6f700b51335

Last updated