# 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):

```bash
curl -L https://foundry.paradigm.xyz | bash
```

Reload your environment:

```bash
foundryup
```

Verify installation:

```bash
forge --version
```

2\. 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:

```bash
iwr -useb https://foundry.paradigm.xyz | iex
```

Restart the terminal and run:

```bash
foundryup
```

Verify installation:

```bash
forge --version
```

***

### Clone and Configure the Repository

Clone the repository and enter its directory:

```bash
git clone https://github.com/polystream-core/polystream-contracts
cd polystream-contracts
```

### Install Dependencies

Foundry requires external libraries (Compound, Aave, LayerBank). Install them using `forge install`:

```bash
forge install \
    foundry-rs/forge-std \
    account-abstraction/contracts \
    aave/aave-v3-core \
    layerbank/layerbank-core \
    openzeppelin/openzeppelin-contracts \
    account-abstraction/contracts
```

This command ensures all dependencies are placed in the `lib/` directory.

#### Configure `foundry.toml`

Set your `foundry.toml` configuration as follows:

```toml
[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 and `lib/` 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:

```bash
anvil --fork-url YOUR_RPC_URL --chain-id YOUR_CHAIN_ID
```

Replace:

* `https://your-rpc-url.com` with your RPC provider.
* `YOUR_CHAIN_ID` with your blockchain's Chain ID (e.g., `1` for Ethereum, `534352` for Scroll).

Example for Scroll Mainnet (using Alchemy):

```bash
anvil --fork-url https://scroll-mainnet.g.alchemy.com/v2/YOUR_ALCHEMY_KEY --chain-id 534352
```

What 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:

```bash
cd test
```

Run specific tests:

```bash
forge test --fork-url http://127.0.0.1:8545 --match-path test/YOUR_TEST_SCRIPT.t.sol -vvv
```

Replace `YOUR_TEST_SCRIPT.t.sol` with the actual test file name. Example:

```bash
forge test --fork-url http://127.0.0.1:8545 --match-path test/CompoundAdapterTest.t.sol -vvv
```

Explanation 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:

```bash
forge test --fork-url http://127.0.0.1:8545 -vvv | tee test-results.log
```

`tee test-results.log` → Saves all logs to test-results.log.

Review logs by opening the file:

```bash
cat test-results.log
```

Example Running Test Script for Combined Vault

```bash
forge test --fork-url http://127.0.0.1:8545 --match-path test/CombinedVault.t.sol -vvv
```

If successful, the output will look like:

```bash
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

* [Foundry Documentation](https://book.getfoundry.sh/)
* [Polystream Documentation](https://docs.polystream.xyz/)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.polystream.xyz/polystream/foundry.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
