Skip to content

A template to create a smart contract project with hardhat-foundry hybrid framework

License

Notifications You must be signed in to change notification settings

SpaceStation09/initialize-contract-template

Repository files navigation

Contract Template

This repo contains the basic files and configuration of a Hardhat-Foundry hybrid development framework. You can simply start a project with Hardhat and Foundry.

If this is your first time with Foundry, check out the installation instructions.

Structure

The initial directory structure looks like:

$ tree . -d -L 1
.
├── deploy
├── src
└── test

Formatter & Code Analyzer

In this template, I configured several formatter and code analyzer in advanced to enhance the coding style for collaborative work: eslint for typescript files, solhint for solidity files, and prettier for all. Furthermore, I also configured cspell to avoid typo in coding.

Dir Explanation

Foundry is responsible for solidity-based test while Hardhat is used to complete deployment of contracts.

  • Solidity-based test:
    • Testing files are placed in ./test.
    • NOTICE: Due to the design in hardhat.config.ts, all testing files should end with .t.sol.
  • Contract Source Code: ./src
  • Deployment: ./deploy is the contract deployment script path.

Installing Dependencies

Foundry typically uses git submodules to manage dependencies, but this template uses Node.js packages because submodules don't scale.

This is how to install dependencies:

  1. Install the dependency using your preferred package manager, e.g. npm install dependency-name
    • Use this syntax to install from GitHub: npm install github:username/repo-name
  2. Add a remapping for the dependency in remappings.txt, e.g. dependency-name=node_modules/dependency-name

Note that forge-std Contracts is pre-installed, so you can follow that as an example.

Build / Compile

Build / Compile the contracts:

$ forge build

Clean

Delete the build artifacts and cache directories:

$ forge clean

Coverage

Get a test coverage report:

$ forge coverage

Generate typechain (by hardhat)

Generate typechain (by hardhat):

$ npx hardhat compile

Deploy

Deploy:

$ npx hardhat --network <network> deploy <tag>

Format

Format the contracts:

$ forge fmt

Gas Usage

Get a gas report:

$ forge test --gas-report

Test

Run the tests:

$ forge test

Run the tests with detailed report:

$ forge test -vvvv

Verify

Verify contracts:

$ ETHERSCAN_API_KEY=<etherscan_api_key> forge verify-contract --watch --compiler-version "v0.8.23" \
  [--verifier-url http://localhost:5000] \
  --constructor-args $(cast abi-encode "constructor(string)" "string") \
  <address> Some-contract

About

A template to create a smart contract project with hardhat-foundry hybrid framework

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published