Categories
Crypto DeFi

DeFi lending: how the Compound protocol works

If you come from a credit/lending traditional banking background and you want to understand DeFi lending, the best place to start is the Compound protocol.

Since the appearance of Bitcoin in 2009 and Ethereum in 2015, the cryptoassets market has grown into a thriving ecosystem of users, investors, speculators, and traders who exchange hundreds of blockchain assets every day. In the last few years, new protocols arose that allow cryptoassets users to trade the time value of the asset they own. These protocols attempt to solve, in the DeFi space, a problem that typically is addressed by traditional financial institutions: lending.

If you come from a credit/lending traditional banking background, probably the best place to start is from gaining an understanding of the Compound protocol. In this post, we will review what lending is in Banking, what the principles of DeFi lending are and, more in the specific, how the Compound protocol works. The official Compound white paper presents some of the concepts we are going to discuss here more in detail – feel free to deep dive into it as a complement to this reading.

What is Lending?

One of the most fundamental aspects of any financial system is represented by the processes of lending and borrowing.

Most people are exposed to lending and borrowing at some time in their lives, generally through taking a personal loan or a mortgage. The premise is straightforward: depositors (or lenders) contribute cash to borrowers in exchange for interest on their deposits. Borrowers (or loan takers) are ready to pay interest on the amount borrowed in exchange for instant access to a lump sum of cash.

Traditionally, financial organizations such as banks or peer-to-peer lenders (eg platforms like Funding Circle and Harmoney) facilitate lending and borrowing. Money Market is the area of Finance that refers to trading in very short-term debt investments. Multiple products, such as Certificate of Deposits, municipal notes, Treasury Bills and others, are available in the money market.

In the cryptoworld, we have Decentralized Finance (DeFi) protocols, such as Aave and Compound, as well as Centralized Finance (CeFi) entities, such as BlockFi and Celsius, that offer lending services.

In Centralized Finance scenarios, entities work mostly like a bank would work: deposited assets are held in trust by third parties (like BlockFi) which loan them out to either institutional actors or users of their platform. If these entities are hacked, customers’ funds are lost. We could say that the CeFi model is philosophically far from the building principle of cryptocurrencies: asset self-custody and decentralization.

This is where DeFi lending enters the stage.

DeFi Lending

Lending using decentralized platforms allows lenders and borrowers to operate without the need of a centralized entity. DeFi lending leverages smart contracts to function: these are contracts written in code that are fulfilled on top of a blockchain, prominently Ethereum.

As we said, Compound is one of the most popular lending platforms in DeFi right now. This works by creating money markets for tokens like ETH, stablecoins like DAI and USDC, or other tokens like LINK or WBTC.

Let’s analyze how lending works in Compound (thanks to Finematics for much input on this analysis).

Users who want to become lenders, supply their tokens to a particular money market and start receiving interest on their tokens according to their current Supply APY.

As a quick refresher, APY refers to the Annual Percentage Yield: the real rate of return earned on an investment, taking into account the effect of compounding interest. Unlike simple interest, compounding interest is calculated periodically and the amount is immediately added to the balance. With each period going forward, the account balance gets a little bigger, so the interest paid on the balance gets bigger as well.

The supply tokens are sent to a smart contract and become available for other users to borrow. In exchange for supply tokens, the smart contract issues other tokens that represents the supply tokens plus interest: these tokens are called ctokens in Compound and they can be redeemed for the underlying tokens.

It is very important to note that, at this stage, in DeFi pretty much all of the loans are over-collateralized: this means that the user who wants to borrow funds has to supply tokens in the form of collateral that is worth more than the actual loan that they want to take.

Is there a limit on how much can be borrowed? The short answer is: YES.

The amount that can be borrowed depends on two main factors:

  1. How much funds are available to be borrowed in a particular market. This is usually not a problem in active markets unless someone is trying to borrow a really big amount of tokens.
  2. What is the collateral factor of supply tokens. A cToken’s collateral factor can range from 0-90%, and represents the proportionate increase in liquidity (borrow limit) that an account receives by minting the cToken. Generally, large or liquid assets have high collateral factors, while small or illiquid assets have low collateral factors. If an asset has a 0% collateral factor, it can’t be used as collateral, though it can still be borrowed. DAI and ETH, for example, have a collateral factor of 75%, this means that up to 75% of the value of the supply DAI or ETH can be used to borrow other tokens.

If a user decides to borrow funds, the value of the borrowed amount must always stay lower than the value of their collateral times its collateral factor. If this condition holds, there is no limit on how long a user can borrow funds for. If the value of the collateral falls below the required collateral level, the user would have their collateral liquidated in order for the protocol to repay the borrowed amount.

The Comptroller is effectively the risk management layer of the Compound protocol: it determines how much collateral a user is required to maintain, and whether (and by how much) a user can be liquidated. Each time a user interacts with a cToken, the Comptroller is asked to approve or deny the transaction. As discussed, the Comptroller maps user balances to prices (via the Price Oracle) to the collateral factors to make its determinations.

The interest that lenders receive and the interest that borrowers have to pay are determined by the ratio between supplied and borrowed tokens in a particular market. The interest that is paid by borrowers is the interest earned by lenders, so the borrow APY is higher than the supply APY in a particular market.

The interest APYs are calculated per Ethereum block. Calculating APYs per block means that DeFi lending provides variable interest rates that can change quite dramatically depending on the lending and borrowing demand for particular tokens.

Let’s run through an example.

A lending scenario in Compound protocol 

A user deposits 10 ETH into Compound. In exchange for 10 ETH, Compound issues cTokens, in this case cETH. 

How many cETH tokens will the user receive? This depends on the current exchange rate for a particular market, in this case, ETH.

When a market is launched, the cToken exchange rate (how much ETH one cETH is worth) begins at 0.020 — and increases at a rate equal to the compounding market interest rate. For example, after a few months, the exchange rate might equal 0.021.

If the user supplied 10 ETH when the market was just created they would’ve received

10/0.020=500 cETH. 

Because the ETH market has been operating for a while we can assume that the exchange rate is already higher, set at 0.021. 

This means that the user would receive

10/0.021=~476.19 cETH. 

If the user decided to immediately redeem their ETH, they should receive roughly the same amount as it was deposited, which is around 10 ETH. 

Now, here is when the magic happens. The user holds their cETH. This is just another ERC20 token and can be sent anywhere. The main difference is that cETH is necessary to redeem the underlying ETH from Compound. On top of that, cETH keeps accumulating interest, even if it is sent from the original wallet that initiated the deposit to another wallet. 

With each Ethereum block, the exchange rate would increase. The rate of the increase depends on the supply APY which is determined by the ratio of supplied/borrowed capital. 

In our example, let’s say that the exchange rate from cETH to ETH increases by 0.0000000002 with each block. Assuming that the rate of increase stays the same for a month (30 days) we can easily calculate the interest that can be made during that time. 

Let’s say on average we have 4 blocks per minute. This gives us the following numbers:

0.0000000002*4*60*24*30=0.00003456. 

Now we can add this number to the previous exchange rate:

 0.021+0.00003456=0.02103456. 

If the user decides to redeem their ETH they would receive

 476.19*0.0213456=~10.0165 ETH. 

So the user just made 0.0165 ETH in a month, which is around 0.16% return on their ETH. It’s worth noting that the original amount of cETH that the user received hasn’t changed at all and only the change in the exchange rate allowed the user to redeem more ETH than was initially deposited. 

When it comes to borrowing, users lock their cTokens as collateral and borrow other tokens. Collateral earns interest, but users cannot redeem or transfer assets while they are being used as collateral. 

In Compound, the Open Price Feed accounts price data for the entire protocol. The protocol’s Comptroller contract uses it as a source of truth for prices, that are updated by Chainlink Price Feeds

The full documentation of Compound is available on the project’s website and it explains in great detail some of the concepts we discussed in this post.

Conclusive thoughts

In this post, we reviewed how the Compound protocol operates and we run through an example of how a lending use case would work in Compound.

As we know, and as I have discussed in a previous post, DeFi is a nascent technology that still presents inherent risks. Despite massive efforts from all Decentralized Finance projects to minimize or avoid bugs in the code, users must understand that there is a non-zero probability of vulnerabilities in the smart contracts, which could be leveraged by malicious parties in a exploit attack or in an hacking event.

It’s impossible at this stage to quantify the potential of DeFi such is vast and it is hard to ignore the possibilities these ecosystems could offer, however would be equally irresponsible to underestimate the risks that this emerging technology presents right now.