Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.
Foundry 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, Hardhat Network.
- Chisel: Fast, utilitarian, and verbose solidity REPL.
$ forge build
$ forge test$ forge fmt
$ forge snapshot
$ anvil
-
设置环境变量(推荐使用
.env文件):# 创建 .env 文件 echo "PRIVATE_KEY=your_private_key_here" > .env echo "SEPOLIA_RPC_URL=https://sepolia.infura.io/v3/your_api_key" >> .env
或者使用其他 RPC 提供商:
- Alchemy:
https://eth-sepolia.g.alchemy.com/v2/your_api_key - Infura:
https://sepolia.infura.io/v3/your_api_key - 公共 RPC:
https://rpc.sepolia.org
- Alchemy:
-
确保账户有足够的 Sepolia ETH:
- 从 Sepolia Faucet 获取测试 ETH
-
部署 Bank 合约到 Sepolia:
# 使用环境变量 forge script script/Bank.s.sol:BankScript \ --rpc-url $SEPOLIA_RPC_URL \ --private-key $PRIVATE_KEY \ --broadcast \ --verify \ --etherscan-api-key $ETHERSCAN_API_KEY
或者直接指定参数:
forge script script/Bank.s.sol:BankScript \ --rpc-url https://sepolia.infura.io/v3/your_api_key \ --private-key your_private_key \ --broadcast
-
仅模拟部署(不实际部署):
forge script script/Bank.s.sol:BankScript \ --rpc-url $SEPOLIA_RPC_URL
--rpc-url: Sepolia 测试网的 RPC 端点--private-key: 部署账户的私钥(需要有足够的 Sepolia ETH)--broadcast: 实际发送交易到网络--verify: 在 Etherscan 上验证合约(需要--etherscan-api-key)--etherscan-api-key: Etherscan API 密钥(用于合约验证)
.env 文件提交到 Git!
确保 .env 文件已添加到 .gitignore 中。
$ cast <subcommand>
$ forge --help $ anvil --help $ cast --help