An Intro To DaiJS

Build applications on top of MakerDao

Here Alan Greenspan (Chair of the Federal Reserve of the United States - 1987 to 2006) is answering David Gregory in 2011:

David Gregory: “Are U.S. treasury bonds still safe to invest in?”

Alan Greenspan: “Very much so. This is not an issue of credit rating, the United States can pay any debt it has because we can always print money to do that. So, there is zero probability of default.”

Current Central Banking systems have the power to print money out of thin air and solving this issue was at the core of Bitcoin’s creation. Now we have technologies which replicate the same systems, but in a more trust-less manner. For example, MakerDAO.

First: What is MakerDao?

The MakerDao project created the DAI stable token. A stable token is pegged to some fiat currency, which allows it to be a unit of account. Dai is a decentralized stable coin pegged to the US Dollar value (i.e. 1 Dai = $1).

Think of Dai as an electronic dollar; a dollar on the Ethereum blockchain.

Dai token governance depends on MKR token, which makes it a DAO (Decentralized Autonomous Organization), hence MakerDAO. At the time of writing this article, the DAI system is 150% collateralized — meaning to get 100 DAI you need at least $150 worth of Ether (ETH).

What is CDP?

A Collateralized Debt Position (CDP) is the only way to create DAI. According to MakerDao white paper:

Anyone who has collateral assets can leverage them to generate Dai on the Maker Platform through Maker’s unique smart contracts known as Collateralized Debt Positions. A Collateral Asset is a digital asset that the decentralized Maker Governance process has input into the system.

In simple words, a CDP is a loan taken against a digital asset (such as Ether/ETH). When you open a CDP, the system will generate Dai.

CDP using Dai JS

Now let's play with Dai JS and create CDP using it. Before that, we need to perform a few steps:

Generate address on Koven network

To play with the dai.js let's create a brand new address on Koven network. We are going to use NodeJs, so install that first in case you don’t have that.

mkdir wallet

cd wallet

npm i ethereumjs-wallet

Now create a file app.js in the same wallet directory and paste below code.

const Wallet = require('ethereumjs-wallet');

const wallet = Wallet.generate();

console.log("privateKey: " , wallet.getPrivateKeyString());

console.log("address: " , wallet.getAddressString());

Now open the terminal and run node app.js , That’s it, you have made an Ethereum private key with a public address.

Import address to Metamask

Next, we will import this address to Metamask so things become easy (You will see this later).

To import address follow these steps-

Click on Metamask and select Koven network

Then click on the top right circle and click on Create Account option

Then click on import and paste your Private keys (generated above)

Now let get some test ETH from Kovan Faucet using this site.

Dai JS

Current MakerDao enables you to do the following:

Opening and closing CDPs

Locking and unlocking collateral

Withdrawing and repaying Dai

Automated token conversions

Token contract functionality for WETH, PETH, MKR, Dai, and ETH

Buying and selling MKR and Dai with built-in DEX integration

MakerDao already created a GitHub repo, with different examples, which we forked. So let’s import the repo using below commands:

cd && git clone https://github.com/buddies2705/integration-examples

This Repo has the following type of examples:

CDP creation and fulfillment

Hardware wallet integration examples such as Ledger and Tezos

Leverage (Similar to margin trading, Create CDP-> Get DAI -> Buy ETH on the exchange using DAI -> Repeat )

Top up your CDP

Using Wyre to covert DAI into fiat

A full example on how to create and close CDP

In this tutorial, we will look into the creating and closing CDP example, but before that let’s run our application and see things in practice.

First, let’s add our private key in our code:

cd integration-examples/react-example

cp .env.example .env

Then add private keys in the .env file. We need a web3 provider to interact with Ethereum blockchain, so get your Web3 URL ready (we’re going to use QuikNode ← you sign up here):

npm install

npm start

This will open a window on the browser. Make sure you have selected the correct Metamask account (account we recently added) and Kovan network above. Next open browser console using F12 and click on the start button. Now, wait you will see the following steps getting completed.

Maker created

CDP opened

Eth Locked

Dai Drawn

Debt Wiped

CDP Shut

You can see more output logs in the browser console.