Modernized FlashLoaner example contracts with Foundry. The project demonstrates how a Uniswap V2 flash swap callback can route liquidity through another AMM and capture any profit while repaying the originating pair.
-
Install Foundry. Once
forgeis available, install dependencies:forge install
-
Copy
.env.exampleto.env(optional) and export addresses for deployment scripts. -
Run the test suite:
forge test -
Format the codebase:
forge fmt
FlashLoaner.sol– Implements theuniswapV2Callcallback, enforces the expected pair, swaps the borrowed asset via a configurable router, and forwards any surplus back to the initiator.UniswapV2Library.sol– Pure/view helpers for token sorting, deterministic pair derivation, and swap math.
Custom errors and structured events have been added to make debugging and integration easier.
Foundry scripts live under script/:
DeployFlashLoaner.s.soldeploys the callback contract usingUNISWAP_V2_FACTORYandUNISWAP_V2_ROUTERenvironment variables.SimulateFlashSwap.s.solstands up mocked pairs/tokens to trace a complete flash swap flow with console logs.QuoteRepayment.s.solqueries live pair reserves and reports how much of the opposite token is needed to repay a flash swap for a given borrowed amount.
Run a script in dry-run mode:
forge script script/SimulateFlashSwap.s.solOr broadcast to a configured RPC:
forge script script/DeployFlashLoaner.s.sol --rpc-url $RPC_URL --private-key $PRIVATE_KEY --broadcasttest/FlashLoaner.t.sol exercises the primary behaviors and includes fuzz cases that sweep token
directions, reserve balances, borrow sizes, and profit/shortfall outputs:
- Successful repayment with profit distribution, verified with emitted event data.
- Guard rails for invalid borrow inputs.
- Reverts for unauthorized pairs and underperforming swaps across token directions.
The tests rely on purpose-built mocks that let you seed reserves, override swap outputs, and verify balances in the simulated pair.
Foundry is the sole toolchain for building, testing, and scripting. No Hardhat dependency is required.