Git Product home page Git Product logo

foundry-hardhat-template's Introduction

Foundry + Hardhat Template Open in Gitpod Github Actions Foundry Hardhat

A Foundry + Hardhat based template for developing Solidity smart contracts, with sensible defaults.

Table of Contents

Getting Started

Click the Use this template button at the top of the page to create a new repository with this repo as the initial state.

Or, if you prefer to install the template manually:

$ mkdir my-project
$ cd my-project
$ forge init --template rube-de/foundry-hardhat-template

Then, install dependencies

$ make setup # install Forge and Node.js deps

or

$ yarn install
$ forge install

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

Features

This template builds upon the frameworks and libraries mentioned above, so for details about their specific features, please consult their respective documentations.

For example, for Hardhat, you can refer to the Hardhat Tutorial and the Hardhat Docs. You might be in particular interested in reading the Testing Contracts section.

For example, for Foundry, you can refer to the Foundry Book. You might be in particular interested in reading the Writing Tests guide.

Sensible Defaults

This template comes with sensible default configurations in the following files:

├── .editorconfig
├── .eslintignore
├── .eslintrc.yml
├── .gitignore
├── .prettierignore
├── .prettierrc.yml
├── .solcover.js
├── .solhintignore
├── .solhint.json
├── .yarnrc.yml
├── foundry.toml
├── hardhat.config.ts
└── remappings.txt

GitHub Actions

This template comes with GitHub Actions pre-configured. Your contracts will be linted and tested on every push and pull request made to the main branch.

Note though that by default it injects .env.example env variables into github action's $GITHUB_ENV.

You can edit the CI script in .github/workflows/ci.yml.

Usage

Pre Requisites

You don't have to create a .env file, but filling in the environment variables may be useful when deploying to testnet or mainnet or debugging and testing against a mainnet fork.

Follow the example in .env.example. You can choose to use either a mnemonic or individual private key by setting MNEMONIC or PRIVATE_KEY in your .env file.

If you don't already have a mnemonic, use this bip39 to generate one Or if you don't already have a private key, use this vanity-eth to generate one.

Lint Solidity

Lint the Solidity code:

$ yarn lint:sol

Foundry

Here's a list of the most frequently needed commands.

Build or Compile

Build the contracts:

$ forge build

Coverage

Get a test coverage report:

$ forge coverage

To get local HTMl reports:

$ make foundry-report

For this to work, you need to have lcov installed.

Clean

Delete the build artifacts and cache directories:

$ forge clean

Deploy

Deploy to Anvil:

# Spin up an anvil local node
$ anvil

# On another terminal
$ forge script scripts/foundry/DeployLock.s.sol:DeployLock \
  --fork-url localhost \
  --broadcast \
  -vvvv

For this script to work for testnet or mainnet, refer to Pre Requisites.

For instructions on how to deploy to a testnet or mainnet, check out the Solidity Scripting tutorial.

Gas Usage

Get a gas report:

$ forge test --gas-report

Test

Run the tests:

$ forge test

You can also use console.log, whose logs you can see in the terminal output by adding the -vvvv flag.

Notes

  1. Foundry piggybacks off git submodules to manage dependencies. There's a guide about how to work with dependencies in the book.
  2. You don't have to create a .env file, but filling in the environment variables may be useful when debugging and testing against a mainnet fork.

Hardhat

Run a Hardhat chain

To run a local network with all your contracts in it, run the following:

$ yarn chain

Compile

Compile the smart contracts with Hardhat:

$ yarn compile

TypeChain

Compile the smart contracts and generate TypeChain bindings:

$ yarn typechain

Test

Run the tests with Hardhat:

$ yarn test

or

$ yarn test:gas         # shows gas report and contract size

or

$ yarn test:trace       # shows logs + calls

or

$ yarn test:fulltrace   # shows logs + calls + sloads + sstores

Optional:

  • See the actual fiat currency rates by setting your coingecko api key from here in .env file or command.

  • Set custom gas price (gwei) in .env file or command or let it automatically fetched by ethgasstationapi.

$ GAS_PRICE=20
$ COIN_MARKET_CAP_API_KEY="your_api_key"

Lint TypeScript

Lint the TypeScript code:

$ yarn lint:ts

Forking mainnet

Starts a local hardhat chain with the state of the last mainnet block

$ yarn fork

Coverage

Generate the code coverage report:

$ yarn coverage

Clean

Delete the smart contract artifacts, the coverage reports and the Hardhat cache:

$ yarn clean

Deploy

Deploy the contracts to Hardhat Network:

$ yarn deploy

Deploy the contracts to a specific network, such as the Goerli testnet:

$ yarn deploy:network goerli

For more information on deploy check out repo hardhat-deploy

Generate Natspec Doc

Generate natspec documentation for your contracts by running

$ yarn hardhat dodoc

For more information on Natspec click here and for dodoc repo click here

View Contracts Size

$ yarn hardhat size-contracts

or turn on for every compile

$ CONTRACT_SIZER=true

Verify Contract

Manual Verify

$ yarn hardhat verify --network <network> DEPLOYED_CONTRACT_ADDRESS "Constructor argument 1" "Constructor argument 2"

For complex arguments you can refer here

$ yarn hardhat verify --contract contracts/CONTRACT_NAME.sol:CONTRACT_NAME --network <network> --constructor-args arguments.js DEPLOYED_CONTRACT_ADDRESS

Verify Contract Programmatically

Verify the contract using verifyContract function in verify.ts

Set block explorer api key in .env file or using command, refer to .env.example for more insight.

Example deploy script with verifyContract function is 00_deploy_lock_contract.ts

Syntax Highlighting

If you use VSCode, you can enjoy syntax highlighting for your Solidity code via the vscode-solidity extension.

Using GitPod

GitPod is an open-source developer platform for remote development.

To view the coverage report generated by yarn coverage, just click Go Live from the status bar to turn the server on/off.

Contributing

Contributions are always welcome! Open a PR or an issue!

Thank You!

Resources

foundry-hardhat-template's People

Contributors

ahmedali8 avatar rube-de avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.