Going Through the CDP Life Cycle of Multi-Collateral DAI

Going Through the CDP Life Cycle of Multi-Collateral DAI

Introduction

MakerDAO released SAI as the Alpha version in December 2016, and DAI was released as a beta version in December 2017. DAI is currently a single-collateral model, which means you can lock only a single type of asset. Thus, DAI support only ETH(PETH, precisely speaking) right now, but they will upgrade this single-collateral DAI to multi-collateral DAI where they support more than one collateral types(PETH will be removed and regular ETH will be usable among other collateral). For example, they are planning to use Digix as its next asset, which is a token backed by physical gold. Eventually, multiple assets could be used to create a single CDP, including ETH, other ERC20 tokens, non-fungible tokens or other fungible token standards like ERC777.

 

This upgrade is expected to make it easy for people to create DAI and reduce the risk of under-collateralization through diversification of their collateral. People will have more options when they lock up their collateral. It’s risky to invest only with ETH because the collateralization ratio depends solely on the price of ETH. If you don’t understand how single-collateral DAI system works, I suggest that you read my previous blog before proceeding.

 

There are already the smart contracts for multi-collateral DAI test releases on Kovan testnet. They have also updated their tools to help us interact with the MCD system. You can check out a CDP portal site, a command line tool, Dai.js library and others. In this post, we are going to use their command line tool, mcd-cli, to go through the lifecycle of CDPs.

 

This guide is based on MakerDAO’s official guide on mcd-cli, but I added some explanations and set up instructions as I had a hard time understanding some parts of it. Also, keep in mind that this guide is going to use v0.2.6 release on Kovan. I tested this out in my Mac but not in Linux.

 

Our goal is to go through the lifecycle of CDP.

 

All right, let’s start!

Set Up

There are some steps we need to take before we start playing. In this setup, we will make a new Ethereum account that interacts with smart contracts, install mcd-cli and then get ETH and COL1 token. The COL1 token is the multi-collateral token for testing purpose(1 USD ≒ 1 DAI ≒ 10 COL1). Below are the steps in detail. We will go over one by one.

 

1) install parity
2) create a new account
3) install dapp tools
4) install the mcd package
5) set up .sethrc
6) get some Kovan ETH to your account
7) get some COL1 tokens via faucet

 

1) install parity

*I haven’t tested yet, but geth should work as well.

 

2) create a new account

You can see your account with this command:

 

3) install dapp tools

If you are running GNU/Linux or macOS you can use their one liner installer:

dapp.tools is a cool command line friendly blockchain development tools. They use the nix package manager that is used in NixOS Linux distribution. This script downloads the Nix package manager, setups binary cache with Cachix and installs our most used tools such as dapp, seth, solc, hevm and ethsign.

 

4) install the mcd package

Again, I am using v0.2.6 here:

*make sure you install v0.2.6. This guide might not work in other versions.

 

5) set up .sethrc

mcd-cil is based on seth, which is an Ethereum Swiss Army knife. If you want to make a function call of smart contracts right now, you need curl, post, rpc methods and it’s horrible. With seth, you can call functions in smart contracts much easier.

Edit ~/.sethrc file like this:

*If you want to configure a custom location for your key files, use the ETH_KEYSTORE variable.

 

6) get some Kovan ETH to your account

Next, go to this faucet and send some Kovan ETH to your address. It requires you to log in with your Github account. You then must paste your ETH address you created in the input box and request the funds. You can also ask to send you some in this Gitter channel. Or simply ask your friend. 0.2 KETH should be enough. Check out the etherscan and make sure you have some Kovan ETH.

 

7) get some COL1 tokens via faucet

Let’s get COL1 token(one of the ERC20 collateral tokens for testing purpose) now. You can withdraw tokens from the smart contract.

Setting the COL1 address to env variable:

Setting the Faucet address to env variable:

Call the gulp(address) function to get some COL1 tokens:

You need to type a password right after you run the command. seth looks for keys and password in the standard directories of Geth and Parity and then it makes a transaction.

You can also use a command line to see the balance of the token:

You can also search the transaction hash above on etherscan. Like this. Then if you jump to your account, you can see that you got COL1 token.
*It says TOK1 in Etherscan but don’t worry about this.

Going Through the CDP Life Cycle With a Command Line Tool

All right! Now we are ready to go through the life cycle of CDP with COL1 token we just acquired. We will take these steps below:

1) Open CDP
2) Deposit COL1 token
3) Withdraw DAI
4) Payback DAI
5) Remove collateral
6) Close CDP

 

Let me go over the first three steps.

1) Open CDP
2) Deposit COL1 token
3) Withdraw DAI

 

First, run this command below:

 

Let’s take a look at this output first:

 

Now your COL1 collateral tokens are added to the internal registry called vat that keeps track of the balance of collateral in CDP. You can see the balance is 30. But they are not yet locked in the CDP. ink is the balance of locked collateral in CDP. Since the collateral is not yet assigned to any CDP, ink says 0. ext is the external token balance outside of MakerDAO system. Since you added 30 COL1 tokens in vat, now it’s 20.

 

Let’s take a look at the gem command:

 

The join subcommand can add collateral from the sender account to any specified CDP. You are specifying the collateral type with — ilk= option and the number of tokens at the end.

 

Now let’s lock your COL1 collateral tokens in CDP and draw some DAI.

 

Run the command below:

 

Let’s take a look at the output first:

 

Now you can see in ink that your COL1 collateral token is locked in CDP.
*urn means a specific CDP

 

You can also see in art that DAI is issued.

 

Once we explicitly lock some collateral tokens from vat with the frob command, some tokens were locked in the urn and DAI was generated.

 

The frob command specifies a collateral type with — ilk= flag, the number of tokens you want to lock(30) and then the amount of DAI(1) you would like to generate.

 

But DAI is still in vat, so let’s withdraw DAI and send it to your ETH personal account.

Run:

 

You can see that DAI is subtracted from vat and then added to ext.

 

The exit subcommand of a dai command can specify the amount of DAI you want to withdraw.

You can also check the balance with the balance subcommand.

I know it was a bit complex, but so far we opened CDP, deposited some COL1 tokens, and withdrew some DAI.

 

Now let’s payback DAI, remove collateral and then finally close CDP.

4) Payback DAI
5) Remove collateral
6) Close CDP

 

First:

 

You can see that DAI was moved from ext to vat.

 

You can use the balance subcommand as well:

 

The join subcommand of a dai command can specify the amount of DAI debt you want to pay back to your urn.

 

Next, remove/burn your Dai debt and remove your COL1 collateral from the internal system(ink).

 

If we look back on the output we had when we used frob command before, we had locked collateral in ink and issued DAI in art.

 

When we look at the output we have here:

You can see that the locked collateral in the ink and issued debt in art is gone.

 

We used the frob command to lock the COL1 collateral token and then generate DAI before. Now we are using it backward. This frob command specifies a collateral type with — ilk= flag, the number of tokens you want to remove from ink(30) and then the amount of DAI(1) you would like to burn.

 

Now your COL1 token collateral is in vat:

 

Finally, remove your collateral COL1 token from the COL1 adapter:

 

You can see that your token is removed from vat and back in ext.

 

Great! Now you should have your initial collateral (COL1) back in your parity account.

 

If you want to learn more about MCD, you can check out their glossary page. You can also read the core smart contracts for multi-collateral DAI here.

 

Profile

Taisuke Mino
Crypto Developer
Tai is currently building on a mobile wallet specifically designed for MakerDAO. He is also involved in a dapps game in ICOVO as a software engineer.