
Chapter 0x14: Getting Started with Foundry Anvil: Simulating a Blockchain Environment Locally Best Guide
Getting Started with Foundry Anvil : If you’re diving into Ethereum development, you’ve probably heard of Foundry, a powerful toolkit for smart contract development. One of its most essential tools is Anvil, a local Ethereum node that lets you simulate a blockchain environment right on your machine. Whether you’re testing multi-account interactions, forking mainnet state, or conducting cross-network testing, Anvil makes blockchain development smoother and more efficient.
Table of Contents
Getting Started with Foundry Anvil
In this tutorial, we’ll explore:
- Setting up and running Anvil
- Simulating a blockchain environment locally
- Testing multi-account interactions and forks
- Cross-Network Testing
By the end, you’ll have a solid understanding of how to deploy a smart contract locally using Anvil Foundry, perform foundry smart contract testing, and even experiment with forking Ethereum’s blockchain.
What is Anvil in Foundry?

Anvil is a lightweight, high-performance Ethereum node designed for local development and testing. It’s part of the Foundry framework, an alternative to Hardhat that offers speed, efficiency, and a Rust-based toolchain for smart contract development.
If you’re wondering, How do I deploy to Anvil Foundry? or What is Anvil in Foundry?, Everything you need to know will be covered in this guide.
Setting Up and Running Anvil

Before we start testing smart contracts, we need to set up Anvil.
Step 1: Install Foundry
First, install Foundry if you haven’t already. Run the following command in your terminal:
curl -L https://foundry.paradigm.xyz | bash
foundryup
This will install Forge (for compiling and testing smart contracts), Cast (for interacting with Ethereum nodes), and Anvil (our local blockchain simulator).
Step 2: Start Anvil
Once Foundry is installed, run Anvil with:
anvil
You should see output like this:
Available Accounts
==================
(0) 0xF39... (10000 ETH)
(1) 0x71B... (10000 ETH)
...
Private Keys
==================
(0) 0x59c...
(1) 0x98c...
This means Anvil is running successfully and has created test accounts with ETH balances.
Simulating a Blockchain Environment Locally

One of Anvil’s biggest advantages is its ability to simulate real blockchain conditions. You can:
- Test contract interactions without spending real ETH
- Manipulate time and block numbers
- Simulate transactions with multiple accounts
- Fork Ethereum’s mainnet for realistic testing
Step 1: Deploy a Smart Contract to Anvil
To deploy a contract, use Forge:
forge create --rpc-url http://localhost:8545 --private-key <YOUR_PRIVATE_KEY> src/MyContract.sol:MyContract
Replace <YOUR_PRIVATE_KEY>
with one of the private keys displayed when you started Anvil.
Step 2: Interact with the Contract
Use Cast to send transactions or call contract functions:
cast send <CONTRACT_ADDRESS> "setValue(uint256)" 42 --private-key <YOUR_PRIVATE_KEY> --rpc-url http://localhost:8545
This executes the setValue
function on your contract.
Testing Multi-Account Interactions and Forks

Anvil allows you to test interactions involving multiple addresses and mainnet forks.
Testing Multi-Account Transactions
To simulate transactions between different users:
cast send <CONTRACT_ADDRESS> "transfer(address,uint256)" 0xReceiverAddress 100 --from 0xSenderAddress --rpc-url http://localhost:8545
This transfers 100 tokens from one test account to another.
Forking Ethereum’s Mainnet
You can test your contract with real mainnet data by forking Ethereum’s blockchain.
anvil --fork-url https://eth-mainnet.alchemyapi.io/v2/YOUR_ALCHEMY_API_KEY
Now your local blockchain contains the latest state of Ethereum’s mainnet, allowing you to test against real contracts and balances.
Cross-Network Testing with Foundry

Cross-network testing ensures your contract works across multiple blockchain environments (e.g., Ethereum, Polygon, Arbitrum).
How to Perform Cross-Network Testing in Foundry
- Deploy the contract on multiple networks:
forge create --rpc-url https://polygon-rpc.com --private-key <PRIVATE_KEY> src/MyContract.sol:MyContract
- Interact with different blockchains using Cast:
cast call <CONTRACT_ADDRESS> "getValue()" --rpc-url https://arbitrum-mainnet.infura.io/v3/YOUR_INFURA_API_KEY
- Compare behaviors across networks.
Foundry vs Hardhat: Which One Should You Use?

Many developers wonder, Hardhat vs Foundry: Which is better? Here’s a comparison:
Feature | Foundry | Hardhat |
---|---|---|
Speed | ✅ Faster | ⛔ Slower |
Language | ✅ Rust-based | ⛔ JavaScript-based |
Gas Optimization | ✅ Built-in | ⛔ Requires Plugins |
Testing | ✅ Native Fuzzing | ⛔ Mocha/Chai |
Verdict: If you need performance, security, and efficiency, go with Foundry. If you’re more comfortable with JavaScript, Hardhat might be a better choice.
Conclusion
In this tutorial, we covered:
- How to set up and run Anvil
- Simulating a blockchain environment locally
- Testing multi-account interactions and forks
- Cross-network testing with Foundry
Now that you know how to deploy a smart contract locally using Anvil Foundry, perform smart contract fork testing, and use Foundry Anvil GitHub tools, you’re ready to experiment with blockchain development like a pro.
If you found this guide useful, please share it with your developer community. Happy coding! 🚀