/hello-foundry

Primary LanguageSolidity

hello-foundry

https://github.com/foundry-rs/foundry

https://book.getfoundry.sh/

Basic

  • Install
curl -L https://foundry.paradigm.xyz | bash
foundryup
  • Init
forge init
  • Basic commands
forge build
forge test
forge test --match-path test/HelloWorld -vvvv

  • Test
    • counter app
    • test setup, ok, fail
    • match
    • verbose
    • gas report
forge test --match-path test/Counter.t.sol -vvv --gas-report

  • Solidity version and optimizer settings

https://github.com/foundry-rs/foundry/tree/master/config


  • Remapping
forge remappings
forge install rari-capital/solmate
forge update lib/solmate
forge remove solmate

npm i @openzeppelin/contracts

  • Formatter
forge fmt


  • console (Counter, test, log int)
forge test --match-path test/Console.t.sol -vv

Intermediate


  • Test auth
  • Test error
    • vm.expectRevert
    • require error message
    • custom error
    • label assertions
  • Test event (expectEmit)
  • Test time (Auction.sol)
  • Test send eth (Wallet.sol) - hoax, deal
  • Test signature

Advanced

  • mainnet fork
forge test --fork-url $FORK_URL --match-path test/Fork.t.sol -vvv
  • main fork deal (whale)
forge test --fork-url $FORK_URL --match-path test/Whale.t.sol -vvv

TODO: need working example for (mainnet - opt)

  • crosschain fork

  • Fuzzing (assume, bound)

forge test --match-path test/Fuzz.t.sol
  • Invariant
# Open testing
forge test --match-path test/invariants/Invariant_0.t.sol -vvv
forge test --match-path test/invariants/Invariant_1.t.sol -vvv
# Handler
forge test --match-path test/invariants/Invariant_2.t.sol -vvv
# Actor management
forge test --match-path test/invariants/Invariant_3.t.sol -vvv
  • FFI
forge test --match-path test/FFI.t.sol --ffi -vvv
  • Differential testing
# virtual env
python3 -m pip install --user virtualenv
virtualenv -p python3 venv
source venv/bin/activate

pip install eth-abi
FOUNDRY_FUZZ_RUNS=100 forge test --match-path test/DifferentialTest.t.sol --ffi -vvv

Misc

  • Vyper

https://github.com/0xKitsune/Foundry-Vyper

  1. Install vyper
# virtual env
python3 -m pip install --user virtualenv
virtualenv -p python3 venv
source venv/bin/activate

pip3 install vyper==0.3.7

# Check installation
vyper --version
  1. Put Vyper contract inside vyper_contracts
  2. Declare Solidity interface inside src
  3. Copy & paste lib/utils/VyperDeployer.sol
  4. Write test
forge test --match-path test/Vyper.t.sol --ffi
  • ignore error code
ignored_error_codes = ["license", "unused-param", "unused-var"]
  • Deploy
source .env
forge script script/Token.s.sol:TokenScript --rpc-url $GOERLI_RPC_URL --broadcast --verify -vvvv
  • Forge geiger
forge geiger

TODO:

  • chisel?
  • debugger?
  • forge snapshot?
  • script?