When a borrower's health is negative and they have insufficient stablecoin collateral to pay off their debt, you can perform an "extended liquidation" through a callbacker contract.
Lesson 7 Recap
The prior unit, "pure liquidation", covered the easiest case, where a borrower's collateral had been completely converted to stablecoin through LLAMMA's `liquidation protection` mechanism.
This was a very simple call to execute. Much of Lesson 8 builds on Lesson 7, so worth reviewing in full before proceeding
Lesson 8: Extended Liquidation
This unit builds off the prior script, but adjusts the circumstances so the borrower does not get converted completely to stablecoin.
When the borrower’s health is <0% and the borrower is subject to liquidation, the borrower’s stablecoin collateral does not cover the debt:
To set the stage for this demonstration, the collateral price asset (WBTC) exhibits faster price swings than the prior unit, with a both downward and upward price pressure to push the borrower's collateral to the middle of their price range.
The traders executes large trades rapidly upwards and downwards to quickly whipsaw the price:
As a result, less than half of the borrower’s collateral gets converted into stablecoin, while the other half remains intact. Their health is subzero, so they are eligible for liquidation, but the collateral as stablecoin is insufficient for a pure liquidation.
To run the liquidation here, you can run the liquidate_extended()
function and convert the leftover collateral to stablecoin.
Within the vault's Controller contract, the liquidate()
and liquidate_extended()
functions are substantially similar processes.
Both trigger the internal _liquidate()
function, but the latter passes a "callbacker" contract address and arguments.
In the internal _liquidate()
function, the logic forks if the callbacker address exists.
The collateral is transferred to the custom callbacker contract and the callback_liquidate()
function is called. If repayment cannot happen at this point, the contract reverts.
Macket7 provides a sample callbacker contract to run $crvUSD through the Curve router. It demonstrates the callback_liquidate()
function called by Controller, and a liquidate()
function run by the liquidator to kick off the entire process
The video shows the entire process, including how the liquidator managed to pocket an extra $3K more than expected due to the routing logic.
Definitely worth keeping this script handy if you plan to borrow, or to liquidate underwater borrowers...
Further resources for this tutorial