From 31c1eb693c5f4d085d21e362a9282f16668809b6 Mon Sep 17 00:00:00 2001 From: haydenadams Date: Fri, 20 Sep 2019 16:17:17 -0400 Subject: [PATCH] simplify contract --- contracts/UniswapETH.sol | 273 +++++---------------------------------- 1 file changed, 30 insertions(+), 243 deletions(-) diff --git a/contracts/UniswapETH.sol b/contracts/UniswapETH.sol index e111911..f1232bd 100644 --- a/contracts/UniswapETH.sol +++ b/contracts/UniswapETH.sol @@ -7,10 +7,10 @@ import './interfaces/IUniswapETH.sol'; contract UniswapETH is ERC20 { - event TokenPurchase(address indexed buyer, uint256 indexed ethSold, uint256 indexed tokensBought); - event EthPurchase(address indexed buyer, uint256 indexed tokensSold, uint256 indexed ethBought); - event AddLiquidity(address indexed provider, uint256 indexed ethAmount, uint256 indexed tokenAmount); - event RemoveLiquidity(address indexed provider, uint256 indexed ethAmount, uint256 indexed tokenAmount); + event TokenPurchase(address indexed buyer, uint256 ethSold, uint256 tokensBought); + event EthPurchase(address indexed buyer, uint256 tokensSold, uint256 ethBought); + event AddLiquidity(address indexed provider, uint256 ethAmount, uint256 tokenAmount); + event RemoveLiquidity(address indexed provider, uint256 ethAmount, uint256 tokenAmount); string public name; // Uniswap V2 string public symbol; // UNI-V2 @@ -20,6 +20,7 @@ contract UniswapETH is ERC20 { bool private rentrancyLock = false; + modifier nonReentrant() { require(!rentrancyLock); rentrancyLock = true; @@ -38,9 +39,7 @@ contract UniswapETH is ERC20 { } - function () external payable { - ethToTokenInput(msg.value, 1, block.timestamp, msg.sender, msg.sender); - } + function () external payable {} function getInputPrice(uint256 inputAmount, uint256 inputReserve, uint256 outputReserve) public pure returns (uint256) { @@ -52,233 +51,27 @@ contract UniswapETH is ERC20 { } - function getOutputPrice(uint256 outputAmount, uint256 inputReserve, uint256 outputReserve) public pure returns (uint256) { - require(inputReserve > 0 && outputReserve > 0); - uint256 numerator = inputReserve.mul(outputAmount).mul(1000); - uint256 denominator = (outputReserve.sub(outputAmount)).mul(997); - return (numerator / denominator).add(1); - } - - function ethToTokenInput(uint256 ethSold, uint256 minTokens, uint256 deadline, address buyer, address recipient) private nonReentrant returns (uint256) { - require(deadline >= block.timestamp && ethSold > 0 && minTokens > 0); + function ethToToken() public payable nonReentrant returns (uint256) { + require(msg.value > 0); uint256 tokenReserve = token.balanceOf(address(this)); - uint256 tokensBought = getInputPrice(ethSold, address(this).balance.sub(ethSold), tokenReserve); - require(tokensBought >= minTokens); - require(token.transfer(recipient, tokensBought)); - emit TokenPurchase(buyer, ethSold, tokensBought); + uint256 tokensBought = getInputPrice(msg.value, address(this).balance.sub(msg.value), tokenReserve); + require(token.transfer(msg.sender, tokensBought)); + emit TokenPurchase(msg.sender, msg.value, tokensBought); return tokensBought; } - function ethToTokenSwapInput(uint256 minTokens, uint256 deadline) public payable returns (uint256) { - return ethToTokenInput(msg.value, minTokens, deadline, msg.sender, msg.sender); - } - - - function ethToTokenTransferInput(uint256 minTokens, uint256 deadline, address recipient) public payable returns(uint256) { - require(recipient != address(this) && recipient != address(0)); - return ethToTokenInput(msg.value, minTokens, deadline, msg.sender, recipient); - } - - function ethToTokenOutput(uint256 tokensBought, uint256 maxEth, uint256 deadline, address payable buyer, address recipient) private nonReentrant returns (uint256) { - require(deadline >= block.timestamp && tokensBought > 0 && maxEth > 0); - uint256 tokenReserve = token.balanceOf(address(this)); - uint256 ethSold = getOutputPrice(tokensBought, address(this).balance.sub(maxEth), tokenReserve); - // Throws if ethSold > maxEth - uint256 ethRefund = maxEth.sub(ethSold); - if (ethRefund > 0) { - buyer.transfer(ethRefund); - } - require(token.transfer(recipient, tokensBought)); - emit TokenPurchase(buyer, ethSold, tokensBought); - return ethSold; - } - - - function ethToTokenSwapOutput(uint256 tokensBought, uint256 deadline) public payable returns(uint256) { - return ethToTokenOutput(tokensBought, msg.value, deadline, msg.sender, msg.sender); - } - - - function ethToTokenTransferOutput(uint256 tokensBought, uint256 deadline, address recipient) public payable returns (uint256) { - require(recipient != address(this) && recipient != address(0)); - return ethToTokenOutput(tokensBought, msg.value, deadline, msg.sender, recipient); - } - - function tokenToEthInput(uint256 tokensSold, uint256 minEth, uint256 deadline, address buyer, address payable recipient) private nonReentrant returns (uint256) { - require(deadline >= block.timestamp && tokensSold > 0 && minEth > 0); - uint256 tokenReserve = token.balanceOf(address(this)); - uint256 ethBought = getInputPrice(tokensSold, tokenReserve, address(this).balance); - require(ethBought >= minEth); - recipient.transfer(ethBought); - require(token.transferFrom(buyer, address(this), tokensSold)); - emit EthPurchase(buyer, tokensSold, ethBought); - return ethBought; - } - - - function tokenToEthSwapInput(uint256 tokensSold, uint256 minEth, uint256 deadline) public returns (uint256) { - return tokenToEthInput(tokensSold, minEth, deadline, msg.sender, msg.sender); - } - - - function tokenToEthTransferInput(uint256 tokensSold, uint256 minEth, uint256 deadline, address payable recipient) public returns (uint256) { - require(recipient != address(this) && recipient != address(0)); - return tokenToEthInput(tokensSold, minEth, deadline, msg.sender, recipient); - } - - function tokenToEthOutput(uint256 ethBought, uint256 maxTokens, uint256 deadline, address buyer, address payable recipient) private nonReentrant returns (uint256) { - require(deadline >= block.timestamp && ethBought > 0); - uint256 tokenReserve = token.balanceOf(address(this)); - uint256 tokensSold = getOutputPrice(ethBought, tokenReserve, address(this).balance); - // tokens sold is always > 0 - require(maxTokens >= tokensSold); - recipient.transfer(ethBought); - require(token.transferFrom(buyer, address(this), tokensSold)); - emit EthPurchase(buyer, tokensSold, ethBought); - return tokensSold; - } - - - function tokenToEthSwapOutput(uint256 ethBought, uint256 maxTokens, uint256 deadline) public returns (uint256) { - return tokenToEthOutput(ethBought, maxTokens, deadline, msg.sender, msg.sender); - } - - - function tokenToEthTransferOutput(uint256 ethBought, uint256 maxTokens, uint256 deadline, address payable recipient) public returns (uint256) { - require(recipient != address(this) && recipient != address(0)); - return tokenToEthOutput(ethBought, maxTokens, deadline, msg.sender, recipient); - } - - function tokenToTokenInput( - uint256 tokensSold, - uint256 minTokensBought, - uint256 minEthBought, - uint256 deadline, - address buyer, - address recipient, - address payable exchangeAddr) - private nonReentrant returns (uint256) - { - require(deadline >= block.timestamp && tokensSold > 0 && minTokensBought > 0 && minEthBought > 0); - require(exchangeAddr != address(this) && exchangeAddr != address(0)); - uint256 tokenReserve = token.balanceOf(address(this)); - uint256 ethBought = getInputPrice(tokensSold, tokenReserve, address(this).balance); - require(ethBought >= minEthBought); - require(token.transferFrom(buyer, address(this), tokensSold)); - uint256 tokensBought = IUniswapExchange(exchangeAddr).ethToTokenTransferInput.value(ethBought)(minTokensBought, deadline, recipient); - emit EthPurchase(buyer, tokensSold, ethBought); - return tokensBought; - } - - - function tokenToTokenSwapInput( - uint256 tokensSold, - uint256 minTokensBought, - uint256 minEthBought, - uint256 deadline, - address tokenAddr) - public returns (uint256) - { - address payable exchangeAddr = factory.getExchange(tokenAddr); - return tokenToTokenInput(tokensSold, minTokensBought, minEthBought, deadline, msg.sender, msg.sender, exchangeAddr); - } - - - function tokenToTokenTransferInput( - uint256 tokensSold, - uint256 minTokensBought, - uint256 minEthBought, - uint256 deadline, - address recipient, - address tokenAddr) - public returns (uint256) - { - address payable exchangeAddr = factory.getExchange(tokenAddr); - return tokenToTokenInput(tokensSold, minTokensBought, minEthBought, deadline, msg.sender, recipient, exchangeAddr); - } - - function tokenToTokenOutput( - uint256 tokensBought, - uint256 maxTokensSold, - uint256 maxEthSold, - uint256 deadline, - address buyer, - address recipient, - address payable exchangeAddr) - private nonReentrant returns (uint256) - { - require(deadline >= block.timestamp && (tokensBought > 0 && maxEthSold > 0)); - require(exchangeAddr != address(this) && exchangeAddr != address(0)); - uint256 ethBought = IUniswapExchange(exchangeAddr).getEthToTokenOutputPrice(tokensBought); - uint256 tokenReserve = token.balanceOf(address(this)); - uint256 tokensSold = getOutputPrice(ethBought, tokenReserve, address(this).balance); - // tokens sold is always > 0 - require(maxTokensSold >= tokensSold && maxEthSold >= ethBought); - require(token.transferFrom(buyer, address(this), tokensSold)); - IUniswapExchange(exchangeAddr).ethToTokenTransferOutput.value(ethBought)(tokensBought, deadline, recipient); - emit EthPurchase(buyer, tokensSold, ethBought); - return tokensSold; - } - - - function tokenToTokenSwapOutput( - uint256 tokensBought, - uint256 maxTokensSold, - uint256 maxEthSold, - uint256 deadline, - address tokenAddr) - public returns (uint256) - { - address payable exchangeAddr = factory.getExchange(tokenAddr); - return tokenToTokenOutput(tokensBought, maxTokensSold, maxEthSold, deadline, msg.sender, msg.sender, exchangeAddr); - } - - - function tokenToTokenTransferOutput( - uint256 tokensBought, - uint256 maxTokensSold, - uint256 maxEthSold, - uint256 deadline, - address recipient, - address tokenAddr) - public returns (uint256) - { - address payable exchangeAddr = factory.getExchange(tokenAddr); - return tokenToTokenOutput(tokensBought, maxTokensSold, maxEthSold, deadline, msg.sender, recipient, exchangeAddr); - } - - - function getEthToTokenInputPrice(uint256 ethSold) public view returns (uint256) { - require(ethSold > 0); - uint256 tokenReserve = token.balanceOf(address(this)); - return getInputPrice(ethSold, address(this).balance, tokenReserve); - } - - - function getEthToTokenOutputPrice(uint256 tokensBought) public view returns (uint256) { - require(tokensBought > 0); - uint256 tokenReserve = token.balanceOf(address(this)); - uint256 ethSold = getOutputPrice(tokensBought, address(this).balance, tokenReserve); - return ethSold; - } - - - function getTokenToEthInputPrice(uint256 tokensSold) public view returns (uint256) { + function tokenToEth(uint256 tokensSold) public nonReentrant returns (uint256) { require(tokensSold > 0); uint256 tokenReserve = token.balanceOf(address(this)); uint256 ethBought = getInputPrice(tokensSold, tokenReserve, address(this).balance); + msg.sender.transfer(ethBought); + require(token.transferFrom(msg.sender, address(this), tokensSold)); + emit EthPurchase(msg.sender, tokensSold, ethBought); return ethBought; } - function getTokenToEthOutputPrice(uint256 ethBought) public view returns (uint256) { - require(ethBought > 0); - uint256 tokenReserve = token.balanceOf(address(this)); - return getOutputPrice(ethBought, tokenReserve, address(this).balance); - } - - function tokenAddress() public view returns (address) { return address(token); } @@ -289,49 +82,43 @@ contract UniswapETH is ERC20 { } - function addLiquidity(uint256 minLiquidity, uint256 maxTokens, uint256 deadline) public payable nonReentrant returns (uint256) { - require(deadline >= block.timestamp && maxTokens > 0 && msg.value > 0, 'INVALID_INPUT'); - uint256 totalLiquidity = totalSupply; + function addLiquidity(uint256 initialTokens) public payable nonReentrant returns (uint256) { + uint256 _totalSupply = totalSupply; - if (totalLiquidity > 0) { - require(minLiquidity > 0); + if (_totalSupply > 0) { + require(msg.value > 0, 'INVALID_INPUT'); uint256 ethReserve = address(this).balance.sub(msg.value); uint256 tokenReserve = token.balanceOf(address(this)); uint256 tokenAmount = (msg.value.mul(tokenReserve) / ethReserve).add(1); - uint256 liquidityMinted = msg.value.mul(totalLiquidity) / ethReserve; - require(maxTokens >= tokenAmount && liquidityMinted >= minLiquidity); + uint256 liquidityMinted = msg.value.mul(_totalSupply) / ethReserve; balanceOf[msg.sender] = balanceOf[msg.sender].add(liquidityMinted); - totalSupply = totalLiquidity.add(liquidityMinted); + totalSupply = _totalSupply.add(liquidityMinted); require(token.transferFrom(msg.sender, address(this), tokenAmount)); emit AddLiquidity(msg.sender, msg.value, tokenAmount); emit Transfer(address(0), msg.sender, liquidityMinted); return liquidityMinted; } else { - require(msg.value >= 1000000000, 'INVALID_VALUE'); - require(factory.getExchange(address(token)) == address(this)); - uint256 tokenAmount = maxTokens; + require(initialTokens > 0 && msg.value >= 1000000000, 'INVALID_VALUE'); uint256 initialLiquidity = address(this).balance; totalSupply = initialLiquidity; balanceOf[msg.sender] = initialLiquidity; - require(token.transferFrom(msg.sender, address(this), tokenAmount)); - emit AddLiquidity(msg.sender, msg.value, tokenAmount); + require(token.transferFrom(msg.sender, address(this), initialTokens)); + emit AddLiquidity(msg.sender, msg.value, initialTokens); emit Transfer(address(0), msg.sender, initialLiquidity); return initialLiquidity; } } - function removeLiquidity(uint256 amount, uint256 minEth, uint256 minTokens, uint256 deadline) public nonReentrant returns (uint256, uint256) { - require(amount > 0 && deadline >= block.timestamp && minEth > 0 && minTokens > 0); - uint256 totalLiquidity = totalSupply; - require(totalLiquidity > 0); + function removeLiquidity(uint256 amount) public nonReentrant returns (uint256, uint256) { + uint256 _totalSupply = totalSupply; + require(amount > 0 && _totalSupply > 0); uint256 tokenReserve = token.balanceOf(address(this)); - uint256 ethAmount = amount.mul(address(this).balance) / totalLiquidity; - uint256 tokenAmount = amount.mul(tokenReserve) / totalLiquidity; - require(ethAmount >= minEth && tokenAmount >= minTokens); + uint256 ethAmount = amount.mul(address(this).balance) / _totalSupply; + uint256 tokenAmount = amount.mul(tokenReserve) / _totalSupply; balanceOf[msg.sender] = balanceOf[msg.sender].sub(amount); - totalSupply = totalLiquidity.sub(amount); + totalSupply = _totalSupply.sub(amount); msg.sender.transfer(ethAmount); require(token.transfer(msg.sender, tokenAmount)); emit RemoveLiquidity(msg.sender, ethAmount, tokenAmount);