Delta Mechanism

If you haven't read it yet, go back and read the fallback mechanism and matching engine sections, it is of great help to understand the delta mechanism.
In the fallback mechanism section, we saw that Morpho has proper guardrails to ensure that users' assets remain liquid. But what if a supplier matched P2P tries to withdraw his funds, and the transaction consumes a lot of gas while iterating through the matching engine? Morpho's team designed the delta mechanism to address this issue and guarantee that funds remain liquid in all situations.
To limit gas consumption to a finite size, Morpho must instantly stop the matching engine at any time. This is feasible with the fallback mechanism, but in Morpho's book, users won't be adequately unmatched and would earn the P2P APY while they should be earning the Pool APY. There is a delta between what is matched P2P and what should be matched P2P if there was no maximum gas consumption. The difference is brought to the underlying pool.
  • In case of excess liquidity (incompletely handled repay), Morpho supplies what remains to repay on the pool. Morpho benefits from the underlying protocol's pool supply yield. In contrast, some lenders are still stored as being matched with the repayer, thus getting a higher yield than the yield accrued by Morpho.
  • In case of excess debt (incompletely handled withdrawal), Morpho borrows what remains to withdraw from the pool. Morpho is paying the underlying protocol's pool borrowing yield. Some borrowers are still stored as being matched with the withdrawer, thus paying a lower yield than Morpho's actual yield.
To cover this cost, the delta mechanism applies a spread in the P2P APY, depending on the ratio of excess liquidity/excess debt over the amount virtually supplied/borrowed in peer-to-peer. This mechanism is formally proven in Morpho's Yellow Paper which will be published later.
Morpho uses a linear complexity algorithm: a P2P matching engine, with a constant complexity algorithm: a lending Pool. Thanks to the Delta Mechanism, the resulting algorithm is of constant complexity, which fits the Blockchain's constraints. The maxGasForMatching parameter defines how much economic efficiency Morpho is capable of versus how much gas (i.e., complexity) it consumes.