Maker Protocol Technical Docs
  • MakerDAO Technical Docs
  • Getting Started
    • Maker Protocol 101
  • Smart Contract Modules
    • Dai Module
      • Dai - Detailed Documentation
    • Core Module
      • Vat - Detailed Documentation
      • Spot - Detailed Documentation
    • Collateral Module
      • Join - Detailed Documentation
    • Liquidation 2.0 Module
    • System Stabilizer Module
      • Flapper - Detailed Documentation
      • Flopper - Detailed Documentation
      • Vow - Detailed Documentation
    • Oracle Module
      • Oracle Security Module (OSM) - Detailed Documentation
      • Median - Detailed Documentation
    • MKR Module
    • Governance Module
      • Spell - Detailed Documentation
      • Pause - Detailed Documentation
      • Chief - Detailed Documentation
    • Rates Module
      • Pot - Detailed Documentation
      • Jug - Detailed Documentation
    • Proxy Module
      • Proxy Actions - Detailed Documentation
      • Vote Proxy - Detailed Documentation
      • CDP Manager - Detailed Documentation
      • DSR Manager - Detailed Documentation
    • Flash Mint Module
    • Maker Protocol Emergency Shutdown
      • Emergency Shutdown for Partners
      • The Emergency Shutdown Process for Multi-Collateral Dai (MCD)
      • End - Detailed Documentation
      • ESM - Detailed Documentation
  • Glossary
    • MCD Glossaries
    • Smart Contract Annotations
  • Deployment Addresses
    • Maker Protocol Deployments
  • Security
    • Security for the Maker Protocol
  • Building on top of the Maker Protocol
    • Developer Guides and Tutorials
    • The Dai Javascript Library of the Maker Protocol
      • Getting started
      • Configuration
        • Plugins
      • Vault manager
      • Collateral types
      • Dai Savings Rate
      • Currency units
      • System data
      • Advanced
        • Transaction manager
        • DSProxy
        • Events
        • Using multiple accounts
        • Adding a new service
      • Single-Collateral Sai
        • Collateralized Debt Position
        • CDP Service
        • Price Service
        • System Status
        • Tokens
        • Token Conversion
        • Exchange Service
    • Pymaker
  • Keepers
    • The Auctions of the Maker Protocol
    • Auction Keepers
      • Auction Keeper Bot Setup Guide
    • Market Maker Keepers
      • Market Maker Keeper Bot Setup Guide
    • Cage Keeper
    • Simple Arbitrage Keeper
    • Chief Keeper
  • Command-line Interfaces
    • Seth
    • Multi Collateral Dai (MCD) CLI
    • Dai and Collateral Redemption during Emergency Shutdown
    • Emergency Shutdown (ES) CLI
  • Miscellaneous
    • Liquidations 1.2 System (Deprecated)
      • Cat - Detailed Documentation
      • Flipper - Detailed Documentation
    • SCD <> MCD Migration
    • Upgrading to Multi-Collateral Dai Guide
Powered by GitBook
On this page
  • Installation
  • Quick Examples
  • Look up information about a vault
  • Create a vault
  • Integration Examples
Export as PDF
  1. Building on top of the Maker Protocol
  2. The Dai Javascript Library of the Maker Protocol

Getting started

PreviousThe Dai Javascript Library of the Maker ProtocolNextConfiguration

Last updated 4 years ago

Installation

Install the package with npm in your terminal:

npm install @makerdao/dai

Once it's installed, import the module into your project as shown below.

import Maker from '@makerdao/dai';
// or
const Maker = require('@makerdao/dai');

Multi-Collateral Dai support in Dai.js is implemented as a . This may change in the future. The MCD Plugin is also available as an package and its source code can be found on .

npm install @makerdao/dai-plugin-mcd

import { McdPlugin } from '@makerdao/dai-plugin-mcd';
// or
const { McdPlugin } = require('@makerdao/dai-plugin-mcd');

(Note the .default at the end of the line when using require.)

UMD

This library is also usable as a , which you can build with npm run build:frontend.

<script src="./dai.js" />

<script>
// once the script loads, window.Maker is available
</script>

Quick Examples

Look up information about a vault

// you provide these values
const infuraKey = 'your-infura-api-key';
const ownerAddress = '0xf00...';

const maker = await Maker.create('http', {
  plugins: [McdPlugin],
  url: `https://mainnet.infura.io/v3/${infuraKey}`
});

const manager = maker.service('mcd:cdpManager');
const proxyAddress = maker.service('proxy').getProxyAddress(ownerAddress);
const data = await manager.getCdpIds(proxyAddress); // returns list of { id, ilk } objects
const vault = await manager.getCdp(data[0].id);

console.log([
  vault.collateralAmount, // amount of collateral tokens
  vault.collateralValue,  // value in USD, using current price feed values
  vault.debtValue,        // amount of Dai debt
  vault.collateralizationRatio, // collateralValue / debt
  vault.liquidationPrice  // vault becomes unsafe at this price
].map(x => x.toString());

Create a vault

The code below opens a Vault, locks ETH into it, and draws out Dai.

Since this code sends transactions, it requires an account that can sign transactions. The simplest way to do this is to provide a privateKey configuration option as shown below, but you can also connect to Metamask or other browser-based providers, or connect to hardware wallets.

import Maker from '@makerdao/dai';
import { McdPlugin, ETH, DAI } from '@makerdao/dai-plugin-mcd';

// you provide these values
const infuraKey = 'your-infura-api-key';
const myPrivateKey = 'your-private-key';

const maker = await Maker.create('http', {
  plugins: [McdPlugin],
  url: `https://mainnet.infura.io/v3/${infuraKey}`,
  privateKey: myPrivateKey
});

// verify that the private key was read correctly
console.log(maker.currentAddress());

// make sure the current account owns a proxy contract;
// create it if needed. the proxy contract is used to 
// perform multiple operations in a single transaction
await maker.service('proxy').ensureProxy();

// use the "vault manager" service to work with vaults
const manager = maker.service('mcd:cdpManager');
  
// ETH-A is the name of the collateral type; in the future,
// there could be multiple collateral types for a token with
// different risk parameters
const vault = await manager.openLockAndDraw(
  'ETH-A', 
  ETH(50), 
  DAI(1000)
);

console.log(vault.id);
console.log(vault.debtValue); // '1000.00 DAI'

Integration Examples

This code uses to look up a vault that was created in the UI. Since this code is only reading data, not creating any transactions, it is not necessary to provide a private key or connect a wallet.

In the next section, learn more about how to configure the Maker instance with Maker.create. Or jump to learning more about , , , and .

For larger examples of integrating this library, check out this and the .

plugin
npm
Github
UMD module
accounts
vaults
proxies
currency units
repo
Dai react template
Oasis Borrow
getCdpIds