1/5
## Why Testing on Live Blockchains is Problematic When developing decentralized applications (DApps), especially the front-end interface, you need to test interactions with your smart contracts. Imagine interacting with a simple "Buy Me a Coffee" dApp. You connect your wallet (like MetaMask), enter an amount (say, 1 ETH), and click "Buy Coffee". If your wallet is connected to a live network like Ethereum Mainnet, you might immediately encounter an error in your browser's developer console, such as: `Uncaught (in promise) ContractFunctionExecutionError: The total cost (gas * gas fee + value) of executing this transaction exceeds the balance of the account.` This error typically occurs because: 1. **Wrong Network:** Your wallet is connected to a network (e.g., Ethereum Mainnet) where the contract might not be deployed, or where you don't intend to test. 2. **Insufficient Funds:** Your connected account on that specific network lacks the necessary funds (the transaction value plus the required gas fees) to execute the transaction. Attempting to test directly on Mainnet or even public testnets introduces significant friction and cost, making it impractical for iterative development. ## The Case for Local Blockchain Development To overcome the challenges of testing on live networks, developers rely on local blockchain environments. These are simulated blockchains running entirely on your own computer. Using a local chain offers several key advantages: * **Zero Cost:** Transactions require gas, but it's "fake" currency provided automatically by the local environment. You don't spend real money or scarce testnet funds. * **Blazing Speed:** Transactions are confirmed almost instantly, dramatically speeding up the development and testing cycle compared to the minutes or even hours it can take on live networks. * **Simplified UI/UX Testing:** Verifying front-end behavior that depends on transaction confirmation (like success messages or animations) becomes trivial, without the delays and costs of real networks. Instead of interacting with Ethereum, ZkSync, or other live networks during development, you should use a dedicated "fake chain" or local development blockchain. ## Introducing Anvil and Saved Blockchain States Anvil is a powerful local testnet node included with the Foundry smart contract development toolkit. It allows you to run a personal Ethereum-like blockchain on your machine. Smart contract developers often use Foundry tools like Forge to compile, test, and deploy their contracts. A common workflow involves deploying contracts to a local Anvil instance. Crucially, Anvil allows developers to *save the entire state* of this local blockchain after deployment into a JSON file. Consider the `html-ts-coffee-cu` front-end project (available on GitHub at `https://github.com/Cyfrin/html-ts-coffee-cu`). Its Readme often links to the corresponding smart contract repository (e.g., `foundry-fund-me-cu` at `https://github.com/Cyfrin/foundry-fund-me-cu`), which contains the contract code and deployment scripts (`script/DeployFundMe.s.sol`). Within the front-end repository (`html-ts-coffee-cu`), you might find a file named `fundme-anvil.json`. This file represents a *snapshot* or *saved state* of an Anvil blockchain *after* the associated `FundMe` contract was successfully deployed to it. While the JSON content might look complex, it contains everything needed to recreate that specific blockchain environment: account balances, nonces, deployed contract code, contract storage, and even the transaction history leading to that state. For front-end development, obtaining this saved state file (e.g., by copying its contents from the repository into a local `fundme-anvil.json` file within your project) is key to setting up an efficient testing environment. ## Setting Up a Basic Anvil Instance Before using Anvil, you need to install it. Anvil is part of the Foundry suite. You can find installation instructions in the official Foundry documentation (`https://book.getfoundry.sh/getting-started/installation`). Installation usually involves running `curl -L https://foundry.paradigm.xyz | bash` followed by `foundryup`. Once installed, you can start a *new, empty* Anvil instance by simply running the command in your terminal: ```bash anvil ``` Anvil will start and output useful information: * **Available Accounts:** Typically 10 pre-funded accounts. * **Private Keys:** The corresponding private keys for each account (keep these secure, though they are only for local testing). * **HD Wallet Mnemonic:** A phrase to regenerate these accounts. * **Base Fee, Gas Price, Gas Limit:** Blockchain parameters. * **Chain ID:** The network identifier (default is `31337`). * **RPC URL:** The address Anvil is listening on (default `http://127.0.0.1:8545`). This basic instance provides a functional local blockchain, but it starts "empty" – any contracts you need are not yet deployed. To stop Anvil, press `Ctrl+C`. ## Connecting MetaMask to Your Local Anvil Network To interact with your dApp's front-end using the local Anvil blockchain, you need to configure your MetaMask wallet to connect to it. 1. **Copy RPC URL:** From the Anvil terminal output, copy the RPC URL (e.g., `127.0.0.1:8545`). Note: You'll need to add the `http://` prefix. 2. **Open MetaMask:** Launch the MetaMask browser extension (using the expanded view can be helpful). 3. **Network Selection:** Click the network dropdown menu (usually at the top-left). 4. **Add Network:** Select "Add network". 5. **Add Manually:** Choose "Add a network manually". 6. **Fill Details:** Enter the configuration for your Anvil network: * **Network name:** Choose a recognizable name, like `Anvil Local`. * **New RPC URL:** Paste the copied URL and add the prefix: `http://127.0.0.1:8545`. * **Chain ID:** Enter the Chain ID shown by Anvil (e.g., `31337`). * **Currency symbol:** `ETH` is appropriate. * **Block explorer URL (Optional):** You can leave this blank. 7. **Save:** Click "Save". MetaMask will now list "Anvil Local" (or your chosen name) as a network option. Select it to connect MetaMask to your running Anvil instance. ## Importing Anvil Test Accounts into MetaMask Your newly configured Anvil network in MetaMask needs an account with funds to send transactions. Anvil conveniently provides pre-funded accounts and their private keys when it starts. You can import one of these into MetaMask. 1. **Copy Private Key:** From the Anvil terminal output, locate the "Private Keys" section and copy one of the keys (e.g., the first one). 2. **Open MetaMask:** Ensure MetaMask is open and connected to your Anvil network. 3. **Account Selection:** Click the account selector icon (usually top-center/right). 4. **Add Account:** Select "Add account or hardware wallet". 5. **Import Account:** Choose "Import account". 6. **Paste Key:** Paste the copied private key into the provided field. 7. **Import:** Click "Import". MetaMask will add the imported account (it might be named something like "Account 2", "Account 8", etc.). This account is now available for use within MetaMask when connected to the Anvil network and will reflect the balance provided by Anvil (typically 10,000 ETH). ## Running Anvil with a Pre-loaded State for Efficient Testing Running the basic `anvil` command gives you a clean slate, which is useful sometimes. However, for front-end testing where you expect a contract to *already be deployed*, starting Anvil empty means you'd need to deploy the contract yourself every time, slowing down development. The ideal solution is to use the saved state file (like `fundme-anvil.json`) provided by the smart contract developers. This file contains the blockchain state *after* the necessary contracts have been deployed. To start Anvil using this saved state, navigate to the directory containing your `fundme-anvil.json` file in your terminal and run: ```bash anvil --load-state fundme-anvil.json ``` This command instructs Anvil to initialize its state using the data from the specified JSON file, rather than starting fresh. **How does this help?** The `fundme-anvil.json` file contains the deployed `FundMe` contract, its address, and its state. Your dApp's front-end is likely configured (e.g., in a `constants.js` or similar file) to interact with a specific contract address. By using `--load-state`, you ensure that Anvil starts with the required contract already deployed at that exact address. You can verify this: Find the `contractAddress` your front-end uses (e.g., `0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512`). Search for this address within the `fundme-anvil.json` file. You should find entries confirming its existence in the saved state. **The Benefit:** Running `anvil --load-state your-state-file.json` allows front-end developers to instantly start a local blockchain environment that precisely matches the required setup, including pre-deployed contracts. Connect MetaMask using the imported Anvil account, point your dApp front-end to the local RPC URL (`http://127.0.0.1:8545`), and you can immediately start testing interactions without the cost, delays, or setup overhead of live networks or manual deployments. This is the recommended, cheapest, and fastest way to conduct robust front-end testing for DApps when a saved state is available.
An essential walkthrough to local dApps testing with Anvil. Discover the advantages of local blockchain environments over live networks for front-end development. Learn to install Anvil, connect MetaMask, and utilize saved states for efficient, zero-cost testing.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
How to build full-stack web3 applications on ZKsync
JavaScript/TypeScript: viem, wagmi, synpress
Nodejs and pnpm
rindexer
Circle Compliance Engine and USDC
Fleek site hosting and CLI
How to build a static and dynamic React/Next.js site
How to leverage AI to code faster and more securely
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Security researcher
$49,999 - $120,000 (avg. salary)
Last updated on May 15, 2025
Solidity Developer
Full-Stack Web3 Development Crash CourseDuration: 1h 12min
Duration: 1h 39min
Duration: 3h 08min
Duration: 1h 44min
Course Overview
About the course
How to build full-stack web3 applications on ZKsync
JavaScript/TypeScript: viem, wagmi, synpress
Nodejs and pnpm
rindexer
Circle Compliance Engine and USDC
Fleek site hosting and CLI
How to build a static and dynamic React/Next.js site
How to leverage AI to code faster and more securely
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Security researcher
$49,999 - $120,000 (avg. salary)
Last updated on May 15, 2025