> For the complete documentation index, see [llms.txt](https://docs.optimumsec.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.optimumsec.xyz/testing/fork-tests.md).

# Develop Comprehensive Fork Tests

Fork testing is a powerful method to ensure the security and functionality of smart contracts by replicating the state of a live blockchain, like Ethereum mainnet, into a local development environment. This allows you to test contracts against real-world data, including existing tokens, liquidity pools, and deployed contracts.

## Why Fork Testing is Essential

Testing in a forked environment helps:

* **Validate External Interactions:** Identify issues with oracles, token standards, or third-party contracts.
* **Detect Edge Cases:** Surface vulnerabilities or unexpected behaviors that may not appear in simulated environments.
* **Ensure Robustness:** Verify that your contracts handle live blockchain conditions effectively.

***

## Steps to Implement Fork Tests

1. **Set Up the Forked Environment**
   * Use tools like [**Hardhat**](https://hardhat.org/) or [**Foundry**](https://book.getfoundry.sh/) to create a local fork of the blockchain.
   * Configure the fork to sync data from the desired network (e.g., Ethereum mainnet or testnet).
2. **Write Fork-Specific Tests**
   * Interact with live contracts in your test cases (e.g., calling a Uniswap pool or querying Chainlink oracles).
   * Validate interactions with real-world data and ensure compatibility with the latest state.
   * Test edge cases like minimal liquidity, extreme slippage, or stale oracle prices.
3. **Simulate Real-World Scenarios**
   * Create scripts to simulate scenarios such as large transfers, sudden price changes, or unexpected behaviors from dependent contracts.
   * Monitor for gas efficiency, transaction reverts, or vulnerabilities in these conditions.
4. **Run Regression Tests**
   * Ensure all fork tests pass after changes to your codebase.
   * Use CI/CD pipelines to automatically run fork tests during development.
5. **Analyze Test Coverage**
   * Measure test coverage specifically for fork tests to identify any gaps in your interaction scenarios.
   * Use tools like [**solidity-coverage**](https://github.com/sc-forks/solidity-coverage) or **Foundry's coverage feature**.

***

## Recommended Tools for Fork Testing

* [**Hardhat**](https://hardhat.org/)\
  Enables seamless forking of Ethereum networks and integrates with plugins for test creation and execution.
* [**Foundry**](https://book.getfoundry.sh/)\
  Offers high-performance testing with built-in support for forking and fuzzing.
* [**Tenderly**](https://tenderly.co/)\
  Provides advanced debugging and testing capabilities with real-time transaction simulation in a forked environment.

***

## Best Practices

* **Use Real RPC Nodes:** Connect to high-quality providers like **Alchemy**, **Infura**, or **QuickNode** for accurate mainnet data.
* **Monitor Performance:** Keep an eye on gas costs and execution times in the forked environment to ensure deployability on mainnet.
* **Keep Forks Updated:** Regularly sync with the latest blockchain state to reflect current conditions.
* **Combine With Static Analysis:** Pair fork testing with tools like **Slither** or **MythX** for comprehensive security assurance.
