Code Monkey home page Code Monkey logo

foundry-template's Introduction

Foundry Template Open in Gitpod Github Actions Foundry License: MIT

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

What's Inside

  • Forge: compile, test, fuzz, format, and deploy smart contracts
  • Forge Std: collection of helpful contracts and cheatcodes for testing
  • PRBTest: modern collection of testing assertions and logging utilities
  • Prettier: code formatter for non-Solidity files
  • Solhint: linter for Solidity code

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 PaulRBerg/foundry-template
$ bun install # install Solhint, Prettier, and other Node.js deps

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 please consult their respective documentation for details about their specific features.

For example, if you're interested in exploring Foundry in more detail, you should look at the Foundry Book. In particular, you may be interested in reading the Writing Tests tutorial.

Sensible Defaults

This template comes with a set of sensible default configurations for you to use. These defaults can be found in the following files:

├── .editorconfig
├── .gitignore
├── .prettierignore
├── .prettierrc.yml
├── .solhint.json
├── foundry.toml
└── remappings.txt

VSCode Integration

This template is IDE agnostic, but for the best user experience, you may want to use it in VSCode alongside Nomic Foundation's Solidity extension.

For guidance on how to integrate a Foundry project in VSCode, please refer to this guide.

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.

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

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. bun install dependency-name
    • Use this syntax to install from GitHub: bun 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 OpenZeppelin Contracts is pre-installed, so you can follow that as an example.

Writing Tests

To write a new test contract, you start by importing PRBTest and inherit from it in your test contract. PRBTest comes with a pre-instantiated cheatcodes environment accessible via the vm property. If you would like to view the logs in the terminal output you can add the -vvv flag and use console.log.

This template comes with an example test contract Foo.t.sol

Usage

This is a list of the most frequently needed commands.

Build

Build the contracts:

$ forge build

Clean

Delete the build artifacts and cache directories:

$ forge clean

Compile

Compile the contracts:

$ forge build

Coverage

Get a test coverage report:

$ forge coverage

Deploy

Deploy to Anvil:

$ forge script script/Deploy.s.sol --broadcast --fork-url http://localhost:8545

For this script to work, you need to have a MNEMONIC environment variable set to a valid BIP39 mnemonic.

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

Format

Format the contracts:

$ forge fmt

Gas Usage

Get a gas report:

$ forge test --gas-report

Lint

Lint the contracts:

$ bun run lint

Test

Run the tests:

$ forge test

Generate test coverage and output result to the terminal:

$ bun run test:coverage

Generate test coverage with lcov report (you'll have to open the ./coverage/index.html file in your browser, to do so simply copy paste the path):

$ bun run test:coverage:report

Related Efforts

License

This project is licensed under MIT.

foundry-template's People

Contributors

0x-r4bbit avatar 0xcacti avatar ahmedali8 avatar d1onys1us avatar juanpcapurro avatar michael-a-heuer avatar mshakeg avatar paulrberg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

foundry-template's Issues

Message: failed to extract foundry config:

forge init my-project --template https://github.com/PaulRBerg/foundry-template
cd my-project
pnpm install # install Solhint, Prettier, and other Node.js deps

I followed the steps above to create a project, but encountered an error when executing forge fmt.

The application panicked (crashed).
Message:  failed to extract foundry config:
foundry config error: Unknown evm version: paris for setting `evm_version`

Location: config\src\lib.rs:441

This is a bug. Consider reporting it at https://github.com/foundry-rs/foundry

Backtrace omitted. Run with RUST_BACKTRACE=1 environment variable to display it.
Run with RUST_BACKTRACE=full to include source snippets.

Can't run tests

Overview

Having trouble using this template out of the box and running my own tests. I reproduced that even with the base repo I can't get the tests to run, just contracts compile. What am I missing possibly?

Running

  • forge init contracts-forge --template https://github.com/PaulRBerg/foundry-template
  • forge install
  • forge test

Result

**forge test**
compiling...
Compiling 1 files with 0.8.19
Compilation finished successfully
success.

Add forge coverage commands to scripts

forge coverage is an essential script. In addition, forge coverage compilation runs without an optimization so it's possible that Stack Too Deep errors may occur for forge coverage but not for forge test(if optimization is enabled). Hence having this script early in the development lifecycle can ensure that any Stack Too Deep issues are quickly resolved and not compounded with other Stack Too Deep errors over time, especially since foundry does not output the specific files that have this issue so it's not easy to isolate the Stack Too Deep errors(which is unlike hardhat in my experience at least, but if this is the case in hardhat too then add scripts there).

Suggested scripts to add:

    "test:coverage": "forge coverage",
    "test:coverage:report": "forge coverage --report lcov",
    "test:coverage:html": "forge coverage --report lcov && genhtml lcov.info --branch-coverage --output-dir coverage",

It might also be helpful to do forge coverage instead of forge test in CI.

Integrate BTT

From X:

would be cool if you added your btt structure in the repo so I don't have to do that either

Failed to create project from `forge init` command

I tried to init the project by using the suggested command forge init my-project --template https://github.com/PaulRBerg/foundry-template but I got a No such file or directory (os error 2) error

full log

Initializing /Users/workspace/project-name from https://github.com/PaulRBerg/foundry-template...
Cloning into '/Users/workspace/project-name'...
remote: Enumerating objects: 662, done.
remote: Counting objects: 100% (271/271), done.
remote: Compressing objects: 100% (134/134), done.
remote: Total 662 (delta 159), reused 218 (delta 130), pack-reused 391
Receiving objects: 100% (662/662), 2.81 MiB | 2.10 MiB/s, done.
Resolving deltas: 100% (307/307), done.
Submodule 'lib/forge-std' (https://github.com/foundry-rs/forge-std) registered for path 'lib/forge-std'
Submodule 'lib/prb-test' (https://github.com/PaulRBerg/prb-test) registered for path 'lib/prb-test'
Cloning into '/Users/workspace/project-name/lib/forge-std'...
remote: Enumerating objects: 1870, done.
remote: Counting objects: 100% (1866/1866), done.
remote: Compressing objects: 100% (721/721), done.
remote: Total 1870 (delta 1201), reused 1715 (delta 1082), pack-reused 4
Receiving objects: 100% (1870/1870), 518.93 KiB | 2.05 MiB/s, done.
Resolving deltas: 100% (1201/1201), done.
Cloning into '/Users/workspace/project-name/lib/prb-test'...
remote: Enumerating objects: 848, done.
remote: Counting objects: 100% (318/318), done.
remote: Compressing objects: 100% (113/113), done.
remote: Total 848 (delta 191), reused 288 (delta 176), pack-reused 530
Receiving objects: 100% (848/848), 2.63 MiB | 2.11 MiB/s, done.
Resolving deltas: 100% (447/447), done.
Submodule 'lib/ds-test' (https://github.com/dapphub/ds-test) registered for path 'lib/forge-std/lib/ds-test'
Cloning into '/Users/workspace/project-name/lib/forge-std/lib/ds-test'...
remote: Enumerating objects: 313, done.
remote: Counting objects: 100% (171/171), done.
remote: Compressing objects: 100% (87/87), done.
remote: Total 313 (delta 90), reused 135 (delta 75), pack-reused 142
Receiving objects: 100% (313/313), 71.98 KiB | 1.33 MiB/s, done.
Resolving deltas: 100% (129/129), done.
Error:
No such file or directory (os error 2)

What could it be?

PRBTest.sol breaks Vm.Log ?

Hi Paul,

I'm getting this odd error when trying to use vm.getRecordedLogs() in a test in a project using foundry-template.

Type struct VmSafe.Log[] memory is not implicitly convertible to expected type struct VmSafe.Log[] memory.

image

It's the first time I use this cheatcode, but someone in the Foundry support tg suggests that PRBTest might be causing the problem. Have you ever encountered this odd error?

Here is the tg mention of PRBTest possibly causing the problem: https://t.me/foundry_support/43751

image

And here are my imports, in case relevant

image

Thanks for any suggestions.

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.