Skip to content

Latest commit

 

History

History
207 lines (132 loc) · 4.94 KB

FOUNDRY.md

File metadata and controls

207 lines (132 loc) · 4.94 KB

Foundryx Hardhat Template

Merging 2 repos: foundry-rs/hardhat-foundry-template + abigger87/femplate


First time with Forge/Foundry?

Installation

  1. Install foundryup (toolchain installer):
curl -L https://foundry.paradigm.xyz | bash
  1. Trigger foundry installation:
foundryup

🎉 Foundry is installed! 🎉

Anytime you need to get the latest forge or cast binaries, you can run foundryup.

Overview: Foundry 🆚 HardHat

  1. Tests are run way faster with Foundry than HardHat: how fast?
  2. Comes with a number of advanced testing methods: Fuzz Testing & Differential Testing (incoming: Invariant Testing, Symbolic Execution & Mutation Testing)
  3. Tests written only in solidity

Notes: If for a specific case/scenario needs to be written in JS/TS you will need to use HardHat (along side Foundry)



Getting Started

Update git submodules & install repo's forge libraries

yarn install & git submodule update --init --recursive && forge install Openzeppelin/openzeppelin-contracts foundry-rs/forge-std rari-capital/solmate --no-commit

Writing Tests with Foundry

Create a test file for your contract in the src/tests/ directory.

To learn more about writing tests in Solidity for Foundry, reference Rari Capital's solmate repository created by @transmissions11.

Run Tests

forge test --gas-report --watch

watch allows to trigger test on every change in test files

Internal audits

For internal audits we will use tools like Echidna, Etheno, Manticore, Slither & Rattle.
We also need to check our code against well known vulnerabilities from Not So Smart Contracts repository (included in the image)

Install and run the toolkit

docker run -it -v ${PWD}:/share trailofbits/eth-security-toolbox

You might need to change default solc version with:

solc-select use 0.8.13

File of the current folder will be in /share folder of the container. When running a command with slither it look like:

slither /share/src/Token.sol --config-file /share/slither.config.json

Forge

Deploy: local network via Anvil

First, start Anvil:

anvil

Then run the following script with one of the private keys given to you by Anvil:

forge script scripts/Token.s.sol:DeployToken --fork-url http://localhost:8545 --private-key $PRIVATE_KEY --broadcast -vvvv

Deploy: existing network

Create an .env file based on .env.example & fill it with your data. Then run:

source .env
forge script scripts/Token.s.sol:DeployToken --rpc-url $RPC_URL --private-key $PRIVATE_KEY --broadcast --verify --etherscan-api-key $EXPLORER_KEY -vvvv

Other commands

Install libraries with Foundry which work with Hardhat. It is always Organization(UserName)/repoName

forge install rari-capital/solmate foundry-rs/forge-std Openzeppelin/openzeppelin-contracts --no-commit # Already in this repo

Updating a library

forge update lib/<dep>

Removing a library

forge update lib/<dep>

HardHat compatibility

Whenever you install new libraries using Foundry, make sure to update your remappings.txt.

Follow steps here to enable HardHat compatibility

Formatting

Solhint

Disable solhint on current line

Disable completely solhint

// solhint-disable-next-line

Disable some solhint rules

// solhint-disable-next-line no-empty-blocks not-rely-on-time

Disable solhint for a group of lines

Disalbe completely solhint

/* solhint-disable */
function transferTo(address to, uint amount) public {
    require(tx.origin == owner);
    to.call.value(amount)();
}
/* solhint-enable */

Disable some solhint rules

  /* solhint-disable avoid-tx-origin not-rely-on-time */
  function transferTo(address to, uint amount) public {
    require(tx.origin == owner);
    to.call.value(amount)();
  }
  /* solhint-enable avoid-tx-origin not-rely-on-time */

Why Husky

Husky allows to manage git hooks and trigger actions when commiting, e.g. npx prettier writte .

Create a hook on commits

npx husky add .husky/pre-commit "npm test"
git add .husky/pre-commit

reference: doc

Some specific package might need specific hooks:

npx husky add .husky/commit-msg 'npx --no commitlint --edit "$1"'

reference: doc


Miscellaneous

IDE interactive rebase

In order to rebase interactively in your IDE (e.g. VSCode), copy this in your terminal:

git config --global core.editor "code --wait"