Ensure the security of your smart contracts

Modern DeFi Lending Protocols, how it's made: Morpho Blue

Author: Sergey Boogerwooger, Pavel Morozov
Security researchers at MixBytes
Intro
The previous version of lending by the Morpho team, Morpho Optimizer, became the third-largest lending platform on Ethereum. However, as time progresses, the DeFi market demands new ideas involving less governance and more permissionless projects. Morpho Blue takes these ideas a step further by enabling the creation of immutable and isolated lending markets with reduced governance. This approach offers greater flexibility for users and projects, helping them better utilize their assets. Let's see how it's made.
Higher-level design
Morpho Blue implements permissionless lending, allowing users to create their own lending markets. Each market is an entity, containing two assets: "collateral" and "loan" tokens. Users select the necessary parameters: collateral and loan token, address of price oracle, interest rate model, and Liquidation Loan-To-Value (LLTV) and create the market. Similar to Euler or Uniswap, market creation is permissionless, and the creator of the market doesn't have access to other users' funds or control over the operations within the market.

Morpho can use any price oracle selected by the market creator, enabling the use of various types of oracles, from standard Chainlink price feeds and Uniswap TWAPs to advanced price mechanics, similar to those used in Ajna (article).

Morpho Blue's governance cannot directly modify market accessibility or its parameters but can provide new combinations of values (such as LLTV, interest rates, etc.) used for market creation by the users.

Liquidation parameters are always a tricky question; it's always a tradeoff between borrower and lender safety and incentivization. Morpho Blue uses a static Liquidation Incentive Factor (LIF) for each market, defined as:

Where maxLIF=1.15, cursor=0.3, LLTV is the current Liquidation Loan-To-Value ratio for the given market. Morpho Blue also has no closefactor, allowing the liquidation of any amount of liquidatable debt.

Morpho Blue has a compact and simple codebase, aiming to be auditable, readable and, consequently, trustless and secure.
Core
Technically, Morpho Blue is a single contract holding all the markets and providing all the necessary functions. The first stop is the Market struct, which contains information about a particular market: total amounts of supply/borrow assets, borrow/supply shares, fees, and the last timestamp update. Market parameters include addresses of collateral/debt tokens the Interest Rate Model (IRM) as a contract address, the price oracle (also as an address), and the LLTV.

The creation of a new market (function createMarket()) is straightforward. It's one of the few places where the governance intervenes, ensuring that the market being created uses an "enabled" (by governance) interest rate model (IRM) and LLTV. Another governance setup function is the setFee() function, which sets a new market fee and assigns a new fee recipient.

It’s important to mention the set of requirements for used tokens, IRMs and LLTVs, as described by Morpho Blue's team to ensure that the created market is "healthy," with no issues in the calculation of token amounts, and to maintain protocol liveness. Described here. These guidelines are valuable for any DeFi project, as rebaseable tokens, tokens with on-transfer fees or non-standard reverts, and similar "balance modifying/locking" features are always tricky and often pose problems for the protocols' security and liveness.

All functions providing supply/withdraw/borrow/repay operations in Morpho Blue accept assets or shares as parameters, allowing users to choose the most convenient parameters (tokens or shares) and simplify interaction with protocols built around Morpho Blue, such as vaults or liquidity management projects.

All these functions (including liquidations) accrue market interest at the beginning of the operations. The _accrueInterest() function is present everywhere and even has a separate external accrueInterest() function that can be called directly. The procedure for accruing interest is very clear, increasing both totalBorrowAssets and totalSupplyAssets at the rate returned by the interest rate model (IRM). The conversion to the absolute amount of accrued interest is handled by the wTaylorCompounded(x,n) function, which uses the first three non-zero terms of a Taylor expansion of eⁿˣ−1. This approach approximates accrued interest with minimal computations (a good point for DeFi developers). The market fees are collected in shares, not in supply/borrow tokens.

After applying the interest rate, all supply/withdraw/borrow/repay functions perform similar actions: calculate shares amounts, update the address' position's shares/assets amounts in the current market, update the totalSupply of shares and assets, call the user provided callback function, and perform the standard ERC20 transfer/transferFrom operations. A straightforward flow with no DeFi surprises. Users' borrowing positions are represented by the Position struct, which contains the amounts of supply/borrows shares and the direct amount of the user's collateral.

Most of the operations mentioned above in Morpho Blue can be delegated to another address. So, almost every method performing supply/withdraw/borrow/repay includes an address onBehalf parameter, allowing these operations to be carried out on behalf of other addresses.

Additionally, Morpho Blue has a flashloan capability, implemented in the really compact flashloan() function. There are no fees, and flashloans in Morpho Blue are completely free.
Oracles
Morpho Blue is an "oracle-agnostic" protocol (according to the whitepaper). This means you can use any oracle as the source of the asset price. The primary use of oracle prices is, of course, in estimating the user's health in _isHealthy(). Oracles in Morpho Blue are designed to be simple; they provide a single price() function that returns the amount of quote tokens per single unit of collateral (scaled by 1e36 to maintain precision in mul/div operations involving the oracle price).
Risk management
Handling of "unhealthy" positions in Morpho Blue is managed through standard liquidations of positions with an "unhealthy" status and is performed "per-borrower" (not per multiple positions, as done in protocols like CrvUSD LLamaLend or Fluid Vault).

The liquidate() function uses the LLTV (Liquidation Loan-To-Value) of the current market to calculate the seized assets (and shares) amounts, updates the user's position and the market's "total" values.

Next step is bad debt socialization. If all collateral of a user is depleted, but the debt still remains, Morpho Blue simply reduces the market's total borrow/supply assets and total shares. This action distributes the "bad debt" among all lenders of the given market. The last steps of the liquidation involve transferring tokens and executing a callback to the caller's contract if needed.
Implementation details
Morpho Blue utilizes well-known standards, such as ERC-4626 for operations with shares and safe ERC20 transfers. The math model in Morpho Blue doesn't require complicated computations; everything is designed to be simple, safe, and reliable.
Conclusion
Morpho Blue is a prime example of a "canonical" DeFi project - a Bible for beginner DeFi developers :) The code is extremely clear and minimalistic, all the maths are simple and reliable. Such projects achieve a high level of security because everything is built on a well-known and secure codebase, with few logical branches, corner cases, and mathematical issues. The absence of complicated algorithms keeps Morpho Blue predictable, making it a "safe landing" spot for market makers and a solid "base layer" for external protocols. Let's see what the next step in this direction will be.

Stay tuned for our upcoming articles!
Who is MixBytes?
MixBytes is a team of expert blockchain auditors and security researchers specializing in providing comprehensive smart contract audits and technical advisory services for EVM-compatible and Substrate-based projects. Join us on Twitter to stay up-to-date with the latest industry trends and insights.
Disclaimer
The information contained in this Website is for educational and informational purposes only and shall not be understood or construed as financial or investment advice.
Other posts