A Foundry + Hardhat based template for developing Solidity smart contracts, with sensible defaults.
Inspiration - Hardhat Template and Foundry Template
- Hardhat: compile, run and test smart contracts
- TypeChain: generate TypeScript bindings for smart contracts
- Ethers: renowned Ethereum library and wallet implementation
- Solcover: code coverage
- Forge: compile, test, fuzz, debug and deploy smart contracts
- Forge Std: collection of helpful contracts and cheatcodes for testing
- Solhint: linter for Solidity code
- Getting Started
- Features
- Usage
- Syntax Highlighting
- Using GitPod
- Contributing
- Resources
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 ahmedali8/foundry-hardhat-templateRecommended node version is v20.x
If you have nvm then run:
$ nvm useThen, install dependencies
$ just setup # install Forge and Node.js depsor
$ bun install
$ forge installIf this is your first time with Foundry, check out the installation instructions.
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.
This template comes with sensible default configurations in the following files:
├── .editorconfig
├── .biome.jsonc
├── .gitignore
├── .prettierignore
├── .prettierrc.yml
├── .solcover.js
├── .solhintignore
├── .solhint.json
├── .bunrc.yml
├── foundry.toml
├── hardhat.config.ts
└── remappings.txt
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.
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:
- Install the dependency using your preferred package manager, e.g.
bun add dependency-name:dependency-url- Use this syntax to install from GitHub:
bun add repo-name@github:username/repo-name#tag-name
- Use this syntax to install from GitHub:
- Add a remapping for the dependency in remappings.txt, e.g.
dependency-name=node_modules/dependency-name
Note that OpenZeppelin Contracts is pre-installed, so you can follow that as an example.
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 the Solidity code:
$ bun lint:solHere's a list of the most frequently needed commands.
Build the contracts:
$ forge buildGet a test coverage report:
$ forge coverageTo get local HTMl reports:
$ just foundry-report
For this to work, you need to have lcov installed.
Delete the build artifacts and cache directories:
$ forge cleanDeploy to Anvil:
# Spin up an anvil local node
$ anvil
# On another terminal
$ forge script scripts/foundry/DeployLock.s.sol:DeployLock \
--fork-url localhost \
--broadcast \
-vvvvFor 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.
Get a gas report:
$ forge test --gas-reportRun the tests:
$ forge testYou can also use console.log, whose logs
you can see in the terminal output by adding the -vvvv flag.
- Foundry piggybacks off git submodules to manage dependencies. There's a guide about how to work with dependencies in the book.
- You don't have to create a
.envfile, but filling in the environment variables may be useful when debugging and testing against a mainnet fork.
To run a local network with all your contracts in it, run the following:
$ bun chainCompile the smart contracts with Hardhat:
$ bun compileCompile the smart contracts and generate TypeChain bindings:
$ bun typechainRun the tests with Hardhat:
$ bun test
or
$ bun test:gas # shows gas report and contract size
or
$ bun test:trace # shows logs + calls
or
$ bun test:fulltrace # shows logs + calls + sloads + sstoresOptional:
-
See the actual fiat currency rates by setting your coingecko api key from here in
.envfile or command. -
Set custom gas price (gwei) in
.envfile or command or let it automatically fetched by ethgasstationapi.
$ GAS_PRICE=20
$ COIN_MARKET_CAP_API_KEY="your_api_key"Lint the TypeScript code:
$ bun lint:tsStarts a local hardhat chain with the state of the last mainnet block
$ bun forkGenerate the code coverage report:
$ bun coverageDelete the smart contract artifacts, the coverage reports and the Hardhat cache:
$ bun cleanDeploy the contracts to Hardhat Network:
$ bun deployDeploy the contracts to a specific network, such as the Goerli testnet:
$ bun deploy:network goerliFor more information on deploy check out repo hardhat-deploy
Generate natspec documentation for your contracts by running
$ bun hardhat dodocFor more information on Natspec click here and for dodoc repo click here
$ bun hardhat size-contractsor turn on for every compile
$ CONTRACT_SIZER=true$ bun hardhat verify --network <network> DEPLOYED_CONTRACT_ADDRESS "Constructor argument 1" "Constructor argument 2"For complex arguments you can refer here
$ bun hardhat verify --contract contracts/CONTRACT_NAME.sol:CONTRACT_NAME --network <network> --constructor-args arguments.js DEPLOYED_CONTRACT_ADDRESSVerify 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
If you use VSCode, you can enjoy syntax highlighting for your Solidity code via the vscode-solidity extension.
GitPod is an open-source developer platform for remote development.
To view the coverage report generated by bun coverage, just click Go Live from the status bar to turn the server
on/off.
Contributions are always welcome! Open a PR or an issue!