### Blockchain Development with Foundry
The blockchain space has witnessed tremendous innovation not only in protocols and applications but also in developer tooling. One such modern tool that’s redefining Ethereum development is
*Foundry*.
Whether you're transitioning from tools like Hardhat or just getting started with smart contract development, Foundry offers a powerful, fast, and developer-friendly environment. Especially for us devs that like to keep a little distance from using js/ts to interact with our contract.
**What is Foundry?**
*Foundry* is a blazing-fast, command-line-based smart contract development framework written in Rust. It includes tools for:
- *Compiling* smart contracts (like Hardhat or Truffle)
- *Running tests* in Solidity
- *Deploying* contracts
- *Interacting* with chains (local or testnet/mainnet)
- *Forking* chains for advanced testing
Its core tools:
- `forge`: For building, testing, and deploying
- `cast`: For interacting with contracts and sending transactions
- `anvil`: A local Ethereum node for rapid testing
**Why Use Foundry?**
- *Speed*: Written in Rust, it's extremely fast at compiling and testing.
- *Native Solidity Testing*: Write tests directly in Solidity, no JS/TS needed.
- *EVM Forking*: Test against real chain state using `anvil` or `forge`.
- *Scripting in Solidity*: You can even write deploy and interaction scripts in Solidity!
- *Compatibility*: Works with existing Solidity projects and integrates with OpenZeppelin, Chainlink, etc.
**Setting Up Foundry**
1. *Install Foundry:*
```bash
curl -L https://foundry.paradigm.xyz | bash
foundryup
```
2. *Create a new project:*
```bash
forge init my-dapp
cd my-dapp
```
3. *Structure:*
- `src/`: Your Solidity contracts
- `script/`: Deployment scripts
- `test/`: Solidity-based unit tests
- `foundry.toml`: Config file
**Writing and Testing Contracts**
Example contract:
```solidity
// src/Counter.sol
pragma solidity ^0.8.24;
contract Counter {
uint public count;
function increment() public {
count++;
}
}
```
Test contract:
```solidity
// test/Counter.t.sol
pragma solidity ^0.8.24;
import "forge-std/Test.sol";
import "../src/Counter.sol";
contract CounterTest is Test {
Counter public counter;
function setUp() public {
counter = new Counter();
}
function testIncrement() public {
counter.increment();
assertEq(counter.count(), 1);
}
}
```
Run tests:
```bash
forge test
```
**Deploying with Foundry**
You can write deployment scripts using Solidity or use `cast`:
```bash
cast send --private-key <key> <contract-address> "increment()"
```
Or use `forge script` to deploy contracts directly.
---
**Forking Mainnet/Testnet**
```bash
forge test --fork-url <your_RPC_url>
```
You can test contracts as if they’re on live networks, ideal for simulating DeFi interactions.
**Conclusion**
Foundry is quickly becoming the go-to tool for Solidity developers who value speed, Solidity-native testing, and modern development patterns. Its no-nonsense CLI, developer-focused design, and production readiness make it a must-learn for blockchain engineers.