### Start Chisel REPL Source: https://context7.com/foundry-rs/foundry/llms.txt Starts the Chisel REPL for rapid prototyping. No setup is required. ```bash chisel ``` -------------------------------- ### Install Foundry Source: https://github.com/foundry-rs/foundry/blob/master/README.md Use this command to install Foundry. Ensure you have curl and bash installed. ```sh curl -L https://foundry.paradigm.xyz | bash foundryup ``` -------------------------------- ### Install Foundry Bench to PATH Source: https://github.com/foundry-rs/foundry/blob/master/benches/README.md Installs the `foundry-bench` executable to your system's PATH. ```bash cd benches && cargo install --path . --bin foundry-bench ``` -------------------------------- ### Bad Interface File Naming Source: https://github.com/foundry-rs/foundry/blob/master/crates/lint/docs/interface-file-naming.md This example shows a Solidity file that contains only an interface declaration but its filename does not start with 'I'. This violates the convention. ```text contracts/Token.sol // file contains only `interface Token { ... }` ``` -------------------------------- ### Install Foundry from Commit Source: https://github.com/foundry-rs/foundry/blob/master/foundryup/README.md Installs Foundry from a specific commit hash. ```sh foundryup -C 94bfdb2 ``` -------------------------------- ### Install Chisel Globally and Run Source: https://github.com/foundry-rs/foundry/blob/master/npm/@foundry-rs/chisel/README.md Install Chisel globally for system-wide access. Once installed, you can run the 'chisel' command from any directory. ```sh npm add --global @foundry-rs/chisel@nightly chisel [args...] ``` -------------------------------- ### Install Foundry Source: https://github.com/foundry-rs/foundry/blob/master/foundryup/README.md Installs the latest Foundry version by downloading and executing a script. ```sh curl -L https://foundry.paradigm.xyz | bash ``` -------------------------------- ### Install Forge Globally and Use Source: https://github.com/foundry-rs/foundry/blob/master/npm/@foundry-rs/forge/README.md Install Forge globally on your system for access from any directory. Once installed globally, you can run Forge commands directly using `forge`. ```sh npm add --global @foundry-rs/forge@nightly forge [args...] ``` -------------------------------- ### Install Forge Locally and Use Source: https://github.com/foundry-rs/foundry/blob/master/npm/@foundry-rs/forge/README.md Install Forge as a project dependency for local use. After installation, you can run Forge commands using `npx forge`. ```sh npm add @foundry-rs/forge@nightly npx forge [args...] ``` -------------------------------- ### Install Foundry Toolchain with foundryup Source: https://context7.com/foundry-rs/foundry/llms.txt Installs or updates all Foundry binaries. Can install specific versions or build from source. Use `forge --version` to check the installed version. ```bash curl -L https://foundry.paradigm.xyz | bash foundryup ``` ```bash foundryup --version nightly ``` ```bash foundryup --branch master ``` ```bash foundryup --build ``` ```bash forge --version cast --version anvil --version chisel --version ``` -------------------------------- ### Script Runner Setup Source: https://github.com/foundry-rs/foundry/blob/master/docs/dev/scripting.md This diagram details the setup phase within the `ScriptRunner`, including preparing the runner, spawning the backend, setting up libraries and contracts, and deploying them. ```mermaid graph TD; subgraph ScriptArgs::execute a[*]-->ScriptArgs::prepare_runner subgraph ::prepare_runner ScriptArgs::prepare_runner--fork_url-->Backend::spawn; ScriptArgs::prepare_runner-->Backend::spawn; end Backend::spawn--> ScriptRunner-->ScriptRunner::setup; subgraph ::setup ScriptRunner::setup--libraries-->Executor::deploy; ScriptRunner::setup--ScriptContract-->Executor::deploy; Executor::deploy--"setUp()"-->Executor::call_committing; end subgraph ::script Executor::call_committing-->ScriptRunner::script; ScriptRunner::script--"run()"-->Executor::call; end end Executor::call-. BroadcastableTransactions .->ScriptArgs::handle_broadcastable_transactions; ``` -------------------------------- ### Install Rust and Cargo Source: https://github.com/foundry-rs/foundry/blob/master/benches/README.md Required for building the benchmark binary. Run this command to install Rust and Cargo. ```bash curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh ``` -------------------------------- ### Install Anvil Locally and Run Source: https://github.com/foundry-rs/foundry/blob/master/npm/@foundry-rs/anvil/README.md Install Anvil as a project dependency and then execute it using npx. This is suitable for project-specific development. ```sh npm add @foundry-rs/anvil@nightly npx anvil [args...] ``` -------------------------------- ### Nonce Management during Setup Source: https://github.com/foundry-rs/foundry/blob/master/docs/dev/scripting.md This diagram illustrates the nonce management process during the `setUp` phase of script execution, including checks for corrected nonces and adjustments when broadcasting transactions. ```mermaid graph TD ScriptRunner::setup-->default_foundry_caller-deployScript; default_foundry_caller-deployScript-->user_sender-deployLibs; user_sender-deployLibs-->Contract.setUp; Contract.setUp-->A0{Executor::call}; A0-->vm.broadcast; A0-->vm.startBroadcast; A0-->vm.getNonce; vm.broadcast--> A{cheatcode.corrected_nonce} vm.startBroadcast-->A vm.getNonce-->A A--true-->continue_setUp; A--false-->B[sender_nonce=-1]; B-->C[cheatcode.corrected_nonce=true]; C-->continue_setUp; continue_setUp-->end_setUp; end_setUp-->D{cheatcode.corrected_nonce} D--true-->E[cheatcode.corrected_nonce=false]; D--false-->F[sender_nonce=initial_nonce+predeployed_libraries_count]; E-->ScriptRunner::script; F-->ScriptRunner::script; ScriptRunner::script-->Contract.run; Contract.run-->G{Executor::call}; G-->H[vm.broadcast]; G-->I[vm.startBroadcast]; G-->J[vm.getNonce]; H--> K{cheatcode.corrected_nonce} I-->K J-->K K--true-->continue_run; K--false-->L[sender_nonce=-1]; L-->M[cheatcode.corrected_nonce=true]; M-->continue_run; continue_run-->end_run; ``` -------------------------------- ### Install Anvil Globally and Run Source: https://github.com/foundry-rs/foundry/blob/master/npm/@foundry-rs/anvil/README.md Install Anvil globally for system-wide access. This allows you to run the 'anvil' command from any directory. ```sh npm add --global @foundry-rs/anvil@nightly anvil [args...] ``` -------------------------------- ### Build Foundry Bench Runner Source: https://github.com/foundry-rs/foundry/blob/master/benches/README.md Build and install the benchmark runner binary. ```bash cargo build --release --bin foundry-bench ``` -------------------------------- ### Install Foundry from Local Path Source: https://github.com/foundry-rs/foundry/blob/master/foundryup/README.md Installs Foundry from a local directory or repository. Note that --branch, --repo, and --version flags are ignored in this mode. ```sh foundryup --path ./git/foundry ``` -------------------------------- ### Install Latest Foundry Version Source: https://github.com/foundry-rs/foundry/blob/master/foundryup/README.md Installs the latest stable release of Foundry. ```sh foundryup ``` ```sh foundryup -i latest ``` -------------------------------- ### Install Dependencies with `forge install` Source: https://context7.com/foundry-rs/foundry/llms.txt Installs Solidity library dependencies as git submodules under `lib/`. Supports Soldeer package registry installs and installing without git submodules. ```bash forge install OpenZeppelin/openzeppelin-contracts ``` ```bash forge install OpenZeppelin/openzeppelin-contracts@v5.0.2 ``` ```bash forge install oz=OpenZeppelin/openzeppelin-contracts ``` ```bash forge install OpenZeppelin/openzeppelin-contracts transmissions11/solmate ``` ```bash forge install OpenZeppelin/openzeppelin-contracts --no-git ``` ```bash forge soldeer install @openzeppelin-contracts~5.0.2 ``` -------------------------------- ### Install and Use Cast Locally Source: https://github.com/foundry-rs/foundry/blob/master/npm/@foundry-rs/cast/README.md Install Cast as a project dependency and execute commands using 'npx'. ```sh npm add @foundry-rs/cast@nightly npx cast [args...] ``` -------------------------------- ### Install Foundry from Branch Source: https://github.com/foundry-rs/foundry/blob/master/foundryup/README.md Installs the latest commit from a specified Foundry branch, like 'release/0.1.0'. ```sh foundryup --branch release/0.1.0 ``` -------------------------------- ### Start Chisel with Mainnet Fork Source: https://context7.com/foundry-rs/foundry/llms.txt Starts the Chisel REPL by forking the mainnet. Requires a mainnet RPC URL. ```bash chisel --fork-url https://eth.merkle.io ``` -------------------------------- ### Good Interface File Naming Source: https://github.com/foundry-rs/foundry/blob/master/crates/lint/docs/interface-file-naming.md This example demonstrates the correct naming convention for Solidity interface files, where the filename is prefixed with 'I'. ```text contracts/IToken.sol // file contains only `interface IToken { ... }` ``` -------------------------------- ### Install and Use Cast Globally Source: https://github.com/foundry-rs/foundry/blob/master/npm/@foundry-rs/cast/README.md Install Cast globally for system-wide access and execute commands directly. ```sh npm add --global @foundry-rs/cast@nightly cast [args...] ``` -------------------------------- ### Install Foundry from Fork's Main Branch Source: https://github.com/foundry-rs/foundry/blob/master/foundryup/README.md Installs the main branch of a specified Foundry fork, e.g., 'transmissions11/foundry'. ```sh foundryup --repo transmissions11/foundry ``` -------------------------------- ### Force Install Foundry Versions Source: https://github.com/foundry-rs/foundry/blob/master/benches/README.md Use this flag to force the installation of specified Foundry versions, even if they are already installed. ```bash foundry-bench --force-install ``` -------------------------------- ### Install Latest Nightly Foundry Version Source: https://github.com/foundry-rs/foundry/blob/master/foundryup/README.md Installs the most recent nightly build of Foundry. ```sh foundryup --install nightly ``` -------------------------------- ### Install Foundry from Pull Request Source: https://github.com/foundry-rs/foundry/blob/master/foundryup/README.md Installs Foundry directly from a specific GitHub Pull Request number. ```sh foundryup --pr 1071 ``` -------------------------------- ### Solidity Source Code Example Source: https://github.com/foundry-rs/foundry/blob/master/crates/fmt/README.md This is an example of Solidity source code before formatting, showcasing various elements like pragmas, contracts, events, and constructor functions. ```solidity pragma solidity ^0.8.10 ; contract HelloWorld { string public message; constructor( string memory initMessage) { message = initMessage;} } event Greet( string indexed name) ; ``` -------------------------------- ### List Installed Foundry Versions Source: https://github.com/foundry-rs/foundry/blob/master/foundryup/README.md Displays a list of all Foundry versions currently installed on the system. ```sh foundryup --list ``` -------------------------------- ### Consistent Solidity Pragmas Example Source: https://github.com/foundry-rs/foundry/blob/master/crates/lint/docs/pragma-inconsistent.md This example shows a project with consistent Solidity pragma directives across all files. Using a single, exact version for all pragmas ensures that the entire project is compiled under the same Solidity version, preventing potential bugs. ```solidity // All files pragma solidity 0.8.20; ``` -------------------------------- ### Single Contract Per File (Good) Source: https://github.com/foundry-rs/foundry/blob/master/crates/lint/docs/multi-contract-file.md This example demonstrates the recommended practice of having one contract definition per file. This improves discoverability and simplifies project management. ```solidity // File: TokenA.sol contract TokenA { /* ... */ } // File: TokenB.sol contract TokenB { /* ... */ } ``` -------------------------------- ### Install Chisel Locally and Run Source: https://github.com/foundry-rs/foundry/blob/master/npm/@foundry-rs/chisel/README.md Install Chisel as a project dependency for local use. This allows running Chisel commands within your project context using 'npx chisel'. ```sh npm add @foundry-rs/chisel@nightly npx chisel [args...] ``` -------------------------------- ### Start Local Ethereum Node with Anvil Source: https://github.com/foundry-rs/foundry/blob/master/crates/forge/assets/README.md Launch a local Ethereum node for development and testing. ```shell anvil ``` -------------------------------- ### Correct ERC20 Interface Example Source: https://github.com/foundry-rs/foundry/blob/master/crates/lint/docs/incorrect-erc20-interface.md This example demonstrates a correctly defined ERC20 interface adhering to the standard return types for all its functions. Use this as a reference for proper ERC20 implementation. ```solidity interface IERC20 { function totalSupply() external view returns (uint256); function balanceOf(address account) external view returns (uint256); function transfer(address to, uint256 value) external returns (bool); function allowance(address owner, address spender) external view returns (uint256); function approve(address spender, uint256 value) external returns (bool); function transferFrom(address from, address to, uint256 value) external returns (bool); } ``` -------------------------------- ### Start Anvil local EVM node Source: https://context7.com/foundry-rs/foundry/llms.txt Start a local Ethereum development node with `anvil`. Options include forking mainnet/testnets, setting custom accounts, balances, chain IDs, hardforks, mining behavior, and state persistence. ```bash # Start with default settings (10 accounts, 10000 ETH each, chain ID 31337) anvil ``` ```bash # Fork mainnet at latest block anvil --fork-url https://eth.merkle.io ``` ```bash # Fork at a specific block number anvil --fork-url https://eth.merkle.io --fork-block-number 20000000 ``` ```bash # Custom accounts and balance anvil --accounts 20 --balance 50000 ``` ```bash # Custom chain ID and hardfork anvil --chain-id 1337 --hardfork cancun ``` ```bash # Enable interval mining (1 block per second) anvil --block-time 1 ``` ```bash # Disable auto-mining (mine on demand) anvil --no-mining ``` ```bash # Start with specific port and host (expose on all interfaces) anvil --port 8546 --host 0.0.0.0 ``` ```bash # Use a specific mnemonic for reproducible accounts anvil --mnemonic "test test test test test test test test test test test junk" ``` ```bash # Persist state to disk anvil --dump-state ./anvil-state.json ``` ```bash # Load persisted state anvil --load-state ./anvil-state.json ``` ```bash # Init from genesis.json (custom genesis block) anvil --init ./genesis.json ``` -------------------------------- ### Start a Named Chisel Session Source: https://context7.com/foundry-rs/foundry/llms.txt Starts the Chisel REPL and names the current session for later retrieval. ```bash chisel --session my-session ``` -------------------------------- ### Solidity Lint Example - Good Case Source: https://github.com/foundry-rs/foundry/blob/master/crates/lint/docs/_template.md This snippet shows the recommended, non-triggering Solidity code pattern for a given lint rule. This serves as a best practice example. ```solidity // non-triggering, recommended example ``` -------------------------------- ### Correct Zero-Address Check in Solidity Source: https://github.com/foundry-rs/foundry/blob/master/crates/lint/docs/missing-zero-check.md This example demonstrates the correct way to implement a zero-address check using `require` before assigning the `newOwner` parameter, preventing potential issues. ```solidity function setOwner(address newOwner) external onlyOwner { require(newOwner != address(0), "zero address"); owner = newOwner; } ``` -------------------------------- ### anvil Source: https://context7.com/foundry-rs/foundry/llms.txt Starts a local Ethereum development node. ```APIDOC ## anvil — Local Ethereum development node ### Description Starts a fast local EVM node with pre-funded dev accounts. Supports mainnet/testnet forking, custom hardforks, block time settings, and state persistence. ### Method `anvil` ### Flags - `--fork-url` (string) - URL of the RPC endpoint to fork from. - `--fork-block-number` (number) - Block number to fork from. - `--accounts` (number) - Number of dev accounts to generate. - `--balance` (number) - Initial balance for each dev account. - `--chain-id` (number) - Custom chain ID. - `--hardfork` (string) - Specify the hardfork (e.g., cancun). - `--block-time` (number) - Interval mining in seconds (1 block per second). - `--no-mining` - Disable auto-mining. - `--port` (number) - Port to listen on. - `--host` (string) - Host to listen on (e.g., 0.0.0.0 to expose on all interfaces). - `--mnemonic` (string) - Use a specific mnemonic for reproducible accounts. - `--dump-state` (string) - Path to save the current state. - `--load-state` (string) - Path to load the persisted state from. - `--init` (string) - Path to a genesis.json file for custom genesis block. ### Request Example (default settings) ```bash anvil ``` ### Request Example (fork mainnet at latest block) ```bash anvil --fork-url https://eth.merkle.io ``` ### Request Example (fork at specific block number) ```bash anvil --fork-url https://eth.merkle.io --fork-block-number 20000000 ``` ### Request Example (custom accounts and balance) ```bash anvil --accounts 20 --balance 50000 ``` ### Request Example (custom chain ID and hardfork) ```bash anvil --chain-id 1337 --hardfork cancun ``` ### Request Example (enable interval mining) ```bash anvil --block-time 1 ``` ### Request Example (disable auto-mining) ```bash anvil --no-mining ``` ### Request Example (specific port and host) ```bash anvil --port 8546 --host 0.0.0.0 ``` ### Request Example (use specific mnemonic) ```bash anvil --mnemonic "test test test test test test test test test test test junk" ``` ### Request Example (persist state to disk) ```bash anvil --dump-state ./anvil-state.json ``` ### Request Example (load persisted state) ```bash anvil --load-state ./anvil-state.json ``` ### Request Example (init from genesis.json) ```bash anvil --init ./genesis.json ``` ### Response Example ``` _ _ (_) | | __ _ _ __ __ __ _ | | / _` | | '_ bsp; | (_| | | | | | V / | | | | \|, |_| |_| \_/ |_| |_| Available Accounts ================== (0) 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 (10000.000000000000000000 ETH) ... Private Keys ================== (0) 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 ... Listening on 127.0.0.1:8545 ``` ``` -------------------------------- ### Correct ERC721 Interface Example Source: https://github.com/foundry-rs/foundry/blob/master/crates/lint/docs/incorrect-erc721-interface.md This example demonstrates a correctly implemented ERC721 interface adhering to the standard return types for its methods. Use this as a reference for compliant interfaces. ```solidity interface IERC721 { function balanceOf(address owner) external view returns (uint256); function ownerOf(uint256 tokenId) external view returns (address); function safeTransferFrom(address from, address to, uint256 tokenId) external; function transferFrom(address from, address to, uint256 tokenId) external; function approve(address to, uint256 tokenId) external; function setApprovalForAll(address operator, bool approved) external; function getApproved(uint256 tokenId) external view returns (address); function isApprovedForAll(address owner, address operator) external view returns (bool); function supportsInterface(bytes4 interfaceId) external view returns (bool); } ``` -------------------------------- ### Build the Foundry Project Source: https://github.com/foundry-rs/foundry/blob/master/docs/dev/README.md Command to build the entire Foundry project. Ensure Rust and Make are installed. ```sh $ make build ``` -------------------------------- ### Run Anvil with Deno Source: https://github.com/foundry-rs/foundry/blob/master/npm/@foundry-rs/anvil/README.md Execute Anvil using Deno. Ensure you have Deno installed and grant necessary permissions. ```sh deno run --quiet --allow-all npm:@foundry-rs/anvil@nightly [args...] ``` -------------------------------- ### Good Title Example for Issues Source: https://github.com/foundry-rs/foundry/blob/master/crates/lint/README.md An example of a concise and descriptive title for a bug report. This helps in quickly understanding the issue. ```text Forge lint does not flag incorrect shift operations ``` -------------------------------- ### Get Help for Foundry Tools Source: https://github.com/foundry-rs/foundry/blob/master/crates/forge/assets/README.md Display help information for Forge, Anvil, and Cast commands. ```shell forge --help ``` ```shell anvil --help ``` ```shell cast --help ``` -------------------------------- ### Start Chisel Forking at a Specific Block Source: https://context7.com/foundry-rs/foundry/llms.txt Starts the Chisel REPL by forking the mainnet at a specific block number. Requires an RPC URL and block number. ```bash chisel --fork-url https://eth.merkle.io --fork-block-number 20000000 ``` -------------------------------- ### Foundry TOML Configuration Example Source: https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md Defines default settings and profile-specific configurations for Foundry tools. Use this to set project-wide defaults and customize behavior for different profiles like 'hardhat' or 'spells'. ```toml ## defaults for _all_ profiles [profile.default] src = "src" out = "out" libs = ["lib"] solc = "0.8.10" ## to use a specific local solc install set the path as `solc = "/solc"` eth-rpc-url = "https://mainnet.infura.io" ## set only when the `hardhat` profile is selected [profile.hardhat] src = "contracts" out = "artifacts" libs = ["node_modules"] ## set only when the `spells` profile is selected [profile.spells] ## --snip-- more settings ``` -------------------------------- ### Run Chisel with Deno Source: https://github.com/foundry-rs/foundry/blob/master/npm/@foundry-rs/chisel/README.md Execute Chisel using Deno's `run` command. Ensure you have Deno installed and the necessary permissions. ```sh deno run --quiet --allow-all npm:@foundry-rs/chisel@nightly [args...] ``` -------------------------------- ### Correct struct naming convention Source: https://github.com/foundry-rs/foundry/blob/master/crates/lint/docs/pascal-case-struct.md This example demonstrates the recommended PascalCase convention for struct names, improving code consistency. ```solidity struct UserInfo { uint256 balance; } ``` -------------------------------- ### Run Forge with Deno Source: https://github.com/foundry-rs/foundry/blob/master/npm/@foundry-rs/forge/README.md Execute Forge commands using Deno. Ensure you have Deno installed and the necessary permissions. ```sh deno run --quiet --allow-all npm:@foundry-rs/forge@nightly [args...] ``` -------------------------------- ### Get Cast Help Source: https://github.com/foundry-rs/foundry/blob/master/crates/forge/assets/tempo/README.md Displays the help message for Cast, listing available subcommands and options. ```shell cast --help ``` -------------------------------- ### Display Dependency Tree with Forge Source: https://context7.com/foundry-rs/foundry/llms.txt Shows the dependency tree of all installed libraries in the project. Includes library names, versions, and paths. ```bash forge tree # Output: # my-project # ├── forge-std 1.8.1 (lib/forge-std) # └── openzeppelin-contracts 5.0.2 (lib/openzeppelin-contracts) # └── ... ``` -------------------------------- ### Inconsistent Solidity Pragmas Example Source: https://github.com/foundry-rs/foundry/blob/master/crates/lint/docs/pragma-inconsistent.md This example demonstrates a project with inconsistent Solidity pragma directives across different files. It shows a mix of exact version, caret, and range-based pragmas, which can lead to compilation issues and behavioral inconsistencies. ```solidity // A.sol pragma solidity 0.8.18; // B.sol pragma solidity ^0.8.20; // C.sol pragma solidity >=0.7.0 <0.9.0; ``` -------------------------------- ### Get Forge Help Source: https://github.com/foundry-rs/foundry/blob/master/crates/forge/assets/tempo/README.md Displays the help message for Forge, listing available commands and options. ```shell forge --help ``` -------------------------------- ### Chisel REPL Example: Checking Contract Balance Source: https://context7.com/foundry-rs/foundry/llms.txt Demonstrates how to check the balance of a specific address within the Chisel REPL. The balance is shown in decimal format. ```solidity address(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2).balance ``` -------------------------------- ### Incorrect struct naming conventions Source: https://github.com/foundry-rs/foundry/blob/master/crates/lint/docs/pascal-case-struct.md Use PascalCase for struct names to adhere to Solidity style guides. This example shows common naming violations. ```solidity struct user_info { uint256 balance; } ``` ```solidity struct USERINFO { uint256 balance; } ``` -------------------------------- ### Bad Function Names (Mixed Case Violation) Source: https://github.com/foundry-rs/foundry/blob/master/crates/lint/docs/mixed-case-function.md These examples show function names that violate the mixedCase convention by using underscores or starting with an uppercase letter. Adhere to mixedCase for better code style. ```solidity function get_balance() external view returns (uint256); ``` ```solidity function GetBalance() external view returns (uint256); ``` -------------------------------- ### Solidity Lint Example - Bad Case Source: https://github.com/foundry-rs/foundry/blob/master/crates/lint/docs/_template.md This snippet demonstrates a Solidity code pattern that triggers a lint rule. Use this to understand what the lint flags. ```solidity // triggering example ``` -------------------------------- ### Run Forge with PNPM Source: https://github.com/foundry-rs/foundry/blob/master/npm/@foundry-rs/forge/README.md Execute Forge commands using PNPM's `dlx` command. This allows running packages without global installation. ```sh pnpm dlx --silent @foundry-rs/forge@nightly [args...] ``` -------------------------------- ### Chisel REPL Example: Arithmetic Operation Source: https://context7.com/foundry-rs/foundry/llms.txt Demonstrates a simple arithmetic operation within the Chisel REPL. The result is displayed in hex and decimal formats. ```solidity uint256 x = 42 x * 2 ``` -------------------------------- ### Run Anvil with pnpm Source: https://github.com/foundry-rs/foundry/blob/master/npm/@foundry-rs/anvil/README.md Execute Anvil using pnpm's dlx command. This allows running packages without global installation. ```sh pnpm dlx --silent @foundry-rs/anvil@nightly [args...] ``` -------------------------------- ### Run Chisel with npx (One-off) Source: https://github.com/foundry-rs/foundry/blob/master/npm/@foundry-rs/chisel/README.md Execute Chisel directly using npx without prior installation. Use '@nightly' for the latest development version. ```sh npx --yes @foundry-rs/chisel@nightly ``` -------------------------------- ### Run Chisel with pnpm Source: https://github.com/foundry-rs/foundry/blob/master/npm/@foundry-rs/chisel/README.md Execute Chisel using pnpm's `dlx` command. This allows running npm packages without global installation. ```sh pnpm dlx --silent @foundry-rs/chisel@nightly [args...] ``` -------------------------------- ### Multiple Contracts in One File (Bad) Source: https://github.com/foundry-rs/foundry/blob/master/crates/lint/docs/multi-contract-file.md This example shows a file containing two contract definitions, which is flagged by the linter. Consider separating each contract into its own file for better project organization. ```solidity // File: Token.sol contract TokenA { /* ... */ } contract TokenB { /* ... */ } ``` -------------------------------- ### Safe Narrowing Typecast with SafeCast in Solidity Source: https://github.com/foundry-rs/foundry/blob/master/crates/lint/docs/unsafe-typecast.md This example demonstrates using OpenZeppelin's `SafeCast` library for a safe narrowing typecast. This is an alternative to manual checks and ensures the value fits the target type. ```solidity smallAmount = SafeCast.toUint128(amount); ``` -------------------------------- ### Combine Benchmark Options Source: https://github.com/foundry-rs/foundry/blob/master/benches/README.md Demonstrates combining multiple command-line options for customized benchmark runs. ```bash foundry-bench \ --versions stable,nightly \ --repos ithacaxyz/account \ --benchmarks forge_build_with_cache ``` -------------------------------- ### Install Specific Foundry Version Source: https://github.com/foundry-rs/foundry/blob/master/foundryup/README.md Installs a specific tagged version of Foundry, such as v1.7.0. ```sh foundryup --install v1.7.0 ``` -------------------------------- ### Initialize, Build, and Test a Foundry Project Source: https://github.com/foundry-rs/foundry/blob/master/README.md Standard commands to create a new Foundry project, compile its contracts, and run its tests. ```sh forge init counter && cd counter forge build forge test ``` -------------------------------- ### Initialize a New Foundry Project with forge init Source: https://context7.com/foundry-rs/foundry/llms.txt Creates a new Solidity or Vyper project with a standard directory structure and `foundry.toml`. Supports remote templates and specific branches. ```bash forge init counter cd counter ``` ```bash forge init my-project --template https://github.com/PaulRBerg/foundry-template ``` ```bash forge init vyper-project --vyper ``` ```bash forge init minimal-project --empty ``` ```bash forge init my-project --template foundry-rs/forge-template --branch main ``` ```text # Resulting project structure: # counter/ # src/Counter.sol # test/Counter.t.sol # script/Counter.s.sol # lib/forge-std/ # foundry.toml ``` -------------------------------- ### Bad Title Examples for Issues Source: https://github.com/foundry-rs/foundry/blob/master/crates/lint/README.md Examples of ineffective titles for bug reports. Titles should be specific and descriptive. ```text Forge lint does not work Forge lint breaks Forge lint unexpected behavior ``` -------------------------------- ### Run Benchmarks with Default Settings Source: https://github.com/foundry-rs/foundry/blob/master/benches/README.md Executes all benchmarks on default repositories using stable and nightly Foundry versions. ```bash foundry-bench --versions stable,nightly ``` -------------------------------- ### Initialize a New Project with Forge Source: https://github.com/foundry-rs/foundry/blob/master/npm/@foundry-rs/forge/README.md Use this command to initialize a new Foundry project using the nightly version of Forge. ```sh npx --yes @foundry-rs/forge@nightly init ``` -------------------------------- ### Formatter Debug Output Example Source: https://github.com/foundry-rs/foundry/blob/master/crates/fmt/README.md Example of annotated output when formatter debug mode is enabled, showing boxes and breaks. ```text pragma solidity ^0.8.10; «‹«contract HelloWorld »{›· ‹‹ string· public· message››;· constructor«(«‹‹string memory initMessage››»)» {»· «‹ message = ·initMessage›·; » }· »}· event Greet(««‹‹string indexed name››»»); ``` -------------------------------- ### Install Foundry from Fork's Specific Branch Source: https://github.com/foundry-rs/foundry/blob/master/foundryup/README.md Installs the latest commit from a specific branch within a specified Foundry fork. ```sh foundryup --repo transmissions11/foundry --branch patch-10 ``` -------------------------------- ### Deploy Contracts with `forge create` Source: https://context7.com/foundry-rs/foundry/llms.txt Compiles and deploys a contract to a network. Supports constructor arguments, verification, and CREATE2 deployment. Use `--broadcast` to deploy live. ```bash forge create src/Counter.sol:Counter \ --rpc-url http://localhost:8545 \ --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 \ --broadcast ``` ```bash forge create src/Token.sol:Token \ --constructor-args "MyToken" "MTK" 1000000 \ --rpc-url https://sepolia.infura.io/v3/YOUR_KEY \ --private-key $PRIVATE_KEY \ --broadcast \ --verify \ --etherscan-api-key $ETHERSCAN_API_KEY ``` ```bash forge create src/Counter.sol:Counter \ --rpc-url http://localhost:8545 \ --keystore ~/.foundry/keystores/mykey \ --broadcast ``` -------------------------------- ### Signing and Cryptography with Foundry Cheatcodes Source: https://context7.com/foundry-rs/foundry/llms.txt Utilize `vm.sign` for signing arbitrary digests with a private key and `ecrecover` to verify signatures. `vm.createWallet` can deterministically generate wallets. ```solidity // SPDX-License-Identifier: MIT\npragma solidity ^0.8.20;\nimport "forge-std/Test.sol";\n\ncontract SigningTest is Test {\n function test_Sign() public {\n uint256 privateKey = 0xA11CE;\n address signer = vm.addr(privateKey);\n\n bytes32 digest = keccak256(abi.encodePacked("hello world"));\n (uint8 v, bytes32 r, bytes32 s) = vm.sign(privateKey, digest);\n\n address recovered = ecrecover(digest, v, r, s);\n assertEq(recovered, signer);\n }\n\n function test_CreateWallet() public {\n // Create a wallet from a label (deterministic private key)\n Vm.Wallet memory wallet = vm.createWallet("test-wallet");\n assertEq(wallet.addr, vm.addr(wallet.privateKey));\n\n // Sign with wallet\n bytes32 digest = keccak256("message");\n (uint8 v, bytes32 r, bytes32 s) = vm.sign(wallet, digest);\n assertEq(ecrecover(digest, v, r, s), wallet.addr);\n }\n} ``` -------------------------------- ### Run Benchmarks with Specific Versions Source: https://github.com/foundry-rs/foundry/blob/master/benches/README.md Allows you to specify a comma-separated list of Foundry versions to test. ```bash foundry-bench --versions stable,nightly,v1.0.0 ``` -------------------------------- ### Incorrect ERC721 Interface Example Source: https://github.com/foundry-rs/foundry/blob/master/crates/lint/docs/incorrect-erc721-interface.md This example shows an interface with incorrect return types for standard ERC721 functions. Ensure return types match the ERC721 specification to avoid integration issues. ```solidity interface IBadERC721 { function balanceOf(address) external view returns (bool); // should be uint256 function ownerOf(uint256) external view returns (bool); // should be address function supportsInterface(bytes4) external view returns (uint256); // should be bool } ``` -------------------------------- ### Incorrect ERC20 Interface Example Source: https://github.com/foundry-rs/foundry/blob/master/crates/lint/docs/incorrect-erc20-interface.md This example shows an ERC20 interface with incorrect return types for `balanceOf` and `transfer`. Ensure return types match the ERC20 specification to avoid integration issues. ```solidity interface IBadERC20 { function balanceOf(address) external view returns (bool); // should be uint256 function transfer(address, uint256) external; // should return bool } ``` -------------------------------- ### Build Project with Forge Source: https://github.com/foundry-rs/foundry/blob/master/crates/forge/assets/README.md Use this command to compile your smart contracts and project dependencies. ```shell forge build ``` -------------------------------- ### Update Project Dependencies with Forge Source: https://context7.com/foundry-rs/foundry/llms.txt Updates all installed dependencies in the Foundry project. ```bash forge update ``` -------------------------------- ### Run Benchmarks on Specific Repositories Source: https://github.com/foundry-rs/foundry/blob/master/benches/README.md Tests benchmarks on a comma-separated list of specified repositories. The default revision for each repo is 'main'. ```bash foundry-bench --repos ithacaxyz/account,Vectorized/solady ``` -------------------------------- ### Run Make Build and PR Commands Source: https://github.com/foundry-rs/foundry/blob/master/CONTRIBUTING.md Alternatively, use make commands to build the project and run pull request checks. These commands offer a convenient way to ensure code quality. ```sh make build make pr ``` -------------------------------- ### File and Environment Cheatcodes Source: https://context7.com/foundry-rs/foundry/llms.txt Read and write files using vm.readFile and vm.writeFile. Parse JSON content with vm.parseJsonAddress and vm.parseJsonUint. Access environment variables with vm.envString and execute shell commands via vm.ffi. ```solidity // SPDX-License-Identifier: MIT\npragma solidity ^0.8.20;\nimport "forge-std/Test.sol";\n\ncontract FileCheatcodesTest is Test {\n function test_ReadWriteFile() public {\n // Write to a file\n vm.writeFile("output/result.txt", "hello world");\n\n // Read it back\n string memory contents = vm.readFile("output/result.txt");\n assertEq(contents, "hello world");\n\n // Read JSON fixture\n string memory json = vm.readFile("test/fixtures/config.json");\n address addr = vm.parseJsonAddress(json, ".contractAddress");\n uint256 val = vm.parseJsonUint(json, ".initialValue");\n\n // Access environment variables\n string memory rpcUrl = vm.envString("MAINNET_RPC_URL");\n\n // Run a shell command via FFI (must enable ffi = true in foundry.toml)\n string[] memory cmd = new string[](2);\n cmd[0] = "date";\n cmd[1] = "+%s";\n bytes memory result = vm.ffi(cmd);\n uint256 ts = abi.decode(result, (uint256));\n assertGt(ts, 0);\n }\n\n function test_ParseJson() public {\n string memory json = '{\"amount\": 1000, \"recipient\": \"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266\"}';\n uint256 amount = vm.parseJsonUint(json, ".amount");\n address recipient = vm.parseJsonAddress(json, ".recipient");\n assertEq(amount, 1000);\n assertEq(recipient, 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266);\n }\n} ``` -------------------------------- ### Use Specific Foundry Version Source: https://github.com/foundry-rs/foundry/blob/master/foundryup/README.md Switches the active Foundry installation to a specific version, identified by its commit hash. ```sh foundryup --use nightly-00efa0d5965269149f374ba142fb1c3c7edd6c94 ``` -------------------------------- ### Get Block Number with Cast Source: https://github.com/foundry-rs/foundry/blob/master/npm/@foundry-rs/cast/README.md Execute a one-off command to retrieve the current block number using Cast. ```sh npx --yes @foundry-rs/cast@nightly block-number ``` -------------------------------- ### Run Chisel with Bun Source: https://github.com/foundry-rs/foundry/blob/master/npm/@foundry-rs/chisel/README.md Execute Chisel using Bun's `x` command. This is a convenient way to run npm packages with Bun. ```sh bun x @foundry-rs/chisel@nightly [args...] ``` -------------------------------- ### Good Interface Naming Source: https://github.com/foundry-rs/foundry/blob/master/crates/lint/docs/interface-naming.md This snippet demonstrates the correct way to name interfaces by prefixing them with 'I'. This convention improves code readability and maintainability. ```solidity interface IERC20 { /* ... */ } ``` -------------------------------- ### Specify Output Directory and File Source: https://github.com/foundry-rs/foundry/blob/master/benches/README.md Directs benchmark results to a specific directory and names the output file. ```bash foundry-bench --output-dir ./results --output-file LATEST_RESULTS.md ``` -------------------------------- ### Bad Mutable Variable Names (Solidity) Source: https://github.com/foundry-rs/foundry/blob/master/crates/lint/docs/mixed-case-variable.md Mutable variable names with underscores or starting with uppercase letters violate the mixedCase convention. ```solidity uint256 public total_supply; address Owner; ```