curl -L https://foundry.paradigm.xyz | bash
Then: (can also be used to update foundry)
foundryup
forge init
forge help
forge compile
anvil
forge create
with rpc url
forge create --rpc-url
Example deploy script:
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
import "forge-std/Script.sol";
import "../src/SimpleStorage.sol";
contract DeploySimpleStorage is Script {
function run() external returns (SimpleStorage) {
vm.startBroadcast();
SimpleStorage simpleStorage = new SimpleStorage();
vm.stopBroadcast();
return simpleStorage;
}
}
Run:
forge script /path --rpc-url --broadcast --private-key
cast --to-base 0x714e1 dec
cast wallet import defaultKey --interactive
where defaultKey is the name of the account
forge script /path --rpc-url [url] --account defaultKey --sender [address of defaultKey] --broadcast
cast wallet list
cast send [to] [function] "store(uint256)" [params] 25 --private-key --rpc-url
cast call [to] [function] "retrieve()"
forge install [package_name]
forge fmt
forge test
for different level of visisbility use -v/ -vv/ -vvv (see stack traces)
forge test --match-test [function_name]
Test the code on a simulated real environment:
forge test --fork-url [rpc-url]
forge coverage
forge coverage --report debug > coverage.txt
forge snapshot
address(i)
address USER = makeAddr("user");
uint256 USER_STARTING_BALANCE = 100 ether;
vm.deal(USER, USER_STARTING_BALANCE);
vm.prank(USER)
hoax(USER, 100 ether)
vm.taxGasPrice(WEI)
vm.expectRevert(Contract.errorName.selector);
contract.functionThatShouldRevert();
vm.expectRevert(abi.encodeWithSelector(Contract.errorName.selector, args1, args2);
contract.functionThatShouldRevert();
Use vm.expectEmit()
vm.expectEmit(trueIfTopic1Exists, trueIfTopic2Exists, trueIfTopic3Exists, trueIfNonIndexedDataExists, contractAddressThatWillEmitEvent)
emit event(params)
topics are the indexed params in the event
for example, here we only have topic1:
event RaffleEntered(address indexed player);
it would look like this:
vm.expectEmit(true, false, false, false, address(raffle)
emit RaffleEntered(PLAYER);
Note: Events need to be added at the top of the test file.
vm.warp(timestamp)
vm.roll(number)
vm.recordLogs();
contract.function();
Vm.Log[] memory entries = vm.getRecordedLogs();
forge test --debug testFunctionName
forge inspect [CONTRACT_NAME] storageLayout
cast storage [CONTRACT_ADDRESS] [SLOT_NUMBER]
cast sig "[FUNCTION_NAME]()"
chisel
Chisel can be used to write Solidity directly in the terminal to quicly test logic.
Generate headers using this tool: https://github.com/transmissions11/headers
headers [name]
For extra information, cyfrin foundry github link.