Configure Foundry
Learn how to set up Foundry for ZippyChain development.
Table of Contents
Introduction
Foundry is a blazing fast, portable and modular toolkit for Ethereum application development. It consists of:
- Forge: Ethereum testing framework (like Truffle, Hardhat and DappTools).
- Cast: Swiss army knife for interacting with EVM smart contracts, sending transactions and getting chain data.
- Anvil: Local Ethereum node, akin to Ganache or Hardhat Network.
This guide will walk you through setting up Foundry for ZippyChain development.
Prerequisites
Before you begin, make sure you have:
- Basic knowledge of Solidity and smart contract development
- A terminal or command prompt
- Git installed on your system
- A code editor (like Visual Studio Code)
- A wallet with some test ZIPPY tokens (for testnet deployment)
Installation
To install Foundry, follow these steps:
macOS or Linux
Open your terminal and run:
curl -L https://foundry.paradigm.xyz | bash
foundryup
Windows
Open PowerShell and run:
irm get.scoop.sh | iex
scoop install foundry
Verify the installation by running:
forge --version
cast --version
anvil --version
Project Setup
Create a new Foundry project:
forge init my_zippy_project
cd my_zippy_project
This creates a new directory with the following structure:
my_zippy_project/
├── .github/
├── lib/
├── script/
├── src/
├── test/
├── .gitignore
├── .gitmodules
├── foundry.toml
└── README.md
Install common dependencies:
forge install OpenZeppelin/openzeppelin-contracts
Configuration
Configure Foundry to work with ZippyChain by updating the `foundry.toml` file:
[profile.default]
src = "src"
out = "out"
libs = ["lib"]
solc = "0.8.20"
optimizer = true
optimizer_runs = 200
[rpc_endpoints]
zippy_mainnet = "https://rpc.zippychain.com"
zippy_testnet = "https://testnet-rpc.zippychain.com"
[etherscan]
zippy_mainnet = { key = "${ZIPPY_API_KEY}", url = "https://explorer.zippychain.com/api" }
zippy_testnet = { key = "${ZIPPY_API_KEY}", url = "https://testnet-explorer.zippychain.com/api" }
Create a `.env` file to store your private key and API key (never commit this file to version control):
PRIVATE_KEY=your_private_key_here
ZIPPY_API_KEY=your_zippy_explorer_api_key_here
Install the `dotenv` package to load environment variables:
forge install foundry-rs/forge-std
Testing
Create a simple contract in `src/Counter.sol`:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract Counter {
uint256 private _count;
function increment() public {
_count += 1;
}
function count() public view returns (uint256) {
return _count;
}
}
Create a test file in `test/Counter.t.sol`:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "forge-std/Test.sol";
import "../src/Counter.sol";
contract CounterTest is Test {
Counter counter;
function setUp() public {
counter = new Counter();
}
function testIncrement() public {
assertEq(counter.count(), 0);
counter.increment();
assertEq(counter.count(), 1);
}
}
Run the tests:
forge test
Deployment
Create a deployment script in `script/Counter.s.sol`:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "forge-std/Script.sol";
import "../src/Counter.sol";
contract CounterScript is Script {
function run() public {
uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
vm.startBroadcast(deployerPrivateKey);
Counter counter = new Counter();
vm.stopBroadcast();
console.log("Counter deployed at:", address(counter));
}
}
Deploy to ZippyChain Testnet:
source .env
forge script script/Counter.s.sol --rpc-url zippy_testnet --broadcast
For deployment to mainnet (when you're ready):
source .env
forge script script/Counter.s.sol --rpc-url zippy_mainnet --broadcast
Contract Verification
Verify your contract on ZippyChain Explorer:
forge verify-contract \
--chain-id 700 \
--compiler-version 0.8.20 \
--constructor-args $(cast abi-encode "constructor()") \
<DEPLOYED_CONTRACT_ADDRESS> \
src/Counter.sol:Counter \
--etherscan-api-key $ZIPPY_API_KEY \
--verifier-url https://testnet-explorer.zippychain.com/api
Replace `<DEPLOYED_CONTRACT_ADDRESS>` with your actual contract address.
After successful verification, you can interact with your contract through the ZippyChain Explorer interface.
Troubleshooting
If you encounter issues, try these solutions:
RPC Connection Issues
If you can't connect to the ZippyChain RPC:
- Check your internet connection
- Verify the RPC URL is correct
- Try using a different RPC endpoint if available
Deployment Failures
If your deployment fails:
- Make sure you have enough ZIPPY tokens for gas
- Check that your private key is correct
- Try increasing the gas limit if necessary
Verification Errors
If contract verification fails:
- Ensure you're using the exact same compiler version
- Check that your API key is valid
- Verify that the constructor arguments are correctly encoded