Overview
Borrower adapters serve as intermediaries between the Flash-Loan Router and flash loan providers. Each adapter implements a standardized interface while handling provider-specific loan request and callback logic.Adapter Architecture
The adapter system uses an abstract base contract that provides common functionality:Key Components
- Router Reference: Immutable reference to the Flash-Loan Router that coordinates execution
- Settlement Reference: Direct reference to CoW Settlement contract for authorization
- Standard Interface: Common
IBorrowerinterface for router interaction - Access Control: Modifiers ensuring only authorized callers
Core Functions
Router Interaction
lender: The flash loan provider contract addresstoken: The ERC-20 token to borrowamount: The amount of tokens to requestcallBackData: Data to pass back to the router unchanged
Fund Management
Only the settlement contract can set approvals, preventing unauthorized access to borrowed funds.
Implementing an Adapter
To support a new flash loan provider:- Inherit the abstract
Borrowercontract - Implement
triggerFlashLoanfor provider-specific requests - Implement the provider’s callback interface
- Forward callbacks to
flashLoanCallBack
Required Implementation
Example: Aave Adapter
- Inherit both
Borrowerand provider interface - Implement
triggerFlashLoanwith provider-specific logic - Implement provider’s callback and forward to base
assets: Single-element array with token addressamounts: Single-element array with loan amountinterestRateModes: Set to[0]to avoid opening debt positionsparams: Pass throughcallBackDataunchangedreferralCode: Set to0(currently inactive)
Example: ERC-3156 Adapter
- Simpler than Aave due to standardized interface
- Must return specific success constant
Access Control
Router-Only Access
Settlement-Only Access
- Only the router can initiate loans
- Only settlements can access borrowed funds
- External callers cannot disrupt execution flow
Fund Flow
Adding New Providers
Steps to follow:- Research Provider API: Understand the provider’s flash loan request and callback mechanisms
- Create Adapter Contract:
- Implement triggerFlashLoan: Map standard parameters to provider-specific request format
- Implement Provider Callback: Forward the provider’s callback to
flashLoanCallBack(callBackData) - Deploy and Test: Deploy the adapter and test with the router on testnets
- Update Documentation: Document the new provider and its adapter address
Best Practices
Always Pass Data Unchanged
ThecallBackData parameter must be passed to the router exactly as received:
Handle Provider-Specific Requirements
Each provider has unique requirements:- Aave requires arrays and specific return values
- ERC-3156 requires returning a success constant
- Some providers may charge fees
Validate Provider Responses
Check for success conditions specific to the provider:Use Immutable References
Router and settlement references should be immutable for security:Next Steps
Flash Loans
Learn about flash loan fundamentals
Security Model
Understand security guarantees