目录
ℹ️ Note
If you’re on Windows, you will need to install and use Git BASH or WSL, as your terminal, since Foundryup currently does not support Powershell or Cmd.
官方提示:如果当前使用的Windows操作系统,可以通过WSL的方式安装和使用Foundry。
Foundry是一个强大的Solidity智能合约开发工具,致力于为区块链开发者提供一流的测试、部署和模拟环境。它以其高效的测试速度、灵活的模拟功能以及对EVM兼容性的强大支持而受到广泛欢迎。Foundry包括Forge、Cast和Anvil三个主要组件,分别负责合约的编译与测试、交易构造与执行以及本地链的模拟与调试,使得智能合约的开发、测试和部署变得前所未有地简单快捷。(from AI)
VSCode安装WSL
安装过程参考:WSL报错Error code: Wsl/Service/CreateVm/HCS/HCS_E_HYPERV_NOT_INSTALLED解决-CSDN博客
Foundry库安装
VSCode左下角切换到WSL下的Ubuntu环境;
运行 curl -L https://foundry.paradigm.xyz | bash
运行 source /home/<abc>/.bashrc
运行 foundryup
done!
Foundry测试案例
运行 forge init hello_foundry,这将从默认模板中创建一个新目录 hello_foundry。
.
└── hello_foundry
├── lib
├── script
├── src
└── test
ℹ️ Note
默认情况下,forge init 还会初始化一个新的 git 仓库,安装一些子模块,并创建一条初始提交信息。 如果不想这样做,可以通过运行 forge init --no-git hello_foundry 来实现。
进入hello_foundry目录中,运行 forge build
[⠒] Compiling...
[⠒] Compiling 27 files with 0.8.25
[⠑] Solc 0.8.25 finished in 1.44s
Compiler run successful!
.
└── hello_foundry
├── cache
├── lib
├── out
├── script
├── src
└── test
此时目录中多了 cache 和 out 的两个文件夹
继续运行 forge test
[⠒] Compiling...
No files changed, compilation skipped
Ran 2 tests for test/Counter.t.sol:CounterTest
[PASS] testFuzz_SetNumber(uint256) (runs: 256, μ: 30765, ~: 31310)
[PASS] test_Increment() (gas: 31325)
Suite result: ok. 2 passed; 0 failed; 0 skipped; finished in 23.68ms (11.74ms CPU time)
Ran 1 test suite in 33.24ms (23.68ms CPU time): 2 tests passed, 0 failed, 0 skipped (2 total tests)
done !