dabit3/foundry-cheatsheet là một trong những repo mã nguồn mở mà TopGit theo dõi, hiện có 543 sao.
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
Forge: Ethereum testing framework (like Truffle, Hardhat and DappTools).
Cast: CLI for interacting with EVM smart contracts, sending transactions and getting chain data.
Anvil: local Ethereum node, similar to Ganache or Hardhat Network.
Forge includes both a CLI as well as a standard library.
For an introduction to Foundry, check out these resources:
Foundry vs Hardhat: Differences in performance and developer experience
Smart Contract Development with Foundry (Video)
Building and testing smart contracts with Foundry (tutorial)
For support, check out the Telegram channel here
Initializing a new project
Initialize a new project from an empty folder:
forge init
Or define a new folder in which to create your project:
forge init my-app
This creates a new project with 4 folders:
src - An example smart contract and where your smart contracts will live.
script - An example deployment script
test - An example test
lib - This is similar to node_modules
Remappings
Forge can remap dependencies to make them easier to import. Forge will automatically try to deduce some remappings for you:
You can view all of the assertion functions available here.
Let's say you start with a basic Counter contract that looks like this (src/Counter.sol):
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract Counter {
int private count;
constructor(int _count) {
count = _count;
}
function incrementCounter() public {
count += 1;
}
function decrementCounter() public {
count -= 1;
}
function getCount() public view returns (int) {
return count;
}
}
Our test might look like this (test/Counter.t.sol):
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
import "forge-std/Test.sol";
import 'src/Counter.sol';
contract ContractTest is Test {
Counter counter;
function setUp() public {
counter = new Counter(10);
}
function testGetCount() public {
int value = counter.getCount();
assertEq(value, 10);
emit log_int(value);
}
function testIncrement() public {
counter.incrementCounter();
counter.incrementCounter();
int value = counter.getCount();
assertEq(value, 12);
emit log_int(value);
}
function testDecrement() public {
counter.decrementCounter();
int value = counter.getCount();
assertEq(value, 9);
emit log_int(value);
}
}
Here, we are using assertEq to assert equality. You can view all of the assertion functions available here.
Logs and traces
You'll also notice that we're logging values using log_int. You log can most types of values this way:
The default behavior for forge test is to only display a summary of passing and failing tests. You can control this behavior by increasing the verbosity (using the -v flag). Each level of verbosity adds more information:
Level 2 (-vv): Logs emitted during tests are also displayed. That includes assertion errors from tests, showing information such as expected vs actual. Level 3 (-vvv): Stack traces for failing tests are also displayed. Level 4 (-vvvv): Stack traces for all tests are displayed, and setup traces for failing tests are displayed. Level 5 (-vvvvv): Stack traces and setup traces are always displayed.
For our logs to show up, we need to run test with at least the -vv flag:
forge test -vv
Cheatcodes
Cheatcodes give you additional assertions, the ability to alter the state of the EVM, mock data, and more.
For example, you can mock a user with prank and startPrank:
As mentioned, you can mock / emulate a user using either .prank or .startPrank. Let's take a look at how this might work.
Let's say we have an ERC721 contract and we'd like to make sure that only the owner of a token could transfer or burn that token. Our tests might look something like this:
// only the owner can transfer
function testTransferToken() public {
// mint the token to bob's address
erc721 = new ERC721();
erc721.mint(bob, 0);
// emulate bob
vm.startPrank(bob);
// transfer to mary
erc721.safeTransferFrom(bob, mary, 0);
// check to make sure mary is the new owner
address owner_of = erc721.ownerOf(0);
assertEq(mary, owner_of);
}
// only the owner can burn
function testBurn() public {
erc721 = new ERC721();
erc721.mint(bob, 0);
vm.startPrank(bob);
erc721.burn(0);
}
Fuzzing
Fuzzing allows us to define function parameter types and the testing framework will populate these values at runtime.
If it does find an input that causes the test to fail, it will return it so you can create a regression test.
For instance, we can create a test function to receive a function argument, and use the value in our test without ever having to define what it is.
For this contract:
// SPDX-License-Identifier: MIT
pragma solidity 0.8.13;
contract HelloWorld {
string private greeting;
uint public version = 0;
constructor (string memory _greeting) {
greeting = _greeting;
}
function greet() public view returns(string memory) {
return greeting;
}
}
We could create this test:
contract ContractTest is Test {
function testFuzzing(string memory _greeting) public {
HelloWorld hello = new HelloWorld(_greeting);
assertEq(
hello.greet(),
_greeting
);
}
}
Gas
You can easily print a pretty looking gas report of your tested functions:
forge test --gas-report
ABIs
ABIs will be located in the out directory after running either a build with forge build or a deployment with a script.
Test Options
You can get a full list of testing options by running the --help command:
forge test --help
Scripts & deploying
Foundry recently released Solidity Scripting.
Scripting gives you a lot of control over how you can deploy contracts using Solidity scripts, and I believe is meant to replace forge create which was previously how you could deploy contracts.
From Foundry Book:
Solidity scripts are like the scripts you write when working with tools like Hardhat; what makes Solidity scripting different is that they are written in Solidity instead of JavaScript, and they are run on the fast Foundry EVM backend, which provides dry-run capabilities.
Let's look at how to deploy our contract using Solidity Scripting.
Scripts are executed by calling the function named run, our entrypoint:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
import "forge-std/Script.sol";
import {Counter} from "src/Counter.sol";
contract ContractScript is Script {
function setUp() public {}
function run() public {
vm.startBroadcast();
new Counter(10);
vm.stopBroadcast();
}
}
Now we can use this script to deploy our smart contract to either a live or test network. 🚀
Using the address that calls the test contract or the address provided as the sender, startBroadcast and startBroadcast(address) will have all subsequent calls (at this call depth only) create transactions that can later be signed and sent onchain.
stopBroadcast stops collecting transactions for later on-chain broadcasting.
You can also use broadcast, using the address that calls the test contract, has the next call (at this call depth only) create a transaction that can later be signed and sent onchain
Or broadcast(address) using the address provided as the sender, have only the next call (at this call depth) create a transaction that can later be signed and sent onchain.
Deploying locally
Next start Anvil, the local testnet:
anvil
Once started, Anvil will give you a local RPC endpoint as well as a handful of Private Keys and Accounts that you can use.
We can now use the local RPC along with one of the private keys to deploy locally:
dabit3/foundry-cheatsheet có 543 sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/dabit3/foundry-cheatsheet. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
dabit3/foundry-cheatsheet có phải mã nguồn mở không?
TopGit chưa ghi nhận license cho dabit3/foundry-cheatsheet. Phần lớn repo public trên GitHub là mã nguồn mở, nhưng điều khoản khác nhau từng repo — mở file LICENSE để xác nhận.
dabit3/foundry-cheatsheet có website riêng không?
TopGit chưa ghi nhận URL trang chủ cho dabit3/foundry-cheatsheet. Phần README ở tab phía trên thường có link demo, hoặc xem mô tả GitHub của repo.
dabit3/foundry-cheatsheet là gì?
dabit3/foundry-cheatsheet (dabit3/foundry-cheatsheet) là dự án đa ngôn ngữ TopGit theo dõi. Tính tới lần đồng bộ gần nhất, repo có 543 sao.
Đọc thêm về dabit3/foundry-cheatsheet ở đâu?
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/dabit3/foundry-cheatsheet là nguồn chính thức.
Đọc đầy đủ README ở tab phía trên.
Muốn nghe thêm một ý kiến về foundry-cheatsheet?
Hỏi một AI đọc được trang này — một cú bấm là có ngay nhận định về foundry-cheatsheet.