*Welcome back, everyone! Kickflip is here to do some more modeling for us 📷😎. While the previous entry in the modeling DeFi series isn\’t mandatory for understanding this entry, it is a good read! Check it out!* *But, if you\’re unfamiliar with lending, this is a must-read!*

## Lending and Liquidation

Given the recent liquidation cascades on FTM and other networks, I wanted to learn more about the mechanics of these cascades. Sure, we all know; price goes down, user gets liquidated, price goes down more. But I want to dive a little deeper into the nuance of DeFi lending and introduce some risk considerations.

I modeled price impact equations, a collateral and loan to value (LTV) framework, and a liquidation event. Some of you may have seen my Twitter thread on the topic, but I hope to provide a more detailed explanation of the model and outcomes in this installment. I will take readers through the equations that govern price impact and look at some parameter sensitivities in liquidations. The underlying concept of price impact is how LPs rebalance, and arbitrage becomes possible. I will offer prompts throughout, but I challenge readers to think critically about how LP rebalancing and arbitrage work. They are topics for a separate discussion.

Like last time, please note that this article may become quite technical, so feel free to follow along with a pen and write the equations yourself to help build your understanding. Alternatively, reach out to the community to help you understand bits and pieces.

## First Step: Price Impact

Start with some kind of liquidity pool that has two tokens in it. One of token A and one of token B, both tokens are worth 1. In mathematical modeling, we call this normalizing. In reality, the LP I described is ridiculous, but the variables are irrelevant to what we are modeling, so we can call them all one and see how they change as a percentage.

In most AMMs, Price Impact is governed by the Constant Product (CP) equation:

That is, the product of the quantities of the two tokens remains constant. If there are 50 of each token, the CP is 2500. This equation governs price impact but also scales with deposits and withdrawals. If a swap is conducted, the CP holds constant, and if liquidity is added or removed, the CP increases or decreases accordingly.

The case of adding or removing liquidity is fairly intuitive, but I will present a quick example of a swap. If FTM were valued at $3 (those were the days), then an LP may contain 1000 FTM and 3000 USDC. The constant product is therefore 3000000.

Now, if someone were to swap 200 USDC for FTM, they would deposit their 200 USDC and get back some FTM. When their USDC goes in, there is 3200 USDC in the pool. It’s a swap, so CP holds. Therefore, the pool rebalances according to 3000000/3200=937.5 FTM. The seller will receive 62.5 FTM (the entire 1000 FTM is accounted for). Notice that they didn’t receive 66.67 FTM? Their swap was a large percentage of the initial pool, so there has been a significant price impact! The price of FTM in the transaction was $3.2 (6.67% price impact).

If the pool were much larger, the price impact would have been much smaller. Hence the importance of ‘deep liquidity.’

Back to the model; we have 1 of each token, so in our case, CP = 1.

## Second Step: Collateral, Borrowing, Liquidation

If we offer collateral, we need to have a means of liquidating, so we will not provide more collateral than 1 of either token because there is only 1 of each token in the LP. We also want to control LTV. LTV describes the amount you can borrow relative to your collateral. For example, if you provide 1 unit value of collateral, at 50% LTV, you can borrow up to 0.5 units value of another asset. The more you borrow, the more you risk approaching the liquidation threshold if price action is unfavorable.

For this model, I looked at every combination of Token A collateral between 0 and 1 and Token B LTV between 0 and 1 to see the price impact if liquidated. To have a detailed enough look between 0 and 1, I discretized the range into 100 steps, meaning there are 100 points between 0 and 1 to be modeled. The more points, the more detail, but it requires more computing power, and the detail becomes unnecessary.

We have liquidity, we have collateral (Token A), and we have borrowed assets (Token B). There are a few things to consider in this modeled liquidation event. The model ignores price action and treats liquidation as instantaneous. When you borrow, prices have to change for liquidation to occur, so consider that prices have changed to the conditions in the model, so we can look at what happens.

## Surf’s Up

This is called a surface plot, and in this case, it demonstrates the price impact of liquidation under the borrowing conditions I described earlier.

The plot is 3-dimensional, so I can rotate it and look around in the model. I think this view is pretty good for this discussion. The X- and Y- axes represent LTV and the number of assets loaned compared to the quantity in the LP. The Z- axis represents the price impact of liquidation. As you’d expect, when we borrow more, the price impact is higher, but we can also look at sensitivities (how quickly the price impact changes when we change the loan amount and LTV in this case). Note that this model presumes partial liquidation, meaning not all of the collateral is liquidated. Instead, enough collateral to cover the loan based on LTV. In the case of a maximum loan (collateral 1, LTV 1, therefore Qty Loaned 1), there is a 50% price impact. Recall that if we have 1 of each token in the LP (CP: 1*1=**1**) and we add 1 of Token A (1+1=2), we will receive 0.5 of Token B (CP: 2*0.5=**1**). So the more collateral the system needs to liquidate, the trickier it becomes to recover borrowed assets.

## Risky Business

This all lends itself to the relationship between lending and liquidity in DeFi. Lending protocols that genuinely care about their ecosystems should carefully select collaterals and allocate LTVs based on available liquidity to ensure the system can handle price impacts from liquidation.

In theory, a lending protocol that monitors market liquidity and current lending/ borrowing statistics could manage risk by altering offered LTV to control price impact from liquidation concerning price action (Collateralisation Ratios can also provide a buffer for price impact). That’s quite a mouthful, but it makes sense in my mind. Expect more price action for volatile assets and adjust risk parameters accordingly. The same goes for less volatile assets and again for stablecoins. Reality is more nuanced and merits considerable discussion, but that is for another time.

## Thanks

Once again, if you made it to the end, thank you. I hope you learned something from this write-up. Please reach out and let me know if this piece was useful (or if you think I made any mistakes). I’d also like to hear from you if there are any concepts you would like to explore. I’ll see if I can figure it out.

Pop pop,