preface

I’ve been working on Compound for a while, made a few detours and waded through some pits, and finally gnawed it off. Recently, some friends have been asking me some questions, so IN line with the good tradition of charity, I decided to share what I have learned in words. I’m going to cover this in several articles: overview, Contract, Subgraph, Liquidation, and Extension.

So, let’s start with the overview.

DeFi

DeFi, short for Decentralized Finance, is a development field Decentralized by the intersection of blockchain, digital assets and financial services. DeFi is a generic term for decentralized applications (Dapps) that provide financial services on the settlement layer of a blockchain, including payments, loans, transactions, investments, insurance and asset management.

The DeFi market has experienced explosive growth since 2020. According to data platform DeFi Pulse, the ** total lock-in value (TVL) ** of digital assets served by DeFi grew from less than us $1 billion in 2019 to US $15 billion by the end of 2020, peaking again at US $88 billion in mid-May 2021. However, this is still a fraction of traditional finance, so DeFi is still at a very early stage.

Traditional finance relied on centralized financial institutions, such as banks. But the financial crisis of 2008 led to the collapse of Washington Mutual, which had more than $188bn in deposits, and Lehman Brothers, which had more than $639bn in assets. Since 2008, there has been growing concern about the inefficiencies, structural inequalities and potential risks of the existing financial system. More recently, controversies such as GameStop’s short sale, in which retail investors are banned from trading during periods of market volatility, have highlighted other flaws in traditional financial infrastructure: slow settlement cycles, inefficient price discovery, liquidity challenges and a lack of assurance over underlying assets. DeFi aims to address some of these challenges, although these issues are also present in the current DeFi ecosystem. DeFi uses blockchain technology to facilitate the substitution of traditional service providers and market structures, which has the potential to improve the efficiency of financial markets.

In the entire DeFi industry, there are already many Dapps providing services, mainly including: Stabelcoins, DEX, Lending, Derivatives, Insurance, Asset Management, and ancillary services, Such as wallets and Oracle. In each subsector, there are already multiple service providers and many products providing a variety of services, and the entire DeFi industry has presented a picture of a hundred flowers blooming.

borrowing

At the time of writing, the total debt outstanding is $1.5 billion, according to DeFi Pulse, and there are 28 loan products included. There are many more that are not. Among them, the top three lending products are Compound, Aave and Maker, with loans of 528 million, 510 million and 479 million, respectively. The sum of the three products has occupied more than 99% of the whole market. Of course, compared to the traditional lending market, it’s not even a fraction of that, which is why new players are constantly coming in to grab the pie.

At present, the lending platforms in DeFi are basically mortgage loans, by overmortgaging one digital asset to lend another asset, such as US $2,000 worth of ETH and US $1,800 worth of USDT. This is similar to a traditional mortgage, except that a traditional mortgage essentially takes an illiquid asset — a house, a car, a piece of land — and uses it as collateral to lend money to a highly liquid asset. However, DeFi’s mortgages, mortgaged assets and lent assets are highly liquid assets. Why can there be a market for this? It’s a little hard to understand at first glance. However, because of the high mortgage interest rate of DeFi lending products and the huge yield in the early stage of DeFi market, high market funds have been attracted. For example, In Compound’s liquid mining model, users can get nt dollars for mining even if they borrow money. The net profit after deducting the interest is still positive, which is why many users are attracted to participate in it.

At present, there are several main demands in the DeFi loan market:

  1. Meet the capital needs of trading activities: including arbitrage, leverage, market making and other trading activities, which is the most important rigid needs.
  2. Passive income: Mainly for investors who hold digital assets for a long time and want to generate additional income.
  3. Access to liquidity: There is a need for short-term liquidity mainly for miners or start-ups in some industries.

The third point is more in line with the logic of traditional lending business, but in the DeFi lending market, the proportion is relatively low. The main demand is still the first case. For example, if I have ETH and I believe that ETH will increase in the next few years, I can mortgage my ETH and lend USDT to buy more ETH, and then I can remortgage the ETH and lend some USDT. Buy more ETH. In this way, a share of capital can also obtain a number of benefits. In essence, this demand is to increase leverage, which is why a number of DeFi products have started to provide directly leveraged mining and leveraged trading services.

The main disadvantage of DeFi loan is low utilization of funds. Credit loans would obviously be more efficient than mortgages, but in the current environment of blockchain anonymity, this is difficult to achieve. Aave’s lightning loan is the first to realize the unsecured loan in the world of blockchain. It takes advantage of the unique characteristics of blockchain to realize the loan and repayment within a block, otherwise it will be invalid. The application scenarios are very limited, and there is a technical threshold, ordinary users cannot participate.

Compound

Compound is the leader in the current loan market. According to our research, many similar products are modified based on Compound. I have studied Compound deeply, from products to techniques.

The Compound model is simple: a pool of capital is set up for each loan asset, from which withdrawals, loans and withdrawals flow in and out. To understand Compound’s business logic, several related concepts also need to be understood:

  • Underlying tokens: The lending assets, such as ETH, USDT, USDC, and WBTC, of which only 14 have been established.
  • CToken: Also known as interest-bearing token, is the user’s certificate to deposit assets on Compound. Each underlying asset has a corresponding cToken. For example, ETH corresponds to cETH, and USDT corresponds to cUSDT. CETH will be returned when the user stores ETH to the Compound. When withdrawing money, you can exchange the cToken for the underlying asset.
  • Exchange Rate: The Exchange Rate between cToken and the underlying asset. For example, the Exchange Rate of cETH is 0.02, that is, one cETH can be exchanged for 0.02 ETH. The exchange rate increases over time, so holding a cToken is equal to earning interest, hence the name interest-bearing token. The calculation formula is: exchangeRate = (totalCash + Totalborrows-Totalreserves)/totalSupply
  • Collateral Factor: Each underlying asset has a Collateral Factor which represents the ratio of the value of the asset pledged to the available loan. This is used as a measure of the amount of Collateral available. The value ranges from 0 to 1. When the value is 0, the asset cannot be used as collateral to borrow other assets. Generally, the maximum is set at 0.75. For example, for ETH, if the user deposits 0.1 ETH and opens it as collateral, and the VALUE of ETH at that time is $2000, the loan limit is 0.1 * 2000 * 0.75 = $150, and the maximum value of other assets is $150.

After the user deposits the underlying asset, Compound returns the cToken corresponding to the underlying asset to the user based on the exchange rate, which serves as a deposit certificate. When the deposit needs to be redeemed, the cToken is returned, and Compound calculates the amount of the underlying asset to be redeemed based on the latest exchange rate and returns it to the user. For example, if the exchange rate of one ETH is 0.1 at that time, 1/0.1 = 10 ceThs will be returned. When the exchange rate is 0.15, the 10 ceths can be redeemed by 10*0.15 = 1.5 ETH. The extra 0.5 ETH is the interest income.

When the user opens the deposited assets as collateral, it is ready to borrow. However, not all assets can be used as collateral. For example, USDT cannot be used as collateral. Its collateral factor is 0. The mortgage factor of ETH, DAI and USDC is 0.75, that is, 75%, which means that the mortgage asset is worth $100, and the borrowing limit is $75, that is, the digital asset can be lent up to $75. However, it is not recommended to use up all the quota, otherwise, there is a risk of liquidation.

When the value of the loan exceeds the amount borrowed, it can be liquidated. However, smart contracts cannot be cleared automatically, so an external liquidator needs to call the smart contract’s clearing function to perform the clearing. In order to motivate the third-party liquidator to perform the liquidation, there is a liquidation incentive, which is borne by the liquidator (i.e., the borrower). In addition, the liquidator is generally undertaken by the procedural liquidation service.

Compound liquidation is a form of repayment on behalf of the borrower, in which the liquidator helps the borrower repay part of the loan and receives a mortgage asset equal to the value of the repaid asset, plus a certain percentage of the liquidation incentive, which is also a mortgage asset. For example, borrower A has A loan of 100 USDT and the collateral is ETH; In the case of liquidating A, liquidator B needs to help A repay part of the payment. At present, Compound can reimburse A up to 50% of the payment per settlement, so B can reimburse A 50 USDT, assuming that the liquidator incentive is 0.05. Then B can get the value of A as 50 * (1 + 0.05) = 52.5 USDT ETH.

The interest rate model

The Compound has two main interest rate models. The first, which I call linear, looks like this:

The black line on the horizontal axis represents the utilization rate, the purple line represents the borrowing rate, and the green line represents the deposit rate.

The Compound used this model in its early days, and it still has a few assets.

Utilization rate is the utilization rate of funds, and the calculation formula is:

utilizationRate = borrows / (cash + borrows - reserves)
Copy the code

Among them, borrows stands for the amount borrowed, cash stands for the balance of the capital pool and reserves stands for reserves. The borrowing rate is determined by the utilization rate and the deposit rate is determined by the borrowing rate.

In essence, the utilization rate is a quantitative index reflecting the supply and demand of loans. Low utilization rate indicates that there are more deposits, but too little loans, that is, the supply is greater than the demand. At this point, users need to be encouraged to borrow more and deposit less, so borrowing rates are low, and deposit rates are also low. When utilization is high, on the contrary, borrowing and deposit rates are high, which encourages people to save more and borrow less.

A high utilization rate means that there is less money left in the pool and there is a risk that the pool will dry up. If the pool of capital dries up, depositors will have no money to borrow and no money to borrow, which could lead to systemic risk. The general fund utilization rate is safer within 80%.

To keep rates within safe limits, The Compound has since upgraded its model to what I call inflection points, as shown below:

As you can see, at the inflection point of 80% utilization, the slope increases sharply, and the very high borrowing rate generally reduces the demand for borrowing, while the very high deposit rate encourages everyone to increase supply, which effectively reduces the overall utilization rate and prevents the pool of capital from drying up.

At present, the Compound uses this rate model for most of its pools.

The technical architecture

At the technical level, the overall structure of the Compound lending system is as follows:

I will briefly introduce each module in the figure:

  • Ethereum: The underlying blockchain system. Compound is deployed on Ethereum, but it can also be deployed on Heco, BSC and other chains with minor modifications.
  • Smart Contracts: All core businesses are implemented using smart contracts, and the code is open source.
  • PriceOracle: A price predictor that regularly sets the price of those digital assets to a smart contract, so that the smart contract can take the asset price and do some calculations accordingly. Compound uses its own Open Price Feed as a Price predictor.
  • Subgraph: Data indexing service that is deployed to the Graph node. It provides front-end and clearing services with some structured data query capabilities that are easier and faster than looking up data on the chain.
  • Clearing service: assume the role of the liquidator, will regularly query the account to be cleared and call the smart contract to perform the clearing operation.
  • Wallet: Wallet tool such as MetaMask.
  • Web client: front-end page for ordinary users, integrate ApolloClient to connect to Subgraph, and then connect to MetaMask and other wallets through Web3 API for operation. Some data can not be read from Subgraph, but need to directly query contract data.

conclusion

So much for DeFi, lending and Compound, the overview is mainly a general introduction, including some current situation and prospects of DeFi and lending market, as well as some basic concepts and core business logic of Compound product level. And a preliminary understanding of the overall technical framework of the Compound.

The technical implementation of each module will be further deepened in the future. Please look forward to it!