Foundry
Running Foundry Test Scripts
This documentation provides a step-by-step guide to setting up Foundry, configuring dependencies, and executing test scripts for integrations with Compound, Aave, and LayerBank protocols.
1. Install Foundry
Foundry is a Rust-based Ethereum development framework providing tools like forge, cast, and anvil for compiling, deploying, and testing smart contracts.
1. Install Foundry (Linux/Mac):
curl -L https://foundry.paradigm.xyz | bashReload your environment:
foundryupVerify installation:
forge --version2. For Windows users (WSL):
Option 1: Using Windows Subsystem for Linux (WSL)
Install WSL and Ubuntu from the Microsoft Store.
Follow the above Linux installation steps within WSL.
Option 2: Using PowerShell Run the following command in PowerShell:
iwr -useb https://foundry.paradigm.xyz | iexRestart the terminal and run:
foundryupVerify installation:
forge --versionClone and Configure the Repository
Clone the repository and enter its directory:
git clone https://github.com/polystream-core/polystream-contracts
cd polystream-contractsInstall Dependencies
Foundry requires external libraries (Compound, Aave, LayerBank). Install them using forge install:
forge install \
foundry-rs/forge-std \
account-abstraction/contracts \
aave/aave-v3-core \
layerbank/layerbank-core \
openzeppelin/openzeppelin-contracts \
account-abstraction/contractsThis command ensures all dependencies are placed in the lib/ directory.
Configure foundry.toml
foundry.tomlSet your foundry.toml configuration as follows:
[profile.default]
optimizer = true
optimizer_runs = 200
via_ir = true
src = 'src/'
out = 'out/'
libs = ['lib/']
remappings = [
"@syncswapcontracts/=lib/core-contracts/contracts/",
"@layerbank-contracts/=lib/contracts/",
"@openzeppelin/=lib/openzeppelin-contracts/",
"@aave/=lib/aave-v3-origin/src/",
"@forge-std/=lib/forge-std/",
"@account-abstraction/=lib/account-abstraction/contracts/"
]This configuration:
Enables IR-based compilation (
via_ir = true) for optimized bytecode.Enables Solidity optimizer with 200 runs (
optimizer = true).Defines remappings for external dependencies.
Specifies
src/as the source directory andlib/as the library directory.
Run Local Fork with Anvil
Anvil is a fast Ethereum RPC fork provider for running local blockchain simulations.
1. Start Local Anvil Node
Run Anvil for local blockchain development:
anvil --fork-url YOUR_RPC_URL --chain-id YOUR_CHAIN_IDReplace:
https://your-rpc-url.comwith your RPC provider.YOUR_CHAIN_IDwith your blockchain's Chain ID (e.g.,1for Ethereum,534352for Scroll).
Example for Scroll Mainnet (using Alchemy):
anvil --fork-url https://scroll-mainnet.g.alchemy.com/v2/YOUR_ALCHEMY_KEY --chain-id 534352What This Does:
Forks the mainnet, allowing contract interactions as if on mainnet.
Persists the blockchain state, so transactions behave realistically.
Runs a local RPC server (
127.0.0.1:8545) for Foundry tests.
2. Run Foundry Tests
Navigate to the test directory:
cd testRun specific tests:
forge test --fork-url http://127.0.0.1:8545 --match-path test/YOUR_TEST_SCRIPT.t.sol -vvvReplace YOUR_TEST_SCRIPT.t.sol with the actual test file name. Example:
forge test --fork-url http://127.0.0.1:8545 --match-path test/CompoundAdapterTest.t.sol -vvvExplanation of Flags:
--fork-url
http://127.0.0.1:8545→ Connects to the locally running Anvil fork.--match-path test/YOUR_TEST_SCRIPT.t.sol→ Runs a specific test script.-vvv→ Enables verbose mode (displays logs, transactions, and errors).
Log Successful Test Runs
Use the following command to log test results and store them in a file:
forge test --fork-url http://127.0.0.1:8545 -vvv | tee test-results.logtee test-results.log → Saves all logs to test-results.log.
Review logs by opening the file:
cat test-results.logExample Running Test Script for Combined Vault
forge test --fork-url http://127.0.0.1:8545 --match-path test/CombinedVault.t.sol -vvvIf successful, the output will look like:
Ran 8 tests for test/CombinedVault.t.sol:CombinedVaultTest
[PASS] testEarlyWithdrawalFee() (gas: 607134)
Logs:
User 1 USDC balance: 1000000000
User 2 USDC balance: 1000000000
Distributing assets to Active Protocol ID: 1
Supplied to Protocol ID: 1 Amount: 100000000
Initial USDC balance before withdrawal: 900000000
Final USDC balance after withdrawal: 995000000
Actual received: 95000000
Expected to receive: 95000000
Early withdrawal fee test passed
// ... other test cases
Suite result: ok. 8 passed; 0 failed; 0 skipped; finished in 2.24s (13.10s CPU time)
Ran 1 test suite in 2.28s (2.24s CPU time): 8 tests passed, 0 failed, 0 skipped (8 total tests)Additional Resources
Last updated