MakerDAO 101 for Coders – How DeFi Lending and Borrowing Works

5/5 - (1 vote)

This is in continuation of the first part of our DeFi series.

💡 Abstract: In this article, we will see how decentralized lending and borrowing work. It starts with comparing centralized finance (CeFi) with decentralized finance (DeFi) lending and borrowing. You’ll then learn about the most important Ethereum based DeFi decentralized apps (dapps) for lending and borrowing. I explain MakerDAO here and address Compound and Aave in the next article.

MakerDAO 101 for Coders - How DeFi Lending and Borrowing Works

🪙 Full DeFi Course: Click the link to access our free full DeFi course that’ll show you the ins and outs of decentralized finance (DeFi).

CeFi vs. DeFi Lending and Borrowing

Lending and borrowing cash are two of the most popular services provided by the financial sector.

Thanks to the idea of credit and collateralization, which makes this workable. The beginning of the period of modernity in European history (also called the Renaissance period) was because of commercial scale lending and borrowing, which led to the furor in economic activity.

Because of this powerful mechanism, the economy rose at an unparalleled speed.

An entrepreneur can borrow the initial money needed for a startup from the lender by providing the business as collateral, or a family can mortgage a house as collateral for loans.

This technique helps in reducing the borrower’s absconding with all the loaned money.

Thus, the CeFi borrowing or lending needs some kind of middleman, such as banks. Before you get the borrowed amount from the bank, you need to show your ability to repay loans and also stick to the restrictions imposed by them.

Besides this, there is a complex system of credit scores that determines an individual’s ability to repay the loan and also considering other factors such as geographical legal restrictions of the banks, multiple applicants etc. add to high barriers in getting the loans approved. Basically, to be even considered for loans, you have to be affluent enough when using CeFi.

With the DeFi system, this barrier and impediments do not exist.

Anybody can access the funds from the pool if you have enough collateral (other crypto currencies or tokens.). All the masses can contribute to the liquidity pool, which the borrowers can withdraw from and repay at the interest rate set by the algorithms of the DeFi system. Using the DeFi ecosystem, asset lending is no longer the privilege of the rich.

Another notable difference between CeFi and DeFi is the bank regulations like Anti Money Laundering (AML) and Know-Your-Customer (KYC) in place, whereas in the DeFi ecosystem you just need to only submit the crypto collateral. It’s very simple and has no hassles.

MakerDAO – A Lending and Borrowing dApp

DeFi breakthroughs have been happening on Ethereum because of its prominence, but it is also emerging on other blockchains like Solana, Polkadot, Algorand, and many more.

We will look at three popular DeFi dApps: MakerDAO, Compound, and Aave. I will explain MakerDAO in this article and the other two – Compound and Aave in the next.

MakerDAO is a good example of understanding the DeFi ecosystem, where DAO stands for decentralized autonomous organization. It employs two tokens. A stablecoin called DAI and a governance token called MKR. We will discuss each of them ‌and their use cases.

Fig: MakerDAO (pic credit)


🪙 DAI is a crypto-collateralized stable coin with a 1:1 parity with US dollar ($), and value backed by ETH (Ether) as collateral in the smart contract called the Maker collateral vault. DAI token has the money function and acts as currency.

🪙 MKR token is the governance token issued by the Maker protocol to the lenders or stakeholders that grants voting rights and decision-making in the improvements of the Maker protocol. The MKR token holders also govern the stability of DAI. Thus MKR token has a utility function.

Users can mint new $DAI tokens when they take their digital assets ($ETH, $BAT, $USDC, $YFI, $LINK) with MakerDAO’s smart contract. Thus, by staking digital assets, you can create $DAI.

Operation model of MakerDAO

The best way to know how MakerDAO operates is by looking at an example. Before we look into an example, let us understand two important terminologies in MakerDAO – debt and CDP (collateralized debt positions).

Users can deposit crypto assets (ETH or other ERC20 tokens) into the smart contract to generate or mint the $DAI tokens up to a particular collateralization ratio (at least 150%).

This generates a position called CDP and ‘debt’ in DAI.

MakerDAO is always over collateralized (as mentioned >=150%) so that the value of the collateral is always greater than the debt. MakerDAO needs this because the price of ETH is highly volatile.

Now let’s take an example to see how MakerDAO algorithms work.

Let’s consider you are the ETH owner who wants to have liquidity but does not intend to sell ETH, as it may rise in value in the future (this is like the owner who wants to mortgage the house or apartment because they want the liquidity but do not want to sell the house).

Let’s consider you have 5 ETH and the ETH price is = $1000, totaling to $5000.

  1. Deposit 5 ETH to open a CDP (collateralized debt position).
  2. If the collateralization requirement is at least 150% for MakerDAO, then you can mint up to 5000/1.5 ~= 3334 DAI. If the ETH plummets below $1000, then in such a case, the contract would be under-collateralized, and the system must also maintain the $1 = 1 DAI ratio. Thus, ‌you cannot mint all the 3334 DAI, as there is no notice from the exchange or broker to deposit more ETH or liquidate the position to maintain this ratio. As liquidation will happen instantly, you always mint less than 3334 DAI to provide a safety net.

Let’s take a case if you only mint 2500 DAI, with a collateralization ratio of 2.0 ($5000/2 = 2500). Now consider the two scenarios:

  1. If collateral price increases, i.e., ETH price increases by 50%, meaning the collateral is now worth $7500 ($5000 + $2500). As collateralization was 200%, you can mint up to 3750 DAI, which is an additional 1250 DAI.
  2. If collateral price decreases, i.e.  ETH price decreases by 25%, it becomes an intriguing issue. The collateral is now worth $3750 ($5000 – $1250). With a maximum mint of 2500 DAI, the collateralization ratio gets reduced to 1.5 (3750/2500). You have three options to solve the problem.

1.) You can increase the amount of collateral in the contract by adding $1250 worth of ETH (or 1.6 ETH).

2.) As you have 2500 DAI, you can pay off the debt and get back 5 ETH, however the total worth of ETH is now reduced to $3750.

3.) Last, if you use neither option, then a third party called keepers will look for liquidation in order to liquidate the debt. The keeper will sell ETH at auction to get back enough DAI to pay off the debt. In our case, 1.6 ETH($1250) gets sold and you get back 3.2 ETH and the keeper gets 0.2 ETH as a percentage fee. So in total you have 3.2 ETH ($2400) and 2500 DAI.

Where is MakerDAO used?

  • Decentralized exchanges like Uniswap, Sushiswap, Curve, Balancer. They use DAI because it makes trade easy and is always in demand.
  • Lending protocols like Aave, Compound, Celsius. They use DAI because it is less volatile and hence provides stability for lending and borrowing.

DAI in Smart contracts

Let us get ‌practical and try using DAI in our smart contract. Below is the smart contract, which can accept/deposit() and withdraw() DAI token. We will make use of the Kovan network, where DAI (ERC20) contract deployed at –   0x4F96Fe3b7A6Cf9725f59d353F723c1bDb64CA6Aa

//SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;

import "@openzeppelin/contracts/access/Ownable.sol";

// Adding only the ERC-20 function we need
interface DaiToken {
    function transfer(address dst, uint wad) external returns (bool);
    function balanceOf(address guy) external view returns (uint);

contract DaiFaucet is Ownable  {
    event Withdrawal(address indexed to, uint amount);
    event Deposit(address indexed from, uint amount);

    DaiToken daitoken;

    constructor() public{
        // kovan network
        daitoken = DaiToken(0x4F96Fe3b7A6Cf9725f59d353F723c1bDb64CA6Aa);

    // Give out Dai to anyone who asks
    function withdraw(uint withdraw_amount) public {
        // Limit withdrawal amount
        require(withdraw_amount <= 0.1 ether);
        require(daitoken.balanceOf(address(this)) >= withdraw_amount,
            "Insufficient balance in faucet for withdrawal request");
        // Send the amount to the address that requested it
        daitoken.transfer(msg.sender, withdraw_amount);
        emit Withdrawal(msg.sender, withdraw_amount);

    // Accept any incoming amount
    fallback () external payable {
        emit Deposit(msg.sender, msg.value);

        // Only owner can shutdown this contract.
    function destroy() public onlyOwner {
        daitoken.transfer(owner(), daitoken.balanceOf(address(this)));

An interface DaiToken interacts with the DAI contract for transfer() and balanceOf().

The contract DaiFucet defines:

  1. constructor() to get an instance of the DAI token contract on Kovan network.
  2. withdraw() function to withdraw DAI. It transfers DAI from this contract to the msg.sender only on the condition that the requested amount is <= 0.1 DAI (you have to use it as <= 0.1 ETH).
  3. A fallback function to accept DAI for the contract.
  4. destroy() to destroy the contract when you no longer need it.

Compile and deploy the contract to the Kovan network using Remix Ide and Metamask. I have already deployed the contract at 0xABC10a81b5d50f4455f4F7cC1f418ff70259dd8A. Your contract will be at a unique address than mine.

How to send DAI to the contract and withdraw?

Use Uniswap to swap some ether to DAI on the Kovan network through your Metamask account. Now you have DAI in your Metamask account. On Metamask, you can see it in the assets section. See the figure below.

Fig: Metamask with DAI

You can now click on DAI and select send, type the address of the above deployed contract and click on send, by signing the transaction, your contract will now receive DAI.

Now you can interact with the contract to withdraw DAI from Remix (You can only withdraw a maximum of 0.1 DAI per transaction as per the rule set in the contract).

After you are done enough with the contract, you can as well destroy it by calling destroy() function.


The article started with discussing lending and borrowing in CeFi and DeFi.

We saw how DeFi is easy to use and you have complete control over your assets with no interference from a central finance institute like banks, etc.

The architecture of the most widely used and popular dapps like MakerDAO was explained, and finally saw an example of sending and receiving DAI in a smart contract.

Feel free to check out our related course on the Finxter Academy: