Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust. it is a toolkit for building decentralized applications.it consist of testing framework like Hardhat, truffle and Dapptools.
- If you use Windows, you need to build from source to get Foundry.
- Download and run rustup-init from rustup.rs. It will start the installation in a console.
- After this, run the following to build Foundry from source:
- cargo install --git https://github.com/foundry-rs/foundry foundry-cli anvil --bins --locked
now create a new project with forge init "projectname"
when we run forge test. foundry then let run anvil the local chain default after execution anvil stop the chain.
to run the code in the test we first create ABI using "forge build" which will compile our code and generate ABI file
if you want to deploy smartcontract in local network you need to start local node using "anvil" command.
after running of node. deploy the smartcontract that is present in src/nameOfcontract from script folder by using command
If we have script to deploye contract then use this below given command will not actual deploy the contract. it will just give the idea how much gas fee will be used to deploy this contract
if we want to call that function that cost some gas fee. we will use command. cast send always use when some data is updated in smartcontract
cast call 0x354e3a1ebffbb055e797ea33883738baf49e4f05 "funcitonName" --rpc-url rpc_url
vm.prank(user) ==> used to set msg.sender all line below this will be execute by the address it self
vm.expectRevert() ==> this cheatcode used for failed test condition. just write the code for failing condtion then that code succeeded without the error.
vm.txGasPrice(gasPrice); ==> it is used to set gasPrice. by default anvil set the gas price to zero.
vm.recordLogs()
vm.getRecordedLogs
here is the snippet how to access the recorded events
/// event LogCompleted(
/// uint256 indexed topic1,
/// bytes data
/// );
vm.recordLogs();
emit LogCompleted(10, "operation completed");
Vm.Log[] memory entries = vm.getRecordedLogs();
assertEq(entries.length, 1);
assertEq(entries[0].topics[0], keccak256("LogCompleted(uint256,bytes)"));
assertEq(entries[0].topics[1], bytes32(uint256(10)));
assertEq(abi.decode(entries[0].data, (string)), "operation completed");
if we set it true then it will allow us to execute other program from the contract itself. like we can execute linux commands and python programs
vm.ffi(linuxCommand)
this cheatcode is used to read file from other directory. Reads the entire content of file to string.
foundry.toml . fs_permissions = [{ access = "read", path = "path of file we want to read"}]
in this testing when we try to randomaly thorugh data to a perticular function to break its functionality.
1. stateless testing
2. stateful testing
first lets understand Invariants ==> Invariants are those property of system that must hold at any condition.
stateLess Fuzzing ==> where the state of previous run is discarded for every new run.means give random input to one function
stateFull Fuzzing ==> where the state of previous run is not for every new run.means give random data and random function calls to many function. this is called Inveraint testing also.
it will list out all methods name with their function selector mentioned in contract itself