From cdb14394b4459bd5ebbe865dfce4bc7a8538f037 Mon Sep 17 00:00:00 2001 From: Sowmayjain Date: Mon, 26 Nov 2018 03:09:47 +0530 Subject: [PATCH] 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; }