From fc6111de6247e7432695c10dad62e0255cc142c0 Mon Sep 17 00:00:00 2001 From: Samyak Jain Date: Fri, 5 Apr 2019 16:58:08 +0530 Subject: [PATCH] Uniswap pooling --- contracts/Bin/UniswapPool.sol | 29 ---- contracts/ProxyLogics/InstaUniswapPool.sol | 148 +++++++++++++++++++++ 2 files changed, 148 insertions(+), 29 deletions(-) delete mode 100644 contracts/Bin/UniswapPool.sol create mode 100644 contracts/ProxyLogics/InstaUniswapPool.sol diff --git a/contracts/Bin/UniswapPool.sol b/contracts/Bin/UniswapPool.sol deleted file mode 100644 index ece65bd..0000000 --- a/contracts/Bin/UniswapPool.sol +++ /dev/null @@ -1,29 +0,0 @@ -pragma solidity 0.5.0; - - -interface UniswapFactoryInterface { - // 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 - -interface UniswapPool { - // Address of ERC20 token sold on this exchange - function tokenAddress() external view returns (address token); - // Address of Uniswap Factory - function factoryAddress() external view returns (address factory); - // Provide Liquidity - function addLiquidity(uint256 min_liquidity, uint256 max_tokens, uint256 deadline) external payable returns (uint256); - function removeLiquidity(uint256 amount, uint256 min_eth, uint256 min_tokens, uint256 deadline) external returns (uint256, uint256); - // ERC20 comaptibility for liquidity tokens - function name() external returns (bytes32); - function symbol() external returns (bytes32); - function decimals() external returns (uint256); - function transfer(address _to, uint256 _value) external returns (bool); - function transferFrom(address _from, address _to, uint256 value) external returns (bool); - function approve(address _spender, uint256 _value) external returns (bool); - function allowance(address _owner, address _spender) external view returns (uint256); - function balanceOf(address _owner) external view returns (uint256); -} diff --git a/contracts/ProxyLogics/InstaUniswapPool.sol b/contracts/ProxyLogics/InstaUniswapPool.sol new file mode 100644 index 0000000..b6d0194 --- /dev/null +++ b/contracts/ProxyLogics/InstaUniswapPool.sol @@ -0,0 +1,148 @@ +pragma solidity ^0.5.0; + + +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; + } +} + + +contract IERC20 { + function balanceOf(address who) public view returns (uint256); + function allowance(address _owner, address _spender) public view returns (uint256); + function transfer(address to, uint256 value) public returns (bool); + function approve(address spender, uint256 value) public returns (bool); + function transferFrom(address from, address to, uint256 value) public returns (bool); +} + + +contract 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 { + // Address of ERC20 token sold on this exchange + function tokenAddress() external view returns (address token); + // Address of Uniswap Factory + function factoryAddress() external view returns (address factory); + // Provide Liquidity + function addLiquidity(uint256 minLiquidity, uint256 maxTokens, uint256 deadline) external payable returns (uint256); + // Remove Liquidity + function removeLiquidity( + uint256 amount, + uint256 minEth, + uint256 minTokens, + uint256 deadline + ) external returns (uint256, uint256); + // ERC20 comaptibility for liquidity tokens + function name() external returns (bytes32); + function symbol() external returns (bytes32); + function decimals() external returns (uint256); + function transfer(address _to, uint256 _value) external returns (bool); + function transferFrom(address _from, address _to, uint256 value) external returns (bool); + function approve(address _spender, uint256 _value) external returns (bool); + function allowance(address _owner, address _spender) external view returns (uint256); + function balanceOf(address _owner) external view returns (uint256); +} + + +contract Helper { + + using SafeMath for uint; + using SafeMath for uint256; + + /** + * @dev get ethereum address for trade + */ + function getAddressETH() public pure returns (address eth) { + eth = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; + } + + /** + * @dev get Uniswap Proxy address + */ + function getAddressUniFactory() public pure returns (address factory) { + factory = 0xc0a47dFe034B400B47bDaD5FecDa2621de6c4d95; + } + + // Get Uniswap's Exchange address from Factory Contract + function getExchangeAddress(address _token) public view returns (address) { + UniswapFactory uniswapMain = UniswapFactory(getAddressUniFactory()); + return uniswapMain.getExchange(_token); + } + + /** + * @dev get admin address + */ + function getAddressAdmin() public pure returns (address admin) { + 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 + * @return ethBal - if not erc20, eth balance + * @return tknBal - if not eth, erc20 balance + */ + function getBal(address src, address _owner) public view returns (uint, uint) { + uint tknBal; + if (src != getAddressETH()) { + tknBal = IERC20(src).balanceOf(address(_owner)); + } + return (address(_owner).balance, tknBal); + } + + /** + * @dev fetching token from the trader if ERC20 + * @param trader is the trader + * @param src is the token which is being sold + * @param srcAmt is the amount of token being sold + */ + function getToken(address trader, address src, uint srcAmt) internal returns (uint ethQty) { + if (src == getAddressETH()) { + require(msg.value == srcAmt, "not-enough-src"); + ethQty = srcAmt; + } else { + manageApproval(src, srcAmt); + IERC20(src).transferFrom(trader, address(this), srcAmt); + } + } + + /** + * @dev setting allowance to kyber for the "user proxy" if required + * @param token is the token address + */ + function setApproval(address token) internal returns (uint) { + IERC20(token).approve(getAddressUniFactory(), 2**255); + } + + /** + * @dev configuring token approval with user proxy + * @param token is the token + */ + function manageApproval(address token, uint srcAmt) internal returns (uint) { + uint tokenAllowance = IERC20(token).allowance(address(this), getAddressUniFactory()); + if (srcAmt > tokenAllowance) { + setApproval(token); + } + } + +} +