Multi-Collateral Dai support in Dai.js is implemented as a plugin. This may change in the future. The MCD Plugin is also available as an npm package and its source code can be found on Github.
(Note the .default at the end of the line when using require.)
UMD
This library is also usable as a UMD module, which you can build with npm run build:frontend.
<scriptsrc="./dai.js" /><script>// once the script loads, window.Maker is available</script>
Quick Examples
Look up information about a vault
This code uses getCdpIds to look up a vault that was created in the Oasis Borrow 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.
// you provide these valuesconstinfuraKey='your-infura-api-key';constownerAddress='0xf00...';constmaker=awaitMaker.create('http', { plugins: [McdPlugin], url:`https://mainnet.infura.io/v3/${infuraKey}`});constmanager=maker.service('mcd:cdpManager');constproxyAddress=maker.service('proxy').getProxyAddress(ownerAddress);constdata=awaitmanager.getCdpIds(proxyAddress); // returns list of { id, ilk } objectsconstvault=awaitmanager.getCdp(data[0].id);console.log([vault.collateralAmount,// amount of collateral tokensvault.collateralValue,// value in USD, using current price feed valuesvault.debtValue,// amount of Dai debtvault.collateralizationRatio,// collateralValue / debtvault.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 valuesconstinfuraKey='your-infura-api-key';constmyPrivateKey='your-private-key';constmaker=awaitMaker.create('http', { plugins: [McdPlugin], url:`https://mainnet.infura.io/v3/${infuraKey}`, privateKey: myPrivateKey});// verify that the private key was read correctlyconsole.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 transactionawaitmaker.service('proxy').ensureProxy();// use the "vault manager" service to work with vaultsconstmanager=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 parametersconstvault=awaitmanager.openLockAndDraw('ETH-A',ETH(50),DAI(1000));console.log(vault.id);console.log(vault.debtValue); // '1000.00 DAI'
In the next section, learn more about how to configure the Maker instance with Maker.create. Or jump to learning more about accounts, vaults, proxies, and currency units.
Integration Examples
For larger examples of integrating this library, check out this repo and the Dai react template.