generated from PaulRBerg/foundry-template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
MorphoBlueWrapper.sol
43 lines (33 loc) · 1.48 KB
/
MorphoBlueWrapper.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// SPDX-License-Identifier: MIT
// Thanks to ultrasecr.eth
pragma solidity ^0.8.19;
import { IMorphoFlashLoanCallback } from "./interfaces/IMorphoFlashLoanCallback.sol";
import { IMorpho } from "./interfaces/IMorpho.sol";
import { BaseWrapper, IERC7399, IERC20 } from "../BaseWrapper.sol";
/// @dev MorphoBlue Flash Lender that uses MorphoBlue as source of liquidity.
contract MorphoBlueWrapper is BaseWrapper, IMorphoFlashLoanCallback {
error NotMorpho();
error UnsupportedAsset(address asset);
IMorpho public immutable morpho;
constructor(IMorpho _morpho) {
morpho = _morpho;
}
/// @inheritdoc IERC7399
function maxFlashLoan(address asset) public view returns (uint256) {
return IERC20(asset).balanceOf(address(morpho));
}
/// @inheritdoc IERC7399
function flashFee(address asset, uint256 amount) external view returns (uint256) {
uint256 max = maxFlashLoan(asset);
if (max == 0) revert UnsupportedAsset(asset);
return amount >= max ? type(uint256).max : 0;
}
function onMorphoFlashLoan(uint256 amount, bytes calldata params) external override {
if (msg.sender != address(morpho)) revert NotMorpho();
(address asset, bytes memory data) = abi.decode(params, (address, bytes));
_bridgeToCallback(asset, amount, 0, data);
}
function _flashLoan(address asset, uint256 amount, bytes memory data) internal override {
morpho.flashLoan(asset, amount, abi.encode(asset, data));
}
}