Verified contractPartialMIT
MorphoBlueWrapper
Address0xa0cb4e1222d813d6e4de79f2a7a0b7759209588f
- Balance
0 ETH
- Transactions
0
- Compiler
solc 0.8.19
- Same code
Only this contract
Details
- Compiler
- solc 0.8.19+commit.7dd6d404
- Language
- solidity
- Optimization
- Yes, 10,000 runs
- EVM version
- london
- License
- MIT
- Match
- Partialcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 19,182,783
- Creation transaction
- 0x3beb2184b1…b6d8a1634a
9 files
- src/morpho/MorphoBlueWrapper.solmain1.51 KB
- lib/erc7399/src/IERC7399.sol2.16 KB
- lib/solmate/src/tokens/ERC20.sol6.81 KB
- src/BaseWrapper.sol5.8 KB
- src/morpho/interfaces/IMorpho.sol169 B
- src/morpho/interfaces/IMorphoFlashLoanCallback.sol181 B
- src/utils/FunctionCodec.sol1.54 KB
- src/utils/RevertMsgExtractor.sol950 B
- src/utils/TransferHelper.sol1.27 KB
src/morpho/MorphoBlueWrapper.sol
// 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, ERC20 } 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 ERC20(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 {
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));
}
}
lib/erc7399/src/IERC7399.sol
// SPDX-License-Identifier: CC0
pragma solidity >=0.6.4;
/// @dev Specification for flash lenders compatible with ERC-7399
interface IERC7399 {
/// @dev The amount of currency available to be lent.
/// @param asset The loan currency.
/// @return The amount of `asset` that can be borrowed.
function maxFlashLoan(address asset) external view returns (uint256);
/// @dev The fee to be charged for a given loan.
/// @param asset The loan currency.
/// @param amount The amount of assets lent.
/// @return The amount of `asset` to be charged for the loan, on top of the returned principal.
function flashFee(address asset, uint256 amount) external view returns (uint256);
/// @dev Initiate a flash loan.
/// @param loanReceiver The address receiving the flash loan
/// @param asset The asset to be loaned
/// @param amount The amount to loaned
/// @param data The ABI encoded user data
/// @param callback The address and signature of the callback function
/// @return result ABI encoded result of the callback
function flash(
address loanReceiver,
address asset,
uint256 amount,
bytes calldata data,
/// @dev callback. This is a combination of the callback receiver address, and the signature of callback
/// function. It is encoded packed as 20 bytes + 4 bytes.
/// @dev the return of the callback function is not encoded in the parameter, but must be `returns (bytes
/// memory)` for compliance with the standard.
/// @param initiator The address that called this function
/// @param paymentReceiver The address that needs to receive the amount plus fee at the end of the callback
/// @param asset The asset to be loaned
/// @param amount The amount to loaned
/// @param fee The fee to be paid
/// @param data The ABI encoded data to be passed to the callback
/// @return result ABI encoded result of the callback
function(address, address, address, uint256, uint256, bytes memory) external returns (bytes memory) callback
) external returns (bytes memory);
}
lib/solmate/src/tokens/ERC20.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.0;
/// @notice Modern and gas efficient ERC20 + EIP-2612 implementation.
/// @author Solmate (https://github.com/transmissions11/solmate/blob/main/src/tokens/ERC20.sol)
/// @author Modified from Uniswap (https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/UniswapV2ERC20.sol)
/// @dev Do not manually set balances without updating totalSupply, as the sum of all user balances must not exceed it.
abstract contract ERC20 {
/*//////////////////////////////////////////////////////////////
EVENTS
//////////////////////////////////////////////////////////////*/
event Transfer(address indexed from, address indexed to, uint256 amount);
event Approval(address indexed owner, address indexed spender, uint256 amount);
/*//////////////////////////////////////////////////////////////
METADATA STORAGE
//////////////////////////////////////////////////////////////*/
string public name;
string public symbol;
uint8 public immutable decimals;
/*//////////////////////////////////////////////////////////////
ERC20 STORAGE
//////////////////////////////////////////////////////////////*/
uint256 public totalSupply;
mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;
/*//////////////////////////////////////////////////////////////
EIP-2612 STORAGE
//////////////////////////////////////////////////////////////*/
uint256 internal immutable INITIAL_CHAIN_ID;
bytes32 internal immutable INITIAL_DOMAIN_SEPARATOR;
mapping(address => uint256) public nonces;
/*//////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/
constructor(
string memory _name,
string memory _symbol,
uint8 _decimals
) {
name = _name;
symbol = _symbol;
decimals = _decimals;
INITIAL_CHAIN_ID = block.chainid;
INITIAL_DOMAIN_SEPARATOR = computeDomainSeparator();
}
/*//////////////////////////////////////////////////////////////
ERC20 LOGIC
//////////////////////////////////////////////////////////////*/
function approve(address spender, uint256 amount) public virtual returns (bool) {
allowance[msg.sender][spender] = amount;
emit Approval(msg.sender, spender, amount);
return true;
}
function transfer(address to, uint256 amount) public virtual returns (bool) {
balanceOf[msg.sender] -= amount;
// Cannot overflow because the sum of all user
// balances can't exceed the max uint256 value.
unchecked {
balanceOf[to] += amount;
}
emit Transfer(msg.sender, to, amount);
return true;
}
function transferFrom(
address from,
address to,
uint256 amount
) public virtual returns (bool) {
uint256 allowed = allowance[from][msg.sender]; // Saves gas for limited approvals.
if (allowed != type(uint256).max) allowance[from][msg.sender] = allowed - amount;
balanceOf[from] -= amount;
// Cannot overflow because the sum of all user
// balances can't exceed the max uint256 value.
unchecked {
balanceOf[to] += amount;
}
emit Transfer(from, to, amount);
return true;
}
/*//////////////////////////////////////////////////////////////
EIP-2612 LOGIC
//////////////////////////////////////////////////////////////*/
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) public virtual {
require(deadline >= block.timestamp, "PERMIT_DEADLINE_EXPIRED");
// Unchecked because the only math done is incrementing
// the owner's nonce which cannot realistically overflow.
unchecked {
address recoveredAddress = ecrecover(
keccak256(
abi.encodePacked(
"\x19\x01",
DOMAIN_SEPARATOR(),
keccak256(
abi.encode(
keccak256(
"Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"
),
owner,
spender,
value,
nonces[owner]++,
deadline
)
)
)
),
v,
r,
s
);
require(recoveredAddress != address(0) && recoveredAddress == owner, "INVALID_SIGNER");
allowance[recoveredAddress][spender] = value;
}
emit Approval(owner, spender, value);
}
function DOMAIN_SEPARATOR() public view virtual returns (bytes32) {
return block.chainid == INITIAL_CHAIN_ID ? INITIAL_DOMAIN_SEPARATOR : computeDomainSeparator();
}
function computeDomainSeparator() internal view virtual returns (bytes32) {
return
keccak256(
abi.encode(
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"),
keccak256(bytes(name)),
keccak256("1"),
block.chainid,
address(this)
)
);
}
/*//////////////////////////////////////////////////////////////
INTERNAL MINT/BURN LOGIC
//////////////////////////////////////////////////////////////*/
function _mint(address to, uint256 amount) internal virtual {
totalSupply += amount;
// Cannot overflow because the sum of all user
// balances can't exceed the max uint256 value.
unchecked {
balanceOf[to] += amount;
}
emit Transfer(address(0), to, amount);
}
function _burn(address from, uint256 amount) internal virtual {
balanceOf[from] -= amount;
// Cannot underflow because a user's balance
// will never be larger than the total supply.
unchecked {
totalSupply -= amount;
}
emit Transfer(from, address(0), amount);
}
}
src/BaseWrapper.sol
// SPDX-License-Identifier: MIT
// Thanks to ultrasecr.eth
pragma solidity ^0.8.19;
import "erc7399/IERC7399.sol";
import { TransferHelper, ERC20 } from "./utils/TransferHelper.sol";
import { FunctionCodec } from "./utils/FunctionCodec.sol";
/// @dev All ERC7399 flash loan wrappers have the same general structure.
/// - The ERC7399 `flash` function is the entry point for the flash loan.
/// - The wrapper calls the underlying lender flash lender on their non-ERC7399 flash lending call to borrow the funds.
/// - The lender sends the funds to the wrapper.
/// - The wrapper receives the callback from the lender.
/// - The wrapper sends the funds to the loan receiver.
/// - The wrapper calls the callback supplied by the original borrower.
/// - The callback from the original borrower executes.
/// - Depending on the lender, the wrapper may have to approve it to pull the repayment.
/// - If there is any data to return, it is kept in a storage variable.
/// - The wrapper exits the callback.
/// - The lender verifies or pulls the repayment.
/// - The wrapper returns to the original borrower the stored result of its callback.
abstract contract BaseWrapper is IERC7399 {
using TransferHelper for address;
struct Data {
address loanReceiver;
address initiator;
function(address, address, address, uint256, uint256, bytes memory) external returns (bytes memory) callback;
bytes initiatorData;
}
bytes internal _callbackResult;
/// @inheritdoc IERC7399
/// @dev The entry point for the ERC7399 flash loan. Packs data to convert the legacy flash loan into an ERC7399
/// flash loan. Then it calls the legacy flash loan. Once the flash loan is done, checks if there is any return
/// data and returns it.
function flash(
address loanReceiver,
address asset,
uint256 amount,
bytes calldata initiatorData,
function(address, address, address, uint256, uint256, bytes memory) external returns (bytes memory) callback
)
external
returns (bytes memory result)
{
Data memory data = Data({
loanReceiver: loanReceiver,
initiator: msg.sender,
callback: callback,
initiatorData: initiatorData
});
return _flash(asset, amount, data);
}
/// @dev Alternative entry point for the ERC7399 flash loan, without function pointers. Packs data to convert the
/// legacy flash loan into an ERC7399 flash loan. Then it calls the legacy flash loan. Once the flash loan is done,
/// checks if there is any return data and returns it.
function flash(
address loanReceiver,
address asset,
uint256 amount,
bytes calldata initiatorData,
address callbackTarget,
bytes4 callbackSelector
)
external
returns (bytes memory result)
{
Data memory data = Data({
loanReceiver: loanReceiver,
initiator: msg.sender,
callback: FunctionCodec.decodeFunction(callbackTarget, callbackSelector),
initiatorData: initiatorData
});
return _flash(asset, amount, data);
}
function _flash(address asset, uint256 amount, Data memory data) internal virtual returns (bytes memory result) {
_flashLoan(asset, amount, abi.encode(data));
result = _callbackResult;
// Avoid storage write if not needed
if (result.length > 0) {
delete _callbackResult;
}
return result;
}
/// @dev Call the legacy flashloan function in the child contract. This is where we borrow from Aave, Uniswap, etc.
function _flashLoan(address asset, uint256 amount, bytes memory data) internal virtual;
/// @dev Handle the common parts of bridging the callback from legacy to ERC7399. Transfer the funds to the loan
/// receiver. Call the callback supplied by the original borrower. Approve the repayment if necessary. If there is
/// any result, it is kept in a storage variable to be retrieved on `flash` after the legacy flash loan is finished.
function _bridgeToCallback(address asset, uint256 amount, uint256 fee, bytes memory params) internal {
Data memory data = abi.decode(params, (Data));
_transferAssets(asset, amount, data.loanReceiver);
// call the callback and tell the callback receiver to repay the loan to this contract
bytes memory result = data.callback(data.initiator, _repayTo(), address(asset), amount, fee, data.initiatorData);
_approveRepayment(asset, amount, fee);
if (result.length > 0) {
// if there's any result, it is kept in a storage variable to be retrieved later in this tx
_callbackResult = result;
}
}
/// @dev Transfer the assets to the loan receiver.
/// Override it if the provider can send the funds directly
function _transferAssets(address asset, uint256 amount, address loanReceiver) internal virtual {
asset.safeTransfer(loanReceiver, amount);
}
/// @dev Approve the repayment of the loan to the provider if needed.
/// Override it if the provider can receive the funds directly and you want to avoid the if condition
function _approveRepayment(address asset, uint256 amount, uint256 fee) internal virtual {
if (_repayTo() == address(this)) {
ERC20(asset).approve(msg.sender, amount + fee);
}
}
/// @dev Where should the end client send the funds to repay the loan
/// Override it if the provider can receive the funds directly
function _repayTo() internal view virtual returns (address) {
return address(this);
}
}
src/morpho/interfaces/IMorpho.sol
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.4;
interface IMorpho {
function flashLoan(address token, uint256 assets, bytes memory data) external;
}
src/morpho/interfaces/IMorphoFlashLoanCallback.sol
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.4;
interface IMorphoFlashLoanCallback {
function onMorphoFlashLoan(uint256 assets, bytes calldata data) external;
}
src/utils/FunctionCodec.sol
// SPDX-License-Identifier: MIT
// Thanks to ultrasecr.eth
pragma solidity ^0.8.19;
library FunctionCodec {
function encodeParams(address contractAddr, bytes4 selector) internal pure returns (bytes24) {
return bytes24(bytes20(contractAddr)) | bytes24(selector) >> 160;
}
function decodeParams(bytes24 encoded) internal pure returns (address contractAddr, bytes4 selector) {
contractAddr = address(bytes20(encoded));
selector = bytes4(encoded << 160);
}
function encodeFunction(
function(address, address, address, uint256, uint256, bytes memory) external returns (bytes memory) f
)
internal
pure
returns (bytes24)
{
return encodeParams(f.address, f.selector);
}
function decodeFunction(
address contractAddr,
bytes4 selector
)
internal
pure
returns (function(address, address, address, uint256, uint256, bytes memory) external returns (bytes memory) f)
{
uint32 s = uint32(selector);
// solhint-disable-next-line no-inline-assembly
assembly {
f.address := contractAddr
f.selector := s
}
}
function decodeFunction(bytes24 encoded)
internal
pure
returns (function(address, address, address, uint256, uint256, bytes memory) external returns (bytes memory) f)
{
(address contractAddr, bytes4 selector) = decodeParams(encoded);
return decodeFunction(contractAddr, selector);
}
}
src/utils/RevertMsgExtractor.sol
// SPDX-License-Identifier: MIT
// Taken from
// https://github.com/sushiswap/BoringSolidity/blob/441e51c0544cf2451e6116fe00515e71d7c42e2c/src/BoringBatchable.sol
pragma solidity ^0.8.19;
library RevertMsgExtractor {
/// @dev Helper function to extract a useful revert message from a failed call.
/// If the returned data is malformed or not correctly abi encoded then this call can fail itself.
function getRevertMsg(bytes memory returnData) internal pure returns (string memory) {
// If the _res length is less than 68, then the transaction failed silently (without a revert message)
if (returnData.length < 68) return "Transaction reverted silently";
// solhint-disable-next-line no-inline-assembly
assembly {
// Slice the sighash.
returnData := add(returnData, 0x04)
}
return abi.decode(returnData, (string)); // All that remains is the revert string
}
}
src/utils/TransferHelper.sol
// SPDX-License-Identifier: MIT
// Taken from https://github.com/Uniswap/uniswap-lib/blob/master/src/libraries/TransferHelper.sol
pragma solidity ^0.8.19;
import { ERC20 } from "solmate/tokens/ERC20.sol";
import { RevertMsgExtractor } from "./RevertMsgExtractor.sol";
// helper methods for interacting with ERC20 tokens and sending ETH that do not consistently return true/false
// USDT is a well known token that returns nothing for its transfer, transferFrom, and approve functions
// and part of the reason this library exists
library TransferHelper {
/// @notice Transfers assets from msg.sender to a recipient
/// @dev Errors with the underlying revert message if transfer fails
/// @param asset The contract address of the asset which will be transferred
/// @param to The recipient of the transfer
/// @param value The value of the transfer
function safeTransfer(address asset, address to, uint256 value) internal {
(bool success, bytes memory data) =
// solhint-disable-next-line avoid-low-level-calls
address(asset).call(abi.encodeWithSelector(ERC20.transfer.selector, to, value));
if (!(success && (data.length == 0 || abi.decode(data, (bool))))) revert(RevertMsgExtractor.getRevertMsg(data));
}
}
Compiler settingsShow
{
"metadata": {
"appendCBOR": false,
"bytecodeHash": "none"
},
"libraries": {},
"optimizer": {
"runs": 10000,
"enabled": true
},
"evmVersion": "london",
"remappings": [
":@prb/test/=lib/prb-test/src/",
":ds-test/=lib/forge-std/lib/ds-test/src/",
":erc3156/=lib/erc3156/contracts/",
":erc7399/=lib/erc7399/src/",
":forge-std/=lib/forge-std/src/",
":prb-test/=lib/prb-test/src/",
":registry/=lib/registry/",
":solmate/=lib/solmate/src/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.