Monitor your Maker CDP Debt Ratio in 5 minutes
Don’t get caught with your pants down on liquidations
For anyone who had a Maker Vault during the latest crypto sell-off (week of 9–13 March 2020), you may have been surprised to find that your vault had been liquidated without warning. For many, this meant the vault was sold off at auction and they lost all their collateral in the process. Today, we’re going to cover how to know when the CDP is getting dangerously close to the automatic liquidation point using the latest MakerDAO extension in BUIDLHub.
Maker Protocol Review
Let’s cover briefly how the Maker protocol works and it will hopefully shed light on how liquidation happens. When you create a Maker Vault, you stake ETH or BAT in a kind of escrow contract called a Vault (Maker also refers to it as an urn). Once staked, you can borrow DAI, using your staked assets as collateral. Like any collateralized loan, there is a buffer or margin between the collateral and borrowed asset values, and in Maker-speak, this is referred to as the CDP or Collateralized Debt Position. The Maker protocol requires that all vaults maintain a minimum of 150% CDP — meaning the total debt you take on cannot go below 150% of your collateral value. So if you lock 10 ETH in a vault, and the price of ETH is $200, you can borrow up to 1333 DAI and maintain a 150% CDP ((10eth*$200)/1.5 == 1333 DAI). Clearly, the price of ETH has a pretty significant impact on the CDP.
If the price of ETH were to drop to, say, $102, then the max you could borrow would only be 680 DAI ((10eth*$102/1.5 == 680 DAI). If, for example, you already borrowed 900 DAI to invest elsewhere, your CDP would suddenly drop to 1.13%, below the 150% minimum, and it would be liquidated.
What does it mean to be liquidated? Well, that’s where things get a little fuzzy.
When you look over the documentation, it kind of implies that you’ll be hit with a 13% penalty and you’ll get your ETH back (at that reduced rate). But that’s not actually what happens. Instead, liquidation means your ENTIRE vault immediately goes to auction. Unless you are running a Keeper bot to look for your vault and outbid others, you lose ALL of your ETH! We will cover running a Keeper bot in another article; for now, let’s see how BUIDLHub can automatically notify you when things are getting close to that 150% liquidation threshold.
We recently added some MakerDAO functionality as an extension in BUIDLHub. Specifically, the extension allows you to subscribe to proposal submissions and easily query for your vault’s CDP. We were hoping that it would be an easy case of adding an App to BUIDLHub and calling on-chain to get the CDP. But it’s not that simple. There are several RPC calls that have to happen to compute the CDP. And those calls are made across two of Maker’s contracts: CDPManager and the Vat.
Reading Contracts for CDP
The first step is to get the vault’s address. Each vault is represented as a smart contract itself but the address isn’t obvious anywhere on Maker’s dashboards — you have to look it up separately on-chain. Once you have the vault address, you can query for the various bits of information you need to compute the CDP. Below is a snippet from the BUIDLHub extension processing code. Note: it doesn’t have to be this complicated 😉
...const VAT_ADDR = "0x35d1b3f3d7966a1dfe207aa4514c12a259a0492b";
const CDP_MGR_ADDR = "0x5ef30b9986345249bc32d8928b7ee64de9435e39";//CHANGE THIS: get vatABI and cdpABI from Etherscan using addresses
let vatABI = null;
let cdpABI = null;//get ethers provider for mainnet (aka 'homestead')
let provider = ethers.defaultProvider("homestead");//create contract instances
let vat = new ethers.Contract(VAT_ADDR, vatABI, provider);
let cdpMgr = new ethers.Contract(CDP_MGR_ADDR, cdpABI, provider);//CHANGE THIS: get vault ID from somewhere like ENV
let vault = 0;//CHANGE THIS: get ilk/collateral string from somewhere like ENV
let ilk = null;//collateral type formatted into hex bytes32
ilk = ethers.utils.formatBytes32String(ilk);//lookup the vault address by its numeric id ( from Oasis dashboard)
let urnADDR = await cdpMgr.urns(vault);//ask vat for computation elements using urn and ilk//first, get the rate and spot from vat for the collateral type
let ilkR = await vat.ilks(ilk);
let urnsR = await vat.urns(ilk, urnADDR);let [__,
rate, //stablecoin debt multiplier (accumulated stability fees)
spot //collateral price with safety margin
] = ilkR;let [
ink, //collateral
art //debt
] = urnsR;//spot price is reduced by the 150% margin requirement. Add it back.
let colPrice = (spot.mul(150));//compute total staked collateral in native price
let totalCol = ink.mul(colPrice);//Compute the total debt including stability fee adjustments
let totalDebt = art.mul(rate);//adjust collateral 2 dec places and compute debt ratio
let cdp = totalCol.mul(100).div(totalDebt);//as a percentage
cdp = (cdp.toString()-0)/100;//do something with CDP
Anyone wanting to perform the simple task of knowing what their current debt ratio is could write this up in a Node.js script and get the CDP. This could be run periodically and you could notify yourself when that ratio falls below a certain level.
Alternatively, you could let BUIDLHub do that for you in a couple of clicks.
Current CDP Debt Ratio
First, head to BUIDLHub.com. We recommend you create an account—don’t worry, there’s no paywall.
When you’re inside the app, click “Design a Flow” and then click “Create a Flow” if prompted.
Okay, we’re now in Flow Designer in a blank template.
First, we’ll add a Timer trigger that periodically starts an event flow. We’ll set the timer to trigger hourly.
Next, we will query the Maker extension to get the CDP for a Vault.
Within the 2nd step, first, input “MakerDAO” as the action source and “Get CDP” as the specific action. Second, for required parameters, you’re asked to provide the Vault Number and the Collateral. The Vault Number
is a number, a counter originating from the contract that increments by one with each new vault. The address is determined from the vault number. Collateral
is either ETH-A or BAT-A.
Next, we will filter the CDP for a specific threshold. Note: if there is no DAI borrowed against the vault, the CDP will default to 0. So we need to filter for that as well.
For the third step, add Filter
as the action source and “Filter Input”
as the specific action.
When applying filters in step 3, choose to “Add Filter” and select collateralDebtRatio. Here, we set it to 300, which reflects a 300% CDP. We’ll be filtering the contract for when this query returns less than that 300%. You’ll manually have to add the <
and input the amount
.
Finally, if the filter passes it means we’re getting too close for comfort and should send ourselves a message to go and payback DAI or add collateral. In Step 4, we selected Email (you could also choose Discord).
Enter a subject and a message body, and then bingo! You’re all set.
Recommendations
What would be really nice is on the Maker/Oasis dashboard to be able to subscribe to a certain threshold and be notified accordingly. BUIDLHub could provide an initial implementation and then we can come up with a more decentralized mechanism for users to run their own notification bots. If you like that or have other ideas, head over to our new BUIDLHub Colony and propose a suggestion!
Looking forward to what these next few months hold in store for us. Share us some of your love ❤️ in our chats to give us new ideas or reach out to us by email to suggest new integrations!
Use Eventflow for FREE to dramatically reduce the time on blockchain-based event integrations.
Please join our community:
ColonyDAO | Twitter | Medium | LinkedIn | Telegram | Discord