Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
//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
let returnedWeth = tx.fillAmount();
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`);
}