CHEATCODES:-
------------------------------------------------------------------------------
๐งช Testing
* `forge test` โ Run tests (fast + parallelized).
* `setUp()` โ Runs before every test (fresh state).
* `beforeTest()/beforeTestSetup()` โ Setup code for specific tests.
* `assertEq`, `assertTrue`, etc. โ Assertions.
* `vm.expectRevert` โ Catch reverts.
* `vm.expectEmit` โ Check for emitted events.
* **Fuzz Testing** โ Auto-generate random inputs.
* **Invariant Testing** โ Ensure rules always hold (`assert` properties across fuzz loops).
---
โก Cheatcodes (vm.)
* `vm.warp` โ Change block timestamp.
* `vm.roll` โ Change block number.
* `vm.prank` / `vm.startPrank` / `vm.stopPrank` โ Impersonate accounts.
* `vm.deal` โ Give ETH to any address.
* `vm.mockCall` / `vm.expectCall` โ Mock/verify contract calls.
* `vm.snapshot` / `vm.revertTo` โ Save/restore EVM state.
* `vm.fee` / `vm.txGasPrice` โ Manipulate gas-related values.
* `vm.env*` โ Load env vars (like private keys).
---
๐ฅ๏ธ Scripts & Deployments
* `forge script` โ Run Solidity deployment scripts.
* `--broadcast` โ Actually send transactions.
* `--verify` โ Auto-verify contracts on Etherscan.
* Multi-chain ready โ Just switch `--rpc-url`.
---
๐ Forking
* `vm.createFork`, `vm.selectFork` โ Fork mainnet/testnet.
* `vm.makePersistent` โ Keep state between forks.
* Common use: Test Uniswap, Aave, Compound logic on **real blockchain state**.
---
๐ Debugging
* `forge test -vvvv` โ Detailed logs (tx traces, storage, logs).
* `console.log` / `console2.log` โ Print variables while testing.
* `cast` โ Inspect chain state (`cast call`, `cast send`, `cast block`, etc.).
---
๐ ๏ธ Developer Utilities
* **Gas Snapshots** โ `forge snapshot` compares gas usage across commits.
* **CI Friendly** โ Works fast in GitHub Actions / pipelines.
* **FFI (Foreign Function Interface)** โ Run external scripts from Solidity (advanced).
* **Configurable via foundry.toml** โ Networks, remappings, compiler settings.
---
๐งพ TL;DR โ Most Used Daily
1. `forge test` (with fuzz + invariants).
2. `vm.prank`, `vm.warp`, `vm.expectRevert`.
3. Forking with `vm.createFork` for DeFi contracts.
4. Scripts: `forge script ... --broadcast`.
5. Debugging: `forge test -vvvv`, `console.log`.
6. Deploy + Verify in one command.
7. Gas snapshots for optimizations.
Cheatcodes are special functions provided by Foundryโs vm (Virtual Machine) object that allow you to manipulate the testing environment in ways that are impossible on a real blockchain. They are your "superpowers" for writing powerful, flexible, and realistic tests.
โ ๏ธ Important Note: Cheatcodes are only available in tests and scripts. They are disabled on real networks. They are not "cheating" in a bad sense โ they are essential tools for robust testing.
Cheatcodes are functions from the Vm interface (usually accessed via the global vm variable in your test contracts) that let you:
curl or node) from within your Solidity test.You get access to vm by inheriting from the Test contract in your test file:
import "forge-std/Test.sol";
contract MyTest is Test {
// You can now use vm.<cheatcodeName>()
}
These cheatcodes let you control the blockchain's "state" during your tests.
vm.warp(uint256 timestamp)