为什么要⽤ Foundry
Foundry
为什么要⽤ Foundry
快 - 加速开发
Solidity - 减少上下⽂切换
功能丰富
⾜够轻量
Foundry 的组件
forge
cast
anvil
安装
https://getfoundry.sh/
curl -L https://foundry.paradigm.xyz | bash foundryup
Cast - 与链交互的⼯具箱 RPC
cast rpc <METHOD> <PARAMS> --rpc-url=$RPC
区块查询
cast block-number --rpc-url=$RPC
cast find-block <TIMESTAMP> --rpc-url=$RPC
cast block <BLOCK> --rpc-url=$RPC
cast block <BLOCK> [FIELD] --json --rpc-url=$RPC
交易查询
cast tx <HASH> [FIELD] --rpc-url=$RPC
cast receipt <HASH> [FIELD] --rpc-url=$RPC
交易解析
cast 4byte <SELECTOR>
cast sig <SIG>
cast upload-signature <SIG>
cast pretty-calldata <CALLDATA>
cast calldata <SIG> <ARGS>
cast 4byte-event <TOPIC_0>
cast keccak <DATA>
账户管理
cast wallet new [OUT_DIR]
cast wallet sign <MESSAGE> --keystore=<PATH>
cast wallet verify --address <ADDRESS> <MESSAGE> <SIGNATURE>
合约查询
cast etherscan-source <ADDRESS> --chain=<CHAIN> --etherscan-api-key=<KEY> -d=<OUT_DIR> cast interface <PATH_OR_ADDRESS> --chain=<CHAIN> --etherscan-api-key=<KEY>
cast storage <ADDRESS> <SLOT> --rpc-url=$RPC
cast index <KEY_TYPE> <KEY> <SLOT_NUMBER>
合约交互
cast call <ADDRESS> <SIG> [ARGS] --rpc-url=$RPC
cast send <TO> <SIG> [ARGS] --rpc-url=$RPC --keystore=<PATH>
ENS
cast resolve-name <NAME> --rpc-url=$RPC
cast lookup-address <ADDRESS> --rpc-url=$RPC
编码解码
cast --to-hex
cast --to-dec
cast --to-unit
cast --to-wei
cast --to-rlp
cast --from-rlp
本地模拟链上交易
cast run <HASH> --rpc-url=$RPC
Traces:
[<Gas Usage>] <Contract>::<Function>(<Parameters>) ├─ [<Gas Usage>] <Contract>::<Function>(<Parameters>) │ └─ ← <Return Value>
└─ ← <Return Value>
Green: For calls that do not revert
Red: For reverting calls
Blue: For calls to cheat codes
Cyan: For emitted logs
Yellow: For contract deployments
Anvil - 本地节点
anvil
anvim --accounts=<NUM> --balance=<NUM>
anvil --mnemonic=<MNEMONIC>
anvil --fork-url=$RPC --fork-block-number=<BLOCK> 特殊 RPC ⽅法
anvil_* -> hardhat_*
anvil_impersonateAccount
anvil_setStorageAt
Forge - 智能合约开发框架 初始化项⽬
forge init <dir_name>
forge init --template <template_path> <dir_name>
依赖管理
forge install <gh_user/gh_repo>
forge update <dep>
forge remove <dep>
依赖映射
forge remappings
forge remappings > remappings.txt
代码美化
forge fmt
编译项⽬
forge build
forge build --watch
FOUNDRY_PROFILE=release forge build
获取更多细节
forge build --extra-output=<SELECTOR>
forge inspect <CONTRACT> <FIELD>
代码测试
forge test
forge test -vvvv
forge test -w
forge test --match-test/--match-contract/--match-path forge test --fork-url=$RPC
测试代码的结构
断⾔库的使⽤
使⽤ cheatcode 改变 vm 状态
ffi 的使⽤
fuzz 测试
invariant 测试
code coverage
gas 报告
forge test --gas-report
forge snapshot
⽤ solidity 写脚本
forge script <PATH>
部署合约
forge script <PATH> --broadcast
Debugger
forge script <PATH> --debug
cast run <HASH> --debug
集成 GitHub actions
continous-integration