Cryo Explorer Ethereum Mainnet

Address Contract Verified

Address 0xCCB68162Badb2AFC5185DD8416d5452fe934BA22
Balance 0 ETH
Nonce 1
Code Size 5671 bytes
Last Active
Indexed Transactions 1 (24,314,18824,314,188)
Gas Used (indexed) 381,244
External Etherscan · Sourcify

Contract Bytecode

5671 bytes
0x6080806040526004361015610012575f80fd5b5f905f3560e01c90816328bd4bcf146107245750806331f57072146100b95780633acb5624146100915763d4b084f01461004a575f80fd5b3461008e578060031936011261008e576040517f000000000000000000000000bbbbbbbbbb9cc5e90e3b3af64bdaf62c37eeffcb6001600160a01b03168152602090f35b80fd5b503461008e578060031936011261008e5760206040515f805160206115d28339815191528152f35b503461008e57604036600319011261008e576024359060043567ffffffffffffffff831161072057366023840112156107205782600401359067ffffffffffffffff82116103d7576024840191808501602481019136831161069a575f805160206115d2833981519152330361071157855480159182156106d9575b50506106cb576101e090869003126103e65761016c90610161816040519561015c876111b2565b611324565b845260c48601611324565b93602083019485526101816101648201611220565b60408401908152606084019361018483013585526101a26101a48401611419565b90608081019182526101e46101ba6101c48601611419565b9460a0830195865201359560028710156104615760c08201879052815180516020909101516001600160a01b0390811698911690610483576001600160801b038651168710610474578899610212888a9b999a611549565b805186516001600160a01b03165f805160206115d28339815191523b156104705760405163238d657960e01b81529161024f90600484019061139a565b8a60a483015260c482015261010060e482015288610104820152888161012481835f805160206115d28339815191525af190811561046557899161044c575b50505182518651604080516350d8cd4b60e01b815295939092869283926102c49230926001600160a01b031691600486016114b0565b03818b5f805160206115d28339815191525af1928315610441576102ef93610423575b505190611549565b60406001600160801b03825193511660018060a01b0385511693610326835195869384936320b76e8160e01b855260048501611443565b0381885f805160206115d28339815191525af1918215610418576001600160801b03926103ea575b5051925191519116906001600160a01b03165f805160206115d28339815191523b156103e65783916103966040519485938493638720316d60e01b855230926004860161147e565b0381835f805160206115d28339815191525af180156103db576103c2575b50506103bf91611549565b80f35b816103cc916111fe565b6103d757825f6103b4565b8280fd5b6040513d84823e3d90fd5b8380fd5b61040b9060403d604011610411575b61040381836111fe565b81019061142d565b5061034e565b503d6103f9565b6040513d87823e3d90fd5b61043b9060403d6040116104115761040381836111fe565b506102e7565b6040513d8a823e3d90fd5b81610456916111fe565b61046157875f61028e565b8780fd5b6040513d8b823e3d90fd5b8980fd5b638dc8d9b360e01b8952600489fd5b9692909586999594989951116106bc5790889392916104a3875189611549565b60406001600160801b03835192511660018060a01b038b5116926104da835194859384936320b76e8160e01b855260048501611443565b0381885f805160206115d28339815191525af180156104185761069e575b50516001600160801b0383511660018060a01b038951165f805160206115d28339815191523b1561069a5785916105456040519485938493638720316d60e01b855230926004860161147e565b0381835f805160206115d28339815191525af1908115610661578491610685575b505061057d906001600160801b0383511690611549565b8251905186516001600160a01b0316906001600160801b03165f805160206115d28339815191523b156103e65760405163238d657960e01b8152926105c690600485019061139a565b60a483015260c482015261010060e482015281610104820152818161012481835f805160206115d28339815191525af180156103db5761066c575b50505181519351604080516350d8cd4b60e01b8152959092869283926106379230926001600160a01b03169190600486016114b0565b0381875f805160206115d28339815191525af1928315610661576103bf9361042357505190611549565b6040513d86823e3d90fd5b81610676916111fe565b61068157845f610601565b8480fd5b8161068f916111fe565b6103d757825f610566565b8580fd5b6106b69060403d6040116104115761040381836111fe565b506104f8565b6308d1fde360e11b8952600489fd5b6282b42960e81b8552600485fd5b9091506040516106f36020601f19601f85011601826111fe565b81815287602080830193808a86378301015251902014155f80610135565b63e51b512360e01b8652600486fd5b5080fd5b823461110757366003190161036081126111075760a0136111075760a03660a31901126111075760026101443510156111075760a036610163190112611107576060366102031901126111075760a0366102631901126111075760603661030319011261110757331561116157610164356001600160a01b038116928382036111075733840361110b5761018435906001600160a01b0382168083036111075730810361110b576101a435908115801592838103611107575061110b576101e4359342851061110b57610264356001600160a01b03811690036111075733610264356001600160a01b03160361110b57610284356001600160a01b03811690036111075730610284356001600160a01b03160361110b576102a435978815158903611107578861110b57426102e4351061110b57633857496960e11b85523360048601526020856024815f805160206115d28339815191525afa9485156110fc575f9561112d575b506101c4359480860361110b5760018101809111611119576102c4350361110b575f805160206115d28339815191523b156111075760405196638069218f60e01b88525060048701525060248501526044840152606483015260848201526102043560ff81168091036111075760a48201526102243560c48201526102443560e48201525f8161010481835f805160206115d28339815191525af180156110fc576110e9575b5061093b611170565b60a4356001600160a01b03811614159290836103d75760a4356001600160a01b039081169116148015906110bc575b6110ad5760843561012435101561109e575f805160206115d28339815191523b1561072057604051630a8e0d6f60e11b81526109a860048201611234565b828160a481835f805160206115d28339815191525af18015610d1857908391611089575b50506109d7366112a3565b6040516109e860208201809361139a565b60a081526109f760c0826111fe565b5190206040516349e2903160e11b81526004810182905233602482015293906060856044817f000000000000000000000000bbbbbbbbbb9cc5e90e3b3af64bdaf62c37eeffcb6001600160a01b03165afa908115610661578495859261103d575b506001600160801b0382161561102e57604051632e3071cd60e11b8152600481019190915260c0816024817f000000000000000000000000bbbbbbbbbb9cc5e90e3b3af64bdaf62c37eeffcb6001600160a01b03165afa9081156104185785908692610fa3575b506001600160801b0380610adf931691166001600160801b0388166114e9565b9485610d3d57508394505f805160206115d28339815191523b15610d2357604051638720316d60e01b8152610b1660048201611234565b6001600160801b03821660a48201523360c48201523060e4820152848161010481835f805160206115d28339815191525af1908115610418578591610d28575b5050610b726001600160801b038216610b6d61119c565b611549565b5f805160206115d28339815191523b15610d235760405163238d657960e01b815291610d23576001600160a01b0360a4358116600484015260c4359081169081900361068157602483015260e4356001600160a01b03811690819003610681576044830152610104356001600160a01b0381169190829003610681576001600160801b039160648401526101243560848401521660a48201523360c482015261010060e482015282610104820152828161012481835f805160206115d28339815191525af1908115610d18578391610d03575b50505b5f805160206115d28339815191523b15610d0057604051638069218f60e01b81526001600160a01b0361026435811660048301526102843516602482015290151560448201526102c43560648201526102e43560848201526103043560ff811690819003610cfc5760a48201526103243560c48201526103443560e4820152818161010481835f805160206115d28339815191525af180156103db57610ceb5750f35b81610cf5916111fe565b61008e5780f35b5050fd5b50fd5b81610d0d916111fe565b610d00578184610c45565b6040513d85823e3d90fd5b505050fd5b81610d32916111fe565b610d23578386610b56565b915060405194610d4c866111b2565b610d55366112a3565b865260405192610d64846111e2565b60a435845260c4356001600160a01b0381168103610f9f57602085015260e4356001600160a01b0381168103610f9f576040850152610104356001600160a01b0381168103610f9f576060850152610124356080850152602087019384526040870190338252606088019083825260808901906001600160801b0316815260a08901916001600160801b038616835260c08a01936101443585526040519760208901809c5190610e139161139a565b5160c08901610e219161139a565b516001600160a01b031661016088015251610180870152516001600160801b039081166101a08701529051166101c085015251956002871015610f8b5785966101e08501526101e08452610e77610200856111fe565b83519020855561014435610f0f5750610e8e611186565b915f805160206115d28339815191523b156106815760405163701195a160e11b815292859284928392610ece926001600160801b031690600485016113d9565b0381835f805160206115d28339815191525af1908115610d18578391610efa575b50505b818055610c48565b81610f04916111fe565b610d00578184610eef565b9050610f19611170565b905f805160206115d28339815191523b15610681578491610f4e604051948593849363701195a160e11b8552600485016113d9565b0381835f805160206115d28339815191525af1908115610d18578391610f76575b5050610ef2565b81610f80916111fe565b610d00578184610f6f565b634e487b7160e01b86526021600452602486fd5b8680fd5b91505060c0813d60c011611026575b81610fbf60c093836111fe565b810103126106815780610fd4610adf92611386565b50610fe160208201611386565b506001600160801b0380610ff760408401611386565b61101c60a061100860608701611386565b9561101560808201611386565b5001611386565b5092935050610abf565b3d9150610fb2565b638dc8d9b360e01b8552600485fd5b955090506060853d606011611081575b8161105a606093836111fe565b810103126103e65761107a604061107360208801611386565b9601611386565b9086610a58565b3d915061104d565b81611093916111fe565b6107205781846109cc565b6357c48ca560e11b8252600482fd5b63936bb5ad60e01b8252600482fd5b506110c5611186565b6001600160a01b036110d561119c565b6001600160a01b039092169116141561096a565b6110f591505f906111fe565b5f82610932565b6040513d5f823e3d90fd5b5f80fd5b6282b42960e81b5f5260045ffd5b634e487b7160e01b5f52601160045260245ffd5b9094506020813d602011611159575b81611149602093836111fe565b810103126111075751938961088c565b3d915061113c565b63f456040360e01b5f5260045ffd5b6004356001600160a01b03811681036111075790565b6024356001600160a01b03811681036111075790565b60c4356001600160a01b03811681036111075790565b60e0810190811067ffffffffffffffff8211176111ce57604052565b634e487b7160e01b5f52604160045260245ffd5b60a0810190811067ffffffffffffffff8211176111ce57604052565b90601f8019910116810190811067ffffffffffffffff8211176111ce57604052565b35906001600160a01b038216820361110757565b6004356001600160a01b038116908190036111075781526024356001600160a01b038116908190036111075760208201526044356001600160a01b038116908190036111075760408201526064356001600160a01b038116908190036111075760608201526080608435910152565b60a090600319011261110757604051906112bc826111e2565b816004356001600160a01b03811681036111075781526024356001600160a01b03811681036111075760208201526044356001600160a01b03811681036111075760408201526064356001600160a01b03811681036111075760608201526080608435910152565b91908260a09103126111075760405161133c816111e2565b608080829461134a81611220565b845261135860208201611220565b602085015261136960408201611220565b604085015261137a60608201611220565b60608501520135910152565b51906001600160801b038216820361110757565b80516001600160a01b03908116835260208083015182169084015260408083015182169084015260608083015190911690830152608090810151910152565b919260809360209260018060a01b0316845282840152606060408401528051918291826060860152018484015e5f828201840152601f01601f1916010190565b35906001600160801b038216820361110757565b9190826040910312611107576020825192015190565b9161145283610140959361139a565b5f60a084015260c083015260018060a01b031660e08201526101206101008201525f6101208201520190565b9260e092959491956114958561010081019861139a565b60a08501526001600160a01b0390811660c085015216910152565b9261010092959491956114c88561012081019861139a565b60a08501525f60c08501526001600160a01b0390811660e085015216910152565b90811590818015611541575b6115395780830292830414171561111957818101809111611119575f198101908111611119578115611525570490565b634e487b7160e01b5f52601260045260245ffd5b505050505f90565b5083156114f5565b6001600160a01b03169081156115cd575f916044602092604051948593849263095ea7b360e01b84525f805160206115d2833981519152600485015260248401525af180156110fc576115995750565b6020813d6020116115c5575b816115b2602093836111fe565b8101031261110757518015150361110757565b3d91506115a5565b505056fe000000000000000000000000bbbbbbbbbb9cc5e90e3b3af64bdaf62c37eeffcba26469706673582212207558e264af5576ccb7938165e249828ee3fde50776b0df542fae7336bde4d13564736f6c634300081a0033

Verified Source Code Full Match

Compiler: v0.8.26+commit.8a97fa7a EVM: cancun Optimization: Yes (200 runs)
IMFMorphoMigrator.flatten.sol 661 lines
// SPDX-License-Identifier: MIT
pragma solidity >=0.5.0 ^0.8.19 ^0.8.20;

// lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol

// OpenZeppelin Contracts (last updated v5.1.0) (token/ERC20/IERC20.sol)

/**
 * @dev Interface of the ERC-20 standard as defined in the ERC.
 */
interface IERC20 {
    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}. `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);

    /**
     * @dev Returns the value of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the value of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves a `value` amount of tokens from the caller's account to `to`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address to, uint256 value) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Sets a `value` amount of tokens as the allowance of `spender` over the
     * caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 value) external returns (bool);

    /**
     * @dev Moves a `value` amount of tokens from `from` to `to` using the
     * allowance mechanism. `value` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(address from, address to, uint256 value) external returns (bool);
}

// src/interfaces/IMorpho.sol

type Id is bytes32;

struct MarketParams {
    address loanToken;
    address collateralToken;
    address oracle;
    address irm;
    uint256 lltv;
}

/// @dev Warning: For `feeRecipient`, `supplyShares` does not contain the accrued shares since the last interest
/// accrual.
struct Position {
    uint256 supplyShares;
    uint128 borrowShares;
    uint128 collateral;
}

/// @dev Warning: `totalSupplyAssets` does not contain the accrued interest since the last interest accrual.
/// @dev Warning: `totalBorrowAssets` does not contain the accrued interest since the last interest accrual.
/// @dev Warning: `totalSupplyShares` does not contain the additional shares accrued by `feeRecipient` since the last
/// interest accrual.
struct Market {
    uint128 totalSupplyAssets;
    uint128 totalSupplyShares;
    uint128 totalBorrowAssets;
    uint128 totalBorrowShares;
    uint128 lastUpdate;
    uint128 fee;
}

struct Authorization {
    address authorizer;
    address authorized;
    bool isAuthorized;
    uint256 nonce;
    uint256 deadline;
}

struct Signature {
    uint8 v;
    bytes32 r;
    bytes32 s;
}

/// @dev This interface is used for factorizing IMorphoStaticTyping and IMorpho.
/// @dev Consider using the IMorpho interface instead of this one.
interface IMorphoBase {
    /// @notice The EIP-712 domain separator.
    /// @dev Warning: Every EIP-712 signed message based on this domain separator can be reused on chains sharing the
    /// same chain id and on forks because the domain separator would be the same.
    function DOMAIN_SEPARATOR() external view returns (bytes32);

    /// @notice The owner of the contract.
    /// @dev It has the power to change the owner.
    /// @dev It has the power to set fees on markets and set the fee recipient.
    /// @dev It has the power to enable but not disable IRMs and LLTVs.
    function owner() external view returns (address);

    /// @notice The fee recipient of all markets.
    /// @dev The recipient receives the fees of a given market through a supply position on that market.
    function feeRecipient() external view returns (address);

    /// @notice Whether the `irm` is enabled.
    function isIrmEnabled(address irm) external view returns (bool);

    /// @notice Whether the `lltv` is enabled.
    function isLltvEnabled(uint256 lltv) external view returns (bool);

    /// @notice Whether `authorized` is authorized to modify `authorizer`'s position on all markets.
    /// @dev Anyone is authorized to modify their own positions, regardless of this variable.
    function isAuthorized(address authorizer, address authorized) external view returns (bool);

    /// @notice The `authorizer`'s current nonce. Used to prevent replay attacks with EIP-712 signatures.
    function nonce(address authorizer) external view returns (uint256);

    /// @notice Sets `newOwner` as `owner` of the contract.
    /// @dev Warning: No two-step transfer ownership.
    /// @dev Warning: The owner can be set to the zero address.
    function setOwner(address newOwner) external;

    /// @notice Enables `irm` as a possible IRM for market creation.
    /// @dev Warning: It is not possible to disable an IRM.
    function enableIrm(address irm) external;

    /// @notice Enables `lltv` as a possible LLTV for market creation.
    /// @dev Warning: It is not possible to disable a LLTV.
    function enableLltv(uint256 lltv) external;

    /// @notice Sets the `newFee` for the given market `marketParams`.
    /// @param newFee The new fee, scaled by WAD.
    /// @dev Warning: The recipient can be the zero address.
    function setFee(MarketParams memory marketParams, uint256 newFee) external;

    /// @notice Sets `newFeeRecipient` as `feeRecipient` of the fee.
    /// @dev Warning: If the fee recipient is set to the zero address, fees will accrue there and will be lost.
    /// @dev Modifying the fee recipient will allow the new recipient to claim any pending fees not yet accrued. To
    /// ensure that the current recipient receives all due fees, accrue interest manually prior to making any changes.
    function setFeeRecipient(address newFeeRecipient) external;

    /// @notice Creates the market `marketParams`.
    /// @dev Here is the list of assumptions on the market's dependencies (tokens, IRM and oracle) that guarantees
    /// Morpho behaves as expected:
    /// - The token should be ERC-20 compliant, except that it can omit return values on `transfer` and `transferFrom`.
    /// - The token balance of Morpho should only decrease on `transfer` and `transferFrom`. In particular, tokens with
    /// burn functions are not supported.
    /// - The token should not re-enter Morpho on `transfer` nor `transferFrom`.
    /// - The token balance of the sender (resp. receiver) should decrease (resp. increase) by exactly the given amount
    /// on `transfer` and `transferFrom`. In particular, tokens with fees on transfer are not supported.
    /// - The IRM should not re-enter Morpho.
    /// - The oracle should return a price with the correct scaling.
    /// @dev Here is a list of assumptions on the market's dependencies which, if broken, could break Morpho's liveness
    /// properties (funds could get stuck):
    /// - The token should not revert on `transfer` and `transferFrom` if balances and approvals are right.
    /// - The amount of assets supplied and borrowed should not go above ~1e35 (otherwise the computation of
    /// `toSharesUp` and `toSharesDown` can overflow).
    /// - The IRM should not revert on `borrowRate`.
    /// - The IRM should not return a very high borrow rate (otherwise the computation of `interest` in
    /// `_accrueInterest` can overflow).
    /// - The oracle should not revert `price`.
    /// - The oracle should not return a very high price (otherwise the computation of `maxBorrow` in `_isHealthy` or of
    /// `assetsRepaid` in `liquidate` can overflow).
    /// @dev The borrow share price of a market with less than 1e4 assets borrowed can be decreased by manipulations, to
    /// the point where `totalBorrowShares` is very large and borrowing overflows.
    function createMarket(MarketParams memory marketParams) external;

    /// @notice Supplies `assets` or `shares` on behalf of `onBehalf`, optionally calling back the caller's
    /// `onMorphoSupply` function with the given `data`.
    /// @dev Either `assets` or `shares` should be zero. Most use cases should rely on `assets` as an input so the
    /// caller is guaranteed to have `assets` tokens pulled from their balance, but the possibility to mint a specific
    /// amount of shares is given for full compatibility and precision.
    /// @dev Supplying a large amount can revert for overflow.
    /// @dev Supplying an amount of shares may lead to supply more or fewer assets than expected due to slippage.
    /// Consider using the `assets` parameter to avoid this.
    /// @param marketParams The market to supply assets to.
    /// @param assets The amount of assets to supply.
    /// @param shares The amount of shares to mint.
    /// @param onBehalf The address that will own the increased supply position.
    /// @param data Arbitrary data to pass to the `onMorphoSupply` callback. Pass empty data if not needed.
    /// @return assetsSupplied The amount of assets supplied.
    /// @return sharesSupplied The amount of shares minted.
    function supply(
        MarketParams memory marketParams,
        uint256 assets,
        uint256 shares,
        address onBehalf,
        bytes memory data
    ) external returns (uint256 assetsSupplied, uint256 sharesSupplied);

    /// @notice Withdraws `assets` or `shares` on behalf of `onBehalf` and sends the assets to `receiver`.
    /// @dev Either `assets` or `shares` should be zero. To withdraw max, pass the `shares`'s balance of `onBehalf`.
    /// @dev `msg.sender` must be authorized to manage `onBehalf`'s positions.
    /// @dev Withdrawing an amount corresponding to more shares than supplied will revert for underflow.
    /// @dev It is advised to use the `shares` input when withdrawing the full position to avoid reverts due to
    /// conversion roundings between shares and assets.
    /// @param marketParams The market to withdraw assets from.
    /// @param assets The amount of assets to withdraw.
    /// @param shares The amount of shares to burn.
    /// @param onBehalf The address of the owner of the supply position.
    /// @param receiver The address that will receive the withdrawn assets.
    /// @return assetsWithdrawn The amount of assets withdrawn.
    /// @return sharesWithdrawn The amount of shares burned.
    function withdraw(
        MarketParams memory marketParams,
        uint256 assets,
        uint256 shares,
        address onBehalf,
        address receiver
    ) external returns (uint256 assetsWithdrawn, uint256 sharesWithdrawn);

    /// @notice Borrows `assets` or `shares` on behalf of `onBehalf` and sends the assets to `receiver`.
    /// @dev Either `assets` or `shares` should be zero. Most use cases should rely on `assets` as an input so the
    /// caller is guaranteed to borrow `assets` of tokens, but the possibility to mint a specific amount of shares is
    /// given for full compatibility and precision.
    /// @dev `msg.sender` must be authorized to manage `onBehalf`'s positions.
    /// @dev Borrowing a large amount can revert for overflow.
    /// @dev Borrowing an amount of shares may lead to borrow fewer assets than expected due to slippage.
    /// Consider using the `assets` parameter to avoid this.
    /// @param marketParams The market to borrow assets from.
    /// @param assets The amount of assets to borrow.
    /// @param shares The amount of shares to mint.
    /// @param onBehalf The address that will own the increased borrow position.
    /// @param receiver The address that will receive the borrowed assets.
    /// @return assetsBorrowed The amount of assets borrowed.
    /// @return sharesBorrowed The amount of shares minted.
    function borrow(
        MarketParams memory marketParams,
        uint256 assets,
        uint256 shares,
        address onBehalf,
        address receiver
    ) external returns (uint256 assetsBorrowed, uint256 sharesBorrowed);

    /// @notice Repays `assets` or `shares` on behalf of `onBehalf`, optionally calling back the caller's
    /// `onMorphoRepay` function with the given `data`.
    /// @dev Either `assets` or `shares` should be zero. To repay max, pass the `shares`'s balance of `onBehalf`.
    /// @dev Repaying an amount corresponding to more shares than borrowed will revert for underflow.
    /// @dev It is advised to use the `shares` input when repaying the full position to avoid reverts due to conversion
    /// roundings between shares and assets.
    /// @dev An attacker can front-run a repay with a small repay making the transaction revert for underflow.
    /// @param marketParams The market to repay assets to.
    /// @param assets The amount of assets to repay.
    /// @param shares The amount of shares to burn.
    /// @param onBehalf The address of the owner of the debt position.
    /// @param data Arbitrary data to pass to the `onMorphoRepay` callback. Pass empty data if not needed.
    /// @return assetsRepaid The amount of assets repaid.
    /// @return sharesRepaid The amount of shares burned.
    function repay(
        MarketParams memory marketParams,
        uint256 assets,
        uint256 shares,
        address onBehalf,
        bytes memory data
    ) external returns (uint256 assetsRepaid, uint256 sharesRepaid);

    /// @notice Supplies `assets` of collateral on behalf of `onBehalf`, optionally calling back the caller's
    /// `onMorphoSupplyCollateral` function with the given `data`.
    /// @dev Interest are not accrued since it's not required and it saves gas.
    /// @dev Supplying a large amount can revert for overflow.
    /// @param marketParams The market to supply collateral to.
    /// @param assets The amount of collateral to supply.
    /// @param onBehalf The address that will own the increased collateral position.
    /// @param data Arbitrary data to pass to the `onMorphoSupplyCollateral` callback. Pass empty data if not needed.
    function supplyCollateral(MarketParams memory marketParams, uint256 assets, address onBehalf, bytes memory data)
        external;

    /// @notice Withdraws `assets` of collateral on behalf of `onBehalf` and sends the assets to `receiver`.
    /// @dev `msg.sender` must be authorized to manage `onBehalf`'s positions.
    /// @dev Withdrawing an amount corresponding to more collateral than supplied will revert for underflow.
    /// @param marketParams The market to withdraw collateral from.
    /// @param assets The amount of collateral to withdraw.
    /// @param onBehalf The address of the owner of the collateral position.
    /// @param receiver The address that will receive the collateral assets.
    function withdrawCollateral(MarketParams memory marketParams, uint256 assets, address onBehalf, address receiver)
        external;

    /// @notice Liquidates the given `repaidShares` of debt asset or seize the given `seizedAssets` of collateral on the
    /// given market `marketParams` of the given `borrower`'s position, optionally calling back the caller's
    /// `onMorphoLiquidate` function with the given `data`.
    /// @dev Either `seizedAssets` or `repaidShares` should be zero.
    /// @dev Seizing more than the collateral balance will underflow and revert without any error message.
    /// @dev Repaying more than the borrow balance will underflow and revert without any error message.
    /// @dev An attacker can front-run a liquidation with a small repay making the transaction revert for underflow.
    /// @param marketParams The market of the position.
    /// @param borrower The owner of the position.
    /// @param seizedAssets The amount of collateral to seize.
    /// @param repaidShares The amount of shares to repay.
    /// @param data Arbitrary data to pass to the `onMorphoLiquidate` callback. Pass empty data if not needed.
    /// @return The amount of assets seized.
    /// @return The amount of assets repaid.
    function liquidate(
        MarketParams memory marketParams,
        address borrower,
        uint256 seizedAssets,
        uint256 repaidShares,
        bytes memory data
    ) external returns (uint256, uint256);

    /// @notice Executes a flash loan.
    /// @dev Flash loans have access to the whole balance of the contract (the liquidity and deposited collateral of all
    /// markets combined, plus donations).
    /// @dev Warning: Not ERC-3156 compliant but compatibility is easily reached:
    /// - `flashFee` is zero.
    /// - `maxFlashLoan` is the token's balance of this contract.
    /// - The receiver of `assets` is the caller.
    /// @param token The token to flash loan.
    /// @param assets The amount of assets to flash loan.
    /// @param data Arbitrary data to pass to the `onMorphoFlashLoan` callback.
    function flashLoan(address token, uint256 assets, bytes calldata data) external;

    /// @notice Sets the authorization for `authorized` to manage `msg.sender`'s positions.
    /// @param authorized The authorized address.
    /// @param newIsAuthorized The new authorization status.
    function setAuthorization(address authorized, bool newIsAuthorized) external;

    /// @notice Sets the authorization for `authorization.authorized` to manage `authorization.authorizer`'s positions.
    /// @dev Warning: Reverts if the signature has already been submitted.
    /// @dev The signature is malleable, but it has no impact on the security here.
    /// @dev The nonce is passed as argument to be able to revert with a different error message.
    /// @param authorization The `Authorization` struct.
    /// @param signature The signature.
    function setAuthorizationWithSig(Authorization calldata authorization, Signature calldata signature) external;

    /// @notice Accrues interest for the given market `marketParams`.
    function accrueInterest(MarketParams memory marketParams) external;

    /// @notice Returns the data stored on the different `slots`.
    function extSloads(bytes32[] memory slots) external view returns (bytes32[] memory);
}

/// @dev This interface is inherited by Morpho so that function signatures are checked by the compiler.
/// @dev Consider using the IMorpho interface instead of this one.
interface IMorphoStaticTyping is IMorphoBase {
    /// @notice The state of the position of `user` on the market corresponding to `id`.
    /// @dev Warning: For `feeRecipient`, `supplyShares` does not contain the accrued shares since the last interest
    /// accrual.
    function position(Id id, address user)
        external
        view
        returns (uint256 supplyShares, uint128 borrowShares, uint128 collateral);

    /// @notice The state of the market corresponding to `id`.
    /// @dev Warning: `totalSupplyAssets` does not contain the accrued interest since the last interest accrual.
    /// @dev Warning: `totalBorrowAssets` does not contain the accrued interest since the last interest accrual.
    /// @dev Warning: `totalSupplyShares` does not contain the accrued shares by `feeRecipient` since the last interest
    /// accrual.
    function market(Id id)
        external
        view
        returns (
            uint128 totalSupplyAssets,
            uint128 totalSupplyShares,
            uint128 totalBorrowAssets,
            uint128 totalBorrowShares,
            uint128 lastUpdate,
            uint128 fee
        );

    /// @notice The market params corresponding to `id`.
    /// @dev This mapping is not used in Morpho. It is there to enable reducing the cost associated to calldata on layer
    /// 2s by creating a wrapper contract with functions that take `id` as input instead of `marketParams`.
    function idToMarketParams(Id id)
        external
        view
        returns (address loanToken, address collateralToken, address oracle, address irm, uint256 lltv);
}

/// @title IMorpho
/// @author Morpho Labs
/// @custom:contact [email protected]
/// @dev Use this interface for Morpho to have access to all the functions with the appropriate function signatures.
interface IMorpho is IMorphoBase {
    /// @notice The state of the position of `user` on the market corresponding to `id`.
    /// @dev Warning: For `feeRecipient`, `p.supplyShares` does not contain the accrued shares since the last interest
    /// accrual.
    function position(Id id, address user) external view returns (Position memory p);

    /// @notice The state of the market corresponding to `id`.
    /// @dev Warning: `m.totalSupplyAssets` does not contain the accrued interest since the last interest accrual.
    /// @dev Warning: `m.totalBorrowAssets` does not contain the accrued interest since the last interest accrual.
    /// @dev Warning: `m.totalSupplyShares` does not contain the accrued shares by `feeRecipient` since the last
    /// interest accrual.
    function market(Id id) external view returns (Market memory m);

    /// @notice The market params corresponding to `id`.
    /// @dev This mapping is not used in Morpho. It is there to enable reducing the cost associated to calldata on layer
    /// 2s by creating a wrapper contract with functions that take `id` as input instead of `marketParams`.
    function idToMarketParams(Id id) external view returns (MarketParams memory);
}

// src/interfaces/IMorphoCallbacks.sol

/// @title IMorphoLiquidateCallback
/// @notice Interface that liquidators willing to use `liquidate`'s callback must implement.
interface IMorphoLiquidateCallback {
    /// @notice Callback called when a liquidation occurs.
    /// @dev The callback is called only if data is not empty.
    /// @param repaidAssets The amount of repaid assets.
    /// @param data Arbitrary data passed to the `liquidate` function.
    function onMorphoLiquidate(uint256 repaidAssets, bytes calldata data) external;
}

/// @title IMorphoRepayCallback
/// @notice Interface that users willing to use `repay`'s callback must implement.
interface IMorphoRepayCallback {
    /// @notice Callback called when a repayment occurs.
    /// @dev The callback is called only if data is not empty.
    /// @param assets The amount of repaid assets.
    /// @param data Arbitrary data passed to the `repay` function.
    function onMorphoRepay(uint256 assets, bytes calldata data) external;
}

/// @title IMorphoSupplyCallback
/// @notice Interface that users willing to use `supply`'s callback must implement.
interface IMorphoSupplyCallback {
    /// @notice Callback called when a supply occurs.
    /// @dev The callback is called only if data is not empty.
    /// @param assets The amount of supplied assets.
    /// @param data Arbitrary data passed to the `supply` function.
    function onMorphoSupply(uint256 assets, bytes calldata data) external;
}

/// @title IMorphoSupplyCollateralCallback
/// @notice Interface that users willing to use `supplyCollateral`'s callback must implement.
interface IMorphoSupplyCollateralCallback {
    /// @notice Callback called when a supply of collateral occurs.
    /// @dev The callback is called only if data is not empty.
    /// @param assets The amount of supplied collateral.
    /// @param data Arbitrary data passed to the `supplyCollateral` function.
    function onMorphoSupplyCollateral(uint256 assets, bytes calldata data) external;
}

/// @title IMorphoFlashLoanCallback
/// @notice Interface that users willing to use `flashLoan`'s callback must implement.
interface IMorphoFlashLoanCallback {
    /// @notice Callback called when a flash loan occurs.
    /// @dev The callback is called only if data is not empty.
    /// @param assets The amount of assets that was flash loaned.
    /// @param data Arbitrary data passed to the `flashLoan` function.
    function onMorphoFlashLoan(uint256 assets, bytes calldata data) external;
}

// src/IMFMorphoMigrator.sol

contract IMFMorphoMigrator is IMorphoFlashLoanCallback {
    IMorphoBase public constant MORPHO = IMorphoBase(0xBBBBBbbBBb9cC5e90e3b3Af64bdAF62C37EEFFCb);
    IMorphoStaticTyping public immutable MORPHO_VIEW = IMorphoStaticTyping(address(MORPHO));

    bytes32 private _expectedCall;

    error Zero();
    error TokenMismatch();
    error NotLowerLLTV();
    error NoDebt();
    error NoCollateral();
    error NotMorpho();
    error Unauthorized();

    enum FlashMode {
        Collateral,
        Debt
    }

    struct MigrationArgs {
        MarketParams oldMarket;
        MarketParams newMarket;
        address borrower;
        uint256 debtAssetsNeeded;
        uint128 borrowShares;
        uint128 collateralToMove;
        FlashMode mode;
    }

    /* CONSTRUCTOR */
    constructor() {}

    /* PUBLIC FUNCTIONS */
    function migratePositionExact(
        MarketParams calldata oldMarket,
        MarketParams calldata newMarket,
        FlashMode mode,
        Authorization calldata grantAuth,
        Signature calldata grantSig,
        Authorization calldata revokeAuth,
        Signature calldata revokeSig
    ) external {
        // 1) safety checks
        if (msg.sender == address(0)) revert Zero();
        if (grantAuth.authorizer != msg.sender) revert Unauthorized();
        if (grantAuth.authorized != address(this)) revert Unauthorized();
        if (!grantAuth.isAuthorized) revert Unauthorized();
        if (grantAuth.deadline < block.timestamp) revert Unauthorized();
        if (revokeAuth.authorizer != msg.sender) revert Unauthorized();
        if (revokeAuth.authorized != address(this)) revert Unauthorized();
        if (revokeAuth.isAuthorized) revert Unauthorized();
        if (revokeAuth.deadline < block.timestamp) revert Unauthorized();
        uint256 currentNonce = MORPHO.nonce(msg.sender);
        if (grantAuth.nonce != currentNonce) revert Unauthorized();
        if (revokeAuth.nonce != currentNonce + 1) revert Unauthorized();

        // 2) grant auth
        MORPHO.setAuthorizationWithSig(grantAuth, grantSig);

        // 2) ensure markets align
        if (oldMarket.loanToken != newMarket.loanToken || oldMarket.collateralToken != newMarket.collateralToken) {
            revert TokenMismatch();
        }
        if (newMarket.lltv >= oldMarket.lltv) revert NotLowerLLTV();

        MORPHO.accrueInterest(oldMarket);
        Id oldId = _id(oldMarket);

        (, uint128 borrowShares, uint128 collateralAmt) = MORPHO_VIEW.position(oldId, msg.sender);
        if (collateralAmt == 0) revert NoCollateral();

        (, , uint128 tBorrowAssets, uint128 tBorrowShares, ,) = MORPHO_VIEW.market(oldId);
        uint256 debtAssetsNeeded = _toAssetsUp(uint256(borrowShares), uint256(tBorrowAssets), uint256(tBorrowShares));

        // 3) if no debt, just withdraw and supply, if debt exists then flash
        if (debtAssetsNeeded == 0) {
            MORPHO.withdrawCollateral(oldMarket, collateralAmt, msg.sender, address(this));
            _approve(newMarket.collateralToken, address(MORPHO), collateralAmt);
            MORPHO.supplyCollateral(newMarket, collateralAmt, msg.sender, "");
        } else {
            bytes memory payload = abi.encode(
                MigrationArgs({
                    oldMarket: oldMarket,
                    newMarket: newMarket,
                    borrower: msg.sender,
                    debtAssetsNeeded: debtAssetsNeeded,
                    borrowShares: borrowShares,
                    collateralToMove: collateralAmt,
                    mode: mode
                })
            );
            _expectedCall = keccak256(payload);

            if (mode == FlashMode.Collateral) {
                MORPHO.flashLoan(oldMarket.collateralToken, uint256(collateralAmt), payload);
            } else {
                MORPHO.flashLoan(oldMarket.loanToken, uint256(debtAssetsNeeded), payload);
            }

            _expectedCall = bytes32(0);
        }

        // 4) revoke sig
        MORPHO.setAuthorizationWithSig(revokeAuth, revokeSig);
    }

    /// @dev Morpho sends tokens, calls this, then pulls them back via transferFrom.
    function onMorphoFlashLoan(uint256 assets, bytes calldata data) external override {
        if (msg.sender != address(MORPHO)) revert NotMorpho();
        if (_expectedCall == bytes32(0) || keccak256(data) != _expectedCall) revert Unauthorized();

        MigrationArgs memory m = abi.decode(data, (MigrationArgs));
        address loanToken = m.oldMarket.loanToken;
        address collToken = m.oldMarket.collateralToken;

        if (m.mode == FlashMode.Collateral) {
            // ==== Collateral-flash mode ====
            if (assets < uint256(m.collateralToMove)) revert NoCollateral();

            // 1) supply flashed collateral to NEW
            _approve(collToken, address(MORPHO), assets);
            MORPHO.supplyCollateral(m.newMarket, assets, m.borrower, "");

            // 2) borrow loan token on NEW to cover old debt
            MORPHO.borrow(m.newMarket, m.debtAssetsNeeded, 0, m.borrower, address(this));

            // 3) repay OLD debt in shares to avoid rounding shortfalls
            _approve(loanToken, address(MORPHO), m.debtAssetsNeeded);
            MORPHO.repay(m.oldMarket, 0, uint256(m.borrowShares), m.borrower, "");

            // 4) withdraw user's collateral from OLD to this contract
            //    NOTE: if you're "last" and withdrawable < expected, this would fail.
            MORPHO.withdrawCollateral(m.oldMarket, uint256(m.collateralToMove), m.borrower, address(this));

            // 5) repay collateral flash-loan
            _approve(collToken, address(MORPHO), assets);
        } else {
            // ==== Debt-flash mode (safe when you're last) ====
            if (assets < m.debtAssetsNeeded) revert NoDebt();

            // 1) repay OLD debt immediately using flashed loan token
            _approve(loanToken, address(MORPHO), m.debtAssetsNeeded);
            MORPHO.repay(m.oldMarket, 0, uint256(m.borrowShares), m.borrower, "");

            // 2) withdraw ALL user's collateral from OLD to this contract
            MORPHO.withdrawCollateral(m.oldMarket, uint256(m.collateralToMove), m.borrower, address(this));

            // 3) supply that collateral to NEW
            _approve(collToken, address(MORPHO), m.collateralToMove);
            MORPHO.supplyCollateral(m.newMarket, m.collateralToMove, m.borrower, "");

            // 4) borrow loan token on NEW and let Morpho pull it back for the flash-loan repayment
            MORPHO.borrow(m.newMarket, m.debtAssetsNeeded, 0, m.borrower, address(this));
            _approve(loanToken, address(MORPHO), m.debtAssetsNeeded);
        }
    }

    /* PRIVATE FUNCTIONS */
    function _id(MarketParams memory p) internal pure returns (Id) {
        return Id.wrap(keccak256(abi.encode(p)));
    }

    function _toAssetsUp(uint256 shares, uint256 totalAssets, uint256 totalShares) internal pure returns (uint256) {
        if (shares == 0 || totalShares == 0) return 0;
        uint256 num = shares * totalAssets;
        return (num + totalShares - 1) / totalShares;
    }

    function _approve(address token, address spender, uint256 amount) internal {
        if (token == address(0)) return;
        IERC20(token).approve(spender, amount);
    }
}

Read Contract

MORPHO 0x3acb5624 → address
MORPHO_VIEW 0xd4b084f0 → address

Write Contract 2 functions

These functions modify contract state and require a wallet transaction to execute.

migratePositionExact 0xb2989aec
tuple oldMarket
tuple newMarket
uint8 mode
tuple grantAuth
tuple grantSig
tuple revokeAuth
tuple revokeSig
onMorphoFlashLoan 0x31f57072
uint256 assets
bytes data

Top Interactions

AddressTxnsSentReceived
0x709D2591...F9a5 1 1

Recent Transactions

CSV
|
Hash Method Block Age From/To Value Txn Fee Type
0x36780e57...852328 0x28bd4bcf 24,314,188 IN 0x709D2591...F9a5 0 ETH 0.000076011498 ETH EIP-1559