curl -L https://foundry.paradigm.xyz | bashThen: (can also be used to update foundry)
foundryupforge initforge helpforge compileanvilforge createwith rpc url
forge create --rpc-urlExample 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-keycast --to-base 0x714e1 deccast wallet import defaultKey --interactivewhere defaultKey is the name of the account
forge script /path --rpc-url [url] --account defaultKey --sender [address of defaultKey] --broadcastcast wallet listcast send [to] [function] "store(uint256)" [params] 25 --private-key --rpc-urlcast call [to] [function] "retrieve()"forge install [package_name]forge fmtforge testfor 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 coverageforge coverage --report debug > coverage.txtforge snapshotaddress(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 testFunctionNameforge inspect [CONTRACT_NAME] storageLayoutcast storage [CONTRACT_ADDRESS] [SLOT_NUMBER]cast sig "[FUNCTION_NAME]()"chiselChisel 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.