From 99786654ef1ce672ce45f346fe54702a04fad6f7 Mon Sep 17 00:00:00 2001 From: Sowmayjain Date: Sat, 6 Apr 2019 18:53:33 +0530 Subject: [PATCH] Proxy Logics => refactoring & removed unused code. --- contracts/ProxyLogics/InstaKyber.sol | 11 -- contracts/ProxyLogics/InstaMaker.sol | 2 - contracts/ProxyLogics/InstaUniswapPool.sol | 27 +---- contracts/ProxyLogics/WipeProxy.sol | 127 --------------------- 4 files changed, 6 insertions(+), 161 deletions(-) delete mode 100644 contracts/ProxyLogics/WipeProxy.sol diff --git a/contracts/ProxyLogics/InstaKyber.sol b/contracts/ProxyLogics/InstaKyber.sol index 1a712e9..276ff29 100644 --- a/contracts/ProxyLogics/InstaKyber.sol +++ b/contracts/ProxyLogics/InstaKyber.sol @@ -1,6 +1,5 @@ pragma solidity ^0.5.0; -import "openzeppelin-solidity/contracts/math/SafeMath.sol"; import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol"; @@ -25,9 +24,6 @@ contract KyberInterface { contract Helper { - using SafeMath for uint; - using SafeMath for uint256; - /** * @dev get ethereum address for trade */ @@ -49,13 +45,6 @@ contract Helper { admin = 0x7284a8451d9a0e7Dc62B3a71C0593eA2eC5c5638; } - /** - * @dev get fees to trade // 200 => 0.2% - */ - function getUintFees() public pure returns (uint fees) { - fees = 200; - } - /** * @dev gets ETH & token balance * @param src is the token being sold diff --git a/contracts/ProxyLogics/InstaMaker.sol b/contracts/ProxyLogics/InstaMaker.sol index 062040c..a7822b4 100644 --- a/contracts/ProxyLogics/InstaMaker.sol +++ b/contracts/ProxyLogics/InstaMaker.sol @@ -1,7 +1,5 @@ pragma solidity ^0.5.0; -import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol"; - interface TubInterface { function open() external returns (bytes32); function join(uint) external; diff --git a/contracts/ProxyLogics/InstaUniswapPool.sol b/contracts/ProxyLogics/InstaUniswapPool.sol index 41e94d6..c575a80 100644 --- a/contracts/ProxyLogics/InstaUniswapPool.sol +++ b/contracts/ProxyLogics/InstaUniswapPool.sol @@ -1,18 +1,14 @@ pragma solidity ^0.5.0; -import "openzeppelin-solidity/contracts/math/SafeMath.sol"; import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol"; - -contract UniswapFactory { +interface UniswapFactory { // Get Exchange and Token Info function getExchange(address token) external view returns (address exchange); function getToken(address exchange) external view returns (address token); } - -// Solidity Interface -contract UniswapPool { +interface UniswapPool { // Address of ERC20 token sold on this exchange function tokenAddress() external view returns (address token); // Address of Uniswap Factory @@ -37,9 +33,6 @@ contract UniswapPool { contract Helper { - using SafeMath for uint; - using SafeMath for uint256; - /** * @dev get Uniswap Proxy address */ @@ -81,18 +74,10 @@ contract Helper { * @dev setting allowance to kyber for the "user proxy" if required * @param token is the token address */ - function setApproval(address token, address to) internal { - IERC20(token).approve(to, 2**255); - } - - /** - * @dev configuring token approval with user proxy - * @param token is the token - */ - function manageApproval(address token, uint srcAmt, address to) internal returns (uint) { + function setApproval(address token, uint srcAmt, address to) internal { uint tokenAllowance = IERC20(token).allowance(address(this), to); if (srcAmt > tokenAllowance) { - setApproval(token, to); + IERC20(token).approve(to, 2**255); } } @@ -144,7 +129,7 @@ contract InstaUniswapPool is Helper { uint tokenToDeposit = msg.value * tokenReserve / ethReserve + 1; require(tokenToDeposit < maxDepositedTokens, "Token to deposit is greater than Max token to Deposit"); IERC20(token).transferFrom(msg.sender, address(this), tokenToDeposit); - manageApproval(token, tokenToDeposit, poolAddr); + setApproval(token, tokenToDeposit, poolAddr); tokensMinted = UniswapPool(poolAddr).addLiquidity.value(msg.value)( uint(0), tokenToDeposit, @@ -168,7 +153,7 @@ contract InstaUniswapPool is Helper { ) public returns (uint ethReturned, uint tokenReturned) { address poolAddr = getAddressPool(token); - manageApproval(poolAddr, amount, poolAddr); + setApproval(poolAddr, amount, poolAddr); (ethReturned, tokenReturned) = UniswapPool(poolAddr).removeLiquidity( amount, minEth, diff --git a/contracts/ProxyLogics/WipeProxy.sol b/contracts/ProxyLogics/WipeProxy.sol deleted file mode 100644 index 1f94814..0000000 --- a/contracts/ProxyLogics/WipeProxy.sol +++ /dev/null @@ -1,127 +0,0 @@ -pragma solidity ^0.5.0; - - -interface TubInterface { - function wipe(bytes32, uint) external; - function gov() external view returns (TokenInterface); - function sai() external view returns (TokenInterface); - function tab(bytes32) external returns (uint); - function rap(bytes32) external returns (uint); - function pep() external view returns (PepInterface); -} - -interface TokenInterface { - function allowance(address, address) external view returns (uint); - function balanceOf(address) external view returns (uint); - function approve(address, uint) external; - function transfer(address, uint) external returns (bool); - function transferFrom(address, address, uint) external returns (bool); -} - -interface PepInterface { - function peek() external returns (bytes32, bool); -} - -interface UniswapExchange { - function getEthToTokenOutputPrice(uint256 tokensBought) external view returns (uint256 ethSold); - function getTokenToEthOutputPrice(uint256 ethBought) external view returns (uint256 tokensSold); - function tokenToTokenSwapOutput( - uint256 tokensBought, - uint256 maxTokensSold, - uint256 maxEthSold, - uint256 deadline, - address tokenAddr - ) external returns (uint256 tokensSold); -} - - -contract DSMath { - - function add(uint x, uint y) internal pure returns (uint z) { - require((z = x + y) >= x, "math-not-safe"); - } - - function mul(uint x, uint y) internal pure returns (uint z) { - require(y == 0 || (z = x * y) / y == x, "math-not-safe"); - } - - uint constant WAD = 10 ** 18; - uint constant RAY = 10 ** 27; - - function rmul(uint x, uint y) internal pure returns (uint z) { - z = add(mul(x, y), RAY / 2) / RAY; - } - - function rdiv(uint x, uint y) internal pure returns (uint z) { - z = add(mul(x, RAY), y / 2) / y; - } - - function wdiv(uint x, uint y) internal pure returns (uint z) { - z = add(mul(x, WAD), y / 2) / y; - } - -} - - -contract WipeProxy is DSMath { - - function getSaiTubAddress() public pure returns (address sai) { - sai = 0x448a5065aeBB8E423F0896E6c5D525C040f59af3; - } - - function getUniswapMKRExchange() public pure returns (address ume) { - ume = 0x2C4Bd064b998838076fa341A83d007FC2FA50957; - } - - function getUniswapDAIExchange() public pure returns (address ude) { - ude = 0x09cabEC1eAd1c0Ba254B09efb3EE13841712bE14; - } - - function wipe( - uint cdpNum, - uint _wad - ) public - { - require(_wad > 0, "no-wipe-no-dai"); - - TubInterface tub = TubInterface(getSaiTubAddress()); - UniswapExchange daiEx = UniswapExchange(getUniswapDAIExchange()); - UniswapExchange mkrEx = UniswapExchange(getUniswapMKRExchange()); - TokenInterface dai = tub.sai(); - TokenInterface mkr = tub.gov(); - - bytes32 cup = bytes32(cdpNum); - - setAllowance(dai, getSaiTubAddress()); - setAllowance(mkr, getSaiTubAddress()); - setAllowance(dai, getUniswapDAIExchange()); - - (bytes32 val, bool ok) = tub.pep().peek(); - - // MKR required for wipe = Stability fees accrued in Dai / MKRUSD value - uint mkrFee = wdiv(rmul(_wad, rdiv(tub.rap(cup), tub.tab(cup))), uint(val)); - - uint daiAmt = daiEx.getTokenToEthOutputPrice(mkrEx.getEthToTokenOutputPrice(mkrFee)); - daiAmt = add(_wad, daiAmt); - require(dai.transferFrom(msg.sender, address(this), daiAmt), "not-approved-yet"); - - if (ok && val != 0) { - daiEx.tokenToTokenSwapOutput( - mkrFee, - daiAmt, - uint(999000000000000000000), - uint(1899063809), // 6th March 2030 GMT // no logic - address(mkr) - ); - } - - tub.wipe(cup, _wad); - } - - function setAllowance(TokenInterface token_, address spender_) private { - if (token_.allowance(address(this), spender_) != uint(-1)) { - token_.approve(spender_, uint(-1)); - } - } - -} \ No newline at end of file