contract LoopNewCDP is GlobalVar {

    event LevNewCDP(uint cdpNum, uint ethLocked, uint daiMinted);

    function pethPEReth(uint ethNum) public view returns (uint rPETH) {
        MakerCDP loanMaster = MakerCDP(cdpAddr);
        rPETH = (ethNum.mul(10 ** 27)).div(loanMaster.per());
    }

    // useETH = msg.sender + personal ETH used to assist the operation
    function riskNewCDP(uint eth2Lock, uint dai2Mint, bool isCDP2Sender) public payable {
        require(!freezed, "Operation Disabled");

        uint contractETHBal = address(this).balance - msg.value;

        MakerCDP loanMaster = MakerCDP(cdpAddr);
        bytes32 cup = loanMaster.open(); // New CDP

        WETHFace wethTkn = WETHFace(getAddress("weth"));
        wethTkn.deposit.value(eth2Lock)(); // ETH to WETH
        uint pethToLock = pethPEReth(eth2Lock); // PETH : ETH
        loanMaster.join(pethToLock); // WETH to PETH
        loanMaster.lock(cup, pethToLock); // PETH to CDP

        loanMaster.draw(cup, dai2Mint);
        address dai2ethContract = getAddress("dai2eth");
        IERC20 daiTkn = IERC20(getAddress("dai"));
        daiTkn.transfer(dai2ethContract, dai2Mint); // DAI >>> dai2eth
        Swap resolveSwap = Swap(dai2ethContract);
        resolveSwap.dai2eth(dai2Mint); // DAI >>> ETH

        uint nowBal = address(this).balance;
        if (nowBal > contractETHBal) {
            msg.sender.transfer(nowBal - contractETHBal);
        }
        require(contractETHBal == address(this).balance, "No Refund of Contract ETH");

        if (isCDP2Sender) { // CDP >>> msg.sender
            loanMaster.give(cup, msg.sender);
        } else { // CDP >>> InstaBank
            InstaBank resolveBank = InstaBank(getAddress("bankv2"));
            resolveBank.claimCDP(uint(cup));
            resolveBank.transferCDPInternal(uint(cup), msg.sender);
        }

        emit LevNewCDP(uint(cup), eth2Lock, dai2Mint);
    }

}
function riskNewCDP(uint eth2Lock, uint dai2Mint, bool isCDP2Sender) public payable {
    require(!freezed, "Operation Disabled");

    uint contractETHBal = address(this).balance - msg.value;

    MakerCDP loanMaster = MakerCDP(cdpAddr);
    bytes32 cup = loanMaster.open(); // New CDP

    WETHFace wethTkn = WETHFace(getAddress("weth"));
    wethTkn.deposit.value(eth2Lock)(); // ETH to WETH
    uint pethToLock = pethPEReth(eth2Lock); // PETH : ETH
    loanMaster.join(pethToLock); // WETH to PETH
    loanMaster.lock(cup, pethToLock); // PETH to CDP

    loanMaster.draw(cup, dai2Mint);
    address dai2ethContract = getAddress("dai2eth");
    IERC20 daiTkn = IERC20(getAddress("dai"));
    daiTkn.transfer(dai2ethContract, dai2Mint); // DAI >>> dai2eth
    Swap resolveSwap = Swap(dai2ethContract);
    resolveSwap.dai2eth(dai2Mint); // DAI >>> ETH

    uint nowBal = address(this).balance;
    if (nowBal > contractETHBal) {
        msg.sender.transfer(nowBal - contractETHBal);
    }
    require(contractETHBal == address(this).balance, "No Refund of Contract ETH");

    if (isCDP2Sender) { // CDP >>> msg.sender
        loanMaster.give(cup, msg.sender);
    } else { // CDP >>> InstaBank
        InstaBank resolveBank = InstaBank(getAddress("bankv2"));
        resolveBank.claimCDP(uint(cup));
        resolveBank.transferCDPInternal(uint(cup), msg.sender);
    }

    emit LevNewCDP(uint(cup), eth2Lock, dai2Mint);
}

// useETH = msg.sender + personal ETH used to assist the operation