Flash loans in Bend
In Bend, flash loans:- Require no collateral
- Must be repaid in the same transaction
- Execute in one block
- Are aimed at developers and advanced users
How they work
The Morpho contract exposesflashLoan and uses a callback to complete the flow.
Flow
- Initiate: Your contract calls
morpho.flashLoan(token, amount, data). - Receive: Morpho transfers the requested amount to your contract.
- Callback: Morpho calls
onMorphoFlashLoan(amount, data)on your contract. - Logic: Your contract runs its logic (arbitrage, swap, etc.).
- Repay: Your contract approves Morpho to pull back the borrowed amount.
- Complete: Morpho pulls the funds. If repayment fails, the whole transaction reverts.
Implementation
To use flash loans:- Implement a contract that conforms to
IMorphoFlashLoanCallback. - Implement
onMorphoFlashLoanwith your logic. - In the callback, approve the Bend (Morpho) contract to pull the borrowed amount before returning.
Use cases
- Arbitrage: Profit from price differences across protocols in one transaction
- Collateral swap: Replace one collateral type with another in one transaction
- Self-liquidation: Liquidate your own position to avoid third-party liquidation
- Flash actions: Combine several Bend operations in one transaction
Security
- Atomicity: If the callback doesn’t approve repayment, the transaction reverts.
- Funds: Don’t leave funds in the flash loan contract after the callback.
- Reentrancy: Be careful calling external contracts inside the callback.
- Gas: Flash loan flows can use a lot of gas.
Bend callbacks
Bend supports additional callbacks for richer flows:IMorphoLiquidateCallback: LiquidationsIMorphoRepayCallback: RepaymentsIMorphoSupplyCallback: SupplyIMorphoSupplyCollateralCallback: Supply collateral