Live on Testnet: Managing MetaMorpho Lending Market Risk with a Smart Contract

Eitan Katchka
B.Protocol
Published in
6 min readDec 20, 2023

Intro

Today we are excited to present another step in the direction of achieving B.Protocol’s vision — managing economic risk in DeFi in an automated manner with a smart contract. As we have already presented, mitigating the human factor in risk management decision-making processes will minimize its susceptibility to bias, while adding transparency and conviction in predetermined rules that are automatically executed as intended.

In this post, we demonstrate how the SmartLTV smart contract is used to manage the minimal loan-to-value ratios in MetaMorpho’s vaults. And in particular, how it prevents the vault allocator from depositing funds into markets with high-risk levels.

Setting a smart contract to manage a MetaMorpho Allocator

We have implemented a smart contract as the Allocator of a testnet MetaMorpho vault. As a reminder, each MetaMorpho vault has four distinct roles to help govern the vault: Owner, Curator, Allocator, and Guardian. The Allocator functionality is to allocate the vault’s funds between markets that the Curator already listed.

The smart contract checks the actual supply level before an allocation is made, and how the new allocation and the new derived supply will impact the risk level of the market. Based on the SmartLTV formula, it calculates the risk factor for the new supply level. If the new risk factor is higher than a pre-determined level, the allocation will be canceled due to a higher risk exposure than was intended by the Curator. The risk level threshold is set by the deployer of the allocator’s smart contract.

The Curator of a vault is responsible for setting the supply cap for each market, which limits how much of the vault’s funds can be allocated to each Morpho Blue market. But as Morpho Blue markets have no supply caps, and users (as well as other Vaults) can deposit funds directly into Morpho Blue markets, the actual supply in the market can be higher than the supply cap set by the Vault Curator. The above implementation enforces allocations to be made only under the risk exposure levels the Curator has set in an automated and predictable manner.

This signifies the first-ever application of a smart contract to autonomously handle risk management decisions on-chain!

Examples

We have implemented the Allocator smart contract in a MetaMorpho vault on testnet and used it to allocate funds to markets to demonstrate how it works.

We set the risk level limit of the market to an r value of 10. We based this value on the Compound Risk Level Index we have recently released. Compound’s overall risk index in the last 6 months fluctuates between 8–12 which can be considered as a safe level. We will soon release an in-depth analysis of the risk level settings for MetaMorpho.

Compound Risk Level Index

Let’s dive into the testing transactions of the smart contract -

Approving withdrawals

The contract never blocks withdrawals to enable the allocator to remove funds from a market that becomes too risky.

For this test, the sDAI market supply cap was set to 10B, to make sure the risk level of it is too high.

Vault state before the tx :

The transaction withdraws 5k from the USDC/sDAI market and 5k from the USDC/USDT market, though the sDAI market is too risky to deposit into. This shows the contract can withdraw whatever the current risk level is, enabling it to exit a risky market. The withdrawn supply is sent to an idle market that has no risk due to a setup of LLTV of 0, no collateral, and/or a null oracle.

Tx: https://goerli.etherscan.io/tx/0xf3a2db81219fef95a0d431150cfefef661ff66454a76e53e2817fbba3f8d8d04

Tenderly: https://dashboard.tenderly.co/tx/goerli/0xf3a2db81219fef95a0d431150cfefef661ff66454a76e53e2817fbba3f8d8d04

Vault state after the tx:

Canceling an allocation

The following transaction tries to allocate USDC from the idle market and send 5k to the sDAI market and 5k to the USDT market.

As expected, the tx reverts, giving an error that the risk level of the sDAI market is too high as the recommended LTV for the sDAI market is 0 under these market conditions.

tx: https://goerli.etherscan.io/tx/0x4e64062dc325ecb6fc4430da7c071475c9cc4baf50ef99b81c5d00cf8fb296db

tenderly: https://dashboard.tenderly.co/tx/goerli/0x4e64062dc325ecb6fc4430da7c071475c9cc4baf50ef99b81c5d00cf8fb296db

Error message for a reverted allocating tx

Verifying an allocation

This transaction takes USDC from the idle market and supplies it to the USDT market so that the USDT market should reach 20k USDC supply. It does not supply anything to the (risky) sDAI market so the Allocator smart contract does not check risk against the sDAI market and the transaction executes successfully.

Vault state before the tx:

tx: https://goerli.etherscan.io/tx/0x32b00d6706596421c39050342641dc942be11255349abe370e4177a7b231c28a

tenderly: https://dashboard.tenderly.co/tx/goerli/0x32b00d6706596421c39050342641dc942be11255349abe370e4177a7b231c28a

Vault state after the tx:

SmartLTV recap

SmartLTV is a smart contract formula that offers a simplified yet robust method for LTV ratio calculations based on quantitative data, minimizing the human factor in the process.

SmartLTV formula

The formula takes into account the following market parameters:

  • σ is the price volatility between the collateral and debt asset (normalized to the base asset price).
  • β is the liquidation bonus.
  • is the available dex liquidity with a slippage of β.
  • d is the debt cap of the borrowable asset.
  • r is a risk level factor. The higher the r is, the odds for insolvency increase.

You can find the full whitepaper for the formula here.

Future use cases

Setting risk level limits for a MetaMorpho Allocator is the first (and safe) use case for a risk management process that is made by a smart contract to demonstrate the capabilities of this new DeFi primitive.

We envision that in the future, more and more functionalities of the decision-making process of risk managers in DeFi will be transferred from humans into smart contracts. It will add transparency and conviction to the process by automating it and minimizing the human factor and the potential bias that comes with it.

The next immediate step for the smart contract risk manager could be an Allocator that withdraws funds from a market in case a risk level threshold was crossed, due to changes in supply caps, DEX liquidity, or a change in other parameters as calculated by SmatLTV.

We keep building towards a future where we could use a smart contract as a sole entity to manage risk in DeFi lending, e.g., set a smart contract as the Curator role in a MetaMorpho vault. This will enable setting the LTVs and the supply caps for the vault’s markets in an automated manner based on calculated risk exposure levels made by the SmartLTV formula. As the Curator role can put user funds at risk, potential smart contract vulnerabilities should be tested thoroughly before this can be used in production.

We will soon release an in-depth analysis of the way we used the SmartLTV formula to calculate risk levels for the MetaMorpho Flagship ETH vault we co-curate with Block Analitica. Stay tuned.

About B.Protocol

B.Protocol has been building open-source protocols and infrastructure for risk mitigation and assessment for the DeFi ecosystem since 2020. Through our research arm, RiskDAO, and its novel risk framework, we have supported over a dozen DeFi protocols with risk analysis, research, audits, and monitoring. Our Risk Oracle and SmartLTV formula automate the process of setting risk parameters for lending platforms in a transparent way, building the next generation of DeFi risk management infrastructure.

Website, Twitter, Discord, Medium, GitHub

--

--