Flash Loans
Flash loans are a powerful DeFi primitive that allow users to borrow assets without collateral, as long as the borrowed amount is returned within the same transaction block.
What Are Flash Loans in Morpho?
Morpho's flash loans are similar to other DeFi protocols where these:
- Allow borrowing without prior collateral
- Require repayment within the same transaction
- Execute in a single block
- Are primarily meant for developers and advanced users
How Morpho Flash Loans Work
The core flash loan functionality is implemented through the flashLoan function in the Morpho contract with a corresponding callback mechanism.
The Flash Loan Flow in Morpho
- Initiation: A user contract calls
morpho.flashLoan(token, amount, data)(refer here for the testing suite example) - Asset Transfer: Morpho transfers the requested token amount to the calling contract
- Callback Execution: Morpho calls
onMorphoFlashLoan(amount, data)on the caller contract (here for the test example) - Execution of Logic: The user's contract executes its intended operations
- Repayment: The user's contract must approve Morpho to pull back the borrowed amount
- Completion: Morpho pulls the funds back from the caller contract
If at any point the flow fails (especially if the repayment fails), the entire transaction reverts.
Implementing a Flash Loan in Morpho
To use a flash loan with Morpho, you need to:
- Create a contract that implements the
IMorphoFlashLoanCallbackinterface - Implement the
onMorphoFlashLoanfunction that will handle your logic - Ensure your callback function approves the Morpho contract to pull back the borrowed amount
Flash Loan Use Cases with Morpho
- Arbitrage: Execute trades across different protocols to profit from price discrepancies
- Collateral Swaps: Replace one collateral type with another in a single transaction
- Self-Liquidation: Liquidate your own position to avoid liquidation penalties
- Leverage Positions: Flash loans can simplify leveraged deposit workflows by avoiding onchain looping. Instead of repeatedly borrowing and re-depositing to build leverage, you can use a flash loan to achieve your exact target leverage in a single transaction with more granular control
- Flash Actions: Combine multiple Morpho operations in a single transaction
Security Considerations for Morpho Flash Loans
- Transaction Atomicity: If your callback fails to approve the repayment, the entire transaction will revert
- Contract Security: Never leave funds in your flash loan contract permanently
- Reentrancy: Be careful about calling external contracts within your flash loan logic
- Gas Management: Flash loans are complex operations that consume significant gas
Example Implementation
The FlashBorrowerMock.sol contract in Morpho's repository provides a simple example of how to implement a flash loan contract:
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.0;
import {IERC20} from "./interfaces/IERC20.sol";
import {IMorpho} from "../interfaces/IMorpho.sol";
import {IMorphoFlashLoanCallback} from "../interfaces/IMorphoCallbacks.sol";
contract FlashBorrowerMock is IMorphoFlashLoanCallback {
IMorpho private immutable MORPHO;
constructor(IMorpho newMorpho) {
MORPHO = newMorpho;
}
function flashLoan(address token, uint256 assets, bytes calldata data) external {
MORPHO.flashLoan(token, assets, data);
}
function onMorphoFlashLoan(uint256 assets, bytes calldata data) external {
require(msg.sender == address(MORPHO));
address token = abi.decode(data, (address));
IERC20(token).approve(address(MORPHO), assets);
// Add your logic here.
// e.g. swap, liquidate, leverage, and much more...
}
}This minimal implementation demonstrates the key components:
- Constructor: Stores the Morpho contract address
- flashLoan function: External entry point that initiates the flash loan
- onMorphoFlashLoan callback: Implements the required callback interface where you add your custom logic (arbitrage, swaps, leverage, etc.)
- Security check: Verifies the caller is the Morpho contract
- Repayment approval: Approves Morpho to pull back the borrowed assets
Morpho-Specific Callbacks
Morpho implements a broader callback system:
IMorphoLiquidateCallback: For liquidation operationsIMorphoRepayCallback: For repayment operationsIMorphoSupplyCallback: For supply operationsIMorphoSupplyCollateralCallback: For supplying collateral
This comprehensive callback system allows for more complex transaction patterns beyond simple flash loans, such as the "Flash Actions" test which combines supply, borrow, repay, and withdraw operations in a single transaction flow.
