$1,000,000 in security audit grants are live now, Apply here →

← Resource Hub

How Does DAI Work? MakerDAO Explained

Updated 9 min read

Quick answer. DAI is an overcollateralized stablecoin: you lock more collateral than you borrow, such as $1.50 of ether per DAI. Rate levers, borrower arbitrage and liquidations keep each DAI backed and near $1.

As of late 2023, when this walkthrough was recorded, MakerDAO held over $8 billion in TVL, and at one point it held around $20 billion. That makes it one of the largest DeFi protocols ever to exist.

So how does DAI work, and how does it hold its peg? This guide walks through the system as it stood then.

MakerDAO is a collateralized debt position (CDP) protocol. Its fundamentals will help you understand a whole family of DeFi protocols built the same way.

How MakerDAO Works

MakerDAO's product is DAI: a collateralized, and overcollateralized, stablecoin. It is a token onchain that is roughly pegged to $1.

To make DAI, you provide collateral. And you do not provide $1 of collateral for $1 of DAI.

Each collateral type has a collateralization ratio. For ether it could be something like 1.5.

That means if you give the protocol $1.50 worth of ether, you can borrow up to one DAI, which is roughly $1.

You do this by creating a vault, which the contracts call an urn. Most DeFi protocols call it a position, and this guide does too.

With $1.50 of ether, one DAI is the most you can borrow. You will usually want to borrow a lot less than the maximum, or you risk being liquidated.

Why Borrow Less Than You Deposit

Why would anyone put in $1.50 to borrow $1? There are constraints that force it, covered below, but the trade is still useful.

Spending without selling

Say you hold 10 ether and you think ether will go up. You want a new MacBook, but you do not want to sell ether to buy it.

  1. Suppose ether is $5,000, so your 10 ether is worth $50,000.
  2. Deposit the 10 ether into MakerDAO. At a 1.5 ratio, you could borrow up to roughly $33,000 of DAI.
  3. You only need a MacBook, so borrow 2,000 DAI.
  4. Swap the DAI to something like USDC, take it offchain and buy the Mac.

You can get your collateral back at any time by repaying the 2,000 DAI you borrowed.

Meanwhile your 10 ether stays 10 ether. If ether goes from $5,000 to $10,000, you still have that exposure, and you never had to sell ether you believed in.

This position is very safe. Your collateralization ratio is $50,000 divided by $2,000, which is 25, far above the required 1.5.

Leverage and Looping

A CDP can also be used to loop, which builds a leveraged long. Using the same numbers:

  1. Deposit 10 ether ($50,000).
  2. Borrow $30,000 of DAI, which is close to the 1.5 limit and not well advised.
  3. Buy 6 more ether with it and deposit them. You now have 16 ether of collateral.
  4. The new $30,000 of collateral lets you borrow more. Borrow $20,000, buy 4 ether and deposit them. You are at 20 ether.

You can keep looping until there is no room left. By then you are so levered that even a small drop in ether makes the whole position liquidatable.

The result is a leveraged long on ether. You started with 10 ether, but you are now exposed to the price changes of 20.

Three Levers on the Peg

The question of the day is how one DAI keeps its peg to $1. That is what the whole protocol is designed to do.

MakerDAO has three configurable levers, controlled by its governance token, MKR.

1. Stability fees

The stability fee is the interest rate you pay when you borrow DAI. A loan with no interest would not be financially feasible, just as it is not for a bank.

It works through basic supply and demand. If DAI's price is too low, the protocol raises the stability fee.

Borrowers then want to repay to stop paying the higher rate. Repaying removes DAI from the market, which shifts supply left and pushes the price up.

If the price needs to come down, the protocol lowers the fee. At a 0% rate, much as the Fed set in the years before 2023, supply grows and DAI's price falls.

2. The DAI Savings Rate

The DAI Savings Rate is what you earn passively by depositing DAI into a smart contract. It is not locked for any period, but you only earn it while you hold the DAI.

A higher rate raises demand, shifting the demand curve right. If you can get a 10% yield on DAI, you buy DAI to earn it.

It arguably shifts supply too: DAI sitting in the contract is off the market. Both effects raise the price, and lowering the rate does the reverse.

3. The debt ceiling

The debt ceiling is a cap on how much DAI can be borrowed, for example against one type of collateral.

Market Mechanics of the Peg

Those levers can take time to work. Two market incentives hold the peg more immediately.

  • DAI above $1 (say $1.05). Deposit collateral, borrow DAI and sell it at the higher price. You borrow at the protocol's fixed ratio, which does not change with whatever the market pays for DAI.
  • DAI below $1 (say $0.95). If you already owe DAI, buy it at $0.95 and repay debt you took on at $1. That is a 5% discount on your debt, and your collateral is freed.

The second incentive only works if one DAI always repays at least $1 of debt. Overcollateralization is what keeps that true.

Why Overcollateralization

A collateralization ratio of exactly 1 is what the protocol needs to function at a baseline level. But it is not enough.

At a ratio of 1, positions would sit at 1.01. Then a scam wick drops the collateral to 0.98, and the position is insolvent.

The protocol can no longer guarantee $1 of backing for every $1 borrowed. The owner would rather walk off with the $1,000 they borrowed than keep collateral now worth $980.

Liquidating them does not help: there is not enough collateral to cover the debt. That gap is a loss the protocol takes directly.

So the 1:1 line cannot be the liquidation threshold. MakerDAO raises the threshold to $1.50 of collateral per $1 borrowed.

Now when collateral dips just below the threshold, from $1,510 to $1,480 say, there is still well over $1,000 to repay the debt. The extra $500 of buffer makes the difference.

The buffer exists because liquidations do not happen at exactly the right price. Price can gap down, and liquidators can be delayed.

When the Buffer Is Not Enough

Even with a buffer, a liquidation can still come too late.

Say $1,000 is borrowed, so the liquidation threshold is $1,500 of collateral. The liquidation should happen at $1,499.

But for whatever reason it is delayed, and only happens once the collateral is worth $950. The protocol must recover $1,000 and only has $950.

That leaves $50 of bad debt for the protocol. The borrower walked away with the $1,000, a good deal for them and a bad deal for the protocol.

MakerDAO's answer is the Flopper, which mints and sells MKR for DAI to cover the loss. The details come after a look at the naming.

The MakerDAO Codebase Naming

The MakerDAO codebase is famous, or infamous, for short and terse names.

  • The Vat contract has a frob function that takes i, u, v, w, dink and dart.
  • Collateral types are ilks, and a vault or position is an urn.
  • The Dog contract liquidates positions with its bark function.

Why name things this way? There are three reasons:

  1. Less verbosity. Short names instead of long ones.
  2. Easier formal verification. The contracts have been formally verified to prove some of the core equations that guarantee the protocol works. Abstract names arguably make that easier.
  3. No assumptions. When you see bark, you do not assume it is a liquidation until you understand it.

That is a trade-off for reviewers. You bring no assumptions, but you cannot glean anything quickly either, so a review takes much longer and may have fewer blind spots.

In our experience the naming is quite annoying. It may well feel natural after you spend 30 to 50 hours learning the mappings.

How Liquidations Work

Once you cross the liquidation threshold, you can be liquidated, or "barked". Here is a worked example.

  1. Ether is $3,000. You deposit 1 ether, so you could borrow up to 2,000 DAI at a 1.5 ratio.
  2. You play it safe and borrow 1,000 DAI. Your ratio is 3.
  3. Ether drops to $1,400. Your ratio is now 1.4, below 1.5, so you can be liquidated.

Someone calls Dog.bark. They can supply a keeper address, which receives a small reward if one is configured.

That reward scales with the size of the debt. The point is to make liquidations happen promptly and react to the market price before it moves down drastically.

The protocol then auctions off your 1 ether in a Dutch auction. The price starts high and trends down over time.

Many functions and curves have been tried for that descent. Bidding early gets a worse price; waiting risks the tokens being gone, so timing is a game.

The auction runs until it raises enough DAI to repay your debt plus a liquidation fee. The fee is a disincentive to ever become liquidatable: borrow $1,000 and you might lose $1,050.

Whatever collateral is left over goes back to you. Your position is closed and your debt repaid.

How MakerDAO Handles Insolvency

What if the auction cannot raise enough DAI to repay the debt? That is an insolvent liquidation: the collateral is not enough to cover the borrowed amount.

In MakerDAO, the uncovered bad debt is recorded as sin in the Vow contract. The Vow has functions such as flog, heal, kiss, flop, flap, cage and fess.

From there, the Flopper contract is used: kick, tick, dent and deal. New MKR is sold to raise DAI, which covers the loss.

If risk parameters are set correctly and liquidations happen as they should, MKR should never have to be sold this way.

The reverse also happens. When the protocol works well, excess DAI from stability fees builds up in the Vow.

Above a configured threshold, that DAI buys MKR off the market and burns it. MKR rises, so large holders have a reason to vote for good risk management.

What Auditors Should Take Away

  • Always test the insolvent liquidation. In almost any DeFi protocol, look for the case where collateral cannot cover the debt, and see what happens.
  • Check the buffer against delays. The collateralization ratio exists because liquidations can be late and prices can gap. Ask what happens when they are.
  • Check the liquidation incentive. Rewards that scale with debt are how MakerDAO keeps liquidations prompt.
  • Budget time for unfamiliar naming. Terse names remove assumptions but slow a review down.

For the same archetype in another codebase, see our CDP lending protocol walkthrough. For what goes wrong in liquidation code, see liquidation vulnerabilities.

How Guardian Helps

Guardian's smart contract audits start with a design review of economic risk and failure modes, then build invariant suites around solvency and accounting.

Senior researchers then attack protocol logic, oracles and privileged flows with proof-of-concept depth. Vanguard adds a second, independent team on the same scope.

Frequently Asked Questions

Why is DAI overcollateralized?

Collateral prices move and liquidations can be late. At 1:1, a small price wick would leave debt larger than its collateral. A ratio such as 1.5 leaves time to liquidate while the collateral still covers the debt.

How does DAI hold its $1 peg?

Governance adjusts stability fees, the DAI Savings Rate and debt ceilings. Market incentives act faster: above $1, people mint and sell DAI; below $1, borrowers buy cheap DAI to repay debt taken at $1.

What happens when a MakerDAO vault is liquidated?

Anyone calls Dog.bark. The collateral is sold in a Dutch auction until it raises the debt plus a liquidation fee. Leftover collateral goes back to the vault owner, and the caller's keeper address can receive a reward.

What does MakerDAO do with bad debt?

Uncovered debt is recorded as sin in the Vow contract. The Flopper then mints and sells new MKR for DAI to cover the loss. When there is surplus DAI instead, it buys back and burns MKR.

Sources

Test your protocol's liquidations with Guardian

Guardian writes your solvency and accounting rules as invariants, then pressure-tests every reachable state against them.