From 3a9322b72c9d14606f6a7f925ab8830ccf75dac0 Mon Sep 17 00:00:00 2001 From: Sowmayjain Date: Sat, 24 Nov 2018 19:38:45 +0530 Subject: [PATCH 01/25] Limit orders implementation on InstaKyber. --- .../{MoatAddress.sol => InstaAddress.sol} | 0 .../{MoatKyber.sol => InstaKyber.sol} | 75 ++++++++++++++----- 2 files changed, 58 insertions(+), 17 deletions(-) rename contracts/{MoatAddress.sol => InstaAddress.sol} (100%) rename contracts/protocols/{MoatKyber.sol => InstaKyber.sol} (64%) diff --git a/contracts/MoatAddress.sol b/contracts/InstaAddress.sol similarity index 100% rename from contracts/MoatAddress.sol rename to contracts/InstaAddress.sol diff --git a/contracts/protocols/MoatKyber.sol b/contracts/protocols/InstaKyber.sol similarity index 64% rename from contracts/protocols/MoatKyber.sol rename to contracts/protocols/InstaKyber.sol index be5e847..be1f439 100644 --- a/contracts/protocols/MoatKyber.sol +++ b/contracts/protocols/InstaKyber.sol @@ -1,3 +1,5 @@ +// limit order event + pragma solidity ^0.4.24; import "openzeppelin-solidity/contracts/math/SafeMath.sol"; @@ -59,34 +61,45 @@ contract Trade is Registry { address affiliate ); + // Market & Limit Order + // tradeAdmin manages the orders on behalf of client + // @param "client" is mainly for limit orders (and it can also be used for server-side market orders) function executeTrade( address src, address dest, uint srcAmt, - uint minConversionRate + uint minConversionRate, + address client ) public payable returns (uint destAmt) { - address protocolAdmin = getAddress("admin"); - uint ethQty; - // fetch token & deduct fees - IERC20 tokenFunctions = IERC20(src); - if (src == getAddress("eth")) { - require(msg.value == srcAmt, "Invalid Operation"); - ethQty = srcAmt; - } else { - tokenFunctions.transferFrom(msg.sender, address(this), srcAmt); + address trader = msg.sender; + if (client != address(0x0)) { + require(msg.sender == getAddress("tradeAdmin"), "Permission Denied"); + trader = client; } + // transferring token from trader and deducting fee if applicable + uint ethQty; + uint srcAmtAfterFees; + uint fees; + (ethQty, srcAmtAfterFees, fees) = getToken( + trader, + src, + srcAmt, + client + ); + + // Interacting with Kyber Proxy Contract Kyber kyberFunctions = Kyber(getAddress("kyber")); destAmt = kyberFunctions.trade.value(ethQty)( src, srcAmt, dest, - msg.sender, + trader, 2**256 - 1, minConversionRate, - protocolAdmin + getAddress("admin") ); emit KyberTrade( @@ -94,9 +107,9 @@ contract Trade is Registry { srcAmt, dest, destAmt, - msg.sender, + trader, minConversionRate, - protocolAdmin + getAddress("admin") ); } @@ -122,12 +135,40 @@ contract Trade is Registry { } } + function getToken( + address trader, + address src, + uint srcAmt, + address client + ) internal returns ( + uint ethQty, + uint srcAmtAfterFees, + uint fees + ) + { + if (src == getAddress("eth")) { + require(msg.value == srcAmt, "Invalid Operation"); + ethQty = srcAmt; + } else { + IERC20 tokenFunctions = IERC20(src); + tokenFunctions.transferFrom(trader, address(this), srcAmt); + ethQty = 0; + } + if (client != address(0x0)) { + fees = srcAmt / 400; // 0.25% + srcAmtAfterFees = srcAmt - fees; + if (ethQty > 0) { + ethQty = srcAmtAfterFees; + } + } + } + } contract MoatKyber is Trade { - event AssetsCollected(address name, uint addr); + event FeesCollected(address tokenAddr, uint amount); constructor(address rAddr) public { addressRegistry = rAddr; @@ -135,14 +176,14 @@ contract MoatKyber is Trade { function () public payable {} - function collectAsset(address tokenAddress, uint amount) public onlyAdmin { + function collectFees(address tokenAddress, uint amount) public onlyAdmin { if (tokenAddress == getAddress("eth")) { msg.sender.transfer(amount); } else { IERC20 tokenFunctions = IERC20(tokenAddress); tokenFunctions.transfer(msg.sender, amount); } - emit AssetsCollected(tokenAddress, amount); + emit FeesCollected(tokenAddress, amount); } } \ No newline at end of file From 8f6d79138b31c8571eb3ffc7a915dfb8fb174593 Mon Sep 17 00:00:00 2001 From: Sowmayjain Date: Sat, 24 Nov 2018 22:04:08 +0530 Subject: [PATCH 02/25] Implementated Limit Orders and Fees. --- contracts/protocols/InstaKyber.sol | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/contracts/protocols/InstaKyber.sol b/contracts/protocols/InstaKyber.sol index be1f439..521ab65 100644 --- a/contracts/protocols/InstaKyber.sol +++ b/contracts/protocols/InstaKyber.sol @@ -1,5 +1,3 @@ -// limit order event - pragma solidity ^0.4.24; import "openzeppelin-solidity/contracts/math/SafeMath.sol"; @@ -58,6 +56,7 @@ contract Trade is Registry { uint destAmt, address beneficiary, uint minConversionRate, + uint fees, address affiliate ); @@ -94,7 +93,7 @@ contract Trade is Registry { Kyber kyberFunctions = Kyber(getAddress("kyber")); destAmt = kyberFunctions.trade.value(ethQty)( src, - srcAmt, + srcAmtAfterFees, dest, trader, 2**256 - 1, @@ -104,11 +103,12 @@ contract Trade is Registry { emit KyberTrade( src, - srcAmt, + srcAmtAfterFees, dest, destAmt, trader, minConversionRate, + fees, getAddress("admin") ); @@ -154,6 +154,8 @@ contract Trade is Registry { tokenFunctions.transferFrom(trader, address(this), srcAmt); ethQty = 0; } + + srcAmtAfterFees = srcAmt; if (client != address(0x0)) { fees = srcAmt / 400; // 0.25% srcAmtAfterFees = srcAmt - fees; @@ -166,7 +168,7 @@ contract Trade is Registry { } -contract MoatKyber is Trade { +contract InstaKyber is Trade { event FeesCollected(address tokenAddr, uint amount); From 544c92dda69fa4c98e8f3f41fc71748cb4b6f5c1 Mon Sep 17 00:00:00 2001 From: Sowmayjain Date: Mon, 26 Nov 2018 00:56:55 +0530 Subject: [PATCH 03/25] Removed fees and tradeAdmin model. --- contracts/protocols/InstaKyber.sol | 118 ++++++++--------------------- 1 file changed, 32 insertions(+), 86 deletions(-) diff --git a/contracts/protocols/InstaKyber.sol b/contracts/protocols/InstaKyber.sol index 521ab65..7f2d4d6 100644 --- a/contracts/protocols/InstaKyber.sol +++ b/contracts/protocols/InstaKyber.sol @@ -56,64 +56,9 @@ contract Trade is Registry { uint destAmt, address beneficiary, uint minConversionRate, - uint fees, address affiliate ); - // Market & Limit Order - // tradeAdmin manages the orders on behalf of client - // @param "client" is mainly for limit orders (and it can also be used for server-side market orders) - function executeTrade( - address src, - address dest, - uint srcAmt, - uint minConversionRate, - address client - ) public payable returns (uint destAmt) - { - - address trader = msg.sender; - if (client != address(0x0)) { - require(msg.sender == getAddress("tradeAdmin"), "Permission Denied"); - trader = client; - } - - // transferring token from trader and deducting fee if applicable - uint ethQty; - uint srcAmtAfterFees; - uint fees; - (ethQty, srcAmtAfterFees, fees) = getToken( - trader, - src, - srcAmt, - client - ); - - // Interacting with Kyber Proxy Contract - Kyber kyberFunctions = Kyber(getAddress("kyber")); - destAmt = kyberFunctions.trade.value(ethQty)( - src, - srcAmtAfterFees, - dest, - trader, - 2**256 - 1, - minConversionRate, - getAddress("admin") - ); - - emit KyberTrade( - src, - srcAmtAfterFees, - dest, - destAmt, - trader, - minConversionRate, - fees, - getAddress("admin") - ); - - } - function getExpectedPrice( address src, address dest, @@ -135,17 +80,41 @@ contract Trade is Registry { } } - function getToken( - address trader, + function executeTrade( address src, + address dest, uint srcAmt, - address client - ) internal returns ( - uint ethQty, - uint srcAmtAfterFees, - uint fees - ) + uint minConversionRate + ) public payable returns (uint destAmt) { + + uint ethQty = getToken(msg.sender, src, srcAmt); + + // Interacting with Kyber Proxy Contract + Kyber kyberFunctions = Kyber(getAddress("kyber")); + destAmt = kyberFunctions.trade.value(ethQty)( + src, + srcAmt, + dest, + msg.sender, + 2**256 - 1, + minConversionRate, + getAddress("admin") + ); + + emit KyberTrade( + src, + srcAmt, + dest, + destAmt, + msg.sender, + minConversionRate, + getAddress("admin") + ); + + } + + function getToken(address trader, address src, uint srcAmt) internal returns (uint ethQty) { if (src == getAddress("eth")) { require(msg.value == srcAmt, "Invalid Operation"); ethQty = srcAmt; @@ -154,15 +123,6 @@ contract Trade is Registry { tokenFunctions.transferFrom(trader, address(this), srcAmt); ethQty = 0; } - - srcAmtAfterFees = srcAmt; - if (client != address(0x0)) { - fees = srcAmt / 400; // 0.25% - srcAmtAfterFees = srcAmt - fees; - if (ethQty > 0) { - ethQty = srcAmtAfterFees; - } - } } } @@ -170,22 +130,8 @@ contract Trade is Registry { contract InstaKyber is Trade { - event FeesCollected(address tokenAddr, uint amount); - constructor(address rAddr) public { addressRegistry = rAddr; } - function () public payable {} - - function collectFees(address tokenAddress, uint amount) public onlyAdmin { - if (tokenAddress == getAddress("eth")) { - msg.sender.transfer(amount); - } else { - IERC20 tokenFunctions = IERC20(tokenAddress); - tokenFunctions.transfer(msg.sender, amount); - } - emit FeesCollected(tokenAddress, amount); - } - } \ No newline at end of file From 70f48cb607daf2c9fbcf2cc77c215c141f13a9c4 Mon Sep 17 00:00:00 2001 From: Sowmayjain Date: Mon, 26 Nov 2018 01:00:47 +0530 Subject: [PATCH 04/25] Added Max Dest Amount Argument in the executeTrade function. --- contracts/protocols/InstaKyber.sol | 11 ++++++----- contracts/protocols/{MoatMaker.sol => InstaMaker.sol} | 0 2 files changed, 6 insertions(+), 5 deletions(-) rename contracts/protocols/{MoatMaker.sol => InstaMaker.sol} (100%) diff --git a/contracts/protocols/InstaKyber.sol b/contracts/protocols/InstaKyber.sol index 7f2d4d6..f10f4a9 100644 --- a/contracts/protocols/InstaKyber.sol +++ b/contracts/protocols/InstaKyber.sol @@ -81,10 +81,11 @@ contract Trade is Registry { } function executeTrade( - address src, - address dest, - uint srcAmt, - uint minConversionRate + address src, // token to sell + address dest, // token to buy + uint srcAmt, // amount of token for sell + uint minConversionRate, // minimum slippage rate + uint maxDestAmt // max amount of dest token ) public payable returns (uint destAmt) { @@ -97,7 +98,7 @@ contract Trade is Registry { srcAmt, dest, msg.sender, - 2**256 - 1, + maxDestAmt, minConversionRate, getAddress("admin") ); diff --git a/contracts/protocols/MoatMaker.sol b/contracts/protocols/InstaMaker.sol similarity index 100% rename from contracts/protocols/MoatMaker.sol rename to contracts/protocols/InstaMaker.sol From 46ba513ad33366b4fbfb31b5f5962cb8f2156b6f Mon Sep 17 00:00:00 2001 From: Sowmayjain Date: Mon, 26 Nov 2018 02:01:17 +0530 Subject: [PATCH 05/25] MaxDestAmt Implementation in Maker - Kyber Interaction. --- contracts/protocols/InstaMaker.sol | 83 ++++++++++++++++-------------- 1 file changed, 45 insertions(+), 38 deletions(-) diff --git a/contracts/protocols/InstaMaker.sol b/contracts/protocols/InstaMaker.sol index bfac644..748c8be 100644 --- a/contracts/protocols/InstaMaker.sol +++ b/contracts/protocols/InstaMaker.sol @@ -29,13 +29,20 @@ interface WETHFace { function withdraw(uint wad) external; } -interface MoatKyber { +interface InstaKyber { function executeTrade( address src, address dest, uint srcAmt, - uint minConversionRate + uint minConversionRate, + uint maxDestAmt ) external payable returns (uint destAmt); + + function getExpectedPrice( + address src, + address dest, + uint srcAmt + ) external view returns (uint, uint); } @@ -128,47 +135,58 @@ contract RepayLoan is IssueLoan { function repay( uint daiWipe, - uint ethFree, - uint mkrFees, // either this... - uint feeMinConRate // or this is 0 + uint ethFree + // bool mkrFees, // either this... + // uint feeMinConRate // or this is 0 ) public payable { - if (daiWipe > 0) {wipeDAI(daiWipe, mkrFees, feeMinConRate);} + if (daiWipe > 0) {wipeDAI(daiWipe);} if (ethFree > 0) {unlockETH(ethFree);} } - function wipeDAI(uint daiWipe, uint mkrFees, uint feeMinConRate) public payable { - IERC20 daiTkn = IERC20(getAddress("dai")); - IERC20 mkrTkn = IERC20(getAddress("mkr")); + function wipeDAI(uint daiWipe) public payable { + address dai = getAddress("dai"); + address mkr = getAddress("mkr"); + address eth = getAddress("eth"); - // MKR now balance - uint nowBal = mkrTkn.balanceOf(address(this)); + IERC20 daiTkn = IERC20(dai); + IERC20 mkrTkn = IERC20(mkr); - // fetch DAI + // contract MKR balance before wiping + uint contractMKR = mkrTkn.balanceOf(address(this)); + // get DAI daiTkn.transferFrom(msg.sender, address(this), daiWipe); // DAI to pay the debt // wipe DAI loanMaster.wipe(cdps[msg.sender], daiWipe); + // MKR fee = before wiping bal - after wiping bal + uint mkrCharged = contractMKR - mkrTkn.balanceOf(address(this)); - // MKR after wiping - uint mkrCharged = nowBal - mkrTkn.balanceOf(address(this)); - - // if fees paid in MKR - if (mkrFees > 0) { - mkrTkn.transferFrom(msg.sender, address(this), mkrCharged); // user paying MKR fees - } else { // otherwise swap ETH via MoatKyber - MoatKyber mtky = MoatKyber(getAddress("moatkyber")); - uint mkrBought = mtky.executeTrade.value(msg.value)( - getAddress("eth"), - getAddress("mkr"), + // claiming paid MKR back + if (msg.value > 0) { + // Interacting with Kyber to swap ETH with MKR + InstaKyber instak = InstaKyber(getAddress("InstaKyber")); + uint minRate; + (, minRate) = instak.getExpectedPrice(eth, mkr, msg.value); + uint mkrBought = instak.executeTrade.value(msg.value)( + eth, + mkr, msg.value, - feeMinConRate + minRate, + mkrCharged ); - if (mkrBought > mkrCharged) { - mkrTkn.transfer(msg.sender, mkrBought - mkrCharged); // pay back balanced MKR tokens + + require(mkrCharged == mkrBought, "ETH not sufficient to cover the MKR fees."); + + // the ether will always belong to sender as there's no way contract can accept ether + if (address(this).balance > 0) { + msg.sender.transfer(address(this).balance); } + + } else { + // take MKR directly from address + mkrTkn.transferFrom(msg.sender, address(this), mkrCharged); // user paying MKR fees } - require(mkrTkn.balanceOf(address(this)) == nowBal, "MKR balance not reimbursed"); emit WipedDAI(msg.sender, daiWipe, mkrCharged); } @@ -230,17 +248,6 @@ contract MoatMaker is BorrowTasks { approveERC20(); } - function () public payable {} - - function collectAsset(address tokenAddress, uint amount) public onlyAdmin { - if (tokenAddress == getAddress("eth")) { - msg.sender.transfer(amount); - } else { - IERC20 tokenFunctions = IERC20(tokenAddress); - tokenFunctions.transfer(msg.sender, amount); - } - } - function freeze(bool stop) public onlyAdmin { freezed = stop; } From 22a93bc62b73b88fb0fc53a169aff6732dbb350b Mon Sep 17 00:00:00 2001 From: Sowmayjain Date: Mon, 26 Nov 2018 02:11:31 +0530 Subject: [PATCH 06/25] Beautified code. --- contracts/protocols/InstaMaker.sol | 62 ++++++++++++++++-------------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/contracts/protocols/InstaMaker.sol b/contracts/protocols/InstaMaker.sol index 748c8be..1c76c91 100644 --- a/contracts/protocols/InstaMaker.sol +++ b/contracts/protocols/InstaMaker.sol @@ -136,8 +136,6 @@ contract RepayLoan is IssueLoan { function repay( uint daiWipe, uint ethFree - // bool mkrFees, // either this... - // uint feeMinConRate // or this is 0 ) public payable { if (daiWipe > 0) {wipeDAI(daiWipe);} @@ -152,38 +150,20 @@ contract RepayLoan is IssueLoan { IERC20 daiTkn = IERC20(dai); IERC20 mkrTkn = IERC20(mkr); - // contract MKR balance before wiping - uint contractMKR = mkrTkn.balanceOf(address(this)); - // get DAI - daiTkn.transferFrom(msg.sender, address(this), daiWipe); // DAI to pay the debt - // wipe DAI - loanMaster.wipe(cdps[msg.sender], daiWipe); - // MKR fee = before wiping bal - after wiping bal - uint mkrCharged = contractMKR - mkrTkn.balanceOf(address(this)); + uint contractMKR = mkrTkn.balanceOf(address(this)); // contract MKR balance before wiping + daiTkn.transferFrom(msg.sender, address(this), daiWipe); // get DAI to pay the debt + loanMaster.wipe(cdps[msg.sender], daiWipe); // wipe DAI + uint mkrCharged = contractMKR - mkrTkn.balanceOf(address(this)); // MKR fee = before wiping bal - after wiping bal // claiming paid MKR back - if (msg.value > 0) { - // Interacting with Kyber to swap ETH with MKR - InstaKyber instak = InstaKyber(getAddress("InstaKyber")); - uint minRate; - (, minRate) = instak.getExpectedPrice(eth, mkr, msg.value); - uint mkrBought = instak.executeTrade.value(msg.value)( + if (msg.value > 0) { // Interacting with Kyber to swap ETH with MKR + swapETHMKR( eth, mkr, - msg.value, - minRate, - mkrCharged + mkrCharged, + msg.value ); - - require(mkrCharged == mkrBought, "ETH not sufficient to cover the MKR fees."); - - // the ether will always belong to sender as there's no way contract can accept ether - if (address(this).balance > 0) { - msg.sender.transfer(address(this).balance); - } - - } else { - // take MKR directly from address + } else { // take MKR directly from address mkrTkn.transferFrom(msg.sender, address(this), mkrCharged); // user paying MKR fees } @@ -201,6 +181,30 @@ contract RepayLoan is IssueLoan { emit UnlockedETH(msg.sender, ethFree); } + function swapETHMKR( + address eth, + address mkr, + uint mkrCharged, + uint ethQty + ) internal + { + InstaKyber instak = InstaKyber(getAddress("InstaKyber")); + uint minRate; + (, minRate) = instak.getExpectedPrice(eth, mkr, ethQty); + uint mkrBought = instak.executeTrade.value(ethQty)( + eth, + mkr, + ethQty, + minRate, + mkrCharged + ); + require(mkrCharged == mkrBought, "ETH not sufficient to cover the MKR fees."); + if (address(this).balance > 0) { + // ether always belong to sender coz no way contract can hold ether + msg.sender.transfer(address(this).balance); + } + } + } From cdb14394b4459bd5ebbe865dfce4bc7a8538f037 Mon Sep 17 00:00:00 2001 From: Sowmayjain Date: Mon, 26 Nov 2018 03:09:47 +0530 Subject: [PATCH 07/25] Code optimisation. --- contracts/protocols/InstaKyber.sol | 39 ++++++++++++++++++++++++++++-- contracts/protocols/InstaMaker.sol | 30 ++++++++++++++++++++--- 2 files changed, 64 insertions(+), 5 deletions(-) diff --git a/contracts/protocols/InstaKyber.sol b/contracts/protocols/InstaKyber.sol index f10f4a9..914b41e 100644 --- a/contracts/protocols/InstaKyber.sol +++ b/contracts/protocols/InstaKyber.sol @@ -1,8 +1,30 @@ pragma solidity ^0.4.24; -import "openzeppelin-solidity/contracts/math/SafeMath.sol"; -import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol"; +library SafeMath { + function mul(uint256 a, uint256 b) internal pure returns (uint256) { + if (a == 0) { + return 0; + } + uint256 c = a * b; + require(c / a == b, "Assertion Failed"); + return c; + } + + function div(uint256 a, uint256 b) internal pure returns (uint256) { + require(b > 0, "Assertion Failed"); + uint256 c = a / b; + return c; + } + +} + +interface IERC20 { + function balanceOf(address who) external view returns (uint256); + function transfer(address to, uint256 value) external returns (bool); + function approve(address spender, uint256 value) external returns (bool); + function transferFrom(address from, address to, uint256 value) external returns (bool); +} interface AddressRegistry { function getAddr(string name) external view returns(address); @@ -103,6 +125,17 @@ contract Trade is Registry { getAddress("admin") ); + // maxDestAmt usecase implementated + if (src == getAddress("eth") && address(this).balance > 0) { + msg.sender.transfer(address(this).balance); + } else { + IERC20 srcTkn = IERC20(src); + uint srcBal = srcTkn.balanceOf(address(this)); + if (srcBal > 0) { + srcTkn.transfer(msg.sender, srcBal); + } + } + emit KyberTrade( src, srcAmt, @@ -135,4 +168,6 @@ contract InstaKyber is Trade { addressRegistry = rAddr; } + function () public payable {} + } \ No newline at end of file diff --git a/contracts/protocols/InstaMaker.sol b/contracts/protocols/InstaMaker.sol index 1c76c91..f490e03 100644 --- a/contracts/protocols/InstaMaker.sol +++ b/contracts/protocols/InstaMaker.sol @@ -1,8 +1,31 @@ pragma solidity 0.4.24; -import "openzeppelin-solidity/contracts/math/SafeMath.sol"; -import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol"; +library SafeMath { + + function mul(uint256 a, uint256 b) internal pure returns (uint256) { + if (a == 0) { + return 0; + } + uint256 c = a * b; + require(c / a == b, "Assertion Failed"); + return c; + } + + function div(uint256 a, uint256 b) internal pure returns (uint256) { + require(b > 0, "Assertion Failed"); + uint256 c = a / b; + return c; + } + +} + +interface IERC20 { + function balanceOf(address who) external view returns (uint256); + function transfer(address to, uint256 value) external returns (bool); + function approve(address spender, uint256 value) external returns (bool); + function transferFrom(address from, address to, uint256 value) external returns (bool); +} interface AddressRegistry { function getAddr(string name) external view returns(address); @@ -200,7 +223,6 @@ contract RepayLoan is IssueLoan { ); require(mkrCharged == mkrBought, "ETH not sufficient to cover the MKR fees."); if (address(this).balance > 0) { - // ether always belong to sender coz no way contract can hold ether msg.sender.transfer(address(this).balance); } } @@ -252,6 +274,8 @@ contract MoatMaker is BorrowTasks { approveERC20(); } + function () public payable {} + function freeze(bool stop) public onlyAdmin { freezed = stop; } From 2fefb96da1cabfd0ae485f9f510f85fabb5ad3c4 Mon Sep 17 00:00:00 2001 From: Sowmayjain Date: Mon, 26 Nov 2018 03:56:31 +0530 Subject: [PATCH 08/25] Fixed err & added function to collect MKR base fees. --- contracts/protocols/InstaKyber.sol | 1 + contracts/protocols/InstaMaker.sol | 22 ++++++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/contracts/protocols/InstaKyber.sol b/contracts/protocols/InstaKyber.sol index 914b41e..d8d1a9e 100644 --- a/contracts/protocols/InstaKyber.sol +++ b/contracts/protocols/InstaKyber.sol @@ -1,5 +1,6 @@ pragma solidity ^0.4.24; + library SafeMath { function mul(uint256 a, uint256 b) internal pure returns (uint256) { diff --git a/contracts/protocols/InstaMaker.sol b/contracts/protocols/InstaMaker.sol index f490e03..9995b68 100644 --- a/contracts/protocols/InstaMaker.sol +++ b/contracts/protocols/InstaMaker.sol @@ -104,8 +104,6 @@ contract GlobalVar is Registry { // address public ethfeed = 0x729D19f657BD0614b4985Cf1D82531c67569197B // pip // address public mkrfeed = 0x99041F808D598B782D5a3e498681C2452A31da08 // pep - MakerCDP loanMaster = MakerCDP(getAddress("cdp")); - bytes32 public blankCDP = 0x0000000000000000000000000000000000000000000000000000000000000000; mapping (address => bytes32) public cdps; // borrower >>> CDP Bytes bool public freezed; @@ -119,11 +117,13 @@ contract IssueLoan is GlobalVar { event OpenedNewCDP(address borrower, bytes32 cdpBytes); function pethPEReth(uint ethNum) public view returns (uint rPETH) { + MakerCDP loanMaster = MakerCDP(getAddress("cdp")); rPETH = (ethNum.mul(10 ** 27)).div(loanMaster.per()); } function borrow(uint daiDraw) public payable { if (cdps[msg.sender] == blankCDP) { + MakerCDP loanMaster = MakerCDP(getAddress("cdp")); cdps[msg.sender] = loanMaster.open(); emit OpenedNewCDP(msg.sender, cdps[msg.sender]); } @@ -135,6 +135,7 @@ contract IssueLoan is GlobalVar { WETHFace wethTkn = WETHFace(getAddress("weth")); wethTkn.deposit.value(msg.value)(); // ETH to WETH uint pethToLock = pethPEReth(msg.value); + MakerCDP loanMaster = MakerCDP(getAddress("cdp")); loanMaster.join(pethToLock); // WETH to PETH loanMaster.lock(cdps[msg.sender], pethToLock); // PETH to CDP emit LockedETH(msg.sender, msg.value, pethToLock); @@ -142,6 +143,7 @@ contract IssueLoan is GlobalVar { function drawDAI(uint daiDraw) public { require(!freezed, "Operation Disabled"); + MakerCDP loanMaster = MakerCDP(getAddress("cdp")); loanMaster.draw(cdps[msg.sender], daiDraw); IERC20 daiTkn = IERC20(getAddress("dai")); daiTkn.transfer(msg.sender, daiDraw); @@ -175,6 +177,7 @@ contract RepayLoan is IssueLoan { uint contractMKR = mkrTkn.balanceOf(address(this)); // contract MKR balance before wiping daiTkn.transferFrom(msg.sender, address(this), daiWipe); // get DAI to pay the debt + MakerCDP loanMaster = MakerCDP(getAddress("cdp")); loanMaster.wipe(cdps[msg.sender], daiWipe); // wipe DAI uint mkrCharged = contractMKR - mkrTkn.balanceOf(address(this)); // MKR fee = before wiping bal - after wiping bal @@ -196,6 +199,7 @@ contract RepayLoan is IssueLoan { function unlockETH(uint ethFree) public { require(!freezed, "Operation Disabled"); uint pethToUnlock = pethPEReth(ethFree); + MakerCDP loanMaster = MakerCDP(getAddress("cdp")); loanMaster.free(cdps[msg.sender], pethToUnlock); // CDP to PETH loanMaster.exit(pethToUnlock); // PETH to WETH WETHFace wethTkn = WETHFace(getAddress("weth")); @@ -236,6 +240,7 @@ contract BorrowTasks is RepayLoan { function transferCDP(address nextOwner) public { require(nextOwner != 0, "Invalid Address."); + MakerCDP loanMaster = MakerCDP(getAddress("cdp")); loanMaster.give(cdps[msg.sender], nextOwner); emit TranferCDP(cdps[msg.sender], msg.sender, nextOwner); cdps[msg.sender] = blankCDP; @@ -267,7 +272,9 @@ contract BorrowTasks is RepayLoan { } -contract MoatMaker is BorrowTasks { +contract InstaMaker is BorrowTasks { + + event MKRCollected(uint amount); constructor(address rAddr) public { addressRegistry = rAddr; @@ -280,4 +287,11 @@ contract MoatMaker is BorrowTasks { freezed = stop; } -} + // MKR fees kept as balance + function collectMKR(uint amount) public onlyAdmin { + IERC20 mkrTkn = IERC20(getAddress("mkr")); + mkrTkn.transfer(msg.sender, amount); + emit MKRCollected(amount); + } + +} \ No newline at end of file From 9bf4a056f2ae3d14b1e1e58f791f651e74a92bed Mon Sep 17 00:00:00 2001 From: Sowmayjain Date: Tue, 27 Nov 2018 01:39:35 +0530 Subject: [PATCH 09/25] Optimised code to reduce gas cost. --- contracts/protocols/InstaMaker.sol | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/contracts/protocols/InstaMaker.sol b/contracts/protocols/InstaMaker.sol index 9995b68..6b6f68c 100644 --- a/contracts/protocols/InstaMaker.sol +++ b/contracts/protocols/InstaMaker.sol @@ -93,20 +93,11 @@ contract GlobalVar is Registry { using SafeMath for uint; using SafeMath for uint256; - // kovan network - // address public weth = 0xd0A1E359811322d97991E03f863a0C30C2cF029C; - // address public peth = 0xf4d791139cE033Ad35DB2B2201435fAd668B1b64; - // address public mkr = 0xAaF64BFCC32d0F15873a02163e7E500671a4ffcD; - // address public dai = 0xC4375B7De8af5a38a93548eb8453a498222C4fF2; - // address public eth = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee; - // address public cdpAddr = 0xa71937147b55Deb8a530C7229C442Fd3F31b7db2; - - // address public ethfeed = 0x729D19f657BD0614b4985Cf1D82531c67569197B // pip - // address public mkrfeed = 0x99041F808D598B782D5a3e498681C2452A31da08 // pep - bytes32 public blankCDP = 0x0000000000000000000000000000000000000000000000000000000000000000; + address cdpAddr; // cups mapping (address => bytes32) public cdps; // borrower >>> CDP Bytes bool public freezed; + } @@ -117,13 +108,13 @@ contract IssueLoan is GlobalVar { event OpenedNewCDP(address borrower, bytes32 cdpBytes); function pethPEReth(uint ethNum) public view returns (uint rPETH) { - MakerCDP loanMaster = MakerCDP(getAddress("cdp")); + MakerCDP loanMaster = MakerCDP(cdpAddr); rPETH = (ethNum.mul(10 ** 27)).div(loanMaster.per()); } function borrow(uint daiDraw) public payable { if (cdps[msg.sender] == blankCDP) { - MakerCDP loanMaster = MakerCDP(getAddress("cdp")); + MakerCDP loanMaster = MakerCDP(cdpAddr); cdps[msg.sender] = loanMaster.open(); emit OpenedNewCDP(msg.sender, cdps[msg.sender]); } @@ -135,7 +126,7 @@ contract IssueLoan is GlobalVar { WETHFace wethTkn = WETHFace(getAddress("weth")); wethTkn.deposit.value(msg.value)(); // ETH to WETH uint pethToLock = pethPEReth(msg.value); - MakerCDP loanMaster = MakerCDP(getAddress("cdp")); + MakerCDP loanMaster = MakerCDP(cdpAddr); loanMaster.join(pethToLock); // WETH to PETH loanMaster.lock(cdps[msg.sender], pethToLock); // PETH to CDP emit LockedETH(msg.sender, msg.value, pethToLock); @@ -143,7 +134,7 @@ contract IssueLoan is GlobalVar { function drawDAI(uint daiDraw) public { require(!freezed, "Operation Disabled"); - MakerCDP loanMaster = MakerCDP(getAddress("cdp")); + MakerCDP loanMaster = MakerCDP(cdpAddr); loanMaster.draw(cdps[msg.sender], daiDraw); IERC20 daiTkn = IERC20(getAddress("dai")); daiTkn.transfer(msg.sender, daiDraw); @@ -177,7 +168,7 @@ contract RepayLoan is IssueLoan { uint contractMKR = mkrTkn.balanceOf(address(this)); // contract MKR balance before wiping daiTkn.transferFrom(msg.sender, address(this), daiWipe); // get DAI to pay the debt - MakerCDP loanMaster = MakerCDP(getAddress("cdp")); + MakerCDP loanMaster = MakerCDP(cdpAddr); loanMaster.wipe(cdps[msg.sender], daiWipe); // wipe DAI uint mkrCharged = contractMKR - mkrTkn.balanceOf(address(this)); // MKR fee = before wiping bal - after wiping bal @@ -199,7 +190,7 @@ contract RepayLoan is IssueLoan { function unlockETH(uint ethFree) public { require(!freezed, "Operation Disabled"); uint pethToUnlock = pethPEReth(ethFree); - MakerCDP loanMaster = MakerCDP(getAddress("cdp")); + MakerCDP loanMaster = MakerCDP(cdpAddr); loanMaster.free(cdps[msg.sender], pethToUnlock); // CDP to PETH loanMaster.exit(pethToUnlock); // PETH to WETH WETHFace wethTkn = WETHFace(getAddress("weth")); @@ -240,7 +231,7 @@ contract BorrowTasks is RepayLoan { function transferCDP(address nextOwner) public { require(nextOwner != 0, "Invalid Address."); - MakerCDP loanMaster = MakerCDP(getAddress("cdp")); + MakerCDP loanMaster = MakerCDP(cdpAddr); loanMaster.give(cdps[msg.sender], nextOwner); emit TranferCDP(cdps[msg.sender], msg.sender, nextOwner); cdps[msg.sender] = blankCDP; @@ -258,7 +249,6 @@ contract BorrowTasks is RepayLoan { } function approveERC20() public { - address cdpAddr = getAddress("cdp"); IERC20 wethTkn = IERC20(getAddress("weth")); wethTkn.approve(cdpAddr, 2**256 - 1); IERC20 pethTkn = IERC20(getAddress("peth")); @@ -278,6 +268,7 @@ contract InstaMaker is BorrowTasks { constructor(address rAddr) public { addressRegistry = rAddr; + cdpAddr = cdpAddr; approveERC20(); } From c44cc2dafc78a1804b7aa3d304f016dfd4dc06bf Mon Sep 17 00:00:00 2001 From: Sowmayjain Date: Tue, 27 Nov 2018 02:11:58 +0530 Subject: [PATCH 10/25] Fixed a silly error. --- contracts/protocols/InstaMaker.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/protocols/InstaMaker.sol b/contracts/protocols/InstaMaker.sol index 6b6f68c..505b577 100644 --- a/contracts/protocols/InstaMaker.sol +++ b/contracts/protocols/InstaMaker.sol @@ -268,7 +268,7 @@ contract InstaMaker is BorrowTasks { constructor(address rAddr) public { addressRegistry = rAddr; - cdpAddr = cdpAddr; + cdpAddr = getAddress("cdp"); approveERC20(); } From 01307bf9ec6a5064405cb960ff18646a0fb4b259 Mon Sep 17 00:00:00 2001 From: Sowmayjain Date: Tue, 27 Nov 2018 03:21:48 +0530 Subject: [PATCH 11/25] Optimised code to reduce gas cost. --- contracts/protocols/InstaKyber.sol | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/contracts/protocols/InstaKyber.sol b/contracts/protocols/InstaKyber.sol index d8d1a9e..c8f9c12 100644 --- a/contracts/protocols/InstaKyber.sol +++ b/contracts/protocols/InstaKyber.sol @@ -97,9 +97,10 @@ contract Trade is Registry { } function approveKyber(address[] tokenArr) public { + address kyberProxy = getAddress("kyber"); for (uint i = 0; i < tokenArr.length; i++) { IERC20 tokenFunctions = IERC20(tokenArr[i]); - tokenFunctions.approve(getAddress("kyber"), 2**256 - 1); + tokenFunctions.approve(kyberProxy, 2**256 - 1); } } @@ -112,7 +113,13 @@ contract Trade is Registry { ) public payable returns (uint destAmt) { - uint ethQty = getToken(msg.sender, src, srcAmt); + address eth = getAddress("eth"); + uint ethQty = getToken( + msg.sender, + src, + srcAmt, + eth + ); // Interacting with Kyber Proxy Contract Kyber kyberFunctions = Kyber(getAddress("kyber")); @@ -127,9 +134,9 @@ contract Trade is Registry { ); // maxDestAmt usecase implementated - if (src == getAddress("eth") && address(this).balance > 0) { + if (src == eth && address(this).balance > 0) { msg.sender.transfer(address(this).balance); - } else { + } else if (src != eth) { // as there is no balanceOf of eth IERC20 srcTkn = IERC20(src); uint srcBal = srcTkn.balanceOf(address(this)); if (srcBal > 0) { @@ -149,8 +156,14 @@ contract Trade is Registry { } - function getToken(address trader, address src, uint srcAmt) internal returns (uint ethQty) { - if (src == getAddress("eth")) { + function getToken( + address trader, + address src, + uint srcAmt, + address eth + ) internal returns (uint ethQty) + { + if (src == eth) { require(msg.value == srcAmt, "Invalid Operation"); ethQty = srcAmt; } else { From db1ba767add68a7c911a5877e01d0ddc93158cad Mon Sep 17 00:00:00 2001 From: Sowmayjain Date: Tue, 27 Nov 2018 04:59:23 +0530 Subject: [PATCH 12/25] Event renaming. --- contracts/protocols/InstaMaker.sol | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contracts/protocols/InstaMaker.sol b/contracts/protocols/InstaMaker.sol index 505b577..203bc7b 100644 --- a/contracts/protocols/InstaMaker.sol +++ b/contracts/protocols/InstaMaker.sol @@ -105,7 +105,7 @@ contract IssueLoan is GlobalVar { event LockedETH(address borrower, uint lockETH, uint lockPETH); event LoanedDAI(address borrower, uint loanDAI); - event OpenedNewCDP(address borrower, bytes32 cdpBytes); + event NewCDP(address borrower, bytes32 cdpBytes); function pethPEReth(uint ethNum) public view returns (uint rPETH) { MakerCDP loanMaster = MakerCDP(cdpAddr); @@ -116,7 +116,7 @@ contract IssueLoan is GlobalVar { if (cdps[msg.sender] == blankCDP) { MakerCDP loanMaster = MakerCDP(cdpAddr); cdps[msg.sender] = loanMaster.open(); - emit OpenedNewCDP(msg.sender, cdps[msg.sender]); + emit NewCDP(msg.sender, cdps[msg.sender]); } if (msg.value > 0) {lockETH();} if (daiDraw > 0) {drawDAI(daiDraw);} From 8d4b5c6f7352929e4646f07572294130d1728fed Mon Sep 17 00:00:00 2001 From: Sowmayjain Date: Wed, 28 Nov 2018 01:23:15 +0530 Subject: [PATCH 13/25] Fixed an error associated with create CDP & code cleanup. --- contracts/protocols/InstaKyber.sol | 17 +++----------- contracts/protocols/InstaMaker.sol | 36 +++++++++++------------------- 2 files changed, 16 insertions(+), 37 deletions(-) diff --git a/contracts/protocols/InstaKyber.sol b/contracts/protocols/InstaKyber.sol index c8f9c12..beb1a9e 100644 --- a/contracts/protocols/InstaKyber.sol +++ b/contracts/protocols/InstaKyber.sol @@ -90,9 +90,7 @@ contract Trade is Registry { { Kyber kyberFunctions = Kyber(getAddress("kyber")); return kyberFunctions.getExpectedRate( - src, - dest, - srcAmt + src, dest, srcAmt ); } @@ -115,10 +113,7 @@ contract Trade is Registry { address eth = getAddress("eth"); uint ethQty = getToken( - msg.sender, - src, - srcAmt, - eth + msg.sender, src, srcAmt, eth ); // Interacting with Kyber Proxy Contract @@ -145,13 +140,7 @@ contract Trade is Registry { } emit KyberTrade( - src, - srcAmt, - dest, - destAmt, - msg.sender, - minConversionRate, - getAddress("admin") + src, srcAmt, dest, destAmt, msg.sender, minConversionRate, getAddress("admin") ); } diff --git a/contracts/protocols/InstaMaker.sol b/contracts/protocols/InstaMaker.sol index 203bc7b..b5bfffc 100644 --- a/contracts/protocols/InstaMaker.sol +++ b/contracts/protocols/InstaMaker.sol @@ -103,7 +103,7 @@ contract GlobalVar is Registry { contract IssueLoan is GlobalVar { - event LockedETH(address borrower, uint lockETH, uint lockPETH); + event LockedETH(address borrower, uint lockETH, uint lockPETH, address lockedBy); event LoanedDAI(address borrower, uint loanDAI); event NewCDP(address borrower, bytes32 cdpBytes); @@ -113,23 +113,24 @@ contract IssueLoan is GlobalVar { } function borrow(uint daiDraw) public payable { - if (cdps[msg.sender] == blankCDP) { - MakerCDP loanMaster = MakerCDP(cdpAddr); - cdps[msg.sender] = loanMaster.open(); - emit NewCDP(msg.sender, cdps[msg.sender]); - } if (msg.value > 0) {lockETH();} if (daiDraw > 0) {drawDAI(daiDraw);} } function lockETH() public payable { + MakerCDP loanMaster = MakerCDP(cdpAddr); + if (cdps[msg.sender] == blankCDP) { + cdps[msg.sender] = loanMaster.open(); + emit NewCDP(msg.sender, cdps[msg.sender]); + } WETHFace wethTkn = WETHFace(getAddress("weth")); wethTkn.deposit.value(msg.value)(); // ETH to WETH uint pethToLock = pethPEReth(msg.value); - MakerCDP loanMaster = MakerCDP(cdpAddr); loanMaster.join(pethToLock); // WETH to PETH loanMaster.lock(cdps[msg.sender], pethToLock); // PETH to CDP - emit LockedETH(msg.sender, msg.value, pethToLock); + emit LockedETH( + msg.sender, msg.value, pethToLock, msg.sender + ); } function drawDAI(uint daiDraw) public { @@ -149,11 +150,7 @@ contract RepayLoan is IssueLoan { event WipedDAI(address borrower, uint daiWipe, uint mkrCharged); event UnlockedETH(address borrower, uint ethFree); - function repay( - uint daiWipe, - uint ethFree - ) public payable - { + function repay(uint daiWipe, uint ethFree) public payable { if (daiWipe > 0) {wipeDAI(daiWipe);} if (ethFree > 0) {unlockETH(ethFree);} } @@ -175,10 +172,7 @@ contract RepayLoan is IssueLoan { // claiming paid MKR back if (msg.value > 0) { // Interacting with Kyber to swap ETH with MKR swapETHMKR( - eth, - mkr, - mkrCharged, - msg.value + eth, mkr, mkrCharged, msg.value ); } else { // take MKR directly from address mkrTkn.transferFrom(msg.sender, address(this), mkrCharged); // user paying MKR fees @@ -210,11 +204,7 @@ contract RepayLoan is IssueLoan { uint minRate; (, minRate) = instak.getExpectedPrice(eth, mkr, ethQty); uint mkrBought = instak.executeTrade.value(ethQty)( - eth, - mkr, - ethQty, - minRate, - mkrCharged + eth, mkr, ethQty, minRate, mkrCharged ); require(mkrCharged == mkrBought, "ETH not sufficient to cover the MKR fees."); if (address(this).balance > 0) { @@ -278,7 +268,7 @@ contract InstaMaker is BorrowTasks { freezed = stop; } - // MKR fees kept as balance + // collecting MKR token kept as balance to pay fees function collectMKR(uint amount) public onlyAdmin { IERC20 mkrTkn = IERC20(getAddress("mkr")); mkrTkn.transfer(msg.sender, amount); From 69ff3e1146811f34cf4e2b8ed43191226d958f86 Mon Sep 17 00:00:00 2001 From: Sowmayjain Date: Wed, 28 Nov 2018 01:26:22 +0530 Subject: [PATCH 14/25] Added feature - lock ETH in CDP without being the owner. --- contracts/protocols/InstaMaker.sol | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/contracts/protocols/InstaMaker.sol b/contracts/protocols/InstaMaker.sol index b5bfffc..89cdad6 100644 --- a/contracts/protocols/InstaMaker.sol +++ b/contracts/protocols/InstaMaker.sol @@ -113,13 +113,14 @@ contract IssueLoan is GlobalVar { } function borrow(uint daiDraw) public payable { - if (msg.value > 0) {lockETH();} + if (msg.value > 0) {lockETH(msg.sender);} if (daiDraw > 0) {drawDAI(daiDraw);} } - function lockETH() public payable { + function lockETH(address borrower) public payable { MakerCDP loanMaster = MakerCDP(cdpAddr); - if (cdps[msg.sender] == blankCDP) { + if (cdps[borrower] == blankCDP) { + require(msg.sender == borrower, "Creating CDP for others is not permitted at the moment."); cdps[msg.sender] = loanMaster.open(); emit NewCDP(msg.sender, cdps[msg.sender]); } @@ -127,9 +128,9 @@ contract IssueLoan is GlobalVar { wethTkn.deposit.value(msg.value)(); // ETH to WETH uint pethToLock = pethPEReth(msg.value); loanMaster.join(pethToLock); // WETH to PETH - loanMaster.lock(cdps[msg.sender], pethToLock); // PETH to CDP + loanMaster.lock(cdps[borrower], pethToLock); // PETH to CDP emit LockedETH( - msg.sender, msg.value, pethToLock, msg.sender + borrower, msg.value, pethToLock, msg.sender ); } From 054f069baaef504555f4ee42c853a88164037e2a Mon Sep 17 00:00:00 2001 From: Sowmayjain Date: Wed, 28 Nov 2018 01:41:04 +0530 Subject: [PATCH 15/25] Added feature - wipe CDP DAI without being the owner. --- contracts/protocols/InstaMaker.sol | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/contracts/protocols/InstaMaker.sol b/contracts/protocols/InstaMaker.sol index 89cdad6..e7513b2 100644 --- a/contracts/protocols/InstaMaker.sol +++ b/contracts/protocols/InstaMaker.sol @@ -148,15 +148,15 @@ contract IssueLoan is GlobalVar { contract RepayLoan is IssueLoan { - event WipedDAI(address borrower, uint daiWipe, uint mkrCharged); + event WipedDAI(address borrower, uint daiWipe, uint mkrCharged, address wipedBy); event UnlockedETH(address borrower, uint ethFree); function repay(uint daiWipe, uint ethFree) public payable { - if (daiWipe > 0) {wipeDAI(daiWipe);} + if (daiWipe > 0) {wipeDAI(daiWipe, msg.sender);} if (ethFree > 0) {unlockETH(ethFree);} } - function wipeDAI(uint daiWipe) public payable { + function wipeDAI(uint daiWipe, address borrower) public payable { address dai = getAddress("dai"); address mkr = getAddress("mkr"); address eth = getAddress("eth"); @@ -167,7 +167,7 @@ contract RepayLoan is IssueLoan { uint contractMKR = mkrTkn.balanceOf(address(this)); // contract MKR balance before wiping daiTkn.transferFrom(msg.sender, address(this), daiWipe); // get DAI to pay the debt MakerCDP loanMaster = MakerCDP(cdpAddr); - loanMaster.wipe(cdps[msg.sender], daiWipe); // wipe DAI + loanMaster.wipe(cdps[borrower], daiWipe); // wipe DAI uint mkrCharged = contractMKR - mkrTkn.balanceOf(address(this)); // MKR fee = before wiping bal - after wiping bal // claiming paid MKR back @@ -179,7 +179,9 @@ contract RepayLoan is IssueLoan { mkrTkn.transferFrom(msg.sender, address(this), mkrCharged); // user paying MKR fees } - emit WipedDAI(msg.sender, daiWipe, mkrCharged); + emit WipedDAI( + borrower, daiWipe, mkrCharged, msg.sender + ); } function unlockETH(uint ethFree) public { From a6b326be90842905a3847c2f22d22d2bc9632212 Mon Sep 17 00:00:00 2001 From: Sowmayjain Date: Wed, 28 Nov 2018 03:32:53 +0530 Subject: [PATCH 16/25] Returning bytes32 of CDP with getCDPID. --- contracts/protocols/InstaMaker.sol | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contracts/protocols/InstaMaker.sol b/contracts/protocols/InstaMaker.sol index e7513b2..779bd67 100644 --- a/contracts/protocols/InstaMaker.sol +++ b/contracts/protocols/InstaMaker.sol @@ -237,8 +237,8 @@ contract BorrowTasks is RepayLoan { return uint(ethrate).div(10**18); } - function getCDPID(address borrower) public view returns (uint) { - return uint(cdps[borrower]); + function getCDPID(address borrower) public view returns (uint, bytes32) { + return (uint(cdps[borrower]), cdps[borrower]); } function approveERC20() public { From ddbd6ec836e28eb56fc8847ef32d99602334dad9 Mon Sep 17 00:00:00 2001 From: Sowmayjain Date: Wed, 28 Nov 2018 03:36:53 +0530 Subject: [PATCH 17/25] Minor change. --- contracts/protocols/InstaMaker.sol | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contracts/protocols/InstaMaker.sol b/contracts/protocols/InstaMaker.sol index 779bd67..2db77d1 100644 --- a/contracts/protocols/InstaMaker.sol +++ b/contracts/protocols/InstaMaker.sol @@ -95,7 +95,7 @@ contract GlobalVar is Registry { bytes32 public blankCDP = 0x0000000000000000000000000000000000000000000000000000000000000000; address cdpAddr; // cups - mapping (address => bytes32) public cdps; // borrower >>> CDP Bytes + mapping (address => bytes32) cdps; // borrower >>> CDP Bytes bool public freezed; } @@ -237,7 +237,7 @@ contract BorrowTasks is RepayLoan { return uint(ethrate).div(10**18); } - function getCDPID(address borrower) public view returns (uint, bytes32) { + function getCDP(address borrower) public view returns (uint, bytes32) { return (uint(cdps[borrower]), cdps[borrower]); } From 8982df94824b536698a3e3d303ed971062a4de56 Mon Sep 17 00:00:00 2001 From: Sowmayjain Date: Wed, 28 Nov 2018 03:40:14 +0530 Subject: [PATCH 18/25] Made blackCDP bytes32 non-public. --- contracts/protocols/InstaMaker.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/protocols/InstaMaker.sol b/contracts/protocols/InstaMaker.sol index 2db77d1..d8f0d4d 100644 --- a/contracts/protocols/InstaMaker.sol +++ b/contracts/protocols/InstaMaker.sol @@ -93,7 +93,7 @@ contract GlobalVar is Registry { using SafeMath for uint; using SafeMath for uint256; - bytes32 public blankCDP = 0x0000000000000000000000000000000000000000000000000000000000000000; + bytes32 blankCDP = 0x0000000000000000000000000000000000000000000000000000000000000000; address cdpAddr; // cups mapping (address => bytes32) cdps; // borrower >>> CDP Bytes bool public freezed; From b1c3e807d5aa0347c3f5809f3aee54642c0656ee Mon Sep 17 00:00:00 2001 From: Sowmayjain Date: Wed, 28 Nov 2018 17:25:10 +0530 Subject: [PATCH 19/25] Minor change. --- contracts/protocols/InstaMaker.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/protocols/InstaMaker.sol b/contracts/protocols/InstaMaker.sol index d8f0d4d..c4b58b4 100644 --- a/contracts/protocols/InstaMaker.sol +++ b/contracts/protocols/InstaMaker.sol @@ -234,7 +234,7 @@ contract BorrowTasks is RepayLoan { PriceInterface ethRate = PriceInterface(getAddress("ethfeed")); bytes32 ethrate; (ethrate, ) = ethRate.peek(); - return uint(ethrate).div(10**18); + return uint(ethrate); } function getCDP(address borrower) public view returns (uint, bytes32) { From 99a1fc199e31647fdda9809e4a4d6f90ddccd929 Mon Sep 17 00:00:00 2001 From: Sowmayjain Date: Fri, 30 Nov 2018 12:20:50 +0530 Subject: [PATCH 20/25] Executed dapp based fee model. --- contracts/protocols/InstaKyber.sol | 32 +++++++++++++++++++++--------- contracts/protocols/InstaMaker.sol | 19 +++++++++++++----- 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/contracts/protocols/InstaKyber.sol b/contracts/protocols/InstaKyber.sol index beb1a9e..d3a8091 100644 --- a/contracts/protocols/InstaKyber.sol +++ b/contracts/protocols/InstaKyber.sol @@ -79,7 +79,9 @@ contract Trade is Registry { uint destAmt, address beneficiary, uint minConversionRate, - address affiliate + address referral, + uint cut, + address partner ); function getExpectedPrice( @@ -106,14 +108,16 @@ contract Trade is Registry { address src, // token to sell address dest, // token to buy uint srcAmt, // amount of token for sell + uint srcAmtToFetch, // amount of token for sell + fees // equal to srcAmt or greater uint minConversionRate, // minimum slippage rate - uint maxDestAmt // max amount of dest token + uint maxDestAmt, // max amount of dest token + address partner // affiliate partner ) public payable returns (uint destAmt) { address eth = getAddress("eth"); uint ethQty = getToken( - msg.sender, src, srcAmt, eth + msg.sender, src, srcAmt, srcAmtToFetch, eth ); // Interacting with Kyber Proxy Contract @@ -129,18 +133,27 @@ contract Trade is Registry { ); // maxDestAmt usecase implementated - if (src == eth && address(this).balance > 0) { - msg.sender.transfer(address(this).balance); + uint cut = srcAmtToFetch - srcAmt; + if (src == eth && (address(this).balance - cut) > 0) { + msg.sender.transfer(address(this).balance - cut); } else if (src != eth) { // as there is no balanceOf of eth IERC20 srcTkn = IERC20(src); uint srcBal = srcTkn.balanceOf(address(this)); - if (srcBal > 0) { - srcTkn.transfer(msg.sender, srcBal); + if ((srcBal - cut) > 0) { + srcTkn.transfer(msg.sender, srcBal - cut); } } emit KyberTrade( - src, srcAmt, dest, destAmt, msg.sender, minConversionRate, getAddress("admin") + src, + srcAmt, + dest, + destAmt, + msg.sender, + minConversionRate, + getAddress("admin"), + cut, + partner ); } @@ -149,6 +162,7 @@ contract Trade is Registry { address trader, address src, uint srcAmt, + uint srcAmtToFetch, address eth ) internal returns (uint ethQty) { @@ -157,7 +171,7 @@ contract Trade is Registry { ethQty = srcAmt; } else { IERC20 tokenFunctions = IERC20(src); - tokenFunctions.transferFrom(trader, address(this), srcAmt); + tokenFunctions.transferFrom(trader, address(this), srcAmtToFetch); ethQty = 0; } } diff --git a/contracts/protocols/InstaMaker.sol b/contracts/protocols/InstaMaker.sol index c4b58b4..ad0643a 100644 --- a/contracts/protocols/InstaMaker.sol +++ b/contracts/protocols/InstaMaker.sol @@ -57,8 +57,10 @@ interface InstaKyber { address src, address dest, uint srcAmt, + uint srcAmtToFetch, uint minConversionRate, - uint maxDestAmt + uint maxDestAmt, + address partner ) external payable returns (uint destAmt); function getExpectedPrice( @@ -207,12 +209,15 @@ contract RepayLoan is IssueLoan { uint minRate; (, minRate) = instak.getExpectedPrice(eth, mkr, ethQty); uint mkrBought = instak.executeTrade.value(ethQty)( - eth, mkr, ethQty, minRate, mkrCharged + eth, + mkr, + ethQty, + ethQty, + minRate, + mkrCharged, + address(this) ); require(mkrCharged == mkrBought, "ETH not sufficient to cover the MKR fees."); - if (address(this).balance > 0) { - msg.sender.transfer(address(this).balance); - } } } @@ -278,4 +283,8 @@ contract InstaMaker is BorrowTasks { emit MKRCollected(amount); } + function collectETH(uint amount) public onlyAdmin { + msg.sender.transfer(amount); + } + } \ No newline at end of file From 8682fc9092ef05ddc0d306d45a26c51b7d8afbe6 Mon Sep 17 00:00:00 2001 From: Sowmayjain Date: Fri, 30 Nov 2018 12:24:38 +0530 Subject: [PATCH 21/25] Asset collection functions. --- contracts/protocols/InstaKyber.sol | 14 ++++++++++++++ contracts/protocols/InstaMaker.sol | 2 ++ 2 files changed, 16 insertions(+) diff --git a/contracts/protocols/InstaKyber.sol b/contracts/protocols/InstaKyber.sol index d3a8091..53eadd0 100644 --- a/contracts/protocols/InstaKyber.sol +++ b/contracts/protocols/InstaKyber.sol @@ -181,10 +181,24 @@ contract Trade is Registry { contract InstaKyber is Trade { + event ERC20Collected(address addr, uint amount); + event ETHCollected(uint amount); + constructor(address rAddr) public { addressRegistry = rAddr; } function () public payable {} + function collectERC20(address tknAddr, uint amount) public onlyAdmin { + IERC20 tkn = IERC20(tknAddr); + tkn.transfer(msg.sender, amount); + emit ERC20Collected(tknAddr, amount); + } + + function collectETH(uint amount) public onlyAdmin { + msg.sender.transfer(amount); + emit ETHCollected(amount); + } + } \ No newline at end of file diff --git a/contracts/protocols/InstaMaker.sol b/contracts/protocols/InstaMaker.sol index ad0643a..8d73ef6 100644 --- a/contracts/protocols/InstaMaker.sol +++ b/contracts/protocols/InstaMaker.sol @@ -263,6 +263,7 @@ contract BorrowTasks is RepayLoan { contract InstaMaker is BorrowTasks { event MKRCollected(uint amount); + event ETHCollected(uint amount); constructor(address rAddr) public { addressRegistry = rAddr; @@ -285,6 +286,7 @@ contract InstaMaker is BorrowTasks { function collectETH(uint amount) public onlyAdmin { msg.sender.transfer(amount); + emit ETHCollected(amount); } } \ No newline at end of file From 9433a394c6d3bc34ca3e7ab200ed46502b8a6706 Mon Sep 17 00:00:00 2001 From: Sowmayjain Date: Fri, 30 Nov 2018 18:26:31 +0530 Subject: [PATCH 22/25] Added return values in borrow functions and affiliation mechanism. --- contracts/protocols/InstaMaker.sol | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/contracts/protocols/InstaMaker.sol b/contracts/protocols/InstaMaker.sol index 8d73ef6..987167d 100644 --- a/contracts/protocols/InstaMaker.sol +++ b/contracts/protocols/InstaMaker.sol @@ -106,7 +106,7 @@ contract GlobalVar is Registry { contract IssueLoan is GlobalVar { event LockedETH(address borrower, uint lockETH, uint lockPETH, address lockedBy); - event LoanedDAI(address borrower, uint loanDAI); + event LoanedDAI(address borrower, uint loanDAI, address partner); event NewCDP(address borrower, bytes32 cdpBytes); function pethPEReth(uint ethNum) public view returns (uint rPETH) { @@ -114,17 +114,18 @@ contract IssueLoan is GlobalVar { rPETH = (ethNum.mul(10 ** 27)).div(loanMaster.per()); } - function borrow(uint daiDraw) public payable { - if (msg.value > 0) {lockETH(msg.sender);} - if (daiDraw > 0) {drawDAI(daiDraw);} + function borrow(uint daiDraw, address partner) public payable returns (bytes32 cdpBytesID) { + if (msg.value > 0) {cdpBytesID = lockETH(msg.sender);} + if (daiDraw > 0) {drawDAI(daiDraw, partner);} } - function lockETH(address borrower) public payable { + function lockETH(address borrower) public payable returns (bytes32 cdpBytesID) { MakerCDP loanMaster = MakerCDP(cdpAddr); if (cdps[borrower] == blankCDP) { require(msg.sender == borrower, "Creating CDP for others is not permitted at the moment."); - cdps[msg.sender] = loanMaster.open(); - emit NewCDP(msg.sender, cdps[msg.sender]); + cdpBytesID = loanMaster.open(); + cdps[msg.sender] = cdpBytesID; + emit NewCDP(msg.sender, cdpBytesID); } WETHFace wethTkn = WETHFace(getAddress("weth")); wethTkn.deposit.value(msg.value)(); // ETH to WETH @@ -136,13 +137,13 @@ contract IssueLoan is GlobalVar { ); } - function drawDAI(uint daiDraw) public { + function drawDAI(uint daiDraw, address partner) public { require(!freezed, "Operation Disabled"); MakerCDP loanMaster = MakerCDP(cdpAddr); loanMaster.draw(cdps[msg.sender], daiDraw); IERC20 daiTkn = IERC20(getAddress("dai")); daiTkn.transfer(msg.sender, daiDraw); - emit LoanedDAI(msg.sender, daiDraw); + emit LoanedDAI(msg.sender, daiDraw, partner); } } @@ -153,12 +154,12 @@ contract RepayLoan is IssueLoan { event WipedDAI(address borrower, uint daiWipe, uint mkrCharged, address wipedBy); event UnlockedETH(address borrower, uint ethFree); - function repay(uint daiWipe, uint ethFree) public payable { - if (daiWipe > 0) {wipeDAI(daiWipe, msg.sender);} + function repay(uint daiWipe, uint ethFree) public payable returns (uint mkrCharged) { + if (daiWipe > 0) {mkrCharged = wipeDAI(daiWipe, msg.sender);} if (ethFree > 0) {unlockETH(ethFree);} } - function wipeDAI(uint daiWipe, address borrower) public payable { + function wipeDAI(uint daiWipe, address borrower) public payable returns (uint mkrCharged) { address dai = getAddress("dai"); address mkr = getAddress("mkr"); address eth = getAddress("eth"); @@ -170,7 +171,7 @@ contract RepayLoan is IssueLoan { daiTkn.transferFrom(msg.sender, address(this), daiWipe); // get DAI to pay the debt MakerCDP loanMaster = MakerCDP(cdpAddr); loanMaster.wipe(cdps[borrower], daiWipe); // wipe DAI - uint mkrCharged = contractMKR - mkrTkn.balanceOf(address(this)); // MKR fee = before wiping bal - after wiping bal + mkrCharged = contractMKR - mkrTkn.balanceOf(address(this)); // MKR fee = before wiping bal - after wiping bal // claiming paid MKR back if (msg.value > 0) { // Interacting with Kyber to swap ETH with MKR From 15fbd1768c7ab27da6041bd9f41797c641ca5597 Mon Sep 17 00:00:00 2001 From: Sowmayjain Date: Fri, 30 Nov 2018 22:58:47 +0530 Subject: [PATCH 23/25] Fixed minimum variable error. --- contracts/protocols/InstaKyber.sol | 64 ++++++++++++++++++++---------- contracts/protocols/InstaMaker.sol | 2 +- 2 files changed, 45 insertions(+), 21 deletions(-) diff --git a/contracts/protocols/InstaKyber.sol b/contracts/protocols/InstaKyber.sol index 53eadd0..b32f358 100644 --- a/contracts/protocols/InstaKyber.sol +++ b/contracts/protocols/InstaKyber.sol @@ -18,6 +18,12 @@ library SafeMath { return c; } + function sub(uint256 a, uint256 b) internal pure returns (uint256) { + require(b <= a, "Assertion Failed"); + uint256 c = a - b; + return c; + } + } interface IERC20 { @@ -84,6 +90,8 @@ contract Trade is Registry { address partner ); + address public eth = 0x00eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee; + function getExpectedPrice( address src, address dest, @@ -104,25 +112,44 @@ contract Trade is Registry { } } + struct TradeUints { + uint srcAmt; + uint ethQty; + uint srcAmtWithFees; + uint cut; + uint destAmt; + int ethBalAfterTrade; // it can be neagtive + } + function executeTrade( address src, // token to sell address dest, // token to buy uint srcAmt, // amount of token for sell - uint srcAmtToFetch, // amount of token for sell + fees // equal to srcAmt or greater + uint srcAmtWithFees, // amount of token for sell + fees // equal or greater than srcAmt uint minConversionRate, // minimum slippage rate uint maxDestAmt, // max amount of dest token address partner // affiliate partner ) public payable returns (uint destAmt) { - address eth = getAddress("eth"); - uint ethQty = getToken( - msg.sender, src, srcAmt, srcAmtToFetch, eth - ); - - // Interacting with Kyber Proxy Contract + require(srcAmtWithFees >= srcAmt, "srcAmtWithFees can't be small than scrAmt"); + if (src == eth) { + require(srcAmtWithFees == msg.value, "Not enough ETH to cover the trade."); + } + + TradeUints memory tradeSpecs; Kyber kyberFunctions = Kyber(getAddress("kyber")); - destAmt = kyberFunctions.trade.value(ethQty)( + + tradeSpecs.srcAmt = srcAmt; + tradeSpecs.srcAmtWithFees = srcAmtWithFees; + tradeSpecs.cut = srcAmtWithFees.sub(srcAmt); + tradeSpecs.ethQty = getToken( + msg.sender, + src, + srcAmt, + srcAmtWithFees + ); + tradeSpecs.destAmt = kyberFunctions.trade.value(tradeSpecs.ethQty)( src, srcAmt, dest, @@ -132,15 +159,14 @@ contract Trade is Registry { getAddress("admin") ); - // maxDestAmt usecase implementated - uint cut = srcAmtToFetch - srcAmt; - if (src == eth && (address(this).balance - cut) > 0) { - msg.sender.transfer(address(this).balance - cut); - } else if (src != eth) { // as there is no balanceOf of eth + // factoring maxDestAmt situation + if (src == eth && address(this).balance > tradeSpecs.cut) { + msg.sender.transfer(address(this).balance.sub(tradeSpecs.cut)); + } else if (src != eth) { IERC20 srcTkn = IERC20(src); uint srcBal = srcTkn.balanceOf(address(this)); - if ((srcBal - cut) > 0) { - srcTkn.transfer(msg.sender, srcBal - cut); + if (srcBal > tradeSpecs.cut) { + srcTkn.transfer(msg.sender, srcBal.sub(tradeSpecs.cut)); } } @@ -152,7 +178,7 @@ contract Trade is Registry { msg.sender, minConversionRate, getAddress("admin"), - cut, + tradeSpecs.cut, partner ); @@ -162,8 +188,7 @@ contract Trade is Registry { address trader, address src, uint srcAmt, - uint srcAmtToFetch, - address eth + uint srcAmtWithFees ) internal returns (uint ethQty) { if (src == eth) { @@ -171,8 +196,7 @@ contract Trade is Registry { ethQty = srcAmt; } else { IERC20 tokenFunctions = IERC20(src); - tokenFunctions.transferFrom(trader, address(this), srcAmtToFetch); - ethQty = 0; + tokenFunctions.transferFrom(trader, address(this), srcAmtWithFees); } } diff --git a/contracts/protocols/InstaMaker.sol b/contracts/protocols/InstaMaker.sol index 987167d..c41ab09 100644 --- a/contracts/protocols/InstaMaker.sol +++ b/contracts/protocols/InstaMaker.sol @@ -162,7 +162,7 @@ contract RepayLoan is IssueLoan { function wipeDAI(uint daiWipe, address borrower) public payable returns (uint mkrCharged) { address dai = getAddress("dai"); address mkr = getAddress("mkr"); - address eth = getAddress("eth"); + address eth = 0x00eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee; IERC20 daiTkn = IERC20(dai); IERC20 mkrTkn = IERC20(mkr); From b621b492d4aec0d7c6c30991792dcadad3e12ecc Mon Sep 17 00:00:00 2001 From: Sowmayjain Date: Sat, 1 Dec 2018 00:51:24 +0530 Subject: [PATCH 24/25] Removed trade with fees and optimised Kyber code. --- contracts/protocols/InstaKyber.sol | 90 +++++++----------------------- contracts/protocols/InstaMaker.sol | 10 +--- 2 files changed, 22 insertions(+), 78 deletions(-) diff --git a/contracts/protocols/InstaKyber.sol b/contracts/protocols/InstaKyber.sol index b32f358..beb1a9e 100644 --- a/contracts/protocols/InstaKyber.sol +++ b/contracts/protocols/InstaKyber.sol @@ -18,12 +18,6 @@ library SafeMath { return c; } - function sub(uint256 a, uint256 b) internal pure returns (uint256) { - require(b <= a, "Assertion Failed"); - uint256 c = a - b; - return c; - } - } interface IERC20 { @@ -85,13 +79,9 @@ contract Trade is Registry { uint destAmt, address beneficiary, uint minConversionRate, - address referral, - uint cut, - address partner + address affiliate ); - address public eth = 0x00eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee; - function getExpectedPrice( address src, address dest, @@ -112,44 +102,23 @@ contract Trade is Registry { } } - struct TradeUints { - uint srcAmt; - uint ethQty; - uint srcAmtWithFees; - uint cut; - uint destAmt; - int ethBalAfterTrade; // it can be neagtive - } - function executeTrade( address src, // token to sell address dest, // token to buy uint srcAmt, // amount of token for sell - uint srcAmtWithFees, // amount of token for sell + fees // equal or greater than srcAmt uint minConversionRate, // minimum slippage rate - uint maxDestAmt, // max amount of dest token - address partner // affiliate partner + uint maxDestAmt // max amount of dest token ) public payable returns (uint destAmt) { - require(srcAmtWithFees >= srcAmt, "srcAmtWithFees can't be small than scrAmt"); - if (src == eth) { - require(srcAmtWithFees == msg.value, "Not enough ETH to cover the trade."); - } - - TradeUints memory tradeSpecs; - Kyber kyberFunctions = Kyber(getAddress("kyber")); - - tradeSpecs.srcAmt = srcAmt; - tradeSpecs.srcAmtWithFees = srcAmtWithFees; - tradeSpecs.cut = srcAmtWithFees.sub(srcAmt); - tradeSpecs.ethQty = getToken( - msg.sender, - src, - srcAmt, - srcAmtWithFees + address eth = getAddress("eth"); + uint ethQty = getToken( + msg.sender, src, srcAmt, eth ); - tradeSpecs.destAmt = kyberFunctions.trade.value(tradeSpecs.ethQty)( + + // Interacting with Kyber Proxy Contract + Kyber kyberFunctions = Kyber(getAddress("kyber")); + destAmt = kyberFunctions.trade.value(ethQty)( src, srcAmt, dest, @@ -159,27 +128,19 @@ contract Trade is Registry { getAddress("admin") ); - // factoring maxDestAmt situation - if (src == eth && address(this).balance > tradeSpecs.cut) { - msg.sender.transfer(address(this).balance.sub(tradeSpecs.cut)); - } else if (src != eth) { + // maxDestAmt usecase implementated + if (src == eth && address(this).balance > 0) { + msg.sender.transfer(address(this).balance); + } else if (src != eth) { // as there is no balanceOf of eth IERC20 srcTkn = IERC20(src); uint srcBal = srcTkn.balanceOf(address(this)); - if (srcBal > tradeSpecs.cut) { - srcTkn.transfer(msg.sender, srcBal.sub(tradeSpecs.cut)); + if (srcBal > 0) { + srcTkn.transfer(msg.sender, srcBal); } } emit KyberTrade( - src, - srcAmt, - dest, - destAmt, - msg.sender, - minConversionRate, - getAddress("admin"), - tradeSpecs.cut, - partner + src, srcAmt, dest, destAmt, msg.sender, minConversionRate, getAddress("admin") ); } @@ -188,7 +149,7 @@ contract Trade is Registry { address trader, address src, uint srcAmt, - uint srcAmtWithFees + address eth ) internal returns (uint ethQty) { if (src == eth) { @@ -196,7 +157,8 @@ contract Trade is Registry { ethQty = srcAmt; } else { IERC20 tokenFunctions = IERC20(src); - tokenFunctions.transferFrom(trader, address(this), srcAmtWithFees); + tokenFunctions.transferFrom(trader, address(this), srcAmt); + ethQty = 0; } } @@ -205,24 +167,10 @@ contract Trade is Registry { contract InstaKyber is Trade { - event ERC20Collected(address addr, uint amount); - event ETHCollected(uint amount); - constructor(address rAddr) public { addressRegistry = rAddr; } function () public payable {} - function collectERC20(address tknAddr, uint amount) public onlyAdmin { - IERC20 tkn = IERC20(tknAddr); - tkn.transfer(msg.sender, amount); - emit ERC20Collected(tknAddr, amount); - } - - function collectETH(uint amount) public onlyAdmin { - msg.sender.transfer(amount); - emit ETHCollected(amount); - } - } \ No newline at end of file diff --git a/contracts/protocols/InstaMaker.sol b/contracts/protocols/InstaMaker.sol index c41ab09..076174c 100644 --- a/contracts/protocols/InstaMaker.sol +++ b/contracts/protocols/InstaMaker.sol @@ -57,10 +57,8 @@ interface InstaKyber { address src, address dest, uint srcAmt, - uint srcAmtToFetch, uint minConversionRate, - uint maxDestAmt, - address partner + uint maxDestAmt ) external payable returns (uint destAmt); function getExpectedPrice( @@ -162,7 +160,7 @@ contract RepayLoan is IssueLoan { function wipeDAI(uint daiWipe, address borrower) public payable returns (uint mkrCharged) { address dai = getAddress("dai"); address mkr = getAddress("mkr"); - address eth = 0x00eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee; + address eth = getAddress("eth"); IERC20 daiTkn = IERC20(dai); IERC20 mkrTkn = IERC20(mkr); @@ -213,10 +211,8 @@ contract RepayLoan is IssueLoan { eth, mkr, ethQty, - ethQty, minRate, - mkrCharged, - address(this) + mkrCharged ); require(mkrCharged == mkrBought, "ETH not sufficient to cover the MKR fees."); } From 7db7156ad7d93bde18997ba8a94980bd2ca96cdb Mon Sep 17 00:00:00 2001 From: Sowmayjain Date: Sat, 1 Dec 2018 01:00:02 +0530 Subject: [PATCH 25/25] Removed fees from Maker. --- contracts/protocols/InstaMaker.sol | 44 ++++++++++++------------------ 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/contracts/protocols/InstaMaker.sol b/contracts/protocols/InstaMaker.sol index 076174c..d34ed81 100644 --- a/contracts/protocols/InstaMaker.sol +++ b/contracts/protocols/InstaMaker.sol @@ -104,7 +104,7 @@ contract GlobalVar is Registry { contract IssueLoan is GlobalVar { event LockedETH(address borrower, uint lockETH, uint lockPETH, address lockedBy); - event LoanedDAI(address borrower, uint loanDAI, address partner); + event LoanedDAI(address borrower, uint loanDAI); event NewCDP(address borrower, bytes32 cdpBytes); function pethPEReth(uint ethNum) public view returns (uint rPETH) { @@ -112,18 +112,17 @@ contract IssueLoan is GlobalVar { rPETH = (ethNum.mul(10 ** 27)).div(loanMaster.per()); } - function borrow(uint daiDraw, address partner) public payable returns (bytes32 cdpBytesID) { - if (msg.value > 0) {cdpBytesID = lockETH(msg.sender);} - if (daiDraw > 0) {drawDAI(daiDraw, partner);} + function borrow(uint daiDraw) public payable { + if (msg.value > 0) {lockETH(msg.sender);} + if (daiDraw > 0) {drawDAI(daiDraw);} } - function lockETH(address borrower) public payable returns (bytes32 cdpBytesID) { + function lockETH(address borrower) public payable { MakerCDP loanMaster = MakerCDP(cdpAddr); if (cdps[borrower] == blankCDP) { require(msg.sender == borrower, "Creating CDP for others is not permitted at the moment."); - cdpBytesID = loanMaster.open(); - cdps[msg.sender] = cdpBytesID; - emit NewCDP(msg.sender, cdpBytesID); + cdps[msg.sender] = loanMaster.open(); + emit NewCDP(msg.sender, cdps[msg.sender]); } WETHFace wethTkn = WETHFace(getAddress("weth")); wethTkn.deposit.value(msg.value)(); // ETH to WETH @@ -135,13 +134,13 @@ contract IssueLoan is GlobalVar { ); } - function drawDAI(uint daiDraw, address partner) public { + function drawDAI(uint daiDraw) public { require(!freezed, "Operation Disabled"); MakerCDP loanMaster = MakerCDP(cdpAddr); loanMaster.draw(cdps[msg.sender], daiDraw); IERC20 daiTkn = IERC20(getAddress("dai")); daiTkn.transfer(msg.sender, daiDraw); - emit LoanedDAI(msg.sender, daiDraw, partner); + emit LoanedDAI(msg.sender, daiDraw); } } @@ -152,12 +151,12 @@ contract RepayLoan is IssueLoan { event WipedDAI(address borrower, uint daiWipe, uint mkrCharged, address wipedBy); event UnlockedETH(address borrower, uint ethFree); - function repay(uint daiWipe, uint ethFree) public payable returns (uint mkrCharged) { - if (daiWipe > 0) {mkrCharged = wipeDAI(daiWipe, msg.sender);} + function repay(uint daiWipe, uint ethFree) public payable { + if (daiWipe > 0) {wipeDAI(daiWipe, msg.sender);} if (ethFree > 0) {unlockETH(ethFree);} } - function wipeDAI(uint daiWipe, address borrower) public payable returns (uint mkrCharged) { + function wipeDAI(uint daiWipe, address borrower) public payable { address dai = getAddress("dai"); address mkr = getAddress("mkr"); address eth = getAddress("eth"); @@ -169,7 +168,7 @@ contract RepayLoan is IssueLoan { daiTkn.transferFrom(msg.sender, address(this), daiWipe); // get DAI to pay the debt MakerCDP loanMaster = MakerCDP(cdpAddr); loanMaster.wipe(cdps[borrower], daiWipe); // wipe DAI - mkrCharged = contractMKR - mkrTkn.balanceOf(address(this)); // MKR fee = before wiping bal - after wiping bal + uint mkrCharged = contractMKR - mkrTkn.balanceOf(address(this)); // MKR fee = before wiping bal - after wiping bal // claiming paid MKR back if (msg.value > 0) { // Interacting with Kyber to swap ETH with MKR @@ -208,13 +207,12 @@ contract RepayLoan is IssueLoan { uint minRate; (, minRate) = instak.getExpectedPrice(eth, mkr, ethQty); uint mkrBought = instak.executeTrade.value(ethQty)( - eth, - mkr, - ethQty, - minRate, - mkrCharged + eth, mkr, ethQty, minRate, mkrCharged ); require(mkrCharged == mkrBought, "ETH not sufficient to cover the MKR fees."); + if (address(this).balance > 0) { + msg.sender.transfer(address(this).balance); + } } } @@ -228,8 +226,8 @@ contract BorrowTasks is RepayLoan { require(nextOwner != 0, "Invalid Address."); MakerCDP loanMaster = MakerCDP(cdpAddr); loanMaster.give(cdps[msg.sender], nextOwner); - emit TranferCDP(cdps[msg.sender], msg.sender, nextOwner); cdps[msg.sender] = blankCDP; + emit TranferCDP(cdps[msg.sender], msg.sender, nextOwner); } function getETHRate() public view returns (uint) { @@ -260,7 +258,6 @@ contract BorrowTasks is RepayLoan { contract InstaMaker is BorrowTasks { event MKRCollected(uint amount); - event ETHCollected(uint amount); constructor(address rAddr) public { addressRegistry = rAddr; @@ -281,9 +278,4 @@ contract InstaMaker is BorrowTasks { emit MKRCollected(amount); } - function collectETH(uint amount) public onlyAdmin { - msg.sender.transfer(amount); - emit ETHCollected(amount); - } - } \ No newline at end of file