Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
log.action('opening CDP...');
const cdp = await maker.openCdp();
const id = await cdp.id;
log.state(`CDP ID: ${id}`);
// calculate a collateralization ratio that will achieve the given price floor
const collatRatio = priceEth * liquidationRatio / priceFloor;
log.state(`Target ratio: ${collatRatio}`);
// lock up all of our principal
await cdp.lockEth(principal);
log.action(`locked ${principal} ETH`);
//get initial peth collateral
const initialPethCollateral = await cdp.getCollateralValue(Maker.PETH);
log.state(`${principal} ETH is worth ${initialPethCollateral}`);
// calculate how much Dai we need to draw in order
// to achieve the desired collateralization ratio
let drawAmt = Math.floor(principal * priceEth / collatRatio);
const drawTx = cdp.drawDai(drawAmt);
await maker.service('transactionManager').confirm(drawTx);
log.action(`drew ${drawAmt} Dai`);
// do `iterations` round trip(s) to the exchange
for (let i = 0; i < iterations; i++) {
// exchange the drawn Dai for W-ETH
let tx = await maker.service('exchange').sellDai(drawAmt, Maker.WETH);
// observe the amount of W-ETH received from the exchange
// by calling `fillAmount` on the returned transaction object
log.action(`exchanged ${drawAmt} Dai for ${returnedWeth}`);
// lock all of the W-ETH we just received into our CDP
await cdp.lockWeth(returnedWeth);
log.action(`locked ${returnedWeth}`);
// calculate how much Dai we need to draw in order to
// re-attain our desired collateralization ratio
drawAmt = Math.floor(returnedWeth.toNumber() * priceEth / collatRatio);
await cdp.drawDai(drawAmt);
log.action(`drew ${drawAmt} Dai`);
}
// get the final state of our CDP
const [pethCollateral, debt] = await Promise.all([
cdp.getCollateralValue(Maker.PETH),
cdp.getDebtValue()
]);
const cdpState = {
initialPethCollateral,
pethCollateral,
debt,
id,
principal,
iterations,
priceFloor,
finalDai: drawAmt
};
log.state(`Created CDP: ${JSON.stringify(cdpState)}`);
return cdpState;
async getCdp(id) {
this.cdp = await this.maker.getCdp(id);
this.liqPrice = await this.cdp.getLiquidationPrice();
this.isSafe = await this.cdp.isSafe();
this.debtValue = (await this.cdp.getDebtValue()).toBigNumber();
this.collatRatio = await this.cdp.getCollateralizationRatio();
this.ethCollateral = (await this.cdp.getCollateralValue()).toBigNumber();
this.pethCollateral = (await this.cdp.getCollateralValue(
Maker.PETH
)).toBigNumber();
this.usdCollateral = (await this.cdp.getCollateralValue(
Maker.USD
)).toBigNumber();
this.maxEthDraw = bnOver(
this.liquidationRatio,
this.usdCollateral,
this.ethPrice
).toString();
this.maxPethDraw = bnOver(
this.pethPrice,
this.pethCollateral,
this.liquidationRatio
).toString();
this.maxDaiDraw = bnOver(
this.ethPrice,
this._proxyAddress = await this.services.getProxy();
this.noProxy = this._proxyAddress === null;
if (this._proxyAddress) {
this.cdp = await this.services.getCdp(cdpId, this._proxyAddress);
} else {
this.cdp = await this.services.getCdp(cdpId, false);
}
const liqPrice = await this.cdp.getLiquidationPrice();
this._liqPrice = liqPrice.toBigNumber().toFixed(2);
this.isSafe = await this.cdp.isSafe();
this.debtValue = (await this.cdp.getDebtValue()).toBigNumber();
this._collatRatio = await this.cdp.getCollateralizationRatio();
this.ethCollateral = (await this.cdp.getCollateralValue()).toBigNumber();
this.pethCollateral = (
await this.cdp.getCollateralValue(Maker.PETH)
).toBigNumber();
this._usdCollateral = (
await this.cdp.getCollateralValue(Maker.USD)
).toBigNumber();
}
}
this._proxyAddress = await this.services.getProxy();
this.noProxy = this._proxyAddress === null;
if (this._proxyAddress) {
this.cdp = await this.services.getCdp(cdpId, this._proxyAddress);
} else {
this.cdp = await this.services.getCdp(cdpId, false);
}
const liqPrice = await this.cdp.getLiquidationPrice();
this._liqPrice = liqPrice.toBigNumber().toFixed(2);
this.isSafe = await this.cdp.isSafe();
this.debtValue = (await this.cdp.getDebtValue()).toBigNumber();
this._collatRatio = await this.cdp.getCollateralizationRatio();
this.ethCollateral = (await this.cdp.getCollateralValue()).toBigNumber();
this.pethCollateral = (
await this.cdp.getCollateralValue(Maker.PETH)
).toBigNumber();
this._usdCollateral = (
await this.cdp.getCollateralValue(Maker.USD)
).toBigNumber();
}