From f4e0ae0d36d3880af613e94da7da27a6e8943fef Mon Sep 17 00:00:00 2001 From: Sowmayjain Date: Sat, 4 May 2019 00:49:13 +0530 Subject: [PATCH] Code refactoring and adding events. --- contracts/ProxyLogics/InstaSave.sol | 88 +++++++++++++++++------------ 1 file changed, 53 insertions(+), 35 deletions(-) diff --git a/contracts/ProxyLogics/InstaSave.sol b/contracts/ProxyLogics/InstaSave.sol index ce0a973..db1b67f 100644 --- a/contracts/ProxyLogics/InstaSave.sol +++ b/contracts/ProxyLogics/InstaSave.sol @@ -338,6 +338,26 @@ contract GetDetails is MakerHelpers { ); } + function getLeverage( + uint cdpID, + uint daiToSwap + ) public view returns ( + uint finalEthCol, + uint finalDaiDebt, + uint finalColToUSD, + bool canLeverage + ) + { + bytes32 cup = bytes32(cdpID); + (uint ethCol, uint daiDebt, uint usdPerEth) = getCDPStats(cup); + (finalEthCol, finalDaiDebt, finalColToUSD, canLeverage) = checkLeverage( + ethCol, + daiDebt, + usdPerEth, + daiToSwap + ); + } + function checkSave( uint ethCol, uint daiDebt, @@ -373,26 +393,6 @@ contract GetDetails is MakerHelpers { } } - function getLeverage( - uint cdpID, - uint daiToSwap - ) public view returns ( - uint finalEthCol, - uint finalDaiDebt, - uint finalColToUSD, - bool canLeverage - ) - { - bytes32 cup = bytes32(cdpID); - (uint ethCol, uint daiDebt, uint usdPerEth) = getCDPStats(cup); - (finalEthCol, finalDaiDebt, finalColToUSD, canLeverage) = checkLeverage( - ethCol, - daiDebt, - usdPerEth, - daiToSwap - ); - } - function checkLeverage( uint ethCol, uint daiDebt, @@ -437,7 +437,7 @@ contract Save is GetDetails { * @param what 2 for SAVE & 3 for LEVERAGE */ event LogTrade( - uint what, // 2 for SAVE & 3 for LEVERAGE + uint what, // 0 for BUY & 1 for SELL address src, uint srcAmt, address dest, @@ -447,17 +447,24 @@ contract Save is GetDetails { address affiliate ); - function getColToFree(uint ethCol, uint daiDebt, uint usdPerEth) internal pure returns (uint colToFree) { - uint colToUSD = sub(wmul(ethCol, usdPerEth), 10); - uint minColNeeded = add(wmul(daiDebt, 1500000000000000000), 10); - colToFree = sub(wdiv(sub(colToUSD, minColNeeded), usdPerEth), 10); - } + event LogSaveCDP( + uint cdpID, + uint srcETH, + uint destDAI + ); + + event LogLeverageCDP( + uint cdpID, + uint srcDAI, + uint destETH + ); + function save(uint cdpID, uint colToSwap) public { bytes32 cup = bytes32(cdpID); (uint ethCol, uint daiDebt, uint usdPerEth) = getCDPStats(cup); uint colToFree = getColToFree(ethCol, daiDebt, usdPerEth); - require(colToFree != 0, "No-collatral-to-free"); + require(colToFree != 0, "no-collatral-to-free"); if (colToSwap < colToFree) { colToFree = colToSwap; } @@ -481,8 +488,10 @@ contract Save is GetDetails { lock(cdpID, balToLock); } + emit LogSaveCDP(cdpID, ethToSwap, destAmt); + emit LogTrade( - 2, + 0, getAddressETH(), colToFree, getAddressDAI(), @@ -493,12 +502,6 @@ contract Save is GetDetails { ); } - function getDebtToBorrow(uint ethCol, uint daiDebt, uint usdPerEth) internal pure returns (uint debtToBorrow) { - uint colToUSD = sub(wmul(ethCol, usdPerEth), 10); - uint maxDebtLimit = sub(wdiv(colToUSD, 1500000000000000000), 10); - debtToBorrow = sub(maxDebtLimit, daiDebt); - } - function leverage(uint cdpID, uint daiToSwap) public { bytes32 cup = bytes32(cdpID); (uint ethCol, uint daiDebt, uint usdPerEth) = getCDPStats(cup); @@ -521,8 +524,11 @@ contract Save is GetDetails { uint ethToDeposit = wdiv(wmul(destAmt, 99750000000000000000), 100000000000000000000); getAddressAdmin().transfer(sub(destAmt, ethToDeposit)); lock(cdpID, ethToDeposit); + + emit LogLeverageCDP(cdpID, debtToBorrow, destAmt); + emit LogTrade( - 3, + 1, getAddressDAI(), debtToBorrow, getAddressETH(), @@ -533,6 +539,18 @@ contract Save is GetDetails { ); } + function getColToFree(uint ethCol, uint daiDebt, uint usdPerEth) internal pure returns (uint colToFree) { + uint colToUSD = sub(wmul(ethCol, usdPerEth), 10); + uint minColNeeded = add(wmul(daiDebt, 1500000000000000000), 10); + colToFree = sub(wdiv(sub(colToUSD, minColNeeded), usdPerEth), 10); + } + + function getDebtToBorrow(uint ethCol, uint daiDebt, uint usdPerEth) internal pure returns (uint debtToBorrow) { + uint colToUSD = sub(wmul(ethCol, usdPerEth), 10); + uint maxDebtLimit = sub(wdiv(colToUSD, 1500000000000000000), 10); + debtToBorrow = sub(maxDebtLimit, daiDebt); + } + }