Overview
The Flash-Loan Router is a specialized solver contract for CoW Protocol that enables settlements to execute with flash loan capital. It orchestrates the loan request process, manages execution state, and ensures secure settlement execution.Entry Point
The router’s main entry point is theflashLoanAndSettle function:
Parameters
Array of flash loan specifications, each containing:
amount: Tokens to borrowtoken: ERC-20 token addresslender: Flash loan provider contractborrower: Adapter contract for the lender
ABI-encoded call data for the
settle() function on the CoW Settlement contractAccess Control: Only registered CoW Protocol solvers can call
flashLoanAndSettle. Authentication is verified through the settlement contract’s authenticator.Execution Flow
The router follows a carefully orchestrated execution sequence:1. Initial Validation
- Verifies no other settlement is in progress
- Emits
Settlementevent with solver’s address - Prevents reentrancy attacks
2. Loan Processing
Loans are processed sequentially in the order provided:Loan Processing Steps
Loan Processing Steps
- Check Remaining Loans: If no loans remain, proceed to settlement
- Extract Next Loan: Pop the next loan from the queue
- Set Pending Borrower: Store which borrower should call back
- Store Data Hash: Save hash of remaining data for validation
- Trigger Flash Loan: Request loan from borrower adapter
- Await Callback: Wait for borrower to receive funds and call back
3. Borrower Callbacks
Each borrower calls back the router after receiving funds:- Authentication: Only the expected borrower can call back
- Data Integrity: Validates the data hasn’t been tampered with
- Continuation: Processes the next loan in the queue
4. Settlement Execution
Once all loans are obtained:- Validates the call is to the
settle()function - Executes the settlement with borrowed funds
- Reverts if settlement fails
5. Cleanup
- Verifies settlement was executed
- Resets state for next use
- Enables same-transaction reuse
State Management
The router uses transient state variables to control execution:Pending Borrower States
State Transitions
The router uses transient storage for state variables, making it gas-efficient and enabling multiple settlements within the same transaction.
Security Guarantees
The router enforces strict execution control:Immutable Execution Flow
Solver Control: Only registered solvers can initiate settlements
Single Settlement: Each call results in exactly one
settle() executionData Integrity: Settlement call data matches the input exactly
Sequential Processing: Loans are processed in the specified order
Protection Against Attacks
Out-of-Order Execution
The router strictly enforces loan ordering:Execution Context
Key properties of the execution environment:- Call Depth: All operations occur within a single transaction
- Call Context: Never transfers control permanently (always returns)
- Atomicity: Any failure reverts the entire transaction
- Gas Efficiency: Optimized for minimal overhead