DeFi Lecture 6: Lending
By Berkeley RDI Center on Decentralization & AI
Summary
## Key takeaways - **Lending Powers Economic Machine**: Lending allows entities to gain upfront capital to produce positive value, forming the basis of short-term and long-term debt cycles in Ray Dalio's economic machine model. [01:20], [03:14] - **Overcollateralized Limits 2x Leverage**: In overcollateralized lending like MakerDAO, borrowers provide collateral worth more than the loan, such as 150% value, capping leverage below 2x while allowing free use of borrowed assets. [05:04], [11:20] - **Undercollateralized Enables Higher Leverage**: Undercollateralized loans allow borrowing more than collateral value for leverage beyond 2x, but restrict funds to predesigned smart contracts like farming pools. [11:35], [24:44] - **Health Factor Triggers Liquidation**: Health factor is collateral value times liquidation threshold divided by debt; below 1, the position becomes liquidatable, as in the example where 1 ETH collateral dropped from health factor 1.2 to 0.96. [13:50], [17:32] - **Liquidators Earned $63M Profits**: Across platforms like Compound and Aave from 2019-2021, liquidators captured 63.6 million USD profit from over 28,000 liquidations totaling 800 million USD collateral sold. [46:38], [47:47] - **Flash Loans Risk-Free Atomic**: Flash loans from pools like Aave must be repaid plus fee within one atomic transaction, enabling arbitrage or liquidations without default risk, as state reverts if unpaid. [03:10], [01:05:20]
Topics Covered
- Lending Powers Economic Cycles
- DeFi Unlocks Unlimited Leverage
- Health Factor Triggers Liquidation
- High Leverage Yields Negative APY
- Flash Loans Enable Riskless Arbitrage
Full Transcript
ARTHUR GERVAIS: Welcome back to the decentralized finance lecture.
My name is Arthur, and today we are going to discuss lending and borrowing in DeFi.
Lending is a fundamental construct in traditional finance as well as in DeFi and is a truly exciting topic.
Come on and jump in.
So why is lending so important?
Who is participating in lending?
What are the different entities?
Why does it matter?
And what are the risks.
So these are all the things that we're going to discuss in this lecture.
And we're going to start with very simple example.
So here we have Bob.
Bob is really passionate about music.
He loves it so much.
He wants to perform and create music all day long.
But he has an issue.
He cannot afford this really cool turntable that he would like to have in order to create very nice music.
So one possible solution is that he finds someone, here, Bart, that is a bit more senior.
Maybe he has accumulated a little bit more wealth.
And he can ask him to provide him a loan.
So he can ask him, can you please give me some cash such that I can purchase a turntable, and I can create value through your investment, through your loan.
So it's really this recurring process where an entity is required or needs upfront cash that it can't afford yet in order to produce ideally positive value for the people and then can generate a positive cash flow.
And this is fundamentally how the economic machine works.
So I can really recommend you going here to Ray Dalio's video on YouTube that explains in a brief 30 minutes how the economic machine works.
It's a beautiful video which basically lays out that we have an assumed productivity growth over time.
We do have short-term debt cycles, and we do have longer term debt cycles.
And all these three cycles, if you over-post them, then it's basically this kind of a structure over time.
And on the y-axis here, you have the growth in terms of GDP that shows you how the economic machine works.
Now, you might notice like these bigger cycles here, they're often referred to as crashes of the financial economy.
However, you can also find that there are some smaller crashes here because where overall the entire system is really cycle-based.
Sometimes people need more money.
Sometimes they need less money.
And this is how, on a very high level, the economic machine works.
So lending and borrowing really is fundamental in this construct.
And whenever there's a correction like here, or even smaller corrections here, this is where the borrower-- so, for example, in our previous example, the younger person who lent money needs to pay money back to the lender for the debt that it accumulated early on.
So in essence, what you can remember is that lending allows you within a short time frame to instantaneously gain more capital.
And this capital, it can be used in order to produce hopefully positive goods.
Naturally, if it's being used for unproductive use, then you will pay a higher price back on later because you can't pay back your debt, and you may go bankrupt, you get liquidated, et cetera.
These are topics that we discuss in this lecture as well.
Very well.
So let's look on-chain lending and borrowing system architecture.
So you can see the following entities here.
We have a lender.
We have a borrower.
We have a liquidator.
And we have a price oracle as well as a vault. Let's start
at the vault.
So the vault here is a smart contract that manages a financial asset among these different entities.
And again, why the blockchain is great at managing assets without a trusted intermediary, and that's exactly what's happening here.
So the vault is taking on various tokens, whether fungible, nonfungible-- doesn't really matter.
And for the sake of this example, here we have a few stablecoins representing US dollars.
So what the lender typically does is he first needs to deposit a principal.
So this is the lender.
He has a surplus of capital.
And he would like this capital to work for him or her.
So the idea is that this capital generates some interest.
For the capital to generate interest, there needs to be a borrower who, in a first step, collateralizes a certain security deposit, such that the borrower can then take on a loan.
For example, in some blockchain-based protocols, you need to collateralize 150% of the value that you want to borrow afterwards.
So MakerDAO is a famous example.
So once the borrower collateralize something, he can then borrow the assets.
And then he effectively-- if he, for example, collateralized something, then he borrowed something.
So the sum of his total assets are now bigger than before.
So the danger, however, is that he might get liquidated.
So let's discuss how the liquidation process works.
So this is the risk in lending and borrowing.
So in essence, if this collateral here, if the value of this collateral drops below a certain value, if it's less than 150%, for instance, then price oracles will update the on-chain prices or the reported prices of the collateral assets.
And if these drop below a certain threshold, which we'll discuss later, then liquidators are incentivized to issue liquidation requests to the vault.
And the liquidators are incentivized to do so because they receive a percentage, or they receive a discount, basically, of this collateral that they can purchase, which is basically incentivizing them to participate in this particular protocol.
So this is really just the high-level overview.
We will go into more details in the following slides, but just to give you a hint of how these different actors are actually working together and playing along.
So from a high-level perspective, I would like you to think now, what is the difference between on-chain lending and borrowing and between real-world lending and borrowing?
So if you go to your bank, for instance, how is this process-- how does this work, this lending and borrowing?
What are the rules, et cetera?
So think about it for a few seconds.
Pause the video, maybe, and then we can resume.
OK, so I think some of the most interesting changes are that at the moment, at this point in time, at least, we cannot collateralize real-world assets.
So if you do have a home or something that you want to collateralize, you currently don't have-- or hopefully, soon at least-- but currently don't have the option to collateralize this as a security.
So this is one of the drawbacks, I'd say, that traditional finance is having an advantage that you cannot collateral.
The other difference is that if there's a debt default, you cannot-- or the blockchain, this vault here, you cannot source, for example, your revenue from your income.
So there's no legal framework that's attached.
So everything is really happening in this vault in this framework.
And it's not confined to-- I mean, it's basically confined to this particular blockchain database.
But it cannot capture real-world events, such as your salary or your income.
Finally, there are several regulations out there that limit you in your ability to take on debt in general or leverage.
And these are not in place in DeFi.
In DeFi, it's currently totally unregulated how much you lend or borrow, which gives you, obviously, a lot of freedom.
But you also must exercise caution to not basically become the victim of excessive losses.
Just to give you a few images of DeFi projects that allow you to perform lending and borrowing where you can become each lender or borrower, so just to give you an example here, this is Aave, for instance.
So you have a certain deposit APY as a lender.
Here, this is curve that we discussed earlier.
Here is this Alpha Homora, as we also discussed earlier.
So whenever you take on debt, you exercise a multiplier.
So you have a certain leverage.
The leverage can be set-- there are various multiplier parameters.
So typically, as long as you're overcollateralized, you don't go beyond 2x in leverage.
And if you're undercollateralized, you can go beyond 2x leverage.
So some centralized exchanges, at least, offer up to 100x, I believe, on-chain.
So far, as far as we have seen, you don't get more than 7x leverage.
And I challenge you to create a DeFi protocol where you can do a 50x leverage.
I hope this gave you the very first good introduction of what lending and borrowing looks like on the blockchain.
In the following, we'll discuss and dive deeper into the various specificities of on-chain lending.
Welcome back.
So while the previous segment already gave you a little bit of an introduction, we will dedicate now this segment to the terminology used in DeFi lending and borrowing.
So let's start with the very first terminology that's quite important, which is the collateral.
Collateral is a security deposit.
It's an asset that the borrower provides in order to secure its debt.
So the more debt you take on, the more collateral you need to provide.
Because the collateral is a security deposit that guarantees that you can pay back your debt.
You cannot take on too much debt.
Otherwise, you would be indebted.
So there needs to be some kind of a security deposit here.
And based on this, there are two possible lending profiles, lending methods, on chain at the moment.
The first is the overcollateralized setting, and second is the undercollateralized setting.
So in the overcollateralized setting is where the borrower provides collateral of which the value is higher than the granted loan.
So this also implies that your leverage multiplier is inferior to 2x.
Undercollateralized loans allow you to borrow more than you actually have.
So the collateral value here can be inferior to the depth value that you take on.
And this also means that you can have leverage beyond 2x, in general.
Now, you might ask, well, great.
I mean, on-chain I'm anonymous.
I can just take on any kind of collateral, and then just run away with the money.
Because, I mean, why would I need to pay it back?
Nobody can catch me on-chain.
Well, the thing is, in the overcollateralized lending systems here, you can freely use your money, freely use the debt that you take on.
So it's actually a token that you can redeposit.
You can even redeposit it as collateral in some systems, while in the under collateralized setting here, they are highly restricted.
So you will not actually have access to the funds.
So the smart contract will basically manage how you can deal with the money or what is being done with the money.
So the undercollateralized setting is much more restricted precisely because, a, you want it to still remain secure in a sense that you shouldn't just leave with your debt.
So the fourth term here that I would like to introduce is a liquidation that we already mentioned, the high-level system figure.
So more formally-- so if the value of the collateral goes beyond a certain threshold, for example, 150% times the value of the debt, then you might become liquidated.
So anyone can open-- can basically create a liquidation request, or anyone can liquidate a debt position.
As mentioned earlier, you might get a percentage discount on the collateral value.
So you get it as a lower value than the market value, which incentivizes you to perform a liquidation.
But there may also be auctions instead of fixed-spread liquidations.
So next terminology is a health factor.
So what is a health factor?
So the health factor is defined as the sum of all the collateral you have times a liquidation threshold.
So liquidation threshold is a value between 0 and 1.
And it basically discounts for the collateral.
And so if you take all the sum of this collateral liquidation threshold, you divide it by the total value of your debt, you end up with the health factor.
So the value of the collateral times liquidation threshold times this discount is referred to as the borrowing capacity.
So this is how much you can borrow.
And as mentioned earlier here, liquidation threshold is between 0.0 and 1 and provides a secure margin.
So it's not really secure in any technical sense.
It's more like it's a buffer.
It's really just a buffer of-- you think of how much will this value fluctuate, and what is the safe way to avoid liquidation?
So when the health factor of a borrowing position declines below 1, then this position becomes liquidatable.
So this is the point in time where a particular liquidator can liquidate your position.
Well, anyone can do this.
But this is the point in time where entities are able to call the smart contract to liquidate your position.
So let's go on.
On this health factor, let's go on with a very concrete example.
So we will have here collateral.
So we will, take-- for example, let's say, we collateralize at the very beginning.
We collateralize 1 Ether.
And the exchange rate between Ether and DAI is 2,000 at the start of this position.
So the exchange rate ETH/DAI is 2,000.
So this means that the collateral value here, because we issue 1 Ether as collateral, the borrowing capacity that we can get is at maximum 1,500 DAI, so in this particular example.
And we say that the liquidation threshold, we set the liquidation threshold to 0.75.
So given these numbers, we can calculate that the health factor in this particular instance is 1.2.
So that's at the very beginning of the borrowing positions.
So now over time-- so we open this position.
So now over time, we can use our debt.
This is capital that we have free access to, so we can do what we want with it, which is great.
We can continue to speculate, for example, on the Ether value.
But we do have additional DAI at our disposal that we actually can convert back to Ether and continue this in the same gain if you want to.
But let's assume we just stay with this 1,250 DAI position, and the Ether price changes.
So here, the Ether price goes down.
So the exchange rate Ether to DAI is now only 1,600.
So the value of the debt remains the same.
This didn't change.
I mean, we didn't pay back any debt.
But now the value of our collateral here is quite smaller, and that's not great.
And we can see that our borrowing capacity is now only 1,200 DAI, which is inferior to our debt, which is 1,250 DAI.
And this, in turn, also means that our health factor is now below 1.
And that's obviously not a great thing because suddenly we became liquidatable, or this borrowing position became liquidatable.
A few more terminology items that might really help you to grasp those protocols and systems. So you have a liquidation spread.
As mentioned, this is a bonus or discount.
The liquidator can collect when liquidating collateral.
So it's basically the value of the collateral to claims equivalent to the value of the debt to repay times 1 plus this liquidation spread.
So this is the value of the collateral that can be claimed.
The close factor is quite important in liquidation setting as well.
So that's the maximum proportion of the debt that is allowed to be repaid in a single fixed-spread liquidation.
So sometimes in some protocols the close factor is 50%.
So this means you can at most payback 50% of the debt in a single fixed-spread liquidation.
Well, 50% of a debt repayment is quite a lot.
In most cases, or in many cases, this will be an excessive liquidation.
And we've seen evidence on-chain that liquidators are excessively liquidating the collateral from borrowers.
So just to give you here some further information.
So the value of the debt to repay should always be inferior to the close factor times the value of the debt.
But this close factor is-- yeah, it's mostly set as a constant.
I think it's like 0.5 in Aave, so 50%, which is quite significant and not in the interest of the borrowers in general.
Very well.
I hope this terminology will help you to speak the same language as we do, as well as to synchronize efficiently with your peers.
Welcome back.
We're going to dive now deep into overcollateralized borrowing.
In overcollateralized borrowing, we have, again, two actors.
So we have a vault here, which is the smart contract that governs the assets.
And we do have a borrower that mints or creates basically depth.
So in this particular example here, the borrower collateralizes, for example, Ether and then borrows DAI.
So that's a very traditional way of creating a stablecoin as in MakerDAO.
So in essence, we have here 150% of the value in Ether as collateral.
And we can then mint 100% of the value in DAI.
So what's important is here that the value of Ether, which is the collateral, exceeds the value of DAI in absolute terms. So if you convert it to a base currency.
And what's also important is that the borrower, in this particular example, can use the DAI freely at will.
So he can push this around.
He can actually convert it back to Ether or to any other currency, borrow more assets.
So that's really like a very nonlocked asset, in a sense.
The drawback, obviously, is that the leverage that the borrower can take on here remains always below 2.x.
So this is probably not the model that a highly speculative trader or borrower would like to take on.
To give you an example, here we have the Aave dashboard.
This is a screenshot of a particular position.
So we have someone with about 10K of collateral here.
And this person also borrowed about a 1,500 US dollar with collateral.
So the health factor looks quite good.
It's well beyond-- maybe above 1.
So we had a 5.36 health factor.
And we've only used a part of the borrowing power so far.
So this particular position can take on more debt if the person wants to.
So if we look at here now the particular elements of this deposit-- so here we have a deposit of DAI.
So this DAIs deposit as the collateral.
What's interesting in our way is that the collateral itself is also lent out, so the borrowers can earn interest.
So you can actually activate this here with this button.
That's obviously a transaction that's being triggered into the smart contract to activate that this collateral is being used as something to be lent out.
Because naturally, there's also the risk of liquidation that comes along with this APY.
What you can also see is that this person borrowed some UNI tokens-- in total, about 500 UNI tokens, and the borrower needs to pay interest.
So here's the interest that the borrower needs to pay.
This is quite high, right at 113% in this particular example.
So as I mentioned at the very beginning of this segment, this model is being heavily used by MakerDAO, or was introduced by MakerDAO, mostly.
So where in step one, a borrower really provides a collateral, for example, in ETH, but it can be various different currencies as well nowadays.
So we have multicollateral DAI nowadays.
So once you provide some collateral in this step, then the smart contract will mint DAI, so the borrower can draw DAI from the particular [INAUDIBLE] smart contract.
So this is the debt that the borrower can freely operate with.
And in order to close this CDP or debt position, this Collateralized Debt Position, the borrower can then repay the debt, which is DAI.
So if DAI is repaid to the contract, then the contract will unlock the Ether, such that the borrower can return the borrowed funds.
Voila.
These were the most important basics for overcollateralized borrowing, borrowing where you cannot surpass 2x leverage.
So if you're interested in high leverage, check out undercollateralized lending.
Welcome back.
We're going to dive now into undercollateralized lending, lending that enables you to leverage beyond 2x.
Similar to overcollateralized lending, here again, we have a borrower.
So the borrower, for instance, can collateralize Ether and borrow DAI.
And again, we have here a vault where the borrower deposits his assets.
However, the difference now is that the value of DAI, which is the debt, can exceed the value of the Ether, which is the collateral in this particular case.
And what's quite critical in undercollateralized borrowing is that the borrowed DAI and the collateralized ETH, they are restricted to be used with predesigned smart contracts.
So you can't code up your own contract and send your debt towards this contract.
You must use predesigned contracts from the lending and borrowing platform itself.
So these are typically farming contracts.
So if you want general yields based on your debt, then that's obviously something that you may want to consider.
But otherwise, it's probably not going to help if you want to do any arbitrary action with your debt.
So what's really important here, the vault remains in control of all assets at any point in time during the life cycle of the debt position.
One example screenshot is the following here.
This is Alpha Homora, or the dashboard of Alpha Homora.
We have here collateral value of a particular peer of $1,900.
Then we have a small amount of debt, which are $350.
And we are receiving some tokens as a reward.
So that's a particular APY based on the tokens.
And you can also see here basically the position that's being open.
So that's the position in the Uniswap ETH/DAI pool.
You may add additional elements, or you may close the position if you'd like to.
In Alpha Homora, you can also see all open positions.
So you can inspect basically on their web page how these positions look like.
What's the debt ratio?
I think this is something that you might be interested to further look into.
You can explore the collateral credit, borrow credit, and the overall collateral value.
Note that there are some pools that are stablecoins, like this one here, and other pools that are more speculative coins that are less-- that are not stable or not supposed to be stable.
So we crawled Alpha Homora actually from its inception.
So Alpha Homora started in October 2020.
We crawled the smart contract data until August 2021, and we found that there are about 3,800 borrowers, of which we found 10,430 leveraged positions that were closed at the end of our measurements.
So we basically were searching for all the open borrowing positions in an effort to be able to calculate their final APY, so their final return, and whether it was worth it or not.
So we got some interesting data.
So for example, we found that the average leverage multipliers are about 2.x and 3.x on the respective Alpha Homora versions.
So there's a version 1 and version 2.
The main difference is that the version 2 allows for more assets beyond Ether to be used.
We also find, rather intriguingly, that stablecoin leverage multipliers on average are quite higher.
So the average stablecoin multiplier is 5.4 almost, which makes sense because stablecoins are by nature less volatile.
So it's less risky to take on a higher leverage.
And the risk of being liquidated is naturally not that big then.
So how are borrowers choosing leverage multipliers?
So we have here plotted our results based on whether these are stablecoins, partial stablecoins, or nonstablecoins.
So stablecoins is any pair of assets that are stable.
Partial stablecoins is where you have one stable but another nonstablecoin.
And nonstablecoins are just tokens that are not stable.
And you can, again, see the distribution here of the leverage multiplier for stablecoins being significantly higher than the leverage multipliers for partial or non stablecoins which are more or less the same.
So they have more or less the same-- users basically, more or less, choose the same leverage multipliers as soon as you have a nonstablecoin in your basket.
So if we look at the various platforms. So here in this second plot, we differentiate between the different platforms. We show Curve, Balancer, Sushiswap, and Uniswap.
And we can find here that for Curve, so that's the red one, most of the leverage multipliers are rather high, which makes sense.
Curve supports the most stablecoin pools.
But also, Uniswap does support a few stablecoin pools.
So it's quite interesting to see here a few high-leverage positions on Uniswap.
And otherwise, Sushiswap is taking quite a bunch of the positions that are there.
So this data is based on leverage multipliers in Alpha Homora version 2.
So about 2,500 positions that we investigated.
Now, you might ask, so what's the API under leverage?
I mean, why wouldn't I take always the highest leverage possible?
So on the x-axis, we plot the initial leverage that we found those Alpha Homora positions to take up to.
So this is the initial position at the time that you open the leverage.
And then we calculated-- after closure of the position, we calculated the APY, the borrower APY.
And you can see here, it's not always shiny in rows, positive.
We have quite a few cases where the APY is actually negative and can even be significantly negative.
So the size of the dots here, they actually represents the relative size of the position.
So the bigger it is, the bigger the position relative to the others.
And we differentiate it here according to the duration of the position.
So the red dots are all the positions that are actually shorter than a day.
And you can see by adding a regression line here that the shorter the duration and the higher the leverage, the worse the API becomes.
So in expectation here, for example, for 7x leverage, the power position is really quite, quite negative regarding the APY.
Luckily-- I mean, these positions are rather short-term lift.
So it might also be that the borrower actually just exited the position quickly in order to avoid further negative exposure.
But it's interesting.
Longer term positions remain rather positive here, or close to positive.
So there's a big variety, at least.
And you can see once we look here at higher leverage positions, we have certainly more data points on the negative side than on the positive side, which appears empty.
Whereas, if we look at the leverage multiplier of like about 2-- so in this spectrum here, 2 to maybe 3, then we can see more or less an even distribution between positive and negative ones.
So I leave the data-- I leave the further inspection of the data or interpretation of the data up to you.
And yeah, feel free to let us know in the comments or in the chat if you have any further thoughts based on these visualizations.
So you might ask, so why shouldn't we choose a leverage to maximize the-- why shouldn't we always choose the maximum leverage?
And why does leverage not always amplify the APY in practice?
So in practice, the leverage multiplier actually has an impact on a variety of different risks or events that we can find.
Naturally, if you just look at the revenue, a leverage multiplier should increase your revenue.
However, because you're borrowing more funds, you want to leverage more, so you need to borrow more.
The borrowing interests also increase, and they might be quite excessive.
So depending on the market state, they can be significant.
Also, your liquidation risk increases as you increase the leverage multiplier, which is a danger you don't want to be exposed to.
Because once you get liquidated, your collateral is sold at a discount.
And obviously, you incur a loss.
So just like the automated market makers also in Alpha Homora are overcollateralized-- are undercollateralized lending platforms, you do experience an impermanent loss.
And this impermanent loss can be sometimes positive, sometimes negative, especially because of the type of margin trading that's possible on these platforms, in which we don't have much time to go into further now.
But I will really encourage you to explore these particular risks here and to further understand them.
Feel free to also ask questions at any point in time.
So thank you very much for your attention in this exciting leverage or undercollateralized lending platform.
I would strongly recommend to try them out yourself, but be careful.
You might incur a total loss of funds, especially if you choose excessive leverage multipliers.
So maybe a little bit, a smaller leverage multiplier is the safer bet, but it's up to you.
Welcome back.
Being liquidated is probably the biggest risk in borrowing/lending.
Because you will be losing quite some value of your collateral as it's being paid out to liquidators at a discount.
So this is something you really want to understand and avoid if possible.
So let's dive into it.
So what can possibly go wrong if you're lending or borrowing assets?
Well, as we mentioned and indicated earlier, the collateral value can decline such that the debt-- it would not be rational to pay back the debt.
And that's really a debt default.
It's not great for the lender in general.
So therefore, there's this security mechanism called a liquidation that should sell the collateral from the borrower to limit the losses of the lender.
But that's, again, the zero-sum game.
That's not great for the borrower because there's a liquidation spread.
So there's a bonus or discount for a liquidator, and there are various means of how something can be liquidated.
There's, for example, a fixed spread liquidation mechanism that we will go into a little bit, or there's a variable on auction-based liquidation mechanism.
So before we go into DeFi, let's look into liquidation and traditional finance.
Well, it's quite a complicated process.
So there might be a court order for liquidation.
Or maybe, for example, the board of a company passes a resolution for voluntary liquidation, or creditors pass a resolution for liquidating at a watershed meeting.
So then the liquidator notifies the company's office.
Then there is an advertisement of liquidation in some public forum that people can look into.
Then creditors are basically having a meeting to confirm the liquidator.
There's an administration of liquidation, which may include the closure of business, the realization of assets or selling of assets, calling for claims, paying for dividends.
So the next step is there's an ongoing report to creditors and company's offices.
And this process really takes eight-- takes many months.
Can take years, even.
And then there's a completion of administration.
So there's a final report that will be created.
There will be a notification of the company's offices.
And then finally, the company will be removed from the commercial register because it's liquidated.
So over and all, this is the liquidation process for corporation-- very high-level, obviously, with probably many nuances.
And in DeFi, it's much more simple.
Let's look, for example, at the fixed-spread liquidation mechanism.
So in a fixed-spread liquidation, we have here, again, the vault. And we
have a liquidator who's interacting with this vault.
So the liquidator repays the debt.
In this particular example, he repays DAI and acquires the collateral, Ether, at the discount.
So it's repairing this borrowing position.
And the discount depends on the particular platform you're looking at, where you've seen anything between 5% to 15%, for example, in Aave.
What's important is that this liquidation is being executed in an atomic transaction.
This is-- in one transaction, the vault is receiving the debt and paying out the collateral.
So if it's an atomic transaction, the liquidator doesn't need to trust the vault.
This is guaranteed to be executed if the liquidator is actually issuing this transaction.
So let's look into a more mathematical example here.
You may remember our previous example where we calculated the health factor of a position.
So we continue actually on this prior example where we have a bad health factor, which is below 1.
So we have a 0.96 health factor, mostly because the borrowing capacity is 1,200 DAI.
And actually, our debt is beyond that.
It's a 1,250 DAI.
So this means currently this position, this debt position, this one here, is liquidatable, which means that the entire collateral, this here, is actually opened up for liquidation, meaning it's opened up for being purchased by some entity at a discount.
So if we have a close factor of 0.5 and a liquidation spread of 0.1, then we can actually calculate how much a liquidator can get from this particular position.
So given that the close factor is 0.5, the maximum that the liquidator can pay back from the debt is 50%.
And that's exactly what the liquidator is doing here.
He's paying back 625 DAI from 1,250.
So 650-- 625 DAI are remaining debt in this particular position.
So the good news is now the health factor grows beyond 1.
We have a health factor of 1.095 here.
And that's because the liquidator got a part of the collateral, but he didn't get everything because he also did not pay back the entirety of the debt.
So however the liquidator here got 0.43 Ether, which corresponds to this amount of die, and this is the entire position how it stays.
So what we see here is that this particular-- I mean, such liquidation is reducing borrower-- I mean is reducing the total locked funds in a borrowing and lending platform because it's effectively taking money out of a borrowing platform.
And this can be quite significant with 50% of the debt available to be repaid.
And naturally, the borrower in this particular example will incur loss because there's a discount-- there's a liquidation spread of 10% that the liquidator can benefit from.
Another type of liquidation mechanism is a liquidation auction.
So you have here a timeline, and here are a few blockchain blocks.
This actually goes over-- this is a nonatomic liquidation, so this requires multiple blockchain transactions.
So let's imagine the auction starts at a particular block height here.
Then we have two liquidators that are competing.
So they can issue basically a few bits in every block.
And then at some point, the auction terminates.
And then the smart contract, given the list of incoming bits, will determine who is the winner of this particular auction.
The main two auctions that exist in DeFi are the English auction, so where the bidders outbid each other increasingly, as well as the Dutch auction-- so where the auction begins with a high asking price, and then over time falls down to lower prices until the auction terminates.
So initially, MakerDAO-- we're going to continue using MakerDAO as an example in our slides here.
So initial MakerDAO followed the tend and dent auction from its inception until April 2021.
So the tent and dent English auction is interesting because once we initiate here the auction, we have two phases-- a tent and a dent phase.
And in the tent phase, you can see here-- so D represents the debt, and C represents the collateral.
So if I want to make a bid, then I have to create basically a bit with a di plus 1, where di plus 1 has to be bigger than di.
So it means that the amount of dept that I want to repay is bigger.
So I have to basically purchase an increasing amount of debt for the entire collateral.
So I'm increasing the debt that I repay based basically on the collateral that I can get there.
And once I reach the equilibrium, let's say, once I get everything back, then we can still continue with the dent phase.
And I have to pay back the entirety of the debt, but I only get a small amount of the collateral in return.
So ci plus 1 here has to be smaller than ci, meaning in this particular case, I have to pay back the entire debt, but I only get this fraction of the collateral in return.
And finally here, once we are finished, the auction terminates.
So there's a certain time constraint on how long an auction can last or should last with hard deadlines, actually, in the case of MakerDAO.
So this model was not great.
There were-- we will look into a case study later on.
But MakerDAO changed their auction model.
From April 2021 to the present.
We now have an instant settlement.
So instant settlement, Dutch auction.
So unlike English auctions, which are operated over multiple blockchain transactions, the Dutch auction now is settled instantly in one atomic transaction.
We can do a flash landing of collateral.
So there's no upfront DAI that's required.
And there's a flash loan which can be used specifically for MakerDAO auctions, which is great because this simplifies the liquidation mechanism.
There's Also a price as a function of time.
So the collateral price decreases over time.
And nobody can get or receive the collateral for free by accident, as happened in MakerDAO, and we will look into in a later slide.
To give you a further insights into liquidation statistics and why these are actually quite important for DeFi, we have crawled the on-chain data for five different platforms. So you can see here, these are the DeFi platforms that we have looked at in a time frame from 2019 here until 2021-- April 2021.
And you can see on the y-axis here the accumulative US dollar amount.
And you can see that most liquidations, for instance, happened on compound.
So compound was the platform that had the biggest amount-- US dollar amount of liquidation.
So in total, we identified over 28,000 successful liquidations and 800 million USD of collateral that was sold through liquidations.
If we look at continuing statistics, we can see, based on the amount of liquidations, we can extract their profit.
So over the same time frame, over the same platforms, we found this is like-- there's a 63.6 million US dollar in profit from liquidations alone.
We can see some outliers.
So in particular, there was an outlier here in March 2020.
There was a liquidation bot failure.
So the default MakerDAO liquidation bot was not functional during a significant price crash of the Ether cryptocurrency.
And this bot failure prompted liquidators to just bid very small or almost no amount in liquidation events.
And this basically caused a spike here.
So we have cut the excess.
So bear in mind, this is actually quite a significant event back there.
So almost-- above, actually, 13 million US dollar of profit for liquidators just that particular day.
And overall, liquidators have shown to actually receive quite a significant profit on compound.
Aave has been not bad also in April for liquidators.
So liquidations are very competitive.
So here, that's why, again, over the same time frame, we plot the gas price in gigawei.
So if you remember the smart contract lecture, gas is the unit to pay transaction fees.
So the more you pay, the earlier you might get included in the blockchain.
And we can see here that we have basically a evolution or a growth of the average gas price as well as of the liquidation price.
So you can see here, we plot this gray, almost black line as the average gas price.
And you can see that consistently, the liquidations are actually above this rather significantly.
That's a log scale.
So we have a y-axis, which is a log scale.
So these amounts are quite significant indicating severe competition among liquidators.
Finally-- so we have further liquidation statistics.
So liquidation-- especially the liquidation sensitivity that we investigated among these different lending platforms. So you can see-- so these plots are basically indicating the same data.
I just would like to explain it here on one.
So on the x-axis, we have the price decline in percentage.
So if you can think of a price decline of 80% of the collateral, then you will see how much collateral becomes liquidatable.
So there are some thresholds you can see here that are quite significant.
And suddenly, a lot of collateral gets liquidatable.
And you can basically see a few thresholds here that are the quite significant depending on the platform and the cryptocurrency that you're looking at.
So if you have any positions in these markets, I would recommend you to basically plot these types of risks assessments on an ongoing real-time basis so that you can make informed decisions yourself.
I hope these statistics and insights about the various liquidation mechanisms were very helpful, especially if you do plan or already have borrowing or lending positions opened.
This is a very exciting field.
I think there's still much room for novelty.
So if you want to propose your own liquidation mechanism or your own liquidation bot, feel free to try it out.
Welcome back.
You're slowly but surely becoming a liquidation expert.
We're looking into further case studies and insights in the following segment.
In March 2020, there was a significant drop in cryptocurrency asset prices, and MakerDAO was heavily affected.
It was called the Black Thursday for MakerDAO, where 8.3 million US dollars was liquidated for zero DAI.
So this was a single liquidation event where a MakerDAO liquidator basically created a liquidation request for zero DAI.
So he wanted to repay the debt, which is denominated in DAI, but he wanted to get the entire collateral back.
And this happened due to the opportunity to win liquidation auctions with zero bits.
So 36% of all liquidations on MakerDAO that day were affected.
So the greatest vault, as we can see here, lost about 35,000 Ether.
And the most successful liquidator had a profit of about 30,000 Ether.
Quite significant and, unfortunately, a big loss to the protocol at that day.
Luckily, since then, MakerDAO innovated and changed their liquidation mechanism since April 2021.
So what was the actual problem?
Well, the problem was that first, the market collapsed.
So if the value of the collateral is declining, then there are liquidation opportunities arising.
At the same time, if there's a lot of frenzy on the market, the gas price increases.
And we've seen this over and over again on the Ethereum chain and other blockchains as well.
What then happens is that MakerDAO bots should actually issue bidding transactions in the auctions.
But because the gas price was so high, the MakerDAO bots were not programmed, they were not parameterized appropriately to actually increase the gas price significantly to mine the liquidation transactions.
And therefore, the issue transactions that should have created a bit in these auctions were not mined.
And if they're not mined, well, then they don't hit the blockchain, and they don't exist.
It's as if they didn't happen.
So this is certainly something that you want to avoid.
And I think what MakerDAO really did beautifully now is to change the liquidation mechanism into a model where it's not possible to gain an auction with a zero bit.
You really have to-- basically, the price goes down and not up.
That's the, I guess, the main insight.
So what further liquidation insights can we find?
So first of all, the health factor is quite critical, as we've seen.
If you have a health factor of a position below 1, then you are liquidatable.
Unfortunately, however, the fixed-spread liquidation does not necessarily increase the health factor.
And we had this example where we are decreasing the-- we're paying back the debt, and we are-- but we're also getting in return collateral.
So the reduction in collateral, again, is actually bad for the health factor.
So the fixed-spread liquidation is not always helpful.
What we've also seen is there's quite an amount of overliquidation.
So liquidators sell excessive amounts of the borrower's collateral because the close factor of, for example, 50%, meaning you can pay back 50% of the debt, but maybe just 10% of the debt would suffice to render the position healthy again.
So there's excessive losses that the borrowers have to pay.
And finally, we have not seen many liquidators performing so-called optimal liquidation strategies.
So liquidating up to the close factor is not necessarily the best strategy.
Instead, it appears that two successive liquidations might offer a higher profit.
And we will go into a specific number example later.
But let me show you the algorithm for this.
So basically, the idea is if you have a position that is liquidatable, you don't directly push it to the close factor.
But because you have a close factor of up to 50%, you want to use this close factor several times.
So in the best case, you liquidate, for example, 0.4999% of the entire debt.
So you repay 49% of the debt-- entire debt.
And then you have a liquidation where you push it beyond-- you actually go to 0.5%.
So you repay 50% of the debt.
And in that particular-- through such case, you can pay back more debt than 50% and benefit from this increased-- or the bonus that the liquidator gets.
So now obviously, it depends the exact numbers depend a bit on the value, on the health factor, on the collateral, on the debt value.
But you can feel free to apply the algorithm here that you see.
So the intuition is really you don't bring-- in the first liquidation, you don't bring the health factor beyond 1.
And a second liquidation, you bring it up above 1, but in a way that you maximize your potential revenue as liquidator.
Furthermore, so if you're a borrower or if you're a lender, you want to know which of the liquidation mechanisms are the best for you.
So especially if you're a borrower, and you see that the platform like dYdX does not have a close factor.
So it means you can actually liquidate 100%.
So there's no limit.
Then you can see that the profit to volume ratio here over time.
So dYdX here is almost always the highest.
So this is the profit to volume ratio for the liquidators.
So if this profit to volume ratio is higher, then this means this is better for liquidators but worse for borrowers.
And so data basically suggests that auction liquidations might be more borrower-friendly because MakerDAO here is the orange line, which is a bit lower.
We interestingly see some difference between Aave version 1 and version 2.
But this is basically the one possible normalized metric to compare which liquidation mechanisms are nicer or more profitable or more safe for which actors of a borrowing-lending market.
Next is the dangers of deleveraging spirals.
So we've seen this on-chain several times.
So if you have a liquidation event, then the liquidator sometimes atomically already liquidate.
So they create a liquidation because the collateral price declines.
Then the liquidators are varied.
So they might actually sell their collateral that they purchased, which will then trigger additional liquidations.
And this is basically a possibly ongoing cycle, and it's called deleveraging spiral.
So upon liquidation here, the liquidators will sell the collateral, which then triggers the price decline, which then triggers, again, further liquidations.
So it might be a good question whether liquidation is a good solution to secure lending pools.
There might be other solutions.
So I'll invite you to propose any ideas that you have.
Let's dive into a particular on-chain study-- so how to apply an optimal fixed-spread liquidation.
So I've already shown you the algorithm.
And you may now look into this very analytical example.
So we take, as an example, a liquidation that happened on November 26, 2020 on compound.
The liquidation threshold is 0.75.
And we have here the particular block state that we give you.
And there's also price update afterwards.
And after the price update, we can see that the position here becomes liquidatable.
So feel free also to stop the slides and look into the data yourself in more detail.
But we have shown you here basically the entire amount.
So the value of the debt, the value of the collateral, and the which tokens were participating in this particular example.
So what we have found is that the original liquidation-- so the liquidators on-chain choose to repay 46 million US dollar.
He then received $49 million.
So he realized a profit of about 3.7 million DAI, which, in itself, is not a bad profit.
But we are speaking here about making things optimally.
So it's clearly not the optimal strategy.
So the liquidators in this particular example has actually not liquidated the maximum close factor that he could have.
So he could have gotten 3.73 million if he would have pushed the liquidation up to the maximum liquidation close factor threshold.
And we show, however, that if we perform two liquidations-- so we have the first liquidation, liquidation number 1, which does not bring the position back to a healthy state.
And then we subsequently apply a second liquidation.
If we do those two together, we could have achieved a profit of 3.743 million DAI instead of the original 3.69 million DAI.
So these amounts are not negligible.
These are significant amounts that we could have gotten in addition to the original liquidation profit.
So we believe that liquidators will become more professional as time goes on, as we have seen in DeFi in general.
So this will become a more competitive game, and liquidators will gain more money from borrowers.
So what ideas do you have to avoid liquidations?
So feel free to pause the video.
Think about this for a bit.
And I think we need solutions for other designs to change the way that the liquidation mechanisms are working.
But I'll leave it up to you.
And feel free also to suggest in the chat or in the forums, and we can discuss further.
Thank you very much for your attention.
I hope these additional insights about liquidations-- who gains, who loses-- are helpful to you.
Feel free to go on-chain and check out liquidation events yourself.
Know, as usual, in DeFi, everything is transparent.
So you can verify the techniques that other people apply and learn yourself.
Welcome back.
In this segment, we will be discussing flash loans, a truly novel construct that DeFi brought us which does not exist in traditional finance.
So it's really exciting.
Let's dive into it.
Hypothetically, what if Bart could grant a loan to Bob without the risks of Bob defaulting on the debt?
Sounds too good to be true, right?
No, it does not.
It is called a flash loan.
So a flash loan is a loan that you can take from a vault.
So this is your lending pool.
It's a smart contract-based lending pool.
So you can take the flash loan.
You can do something with it in your smart contract.
So go wild with the loan if, and only if, by the end of your transaction you repay the loan plus the accrued interest only, the requested interest, basically, which depend on the size of your loan.
The interest here, in this particular example, do not depend on the time.
There is no notion of time in a flash loan because the loan is only granted within a single atomic transaction.
Because the taking of the loan and the repayment of the loan has to happen in the same transaction.
And therefore, there is no notion of time.
So the interests here are only a function of the amount of the loan.
So for example, various platforms, like Uniswap or Aave offer flash loans at a 0.3% fee, which is quite significant if you extrapolate it for an APY if you're going on every day doing flash loans, but that's the current state of the market.
So if you look at it a bit more, slightly more technically.
So we have here a smart contract pool.
We do have Alice again, who is adding liquidity to this pool.
So she's a liquidity provider.
And we have somebody else like Bob here, who would like to lend the asset X from Alice within a single atomic transaction, and she will repay it by the end of that transaction and pay the fee to Alice.
So it's really like a special form of lending.
It's not-- so you shouldn't be thinking of it as a long-term lending or any time component.
There is really just a very instantaneous lending process.
So the beauty is that if the repayment and the fees here, if they're not paid, then this loan will not happen.
So then it's as if this loan was never granted at all.
So this is really the gist of the DeFi flash loans.
So either you pay it back, or you don't pay it back.
So if the repayment is not performed, I repeat, then the transaction will fail.
You have these three steps.
You have take loan, do something with the loan, and pay it back.
If step number 3 does not happen, and this is one transaction, then the entire transaction will fail.
So the state changes of step 2 and 1 will actually never materialize on-chain.
It's as if the loan was never given.
So it's as if the entire transaction did not happen, or at least that the steps in it did not happen.
So what are the common flash loan pools that we can look into and that we can use nowadays?
Uniswap has two versions, version 2 and 3, with an accumulated 7 billion US dollar that you can lend.
So everybody can do this.
You can do it today, and you would need to pay a 0.3% fee.
Aave has a significant pool of 10 billion US dollar, and dYdX has a much smaller pool, however, also a much smaller fee.
So it's a constant fee of one way per flash loan, which is quite cheap.
And so keep in mind that fees are important, especially at those amounts that we are here discussing.
0.3% is not little money.
So we've looked at the on-chain data of various pools, and we've looked into the accumulative flash loan amounts.
So keep in mind that this is data that's a little bit outdated.
And so it went until October 2020.
But it has a logarithmic scale on the y-axis.
So keep that in mind.
And you can see that it seems that DAI, at least until then, was the most popular coin.
So this is one of the most popular coins that got lent out in flash loans.
So based on these data, you might ask yourself, so what is actually the use case of a flash loan?
So where can I take advantage of a flash loan, or what can I do with a flash loan?
So in most cases, a flash loan is rather helpful in DeFi attacks.
So it's not the cause of a DeFi attack, but it's a tool that an adversary can use.
So in particular, if you're using a price oracle, or if a DeFi platform is using a price oracle that can be manipulated with significant transactions, trades, then a flash loan is like a perfect tool to manipulate this price oracle, as well as
to perform potential pump and dump transactions.
And that's why I would always recommend to use more advanced price oracles, like either external ones or time-weighted average prices as reported and now used by Uniswap, for instance, which are quite more resilient to DeFi attacks based on flash loans, which is great.
A flash loan-- the probably biggest and most beautiful use case I've encountered so far for flash loans is Arbitrage.
We have already looked into this earlier.
In the next lecture, we will revisit this in the next slide.
There's also the option of performing wash trading.
You remember if we have two markets with asset X and Y, then-- this is for Arbitrage.
But if we have wash trading, you might want to purchase here asset X-- or sell asset X. You get asset Y. And then with the asset Y, you get-- again, here, you sell asset Y for asset X.
So this is a cyclic trade on this particular market here.
So this could be a form of wash trading.
I mean, it is a legitimate trade.
But your intent here is probably not to make a profit, but rather to just artificially inflate the trading volume.
And you can obviously use flash loans to perform these types of transactions, as you can also do it to communicate or to exchange assets on any other markets with these assets.
And the beautiful thing is really to keep in mind, if you take a flash loan-- so let's say this is the flash loan provider.
So we can go and do anything.
You can buy assets here, then hop here, then go there, and hop back to the flash loan pool.
The only important thing is that you pay back your flash loan here by the end of the transaction plus the accrued interest.
Another feature that I have not found much use yet, but there might be some use, is flash minting.
So if you do have a token, like an ERC 20 token, you can inject a functionality that this token is mintable, but flash mintable, meaning you can mint as many tokens as you'd like, but you would need to destroy these tokens by the end of your transaction.
So that's not something that you could use in governance voting because governance voting typically is over a time frame, for instance.
But that's basically a potential use case, but I haven't seen anything.
If you know of any cool use cases of flash minting, please comment in the chat or in the forum.
And finally, collateral swapping is actually a use case of flash loans that's actively being used, where the borrower can replace its collateral with a flash loan.
We will show a particular example at the end of this segment.
So to iterate on flash loan arbitrage-- so remember, we have here two assets, two markets-- a Curve Y pool and Curve sUSD pool.
And we do have a flash loan provider.
The flash loan provider only requires a payment of plus 1 wei as a fee, which is rather cheap.
So what we can do is we can create-- we can request a flash loan of 2.048 million USDC.
We then exchange it at the Curve Y pool for a specific amount of DAI.
With this DAI, we go to the Curve sUSD pool, and we exchange it back to USDC.
And we end up with a profit of about 16,000 USDC.
Now, at the very end, we have to pay back the initial flash loan that we requested plus the one wei.
And this is where if you have to pay a 0.03% fee, depending on the size of the loan, the profit might be really small.
So this is where, especially for arbitrage amounts below 100 million, I think the dYdX pool is probably the best flash loan provider for you because you're just paying a much smaller fee than in bigger providers such as Aave and others.
So keep in mind that there's no official documentation for the dYdX contract on how to use flash loans there.
It's not that simple.
It's not as simple as an Aave.
But it's likely worth it, especially if you're being a professional arbitrageur.
So another use case of flash loan is flash loan-based liquidation.
So when a liquidator does not have access to the cryptocurrency that he needs to repay debt, he can use a flash loan.
This obviously only works when the liquidation mechanism completes in a single transaction.
So otherwise, this would not operate.
So given a liquidatable borrowing position with a debt of 2,000 DAI and collateralized by 2 ETH right here, as in the following, we can do this.
So we can take 1,000 DAI from a flash loan.
And we provide the 1,000 DAI.
So from the 2,000, right.
We repay 1,000 DAI, and we can then claim 1.5 ETH.
Just a numerical example here.
So given this 1.5 ETH, what we can do is we can swap this at Uniswap, for instance, or some other decentralized exchange.
And we can all do this in a single atomic transaction.
So we go to the ETH/DAI market on some [? decks. ?] And we then receive 1,500 DAI from the swap.
We proceed to pay back the flash loan plus potential interests, let's say 1,000 DAI here, and we end up with 500 DAI in profit for this particular liquidation.
So that's naturally making the liquidation mechanisms much more flexible and quick because the cost for on-chain liquidators are rather minimal.
They only have to be aware that this is happening, and if they can issue this particular transaction, they need to manage the gas price, et cetera.
And this is really making flash loans useful and liquidations more stable compared to a model where the liquidator needs to operate a cash reserve to perform a liquidation.
The last example that we're looking into in this segment is the collateral swapping.
So let's assume that at the beginning, we have a debt position here where we have a debt of 1,000 DAI.
And we have a collateral of one ETH that we're currently having in this position.
Now, imagine that you want to keep the debt of 1,000 DAI, but you no longer want to hold the ether in this particular debt position.
So you want to swap it.
You want to swap it out for something else.
You want to swap it for USDC.
Then the following steps can be taken in order to swap out the debt position.
So step number one, we take a flash loan of 1,000 DAI.
We repay the DAI.
So we repay the debt basically in the flash loan.
This allows us to regain access to the Ether that we collateralized earlier.
And now we can swap the Ether, for example, to 2,000 DAI, if this is the equivalent value at the time of the flash loan.
Then we simply need to collateralize the 2,000 USDC if the lending market allows such collateral.
And then we can borrow again the 1,000 DAI.
So this is basically the entire process of collateral swap.
So we end up with a more stablecoin debt position.
So the risk of being liquidated here is much smaller compared to the volatility of the ether currency in this particular example.
Now, keep in mind that flash loans overall are quite expensive in terms of gas prices.
So you would not want to do this every day.
But at times of high volatility, maybe it can rescue your debt positions.
Thank you so much for your attention.
Flash loans are a truly amazing construct.
And if you have any idea of what use cases we can perform with flash loans, please let us know.
We're eager to hear about your ideas.
Loading video analysis...