Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async getCdp()
{
const maker = await Maker.create("http", {
privateKey: 'dbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdb',
url: 'https://mainnet.infura.io/nWQTOIHYhOavIVKVvNah'
});
// await maker.authenticate();
// const cdp = await maker.getCdp(420);
// console.log(cdp);
// await Maker.authenticate();
// const objCdp = await Maker.getCdp(4335);
// const debtLevel = objCdp.getDebtValue(Maker.USD);
// this.setState({
// CDP: {
// Debt: {
// usd: debtLevel
module.exports = async (iterations, priceFloor, principal) => {
try {
// connect to blockchain using infura
const maker = await Maker.create('http', {
privateKey: process.env.PRIVATE_KEY,
plugins: [Eth2DaiInstant],
log: true,
url: 'https://kovan.infura.io/v3/11465e3f27b247eb8b785c23047b29fd'
});
await maker.authenticate();
invariant(
iterations !== undefined &&
priceFloor !== undefined &&
principal !== undefined,
'Not all parameters (iterations, priceFloor, principal) were received'
);
log.title('Creating a leveraged cdp with the following parameters:');
log.title(`Iterations: ${iterations}`);
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;
//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`);
}
module.exports = async function(cdpId, options) {
let targetRatio;
try {
targetRatio = Maker.USD_DAI(options.targetRatio);
} catch (err) {
throw new Error(`Invalid value for targetRatio: ${err}`);
}
const maker = Maker.create(process.env.NETWORK, {
privateKey: process.env.PRIVATE_KEY,
log: false,
provider: {
infuraProjectId
}
});
const cdp = await maker.getCdp(cdpId);
const collateral = await cdp.getCollateralValue();
console.log(`collateral: ${collateral}`);
const debt = await cdp.getDebtValue(Maker.USD);
console.log(`debt: ${debt}`);
const collateralPrice = await maker.service('price').getEthPrice();
console.log(`price: ${collateralPrice}`);
export const startAsync = () => async dispatch => {
dispatch(started());
const maker = await Maker.create(process.env.REACT_APP_NETWORK, {
privateKey: process.env.REACT_APP_PRIVATE_KEY,
overrideMetamask: true,
provider: {
infuraProjectId
}
});
await maker.authenticate();
console.log('maker:', maker);
dispatch(makerCreated());
const cdp = await maker.openCdp();
console.log('cdp:', cdp);
dispatch(cdpOpened());
const lockEthTx = await cdp.lockEth(0.01);
console.log('transaction to lock eth:', lockEthTx);
dispatch(ethLocked());
await dispatch(drawDaiAsync(maker, cdp));
async function start(wyreWallet, amountToTransfer, accountPrivateKey) {
try {
const maker = Maker.create('kovan', {
privateKey: accountPrivateKey,
web3: {
confirmedBlockCount: 15
},
provider: {
infuraProjectId
}
});
await maker.authenticate();
console.log('Authenticated');
//Get account and balance
const balance = await maker.getToken('ETH').balanceOf(maker.currentAddress());
console.log('Account: ', maker.currentAddress());
console.log('balance', balance.toString());
throw new Error(`Invalid value for targetRatio: ${err}`);
}
const maker = Maker.create(process.env.NETWORK, {
privateKey: process.env.PRIVATE_KEY,
log: false,
provider: {
infuraProjectId
}
});
const cdp = await maker.getCdp(cdpId);
const collateral = await cdp.getCollateralValue();
console.log(`collateral: ${collateral}`);
const debt = await cdp.getDebtValue(Maker.USD);
console.log(`debt: ${debt}`);
const collateralPrice = await maker.service('price').getEthPrice();
console.log(`price: ${collateralPrice}`);
const ratio = await cdp.getCollateralizationRatio();
if (targetRatio.gt(ratio)) {
let addAmount = debt
.times(targetRatio)
.minus(collateral.times(collateralPrice))
.div(collateralPrice)
.toNumber();
if (addAmount < MIN_ADD_AMOUNT) {
addAmount = MIN_ADD_AMOUNT;
} else {
module.exports = async function(cdpId, options) {
let targetRatio;
try {
targetRatio = Maker.USD_DAI(options.targetRatio);
} catch (err) {
throw new Error(`Invalid value for targetRatio: ${err}`);
}
const maker = Maker.create(process.env.NETWORK, {
privateKey: process.env.PRIVATE_KEY,
log: false,
provider: {
infuraProjectId
}
});
const cdp = await maker.getCdp(cdpId);
const collateral = await cdp.getCollateralValue();
console.log(`collateral: ${collateral}`);