msfew

Posted on Nov 08, 2023Read on Mirror.xyz

Marine: Compound Liquidation Keeper with zkGraph

TL;DR

  • Hyper Oracle has successfully implemented Marine: Compound Liquidation Keeper with zkGraph.

  • Marine fetches data from 92 different events from 19 different smart contracts, computes for liquidatable accounts, and initiates onchain liquidation call. All these are performed in a trustless and programmable way, leveraging the capabilities of zkOracle and zkGraph.

  • This project is supported by the Compound Grants Program. For further details on the grant, please visit our project page.

0. Background

a) Compound

Compound Protocol is a leading permissionless DeFi protocol for supplying and borrowing assets. Compound is one of the top 3 lending protocols in Total Value Locked (TVL).

The workflow of Compound is as follows: Accounts can supply capital in Ether or ERC-20 tokens to receive cTokens, or borrow assets from the protocol by holding other assets as collateral.

Source: https://medium.com/compound-finance/borrowing-assets-from-compound-quick-start-guide-f5e69af4b8f4

b) Liquidation Keeper

Liquidation is a core component of DeFi lending protocols:

  • Definition in DeFi - a process of selling assets or cryptocurrency from an under-collateralized loan to other stablecoins or fiat. This liquidation can be either forced or voluntary, depending on the loan conditions.

  • Definition in MEV - a process that occurs when a borrower's collateral value does not adequately cover their loan/debt value.

Source: https://info.etherscan.com/explanation-on-defi-liquidation/

We can also see dashboards, such as bad-debt by Risk DAO or Gauntlet Network’s dashboard, dedicated to the collection of Liquidations At Risk / Bad Debts for the DeFi protocol.

Source: https://risk.gauntlet.network/protocols/compound

In order to maintain decentralization, DeFi protocols often encourage the community to run the liquidation process. As a result, a liquidation keeper was created.

The Liquidation keeper's main tasks include:

  • Search: Based on the design of the DeFi protocol, it searches for liquidatable accounts.

  • Liquidate: If there is a profit to be made from the liquidation of these assets, then an attempt is made to capture this profit.

These keepers are primarily driven by spontaneous profit-seeking behavior, which helps protect the stability and decentralization of the entire DeFi protocol.

c) Compound Liquidation

Compound has also demonstrated a great deal of resourcefulness and hard work in the field of liquidation modules for the protocol:

These resources have greatly assisted in our development.

1. Compound Liquidation Keeper with zkGraph

a) zkGraph and zkOracle

Hyper Oracle is a programmable zkOracle protocol. We aim to unlock historical data access and complex compute for smart contracts in a trustless and secure way.

zkOracle expands the computational capabilities of smart contracts. Any DApp that uses a zkOracle in their architecture is a zkDApp that has more potential than a traditional DApp.

To program a zkOracle, you can write code in zkGraph (a piece of code for custom logic) to define your own “API”. Also, we provide zkGraph Standards, including zkAutomation (smart contract automation of zkGraph), so you can use it as a template to power your smart contract to achieve the ability of end-to-end trustless automation.

You can simply understand zkOracle and zkGraph as:

  • zkOracle: the node of Hyper Oracle

  • zkGraph: the program run in a zkOracle node

b) Implementation

Our application Marine is an implementation of a Compound Liquidation Keeper based on a zkOracle. Marine is implemented as a complete zkGraph.

In general, Marine uses the trustless historical data obtained by the zkOracle to determine whether a user's address needs to be liquidated and, if so, initiate an onchain transaction through zkAutomation to perform the liquidation operation.

In terms of the detailed workflow, Marine achieves the following:

  1. Collect events for a specific contract at any block through zkOracle:

    • Monitor ComptrollerG7 contract to determine if the closeFactor has changed.

    • Monitor cToken contract events to sense changes in user pledge and loan amount.

    • Monitor the onchain price of tokens on Uniswap to get the latest price of underlying assets.

  2. Through a series of compute, count the total value of the collateral and the total value of the debt of the user's address

  3. Determine if totalValue < totalPrincipal, if true, liquidatable.

  4. If liquidatable, the zkGraph is executed and the zkOracle generates the corresponding zero-knowledge proof.

  5. Once the proof is generated and verified on Ethereum (finished within 12 seconds), an onchain liquidation is triggered.

You can check out the source code here: https://github.com/hyperoracle/Marine.

c) Features

Historical Data Access

zkOracle brings historical data access to Marine.

As a result, Marine can look back at any historical block of data and calculate all the debt and collateral of a user to get the liquidation health of an address.

Expansive Computation

With zkGraph, Marine has unlimited computational power without onchain overhead.

With the programmability of zkGraph and the succinct validity proof of zkOracle, Marine fetches, processes and analyzes large amounts of data. This cannot be done efficiently in an EVM.

Multi-Event & Multi-Source Access

zkOracle supports unlimited multi-source and multi-event data access. These rich data sources can be processed by zkGraph.

Marine’s zkGraph fetches data from 92 different Events from 19 different smart contracts, including but not limited to: Mint() and Borrow() for cToken and Sync() for underlying asset.

d) Other Advantages

Trustless and Secure Compute

The computational validity and security of zkOracle's data access and computations on zkOracle data in zkGraph are provided by zero-knowledge proofs. These proofs are being verified on Ethereum mainnet.

Therefore, there is no need for any additional trust and therefore Marine is trustless and secure.

Diversity Added to Compound

The liquidation keeper is an important part of the DeFi protocol and ensures the health of the DeFi protocol.

Similar to client diversity, keeper diversity is an important yet often overlooked factor. For example, in MakerDAO MIP-63, MakerDAO runs four different keeper networks to ensure keeper diversity, thus achieving high fault tolerance and decentralization.

As a community-run keeper, Marine has a high level of diversity.

  • Architecture: Uses a zkOracle-based implementation.

  • Technology Stack: Uses Halo2 for ZK system and AssemblyScript as development language, not just a Solidity contract.

  • Operation: Runs with zkAutomation.

Privacy

With the technology of ZK, it is feasible to add a privacy feature to Marine’s computation.

Another major difference between Marine and a Solidity-based liquidator is the possibility to implement privacy features on the data itself and the data processing strategy. This is similar to how privacy prevents MEV behaviors like copy-tx-then-front-running.

e) Onchain Liquidation Catches

We have deployed Marine into the arena, and by running Marine, we have successfully performed liquidations on mainnet Ethereum:

f) Future Steps

zkML-based Price Prediction

Marine has both rich historical data and unlimited computational power, so it can forecast asset prices through Historical Data + Machine Learning.

Price prediction can predict liquidation before a liquidatable status of account occurs onchain, thus increasing the likelihood of successful liquidation and profit.

Offchain Asset Price Feed

Currently, Marine calculates the asset price based on the onchain price in order to ensure the complete validity and finality of the computation. Compound itself provides asset prices based on Chainlink.

In the future, we can use the offchain data source function to access more price feeds, so that we can determine the asset value without the possibility of price manipulation.

Cross-chain Potential

One of the powers of zkGraph is that it can natively trigger cross-chain calls between data sources and data destinations, so Marine can implement: fetch the data from chainA, and verify the ZKP on chainB to trigger actions.

In the case of liquidation, Marine can obtain the most “accurate” asset prices with more liquidity on Ethereum mainnet, and then perform Compound's liquidation on the Alt-chain with less liquidity (where price data is not necessarily up-to-date).

2. Contribute to Compound and DeFi

We would like to thank the Compound Grants Program for their help in the development of Marine, and allthecolors as a reviewer for suggestions and insights.

With Marine and the zkOracle and zkGraph technologies underneath it, we hope to make the Compound ecosystem and DeFi as a whole more decentralized with trustless computing.


About Hyper Oracle

Hyper Oracle is a programmable zkOracle protocol that powers smart contracts with arbitrary compute and richer data sources. Hyper Oracle offers full security and decentralization for trustless automation and onchain AI/ML so builders can easily create next-gen dApps.

hyperoracle.io | x.com/hyperoracle | github.com/hyperoracle