December 11, 2023: Scrape Framework 🦍🐍
$crvUSD Ape Tutorial 3: scraping a user's historical $crvUSD loan data
Lesson 3 in our $crvUSD Ape Framework tutorial is live:
For more information on how to generate the chart in the video by using Ape Framework to iteratively scrape over historical blockchain data, we recommend consulting the companion Github repository, which has all the code you may need.
We wanted to use our newsletter today to go into a deeper review of what exactly was happening on the user’s actual $crvUSD loan history.
What this all means…
GREEN: A solid line showing the price showing the history of the WBTC price pulled from the market's AMM Contract, compared with a shaded range showing the user's liquidation price.
RED: The user's corresponding loan health history as measured by the market's Controller Contract.
BLUE: The user's percentage of collateral which has been converted to stablecoins, as opposed to its original WBTC collateral.
The particular user we chose to single out was a good sample because they were notably degenerate.
When taking out a $crvUSD loan, it’s possible to set the number of bands, which corresponds to the size of the green shaded band. A typical loan will default to 10 bands, a fairly normal safe range that allows your position to stay relatively safe from entering into soft liquidation.
This user, in contrast, selected just 4 bands and initially pushed it right up against the liquidation price. This made their above loan history extremely volatile. The red line showing loan health frequently abutted the dangerous 0 mark, where the rare and dreaded hard liquidation would have taken place.
In this manner, the risky loan highlights the $crvUSD liquidation protection mechanisms. Every time the green line dips into the liquidation range, observe how the blue chart spikes. This signifies the collateral being automatically converted into $crvUSD as a means of protecting the user’s collateral value.
The user was mostly very active in maintaining their loan to avoid this “soft liquidation” mode, as evidenced by the frequent changes in their liquidation range signified by the green shaded line. Even though the entire system does a good job of protecting users, being caught with your hand in the cookie jar is no fun. When your portfolio has been converted wholly to stablecoins, your ability to interact with your loan is severely curtailed, so the user does a good job of actively managing their position wherever possible.
Note that the user does not really represent the median $crvUSD user. Most users take a far more conservative approach to their loans. Due perhaps to the HODL-er mindset of Bitcoiners, the WBTC market tends to capture users who keep their collateral well outside the liquidation range by simply taking out a smaller loan and leaving their liquidation price well below liquidation range.
The $crvUSD website’s LLAMMA Details tab shows how several users are clustered around a liquidation price of $29K, with a long tail of users stretching this as low as $3000.
A more typical loan might look like this passive user, who basically just took out a fairly safe loan and let it ride.
You can observe the wider n=10 band range, but they managed to avoid ever being at too much risk. At the outset, when Bitcoin dropped from ~$30K to ~$25K, they revised their position lower to keep their health factor above 40. Since Bitcoin skyrocketed, they’ve kept their loan intact, as their health has also rocketed above 100.
At this point, if the user wanted, they could borrow significantly more. Perhaps they worry that Bitcoin’s price rise is illusory and may crash. They may be dissuaded from doing so by recent spikes in the borrow rate.
This is one of the more common users stories for $crvUSD borrowers. The soft liquidation protections provide a nice user experience for more passive users who don’t want to fret actively manage their loan. One may hypothesize that the fact $crvUSD users are generally unwilling to repay their loans amidst higher borrow rates suggests that users are willing to pay a premium for the superior user experience. Or maybe they just ran out of gas.
December 6, 2023: The Cash Cow 💰🐮
We’ve got ourselves an old-fashioned standoff. …borrowers won’t repay… …so $crvUSD won’t repeg… …so interest rates keep going up… …to force the borrowers to repay… …but (go back to the first step) The cycle will continue until somebody blinks. And Curve is the unambiguous winner until such time.
Although the typical user takes a rather slow and steady approach, it’s the active loans who are the interesting ones. We’ll close by showing off another user who showed off how to play with a leveraged loan.
This user also took out a very risky position. They also used just 4 bands right at the maximum liquidation price, and also happened to max out their leverage around the time the feature was introduced.
This happened to occur at a time where Bitcoin price was relatively stagnant in the $25-$30K range. They initially got bit, riding out most of the crab market underwater in the “soft liquidation” mode. Yet they kept their position open throughout, with their health getting gnawed away at very slowly while their position was in range.
Fortunately, as many of us saw, the price of Bitcoin has skyrocketed in recent weeks. As Bitcoin price jumped above $30K, they sort of doubled down, using the price gain to take out more leverage.
If you’d like to extend this script to produce your own visualizations, or simply use it to explore more user loans, we’ve made it quite easy to use. The configuration can easily be adjusted to look at other controllers or users.
Simply swap out the USER_ADDRESS to the address of interest, and flip the TEST_MODE to false when you want to get a finer resolution (note that a PROD_RESOLUTION of 1000 took about 20 minutes on my Macbook Pro).
If you wanted to scrape the full history of every loan, the smart contract infrastructure is thoughtfully designed to permit scraping. For the WBTC Controller, you can iterate over every active loan using a combo of the n_loans()
function to get the bounds and iterating through the loans()
function to grab the user associated with a loan index.
Similarly, if you want to dynamically pull every $crvUSD market, as opposed to just WBTC, you can look at the Controller Factory and loop over all 6 active collaterals (and any future additions) to pull the controller address.
Ape Framework is one of many tools data scientists should know about for scraping historical blockchain data. Please let us know if you have any questions, or if you adapt the script for your own usage.
Useful Links
Github Notes
Within the technical deep dive, we explore the following Ape Framework concepts:
Chain Manager
Logger
Block Identifier