hew / forge-example

Example project for forge

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Forge Example

A barebones example project on how I setup forge with VSCode along with custom deploy scripts.

Tested on forge 0.1.0 (03bed46 2022-03-15T00:11:36.438888+00:00)

Screen.Recording.2022-03-19.at.11.05.32.am.mov

Overview

  • Uses forge to compile, test, and debug.
  • Uses a custom JS script to deploy, see deploy.js.

I prefer to write the deployment scripts as programatically as possible on a widely supported language as every deployment itself is unique, and sometimes there's inter-dependencies between contracts.

For example:

  1. Deploy contract A
  2. Deploy contract B
  3. Call addOwner on B with A's address

This is overly tricky with bash for me, and would require a LOT of unreadable (and error-prone) commands-line composition with jq and awk to achieve that with the default forge create.

Development

Building and testing

forge build
forge test

# forking from existing state
# -vvv = very very verbose
# forge test -f http://127.0.0.1:8545 -vvv

# To access the debugger
# forge run --debug src/test/Contract.t.sol --sig "testExample()"

Contract Deployment

Copy .env.example to .env and fill it out with correct details.

node --experimental-json-modules scripts/deploy.js

Etherscan Verification

Forge has a very useful in-built etherscan verification utility.

# For list of compiler versions https://etherscan.io/solcversions
forge verify-contract --compiler-version v0.8.12+commit.f00d7308 [CONTRACT ADDRESS] --constructor-args <ARGS> --num-of-optimizations 200 [CONTRACT_PATH:CONTRACT_NAME] [ETHERSCAN_API_KEY]

# To check verification status
forge verify-check [GUID] [ETHERSCAN_KEY]

If you have multiple contracts with pragma abiencoderv2, there is a possibility that you can't verify your contracts on etherscan, there's an open issue on this.

One way to get around it is via paulrberg's multisol or hjubb's solt and verify it manually via the official interface.

The caveat to the approach above is that you have to copy the libraries in lib to node_modules and make sure the path matches as it doesn't read remappings.

For example:

cp -r lib/openzeppelin-contracts node_modules/@openzeppelin
mv node_modules/@openzeppelin/contracts/* node_modules/@openzeppelin
multisol src/Contract.sol

About

Example project for forge


Languages

Language:JavaScript 83.9%Language:Solidity 13.9%Language:Shell 2.2%