# Uncovering hidden risks in DeFi lending with Agent Based Modeling | Jakub Warmuz | ETHWarsaw [4]

- Channel: [ETH Warsaw](https://streameth.org/eth-warsaw)
- Date: 2025-11-09
- Duration: 28:55
- Watch: https://streameth.org/watch/yt-qhhNr97WCuc
- YouTube: https://www.youtube.com/watch?v=qhhNr97WCuc

## Description

Jakub Warmuz from Twyne shows us how agent-based modeling helps surface emergent behaviors, liquidity shocks, and hidden protocol fragilities with case studies on liquidations and parameter tuning.

🎥 Recorded at ETHWarsaw 2025

Follow ETHWarsaw on social media for the latest updates!
X (Twitter): https://x.com/ETHWarsaw  
LinkedIn: https://www.linkedin.com/company/ethwarsaw
Telegram chat: https://t.me/joinethwarsaw

## Transcript

uh the hidden risks in DeFi with agent-based modeling. Welcome him. Warm welcome. Awesome. Thank you so much. Uh so yeah, my name is Yakoup. I'm a co-founder and a data scientist at Twine. Uh and the title of my presentation is uncovering hidden risks in DeFi with agent-based modeling. uh but more broadly I will talk about uh the lending markets what are the problems with them currently uh how what we're doing at Twine aims to solve those problems how risky what we're doing is and what was the methodology that we used to really find out about find out about that uh find it out and uh what did we discover in the process all right so let's jump right into it um can I maybe get a quick show of hands who knows about lending protocols like aa oiler or morpho okay I think everyone almost everyone so I will not um explain how those protocols work. I'll assume everyone kind of knows uh what's what they are about. All right. So um the first thing I want to talk about is the LTV distribution. So what you can see on this chart is the loan to value distribution of all of the users of AVA V3 on uh the Ethereum mainet. Uh this snapshot was taken around the beginning of the summer. Uh but generally I uh checked a couple of different different time frames and generally you would see um qualitatively the same chart regardless of when in time you would take the snapshot. Um and I want you to pay attention to the two um to the following. So on the left side we have uh maybe maybe first um on the x-axis is the LTV on the y- axis is the uh total supply and and a given LTV bucket right. So on the left side, what you can see are all of the users who are only depositing their collateral to a lending protocol, but they're not borrowing anything. Now, that might sound stupid, but uh those people are not really stupid. They are doing it because they only want some passive yield on their deposits, right? They have they're sitting on a bunch of ETH. They want to deposit it to a lending protocol and they want to earn some yield on it. Um that's how they're using lending protocols. And as you can see, this is the biggest by far uh cohort of the users of lending protocols. And uh this is not only on a right if you were to do the same analysis for for like oiler morpho you would generally see um a very very similar chart. Now on the right hand side are uh the emote loopers right so uh the efficiency mode uh users. So basically a allows you to take a position in correlated assets at higher uh liquidation thresholds. uh so you can yeah like leverage up uh much much much more uh and that's of course possible because the assets are correlated so you don't have uh the same liquidation risk and finally in the middle are all of the like um you know traditional lending market uh users borrowers uh right so those are people who come to the protocol deposit some collateral borrow some assets now what's going on okay cool that's on my oh maybe I'll try again. Are we live? No. Well, it clearly is on. Okay, awesome. Cool. We're back. Uh, okay. Cool. Uh, yeah. So, those users, but as you as you can see, they're mainly concentrated within like 20 to 40% LTV. Now, Ava generally would have 60 to 80% for most of their uh assets uh as as loan to value ratio. So as you can see most of the users are maintaining some safety threshold between their liquidation levels and their actual positions right because of course nobody wants to get liquidated. I think every every lending market user has been in a situation where they go to sleep the prices they wake up to prices dropping by 10 15% their position gets wiped out. That of course sucks, right? So we have a couple of problems here, right? First, uh there are the passive lenders who have a lot of borrow borrowing power that they're not using whatsoever. Uh then there are the borrowers who are restricted by conservative risk parameters and generally do not get to use the full um value of their um the full borrowing power of their collateral. Uh and then the protocols of course suffer from low uh utilization rates if those people cannot take high leverage uh and are restricted by conservative risk parameters. Okay, now let's let's look at the journey of a boro specifically. Um another way to um to represent LTV is using a hell factor which is um basically the the inversion of it but the collateral is scaled by its loan to value ratio. Right? So the uh that hell factor would be collateral times loan to value ratio divided by the value of the debt. Um so generally as a as a borrower you would start at some point above uh one uh right which is uh basically when your health factor reaches one that's a liquidation threshold any position can be liquidated right so you generally start at um at a health factor above one and at some point the prices might start moving against you and if they keep on moving against you at some point you might get liquidated right uh but now let's play pretend that the liquidation threshold is actually not at one but we're going to move it slightly slightly slightly lower, right? And we let the position evolve over time and let's see what happens. So two things can happen, right? Either the prices can keep on moving against you, in which case you would still get liquidated at that lower liquidation threshold or the prices can mean revert in which case the position becomes healthy again at some some uh point in time. Okay, so that's kind of the intuition we had around uh around the journey um of the borrowers on the protocol. Uh but now we looked at the actual data. So what you can see on this chart are um the AVA liquidations um all all all of the AV liquidations I think until like um end of last year um on a v3 as well and uh on the x-axis you have hours after a liquidation event took place on the y-axis the health factor so basically the first time stamp is uh the moment at which a position got liquidated now every line represents um different borrowers um health factor over time. If we pretend that they didn't actually get liquidated, right? So, what we're doing here is we're saying, okay, AVA gives you, let's say, 80% loan to value ratio, we're going to pretend that it wasn't 80%, we're going to pretend that it was actually 90%, and we're going to let the position evolve over time using the historical prices and kind of see what happens. So, what does happen with only adding only 10% of more borrowing power, we can save up to 94% of all of the liquidations, right? um that's that's represented by those blue lines, right? So the blue lines are all of the positions that wouldn't have gotten liquidated at this lower threshold. The red positions the red lines are um those positions that would have gotten liquidated even with this with this riskier uh parameter setting. Okay. So uh Twine is um a credit delegation protocol that aims to solve those two problems. So unused borrowing power of passive lenders on lending protocol and then borrowers being restricted by conservative risk parameters like like loan to value ratio and thus getting liquidated because of them. How can we do it? We can create a market such that the people the people who have unused borrowing power can lend it out to borrowers so that they can take either higher leverage loans or they can uh you know protect themselves themselves from liquidations by using that borrowing power as their collateral. How does it work mechanistically? Um so we have those two two types of users right we have what we call credit LPS those are the people who have uh unused borrowing power uh and we have the borrowers. Now the journey starts with uh with a credit LP going to an external lending protocol like A for example depositing their tokens and um they get receipt tokens back right those receipt tokens represent the borrowing power that people u that that those users have on the protocol um now they can deposit those receipt tokens to a credit vault on Twine now when the borrower comes to to the protocol let's say they want they want to use oiler and they you know they see that their ETH has 7 85% uh liquidation LTV but they want to take higher leverage. they want to uh you know leverage up by let's say like whatever 10x uh so of course they can't do it with 85% uh liquidation uh LTV they need they need a higher threshold uh but they still want to use the underlying protocol right because for example oiler is established it's safe it's well audited they want to keep on using that protocol so what they can do is they can deposit their collateral to our collateral vault and collateral vaults are u basically vaults owned by um a specific user on the protocol and now this collateral vault is a special type of a vault that's allow out to uh borrow that borrowing power from the credit vault, right? That the uh that the unused power was was deposited into. So the collateral vault u borrows the the receipt tokens from the credit vault on the on the borrower's behalf and deposits the user owned collateral plus that additional borrowing power to the underlying protocol for example to oiler right on a doesn't doesn't really matter. Um and then of course the borrower you know from from the external um protocols perspective now the position has like the collateral there's more collateral simply right so you can borrow more as a as a borrower right so let's say it was 85% now you borrow additional 10% borrowing power from the from the credit LP now your liquidation threshold is 95%. So why we think that is pretty cool? Uh first we are using the underlying lending protocol, right? We don't have to bootstrap liquidity on on both the lenders and the borrower side. Um what's what's the problem here? Um from scratch. Okay. Hello. Yeah, I need to be um clicking this thing more often. Okay, cool. Anyways, um yeah, so we're still using the the underlying lending protocol, right? So, um for from the lending protocols perspective, we are still fully adhering to their risk parameters. So, we're not really changing the structure of the risk. We're not really messing with their uh risk assessment in any way because from their perspective, uh a borrower on Twine is still fully fully over over collateralized and within the confines of their risk parameters. And doing that increases the utilization rate on the underlying lending market, right? because we're taking the unused borrowing power of some of their lenders and we're allowing other people to borrow more from the protocol which increases the utilization rate and that's how the lending protocols make money. Um okay so we had this new design we had this new idea but um you know same same old questions that every DeFi founder will face at some point right how do you really go about choosing the choosing the parameters what liquidation loan to value ratios are safe how do you use them um how do you how do you choose your interest rate um can the protocol get insolvent if so under what circumstances and finally uh when can lenders on the protocol uh lose their deposits and this is um this this last question is what I wanted to focus on during this uh talk. Okay. So how did we go about uh about measuring that? So we had three different distinct ways of uh of really going about this. First was uh the theory, right? So um at this step we want to develop a really uh deep understanding of the mechanics of the protocol. We want to identify all of the different variables of all the different parameters, how they interact with one another and hopefully at the end of that exercise uh develop an analytical framework. Um that's you know allows us to to measure that exact um problem that we're that we're um we're tackling. Another way we can go about this is by building a simulation. Right? So that's this agent-based modeling part from the title. Uh so we can develop a simulation engine and that in in our case using Python that basically represents uh all of the all of the mechanics of the protocol. We can unleash agents on that protocol to take different actions like um regular users of the protocol would and then we can use Monte Carlo simulations and different scenario testing to really try to break the protocol and see under what circumstances it breaks and uh you know measure uh what we're trying to uh to really measure here. And finally, we want to do the same thing but using the actual contracts, right? Like the our our theory might be right, our simulation might be right, but if the contracts do not uh perfectly match what we intended, then you know like this whole exercise was kind of useless. Uh so we at the end of that we also want to use the contracts, run the tests and uh really make sure that everything matches up to the same uh to the same result. Um okay so again the problem that we're we're thinking about when the position through Twine gets created right you have the user user owned deposited collateral right in that case that's $100 $100 but also a part of that position is comes from the unused borrowing power that the that the borrower borrower has um you know taken from the from the credit LP. Now at some point that position might get liquidated, right? Um and there are two different liquidation types on Twine. So first one is internal and what that means is that the the liquidation proceeds through our contracts. In that case, we simply do our post liquidation accounting and there's simply no way for the um for the for the credit LPS to lose their assets in native terms of course, right? like in in dollars it might be something uh it might be a completely different story but in native uh asset terms but the position might also get liquidated on the underlying lending protocol right and in that case our CLPS might be in trouble there might be scenarios in which they they lose money now the question is how much do they lose under and under what circumstances okay so first the theory so after after studying the problem uh we uh analyzed that we well we we found out that Um this those CLP losses basically depend on a couple of factors. Some of which are internal to our protocol and some of which are external. Um the internal ones are the safety buffer and the maximum liquidation LTV that we allow the users to take. Um the safety buffer is basically like a distance between uh when the liquidation can be performed on Twine versus on the underlying lending protocol. Um then there's the liquidation LTV chosen by the user, right? So they can basically when they when they establish their lending position they can choose anything between the underlying lending protocols uh LTV and the maximum allowed by twine and finally there are the external parameters there's the uh the liquidation LTV that they uh that they choose for a given for a given asset and there are closing factor and the liquidation incentive realized uh by the liquidation. Now those last two are the most important because for us the first protocol we integrated was Oiler. Some protocols have static closing factor and liquidation incentive. AVA is an example of such a protocol and in that case you know you you know like you know beforehand what those values will be right. So you can kind of design your your risk uh management around those numbers but for example in case of oiler that we integrated with at first um they run Dutch auctions. So you simply have no way of knowing, right? Because every every um liquidation that happens can happen with a different configuration of those two parameters. Okay. After uh after this entire analysis, we came up with a formula. I'm not going to really go through it cuz yeah, it looks ugly. Um well, maybe not ugly, but um yeah, probably difficult to explain in words. Uh but anyways, that's that's that's the formula we have. Okay. So we can plot that uh CLP credit uh liquidity providers uh losses as a function of this liquidation incentive and closing factor. And this is what we get. So on the x-axis are the liquidation incentives on the y-axis is are are the closing factors. Um the blue parts represent uh you know regions where CLPS don't lose any money. The red ones represent when they lose most money. Right? you have the the legend on the right side. Um the the dashed line represents what we call a theoretical CLP loss frontier. So basically everything to the left of that of the dashed line is a region in which uh CLPS do not lose any funds and everything to the right is where they might lose some. Right? Um and as you can see there are two charts and the distinction between those two charts is that this risk surface uh looks completely different in two different regimes and those two regimes are when the row parameter is less than the safety beta or greater than the safety beta. Now what is row? The row is basically um the ratio between the user chosen liquidation LTV and the max allowed by the protocol. Right? So let's say we allow the maximum of 95 but the user can choose anything between let's say 85 and 95 right so this row would be would would be um would denote that fraction that fraction so as you can see in this latter case which is probably more likely to occur um the CLPS are likely to lose some money which of course sucks. Um okay now the other mode of cognition we have here right is the simulation engine. So in our case we used Python to uh implement uh an agentbased model that represents how the protocol um behaves and in our case this um the simulation engine is um composed of four different modules. So first we have the twine state machine uh that's basically you know a module that represents how all of the different interactions on the protocol impact uh h how different state transitions uh occur in the protocol as users interact with it. Um then we have the simulation assets which is a module that's responsible for you know fetching historical prices historical volatilities um using different pricing mechanisms uh stuff like that and then finally we have an AMM module that allows us to simulate swaps and I'm not really going to go into details of why this is important but uh the liquidity on AMMs is absolutely crucial for understanding the risk of lending markets so we're trying to do it in as um realistic way as possible. That's why we have this this module developed. Um, and finally, there's a simulation manager that kind of ingests all of those different modules and allows us to really um perform um the different simulations, different scenarios, run Monte Carlo, parallelize the entire thing. Um, yeah, etc., etc. Okay, so uh we have the simulation engine, we run it and at the end of it, we we of course measure the CLP losses. At the end, we end up with the same charts, which is awesome, right? because we we used two completely different ways uh of of measuring the problem and at the end we arrived at the exact same solutions. Right? So that tells us that first both our simulation and the analytical results are good and second we have a really good understanding of the problem. Um okay so again um as you can see in this case of row being greater than safety beta there's a pretty big um loss surface for the for the CLPS. We started thinking okay how can we change our mechanism to really minimize those losses and one of the ideas was uh making that safety safety beta parameter dynamic right because in the in the original iteration of the protocol it was static uh we came up with a with a way to make it dynamic and hopefully minimize the losses um the cool cool thing at this point is that you know we don't have to go straight to uh changing our smart contracts but we can actually first implement that in Python which is way way faster after uh we can run the simulations and actually see what happens. So what happens we were able to actually minimize that that that uh loss surface. So what you can see on this chart are um again those loss surfaces basically the the left column are the situations where the row is less than the safety beta. The right hand side is where it's bigger. The point is in both of these cases the risk surface is smaller than it was initially and uh the top row represents the uh theoretical um results. The bottom represents what we got from the simulation. So again as you can see virtually the same results. Now you might say wait a second uh there's that red uh part on the on the right side in the simulations. What's that? Uh and your your theory sucks because it doesn't represent that well. Um we were kind of perplexed by that ourselves as well. Uh but after really investigating what happens in this in this area, we realized that those are so-called toxic liquidation spirals. Uh which funnily enough we wrote a research paper on like two years ago. Uh so it was very cool to see that uh what we forgot about in our theory was actually uh spotted by our simulation. Um okay. And then finally contracts, right? So like we did we did this exercise that's awesome. But now if our smart contracts are not really implementing the logic in the exact same way, you know, like it doesn't really matter what what what we you know, all of the all of the findings we did we had. Um so we can we can do that as well, right? We can set up a bunch of foundry tests on a local fork. We can we can set up artificial positions. uh we can you know nuke the prices and liquidate people at different and varying um closing factor and liquidation incentive uh pairs uh and actually liquidate them through oiler contracts right which is pretty pretty important right because in our case we are integrating with oiler first um and yeah those are those are you know like not only our contracts are the dependency but also the the external contracts so we were able to um yeah measure that with uh with uh with the use of of foundry and some tests and at the end we got the exact same results as both our theory predicted and the simulations gave us. So that's awesome right because our theory our simulations and our contracts match uh perfectly. So uh yeah that makes us think we have a pretty pretty good grasp of how this uh problem uh really um really looks like. Okay, but so what right like why did we go through through all of this trouble? um we have a okay so we developed the theory right and then we use those two different ways to really confirm that our theory is correct. Now we can go back in time and look at all of the historical liquidations that have happened and we can assume the worst case scenario right like what if all of those all of those users that got liquidated were actually using our protocol with the riskiest uh settings possible and all of them got liquidated on the external lending protocol. Uh what happens? How much how much money do our CLPS lose? Well, this is what that chart on the left side represents. So on the x-axis you have the CLP loss and percentage percentage terms and on the y-axis you have the the frequency. As you can see the biggest by far back is uh you know at like minimal CLP losses which is which is cool. But you can say well but the the tail is pretty huge right like you have some liquidations that lead to like even 40 50% CLP losses. So that that must suck for those CLPS. Well, not really. Because if you actually scale that by um by the dollar value of those positions, turns out that all of those outliers were basically D positions that the liquidators didn't really bother liquidating because probably the gas was too high at that point. Um okay, so if you actually run the numbers, the expected CL loss from all of these liquidations was 0.1%. Which is quite amazing, right? because you can shift the risk, you can shift the the liquidation threshold by even 10%. And as a person who's underwriting those loans, you would only expect to lose on average 0.1%. That's pretty remarkable because if you flip if you flip that finding, if you're earning as a CLP, if you're learning if you're earning at least 0.1% um in and interest from from those activities, you should be making money, right? it should be profitable for you to do it. But okay, we can go even further and now we can ask okay like what would um the expected CLP losses be at different liquidation thresholds right because this is this is the parameter that we have control over um and you know of course we want to you know give users maximum liquidation LTV possible but you know that comes with c certain risks what those risks are we can plot it and that's what you get on the on the right hand side um right so on the x axis you have the maximum liquidation LTV on the Y-axis you have the weighted uh CLP loss and as you can see this line is pretty much flat until a certain point and after that inflection point it uh starts growing exponentially. Um so yeah this this chart pretty much informed how we went about uh setting our risk parameters. Um this this inflection point if you will was around 95%. We went a bit more conservative so chose 94% for our initial parameter. Um, yeah, I think that's it. Um, thank you so much. We are live. We just launched two days ago, so we're pretty new on the Ethereum mainet. If you want to check us out, that'll be that'll be awesome. Um, yeah. And thank you so much. I think we might have some time for questions. Maybe we do have uh two minutes for questions. Are there any questions? No. Yeah, there is a question. There you go. Hi M if I understood correctly the borrowing power of the institutions that are holding fast amounts in for example stable coins might be used here. Yes. And they are um you are saying how to limit this risks but uh how do you assess these interest they can earn on it? Uh, of course, order of magnitude. &gt;&gt; You mean like what do we project it to be or like Okay. So, I I can tell you about like what &gt;&gt; how much is to be earned by them because what big is an incentive for those boring power institutions to actually get in those &gt;&gt; trades because the risk calculation is really uh stunning. Congratulations for that. I I haven't seen something like this but still those risk people in those boards will say okay but uh probably it might be higher so um what is the riskreward ratio? Yeah. &gt;&gt; Um &gt;&gt; thank you. &gt;&gt; Yeah I mean like riskreward ratio is probably a little bit subjective right and that's probably not up to me to really truly um tell for for an institution how to do that how to assess that. Uh but in terms of yield we are currently paying additional 3%. Um where whereas oiler on the same token is paying 2.2%. Uh so right so like by depositing your if you if you had like ETH on um on Oiler and you like deposited your E tokens to to our protocol you would get additional 3.7% on it. Um that number has actually dropped. It was like 8% this morning but okay cool. Uh but yeah, this is like uh fully sustainable, you know, like no incentives. This is the actual like real yield. Now, when it comes to the uh like what we think is a fair rate to be paying, we think low double digits are absolutely um reasonable. And the reason for that is that the interest rate that you are earning as a CLP is an order of magnitude greater than the interest rate that the borrower is paying. Right? Because as a borrower, you're only borrowing like a small fraction of your entire collateral position. So when you measure that interest rate in terms of the whole value of your collateral, it's tiny, right? Like you might be paying Exactly. Right. So like you could be like on if you go on our borrow page and you look at So this this assumes that you would be, you know, borrowing the maximum. It's like um yeah, and it's it's like minus one 1% uh you know, like a negative 1%. Okay, that's not not the best example right now. But yeah, like the point is that the the interest you're paying on like you as a borrower, you're paying interest on only a fraction of your collateral relatively, right? Whereas as a CLP, as a as a as a lender, you're getting all of that all of that yield back. So yeah, we we we run some analysis on like historical data. We think like double digits are like very low double digits of course, but yeah, that's like absolutely reasonable. &gt;&gt; Try it out. Okay, I guess thank you very much. Thank you for this presentation. It was really cool.
