From 296f7d929c54bad0d2f5e16808ca36a0679b82f1 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Mon, 2 May 2022 01:57:10 +0530 Subject: [PATCH] updated functions to take non-struct params --- .../connectors/uniswap/v3_swap/helpers.sol | 124 ++++++++++++ .../connectors/uniswap/v3_swap/main.sol | 179 ++++++------------ .../connectors/uniswap/v3_swap/helpers.sol | 124 ++++++++++++ .../connectors/uniswap/v3_swap/main.sol | 176 ++++++----------- .../connectors/uniswap/v3_swap/helpers.sol | 124 ++++++++++++ .../connectors/uniswap/v3_swap/main.sol | 176 ++++++----------- .../connectors/uniswap/v3_swap/helpers.sol | 126 +++++++++++- .../connectors/uniswap/v3_swap/main.sol | 177 ++++++----------- 8 files changed, 726 insertions(+), 480 deletions(-) diff --git a/contracts/arbitrum/connectors/uniswap/v3_swap/helpers.sol b/contracts/arbitrum/connectors/uniswap/v3_swap/helpers.sol index 02d335bc..dd917125 100644 --- a/contracts/arbitrum/connectors/uniswap/v3_swap/helpers.sol +++ b/contracts/arbitrum/connectors/uniswap/v3_swap/helpers.sol @@ -33,4 +33,128 @@ abstract contract Helpers is DSMath, Basic { uint256 getId; //Id to get buyAmt uint256 setId; //Id to store sellAmt } + + /** + * @dev Buy Function + * @notice Swap token(sellAddr) with token(buyAddr), buy token with minimum sell token + * @param buyData Data input for the buy action + */ + function _buy( + BuyInfo memory buyData + ) + internal + payable + returns (string memory _eventName, bytes memory _eventParam) + { + uint256 _buyAmt = getUint(buyData.getId, buyData.buyAmt); + ISwapRouter.ExactOutputSingleParams memory params; + + (TokenInterface _buyAddr, TokenInterface _sellAddr) = changeEthAddress( + buyData.buyAddr, + buyData.sellAddr + ); + + uint _slippageAmt = convert18ToDec(_sellAddr.decimals(), + wmul(buyData.unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt)) + ); + bool isEth = address(_sellAddr) == wethAddr; + convertEthToWeth(isEth, _sellAddr, _slippageAmt); + approve(_sellAddr, address(swapRouter), _slippageAmt); + + { + params = ISwapRouter.ExactOutputSingleParams({ + tokenIn: buyData.sellAddr, + tokenOut: buyData.buyAddr, + fee: buyData.fee, + recipient: address(this), + deadline: block.timestamp + 1, + amountOut: _buyAmt, + amountInMaximum: _slippageAmt, //require(_sellAmt <= amountInMaximum) + sqrtPriceLimitX96: 0 + }); + } + + uint256 _sellAmt = swapRouter.exactOutputSingle(params); + require(_slippageAmt >= _sellAmt, "Too much slippage"); + + isEth = address(_buyAddr) == wethAddr; + convertWethToEth(isEth, _buyAddr, _buyAmt); + + setUint(buyData.setId, _sellAmt); + + _eventName = "LogBuy(address,address,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode( + buyData.buyAddr, + buyData.sellAddr, + _buyAmt, + _sellAmt, + buyData.getId, + buyData.setId + ); + } + + /** + * @dev Sell Function + * @notice Swap token(sellAddr) with token(buyAddr), to get max buy tokens + * @param sellData Data input for the sell action + */ + function _sell( + SellInfo memory sellData + ) + internal + payable + returns (string memory _eventName, bytes memory _eventParam) + { + uint256 _sellAmt = getUint(sellData.getId, sellData.sellAmt); + (TokenInterface _buyAddr, TokenInterface _sellAddr) = changeEthAddress( + sellData.buyAddr, + sellData.sellAddr + ); + + if (_sellAmt == uint(-1)) { + _sellAmt = sellData.sellAddr == ethAddr + ? address(this).balance + : _sellAddr.balanceOf(address(this)); + } + + uint _slippageAmt = convert18ToDec(_buyAddr.decimals(), + wmul(sellData.unitAmt, convertTo18(_sellAddr.decimals(), _sellAmt)) + ); + + bool isEth = address(_sellAddr) == wethAddr; + convertEthToWeth(isEth, _sellAddr, _sellAmt); + approve(_sellAddr, address(swapRouter), _sellAmt); + ISwapRouter.ExactInputSingleParams memory params; + + { + params = ISwapRouter.ExactInputSingleParams({ + tokenIn: sellData.sellAddr, + tokenOut: sellData.buyAddr, + fee: sellData.fee, + recipient: address(this), + deadline: block.timestamp + 1, + amountIn: _sellAmt, + amountOutMinimum: _slippageAmt, //require(_buyAmt >= amountOutMinimum) + sqrtPriceLimitX96: 0 + }); + } + + uint256 _buyAmt = swapRouter.exactInputSingle(params); + require(_slippageAmt <= _buyAmt, "Too much slippage"); + + isEth = address(_buyAddr) == wethAddr; + convertWethToEth(isEth, _buyAddr, _buyAmt); + + setUint(sellData.setId, _buyAmt); + + _eventName = "LogSell(address,address,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode( + sellData.buyAddr, + sellData.sellAddr, + _buyAmt, + _sellAmt, + sellData.getId, + sellData.setId + ); + } } \ No newline at end of file diff --git a/contracts/arbitrum/connectors/uniswap/v3_swap/main.sol b/contracts/arbitrum/connectors/uniswap/v3_swap/main.sol index ed9c8208..7db95ca1 100644 --- a/contracts/arbitrum/connectors/uniswap/v3_swap/main.sol +++ b/contracts/arbitrum/connectors/uniswap/v3_swap/main.sol @@ -13,129 +13,68 @@ import {Events} from "./events.sol"; import "./interface.sol"; abstract contract UniswapResolver is Helpers, Events { - /** - * @dev Buy Function - * @notice Swap token(sellAddr) with token(buyAddr), buy token with minimum sell token - * @param buyData Data input for the buy action - */ - function buy( - BuyInfo memory buyData - ) - external - payable - returns (string memory _eventName, bytes memory _eventParam) - { - uint256 _buyAmt = getUint(buyData.getId, buyData.buyAmt); - ISwapRouter.ExactOutputSingleParams memory params; - - (TokenInterface _buyAddr, TokenInterface _sellAddr) = changeEthAddress( - buyData.buyAddr, - buyData.sellAddr - ); - - uint _slippageAmt = convert18ToDec(_sellAddr.decimals(), - wmul(buyData.unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt)) - ); - bool isEth = address(_sellAddr) == wethAddr; - convertEthToWeth(isEth, _sellAddr, _slippageAmt); - approve(_sellAddr, address(swapRouter), _slippageAmt); - - { - params = ISwapRouter.ExactOutputSingleParams({ - tokenIn: buyData.sellAddr, - tokenOut: buyData.buyAddr, - fee: buyData.fee, - recipient: address(this), - deadline: block.timestamp + 1, - amountOut: _buyAmt, - amountInMaximum: _slippageAmt, //require(_sellAmt <= amountInMaximum) - sqrtPriceLimitX96: 0 - }); - } - - uint256 _sellAmt = swapRouter.exactOutputSingle(params); - require(_slippageAmt >= _sellAmt, "Too much slippage"); - - isEth = address(_buyAddr) == wethAddr; - convertWethToEth(isEth, _buyAddr, _buyAmt); - - setUint(buyData.setId, _sellAmt); - - _eventName = "LogBuy(address,address,uint256,uint256,uint256,uint256)"; - _eventParam = abi.encode( - buyData.buyAddr, - buyData.sellAddr, - _buyAmt, - _sellAmt, - buyData.getId, - buyData.setId - ); + /** + * @dev Buy Function + * @notice Swap token(sellAddr) with token(buyAddr), buy token with minimum sell token + * @param buyAddr token to be bought + * @param sellAddr token to be sold + * @param fee pool fees for buyAddr-sellAddr token pair + * @param unitAmt The unit amount of sellAmt/buyAmt with slippage + * @param buyAmt amount of token to be bought + * @param getId Id to get buyAmt + * @param setId Id to store sellAmt + */ + function buy( + address _buyAddr, + address _sellAddr, + uint24 _fee, + uint256 _unitAmt, + uint256 _buyAmt, + uint256 _getId, + uint256 _setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + BuyInfo memory buyData = BuyInfo({ + buyAddr: _buyAddr, + sellAddr: _sellAddr, + fee: _fee, + unitAmt: _unitAmt, + buyAmt: _buyAmt, + getId: _getId, + setId: _setId + }); + return _buy(buyData); } /** - * @dev Sell Function - * @notice Swap token(sellAddr) with token(buyAddr), to get max buy tokens - * @param sellData Data input for the buy action - */ - function sell( - SellInfo memory sellData - ) - external - payable - returns (string memory _eventName, bytes memory _eventParam) - { - uint256 _sellAmt = getUint(sellData.getId, sellData.sellAmt); - (TokenInterface _buyAddr, TokenInterface _sellAddr) = changeEthAddress( - sellData.buyAddr, - sellData.sellAddr - ); - - if (_sellAmt == uint(-1)) { - _sellAmt = sellData.sellAddr == ethAddr - ? address(this).balance - : _sellAddr.balanceOf(address(this)); - } - - uint _slippageAmt = convert18ToDec(_buyAddr.decimals(), - wmul(sellData.unitAmt, convertTo18(_sellAddr.decimals(), _sellAmt)) - ); - - bool isEth = address(_sellAddr) == wethAddr; - convertEthToWeth(isEth, _sellAddr, _sellAmt); - approve(_sellAddr, address(swapRouter), _sellAmt); - ISwapRouter.ExactInputSingleParams memory params; - - { - params = ISwapRouter.ExactInputSingleParams({ - tokenIn: sellData.sellAddr, - tokenOut: sellData.buyAddr, - fee: sellData.fee, - recipient: address(this), - deadline: block.timestamp + 1, - amountIn: _sellAmt, - amountOutMinimum: _slippageAmt, //require(_buyAmt >= amountOutMinimum) - sqrtPriceLimitX96: 0 - }); - } - - uint256 _buyAmt = swapRouter.exactInputSingle(params); - require(_slippageAmt <= _buyAmt, "Too much slippage"); - - isEth = address(_buyAddr) == wethAddr; - convertWethToEth(isEth, _buyAddr, _buyAmt); - - setUint(sellData.setId, _buyAmt); - - _eventName = "LogSell(address,address,uint256,uint256,uint256,uint256)"; - _eventParam = abi.encode( - sellData.buyAddr, - sellData.sellAddr, - _buyAmt, - _sellAmt, - sellData.getId, - sellData.setId - ); - } + * @dev Sell Function + * @notice Swap token(sellAddr) with token(buyAddr), buy token with minimum sell token + * @param buyAddr token to be bought + * @param sellAddr token to be sold + * @param fee pool fees for buyAddr-sellAddr token pair + * @param unitAmt The unit amount of buyAmt/sellAmt with slippage + * @param sellAmt amount of token to be sold + * @param getId Id to get sellAmt + * @param setId Id to store buyAmt + */ + function sell( + address _buyAddr, + address _sellAddr, + uint24 _fee, + uint256 _unitAmt, + uint256 _sellAmt, + uint256 _getId, + uint256 _setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + return _sell(SellInfo({ + buyAddr: _buyAddr, + sellAddr: _sellAddr, + fee: _fee, + unitAmt: _unitAmt, + sellAmt: _sellAmt, + getId: _getId, + setId: _setId + })); + } } contract ConnectV2UniswapV3Arbitrum is UniswapResolver { diff --git a/contracts/mainnet/connectors/uniswap/v3_swap/helpers.sol b/contracts/mainnet/connectors/uniswap/v3_swap/helpers.sol index 607687d5..2f0e0f9e 100644 --- a/contracts/mainnet/connectors/uniswap/v3_swap/helpers.sol +++ b/contracts/mainnet/connectors/uniswap/v3_swap/helpers.sol @@ -33,4 +33,128 @@ abstract contract Helpers is DSMath, Basic { uint256 getId; //Id to get buyAmt uint256 setId; //Id to store sellAmt } + + /** + * @dev Buy Function + * @notice Swap token(sellAddr) with token(buyAddr), buy token with minimum sell token + * @param buyData Data input for the buy action + */ + function _buy( + BuyInfo memory buyData + ) + internal + payable + returns (string memory _eventName, bytes memory _eventParam) + { + uint256 _buyAmt = getUint(buyData.getId, buyData.buyAmt); + ISwapRouter.ExactOutputSingleParams memory params; + + (TokenInterface _buyAddr, TokenInterface _sellAddr) = changeEthAddress( + buyData.buyAddr, + buyData.sellAddr + ); + + uint _slippageAmt = convert18ToDec(_sellAddr.decimals(), + wmul(buyData.unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt)) + ); + bool isEth = address(_sellAddr) == wethAddr; + convertEthToWeth(isEth, _sellAddr, _slippageAmt); + approve(_sellAddr, address(swapRouter), _slippageAmt); + + { + params = ISwapRouter.ExactOutputSingleParams({ + tokenIn: buyData.sellAddr, + tokenOut: buyData.buyAddr, + fee: buyData.fee, + recipient: address(this), + deadline: block.timestamp + 1, + amountOut: _buyAmt, + amountInMaximum: _slippageAmt, //require(_sellAmt <= amountInMaximum) + sqrtPriceLimitX96: 0 + }); + } + + uint256 _sellAmt = swapRouter.exactOutputSingle(params); + require(_slippageAmt >= _sellAmt, "Too much slippage"); + + isEth = address(_buyAddr) == wethAddr; + convertWethToEth(isEth, _buyAddr, _buyAmt); + + setUint(buyData.setId, _sellAmt); + + _eventName = "LogBuy(address,address,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode( + buyData.buyAddr, + buyData.sellAddr, + _buyAmt, + _sellAmt, + buyData.getId, + buyData.setId + ); + } + + /** + * @dev Sell Function + * @notice Swap token(sellAddr) with token(buyAddr), to get max buy tokens + * @param sellData Data input for the sell action + */ + function _sell( + SellInfo memory sellData + ) + internal + payable + returns (string memory _eventName, bytes memory _eventParam) + { + uint256 _sellAmt = getUint(sellData.getId, sellData.sellAmt); + (TokenInterface _buyAddr, TokenInterface _sellAddr) = changeEthAddress( + sellData.buyAddr, + sellData.sellAddr + ); + + if (_sellAmt == uint(-1)) { + _sellAmt = sellData.sellAddr == ethAddr + ? address(this).balance + : _sellAddr.balanceOf(address(this)); + } + + uint _slippageAmt = convert18ToDec(_buyAddr.decimals(), + wmul(sellData.unitAmt, convertTo18(_sellAddr.decimals(), _sellAmt)) + ); + + bool isEth = address(_sellAddr) == wethAddr; + convertEthToWeth(isEth, _sellAddr, _sellAmt); + approve(_sellAddr, address(swapRouter), _sellAmt); + ISwapRouter.ExactInputSingleParams memory params; + + { + params = ISwapRouter.ExactInputSingleParams({ + tokenIn: sellData.sellAddr, + tokenOut: sellData.buyAddr, + fee: sellData.fee, + recipient: address(this), + deadline: block.timestamp + 1, + amountIn: _sellAmt, + amountOutMinimum: _slippageAmt, //require(_buyAmt >= amountOutMinimum) + sqrtPriceLimitX96: 0 + }); + } + + uint256 _buyAmt = swapRouter.exactInputSingle(params); + require(_slippageAmt <= _buyAmt, "Too much slippage"); + + isEth = address(_buyAddr) == wethAddr; + convertWethToEth(isEth, _buyAddr, _buyAmt); + + setUint(sellData.setId, _buyAmt); + + _eventName = "LogSell(address,address,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode( + sellData.buyAddr, + sellData.sellAddr, + _buyAmt, + _sellAmt, + sellData.getId, + sellData.setId + ); + } } \ No newline at end of file diff --git a/contracts/mainnet/connectors/uniswap/v3_swap/main.sol b/contracts/mainnet/connectors/uniswap/v3_swap/main.sol index e75689be..d464bfe4 100644 --- a/contracts/mainnet/connectors/uniswap/v3_swap/main.sol +++ b/contracts/mainnet/connectors/uniswap/v3_swap/main.sol @@ -14,129 +14,65 @@ import "./interface.sol"; abstract contract UniswapResolver is Helpers, Events { /** - * @dev Buy Function - * @notice Swap token(sellAddr) with token(buyAddr), buy token with minimum sell token - * @param buyData Data input for the buy action - */ - function buy( - BuyInfo memory buyData - ) - external - payable - returns (string memory _eventName, bytes memory _eventParam) - { - uint256 _buyAmt = getUint(buyData.getId, buyData.buyAmt); - uint _slippageAmt; - bool isEth; - ISwapRouter.ExactOutputSingleParams memory params; - - (TokenInterface _buyAddr, TokenInterface _sellAddr) = changeEthAddress( - buyData.buyAddr, - buyData.sellAddr - ); - - _slippageAmt = convert18ToDec(_sellAddr.decimals(), - wmul(buyData.unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt)) - ); - isEth = address(_sellAddr) == wethAddr; - convertEthToWeth(isEth, _sellAddr, _slippageAmt); - approve(_sellAddr, address(swapRouter), _slippageAmt); - - { - params = ISwapRouter.ExactOutputSingleParams({ - tokenIn: buyData.sellAddr, - tokenOut: buyData.buyAddr, - fee: buyData.fee, - recipient: address(this), - deadline: block.timestamp + 1, - amountOut: _buyAmt, - amountInMaximum: _slippageAmt, //require(_sellAmt <= amountInMaximum) - sqrtPriceLimitX96: 0 - }); - } - - uint256 _sellAmt = swapRouter.exactOutputSingle(params); - require(_slippageAmt >= _sellAmt, "Too much slippage"); - - isEth = address(_buyAddr) == wethAddr; - convertWethToEth(isEth, _buyAddr, _buyAmt); - - setUint(buyData.setId, _sellAmt); - - _eventName = "LogBuy(address,address,uint256,uint256,uint256,uint256)"; - _eventParam = abi.encode( - buyData.buyAddr, - buyData.sellAddr, - _buyAmt, - _sellAmt, - buyData.getId, - buyData.setId - ); + * @dev Buy Function + * @notice Swap token(sellAddr) with token(buyAddr), buy token with minimum sell token + * @param buyAddr token to be bought + * @param sellAddr token to be sold + * @param fee pool fees for buyAddr-sellAddr token pair + * @param unitAmt The unit amount of sellAmt/buyAmt with slippage + * @param buyAmt amount of token to be bought + * @param getId Id to get buyAmt + * @param setId Id to store sellAmt + */ + function buy( + address _buyAddr, + address _sellAddr, + uint24 _fee, + uint256 _unitAmt, + uint256 _buyAmt, + uint256 _getId, + uint256 _setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + return _buy(BuyInfo({ + buyAddr: _buyAddr, + sellAddr: _sellAddr, + fee: _fee, + unitAmt: _unitAmt, + buyAmt: _buyAmt, + getId: _getId, + setId: _setId + })); } /** - * @dev Sell Function - * @notice Swap token(sellAddr) with token(buyAddr), to get max buy tokens - * @param sellData Data input for the buy action - */ - function sell( - SellInfo memory sellData - ) - external - payable - returns (string memory _eventName, bytes memory _eventParam) - { - uint256 _sellAmt = getUint(sellData.getId, sellData.sellAmt); - (TokenInterface _buyAddr, TokenInterface _sellAddr) = changeEthAddress( - sellData.buyAddr, - sellData.sellAddr - ); - - if (_sellAmt == uint(-1)) { - _sellAmt = sellData.sellAddr == ethAddr - ? address(this).balance - : _sellAddr.balanceOf(address(this)); - } - - uint _slippageAmt = convert18ToDec(_buyAddr.decimals(), - wmul(sellData.unitAmt, convertTo18(_sellAddr.decimals(), _sellAmt)) - ); - - bool isEth = address(_sellAddr) == wethAddr; - convertEthToWeth(isEth, _sellAddr, _sellAmt); - approve(_sellAddr, address(swapRouter), _sellAmt); - ISwapRouter.ExactInputSingleParams memory params; - - { - params = ISwapRouter.ExactInputSingleParams({ - tokenIn: sellData.sellAddr, - tokenOut: sellData.buyAddr, - fee: sellData.fee, - recipient: address(this), - deadline: block.timestamp + 1, - amountIn: _sellAmt, - amountOutMinimum: _slippageAmt, //require(_buyAmt >= amountOutMinimum) - sqrtPriceLimitX96: 0 - }); - } - - uint256 _buyAmt = swapRouter.exactInputSingle(params); - require(_slippageAmt <= _buyAmt, "Too much slippage"); - - isEth = address(_buyAddr) == wethAddr; - convertWethToEth(isEth, _buyAddr, _buyAmt); - - setUint(sellData.setId, _buyAmt); - - _eventName = "LogSell(address,address,uint256,uint256,uint256,uint256)"; - _eventParam = abi.encode( - sellData.buyAddr, - sellData.sellAddr, - _buyAmt, - _sellAmt, - sellData.getId, - sellData.setId - ); + * @dev Sell Function + * @notice Swap token(sellAddr) with token(buyAddr), buy token with minimum sell token + * @param buyAddr token to be bought + * @param sellAddr token to be sold + * @param fee pool fees for buyAddr-sellAddr token pair + * @param unitAmt The unit amount of buyAmt/sellAmt with slippage + * @param sellAmt amount of token to be sold + * @param getId Id to get sellAmt + * @param setId Id to store buyAmt + */ + function sell( + address _buyAddr, + address _sellAddr, + uint24 _fee, + uint256 _unitAmt, + uint256 _sellAmt, + uint256 _getId, + uint256 _setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + return _sell(SellInfo({ + buyAddr: _buyAddr, + sellAddr: _sellAddr, + fee: _fee, + unitAmt: _unitAmt, + sellAmt: _sellAmt, + getId: _getId, + setId: _setId + })); } } diff --git a/contracts/optimism/connectors/uniswap/v3_swap/helpers.sol b/contracts/optimism/connectors/uniswap/v3_swap/helpers.sol index 73ef69df..c0f2e442 100644 --- a/contracts/optimism/connectors/uniswap/v3_swap/helpers.sol +++ b/contracts/optimism/connectors/uniswap/v3_swap/helpers.sol @@ -33,4 +33,128 @@ abstract contract Helpers is DSMath, Basic { uint256 getId; //Id to get buyAmt uint256 setId; //Id to store sellAmt } + + /** + * @dev Buy Function + * @notice Swap token(sellAddr) with token(buyAddr), buy token with minimum sell token + * @param buyData Data input for the buy action + */ + function _buy( + BuyInfo memory buyData + ) + internal + payable + returns (string memory _eventName, bytes memory _eventParam) + { + uint256 _buyAmt = getUint(buyData.getId, buyData.buyAmt); + ISwapRouter.ExactOutputSingleParams memory params; + + (TokenInterface _buyAddr, TokenInterface _sellAddr) = changeEthAddress( + buyData.buyAddr, + buyData.sellAddr + ); + + uint _slippageAmt = convert18ToDec(_sellAddr.decimals(), + wmul(buyData.unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt)) + ); + bool isEth = address(_sellAddr) == wethAddr; + convertEthToWeth(isEth, _sellAddr, _slippageAmt); + approve(_sellAddr, address(swapRouter), _slippageAmt); + + { + params = ISwapRouter.ExactOutputSingleParams({ + tokenIn: buyData.sellAddr, + tokenOut: buyData.buyAddr, + fee: buyData.fee, + recipient: address(this), + deadline: block.timestamp + 1, + amountOut: _buyAmt, + amountInMaximum: _slippageAmt, //require(_sellAmt <= amountInMaximum) + sqrtPriceLimitX96: 0 + }); + } + + uint256 _sellAmt = swapRouter.exactOutputSingle(params); + require(_slippageAmt >= _sellAmt, "Too much slippage"); + + isEth = address(_buyAddr) == wethAddr; + convertWethToEth(isEth, _buyAddr, _buyAmt); + + setUint(buyData.setId, _sellAmt); + + _eventName = "LogBuy(address,address,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode( + buyData.buyAddr, + buyData.sellAddr, + _buyAmt, + _sellAmt, + buyData.getId, + buyData.setId + ); + } + + /** + * @dev Sell Function + * @notice Swap token(sellAddr) with token(buyAddr), to get max buy tokens + * @param sellData Data input for the sell action + */ + function _sell( + SellInfo memory sellData + ) + internal + payable + returns (string memory _eventName, bytes memory _eventParam) + { + uint256 _sellAmt = getUint(sellData.getId, sellData.sellAmt); + (TokenInterface _buyAddr, TokenInterface _sellAddr) = changeEthAddress( + sellData.buyAddr, + sellData.sellAddr + ); + + if (_sellAmt == uint(-1)) { + _sellAmt = sellData.sellAddr == ethAddr + ? address(this).balance + : _sellAddr.balanceOf(address(this)); + } + + uint _slippageAmt = convert18ToDec(_buyAddr.decimals(), + wmul(sellData.unitAmt, convertTo18(_sellAddr.decimals(), _sellAmt)) + ); + + bool isEth = address(_sellAddr) == wethAddr; + convertEthToWeth(isEth, _sellAddr, _sellAmt); + approve(_sellAddr, address(swapRouter), _sellAmt); + ISwapRouter.ExactInputSingleParams memory params; + + { + params = ISwapRouter.ExactInputSingleParams({ + tokenIn: sellData.sellAddr, + tokenOut: sellData.buyAddr, + fee: sellData.fee, + recipient: address(this), + deadline: block.timestamp + 1, + amountIn: _sellAmt, + amountOutMinimum: _slippageAmt, //require(_buyAmt >= amountOutMinimum) + sqrtPriceLimitX96: 0 + }); + } + + uint256 _buyAmt = swapRouter.exactInputSingle(params); + require(_slippageAmt <= _buyAmt, "Too much slippage"); + + isEth = address(_buyAddr) == wethAddr; + convertWethToEth(isEth, _buyAddr, _buyAmt); + + setUint(sellData.setId, _buyAmt); + + _eventName = "LogSell(address,address,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode( + sellData.buyAddr, + sellData.sellAddr, + _buyAmt, + _sellAmt, + sellData.getId, + sellData.setId + ); + } } \ No newline at end of file diff --git a/contracts/optimism/connectors/uniswap/v3_swap/main.sol b/contracts/optimism/connectors/uniswap/v3_swap/main.sol index 5bca746f..193834c2 100644 --- a/contracts/optimism/connectors/uniswap/v3_swap/main.sol +++ b/contracts/optimism/connectors/uniswap/v3_swap/main.sol @@ -14,129 +14,65 @@ import "./interface.sol"; abstract contract UniswapResolver is Helpers, Events { /** - * @dev Buy Function - * @notice Swap token(sellAddr) with token(buyAddr), buy token with minimum sell token - * @param buyData Data input for the buy action - */ - function buy( - BuyInfo memory buyData - ) - external - payable - returns (string memory _eventName, bytes memory _eventParam) - { - uint256 _buyAmt = getUint(buyData.getId, buyData.buyAmt); - uint _slippageAmt; - bool isEth; - ISwapRouter.ExactOutputSingleParams memory params; - - (TokenInterface _buyAddr, TokenInterface _sellAddr) = changeEthAddress( - buyData.buyAddr, - buyData.sellAddr - ); - - _slippageAmt = convert18ToDec(_sellAddr.decimals(), - wmul(buyData.unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt)) - ); - isEth = address(_sellAddr) == wethAddr; - convertEthToWeth(isEth, _sellAddr, _slippageAmt); - approve(_sellAddr, address(swapRouter), _slippageAmt); - - { - params = ISwapRouter.ExactOutputSingleParams({ - tokenIn: buyData.sellAddr, - tokenOut: buyData.buyAddr, - fee: buyData.fee, - recipient: address(this), - deadline: block.timestamp + 1, - amountOut: _buyAmt, - amountInMaximum: _slippageAmt, //require(_sellAmt <= amountInMaximum) - sqrtPriceLimitX96: 0 - }); - } - - uint256 _sellAmt = swapRouter.exactOutputSingle(params); - require(_slippageAmt >= _sellAmt, "Too much slippage"); - - isEth = address(_buyAddr) == wethAddr; - convertWethToEth(isEth, _buyAddr, _buyAmt); - - setUint(buyData.setId, _sellAmt); - - _eventName = "LogBuy(address,address,uint256,uint256,uint256,uint256)"; - _eventParam = abi.encode( - buyData.buyAddr, - buyData.sellAddr, - _buyAmt, - _sellAmt, - buyData.getId, - buyData.setId - ); + * @dev Buy Function + * @notice Swap token(sellAddr) with token(buyAddr), buy token with minimum sell token + * @param buyAddr token to be bought + * @param sellAddr token to be sold + * @param fee pool fees for buyAddr-sellAddr token pair + * @param unitAmt The unit amount of sellAmt/buyAmt with slippage + * @param buyAmt amount of token to be bought + * @param getId Id to get buyAmt + * @param setId Id to store sellAmt + */ + function buy( + address _buyAddr, + address _sellAddr, + uint24 _fee, + uint256 _unitAmt, + uint256 _buyAmt, + uint256 _getId, + uint256 _setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + return _buy(BuyInfo({ + buyAddr: _buyAddr, + sellAddr: _sellAddr, + fee: _fee, + unitAmt: _unitAmt, + buyAmt: _buyAmt, + getId: _getId, + setId: _setId + })); } /** - * @dev Sell Function - * @notice Swap token(sellAddr) with token(buyAddr), to get max buy tokens - * @param sellData Data input for the buy action - */ - function sell( - SellInfo memory sellData - ) - external - payable - returns (string memory _eventName, bytes memory _eventParam) - { - uint256 _sellAmt = getUint(sellData.getId, sellData.sellAmt); - (TokenInterface _buyAddr, TokenInterface _sellAddr) = changeEthAddress( - sellData.buyAddr, - sellData.sellAddr - ); - - if (_sellAmt == uint(-1)) { - _sellAmt = sellData.sellAddr == ethAddr - ? address(this).balance - : _sellAddr.balanceOf(address(this)); - } - - uint _slippageAmt = convert18ToDec(_buyAddr.decimals(), - wmul(sellData.unitAmt, convertTo18(_sellAddr.decimals(), _sellAmt)) - ); - - bool isEth = address(_sellAddr) == wethAddr; - convertEthToWeth(isEth, _sellAddr, _sellAmt); - approve(_sellAddr, address(swapRouter), _sellAmt); - ISwapRouter.ExactInputSingleParams memory params; - - { - params = ISwapRouter.ExactInputSingleParams({ - tokenIn: sellData.sellAddr, - tokenOut: sellData.buyAddr, - fee: sellData.fee, - recipient: address(this), - deadline: block.timestamp + 1, - amountIn: _sellAmt, - amountOutMinimum: _slippageAmt, //require(_buyAmt >= amountOutMinimum) - sqrtPriceLimitX96: 0 - }); - } - - uint256 _buyAmt = swapRouter.exactInputSingle(params); - require(_slippageAmt <= _buyAmt, "Too much slippage"); - - isEth = address(_buyAddr) == wethAddr; - convertWethToEth(isEth, _buyAddr, _buyAmt); - - setUint(sellData.setId, _buyAmt); - - _eventName = "LogSell(address,address,uint256,uint256,uint256,uint256)"; - _eventParam = abi.encode( - sellData.buyAddr, - sellData.sellAddr, - _buyAmt, - _sellAmt, - sellData.getId, - sellData.setId - ); + * @dev Sell Function + * @notice Swap token(sellAddr) with token(buyAddr), buy token with minimum sell token + * @param buyAddr token to be bought + * @param sellAddr token to be sold + * @param fee pool fees for buyAddr-sellAddr token pair + * @param unitAmt The unit amount of buyAmt/sellAmt with slippage + * @param sellAmt amount of token to be sold + * @param getId Id to get sellAmt + * @param setId Id to store buyAmt + */ + function sell( + address _buyAddr, + address _sellAddr, + uint24 _fee, + uint256 _unitAmt, + uint256 _sellAmt, + uint256 _getId, + uint256 _setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + return _sell(SellInfo({ + buyAddr: _buyAddr, + sellAddr: _sellAddr, + fee: _fee, + unitAmt: _unitAmt, + sellAmt: _sellAmt, + getId: _getId, + setId: _setId + })); } } diff --git a/contracts/polygon/connectors/uniswap/v3_swap/helpers.sol b/contracts/polygon/connectors/uniswap/v3_swap/helpers.sol index a60bd9bb..99aa353d 100644 --- a/contracts/polygon/connectors/uniswap/v3_swap/helpers.sol +++ b/contracts/polygon/connectors/uniswap/v3_swap/helpers.sol @@ -28,9 +28,133 @@ abstract contract Helpers is DSMath, Basic { address buyAddr; //token to be bought address sellAddr; //token to be sold uint24 fee; //pool fees for buyAddr-sellAddr token pair - uint256 unitAmt; //The unit amount of sellAmt/buyAmt with slippage + uint256 unitAmt; //The unit amount of buyAmt/sellAmt with slippage uint256 sellAmt; //amount of token to be bought uint256 getId; //Id to get buyAmt uint256 setId; //Id to store sellAmt } + + /** + * @dev Buy Function + * @notice Swap token(sellAddr) with token(buyAddr), buy token with minimum sell token + * @param buyData Data input for the buy action + */ + function _buy( + BuyInfo memory buyData + ) + internal + payable + returns (string memory _eventName, bytes memory _eventParam) + { + uint256 _buyAmt = getUint(buyData.getId, buyData.buyAmt); + ( + TokenInterface _buyAddr, + TokenInterface _sellAddr + ) = changeMaticAddress(buyData.buyAddr, buyData.sellAddr); + + uint _slippageAmt = convert18ToDec(_sellAddr.decimals(), + wmul(buyData.unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt)) + ); + + bool isMatic = address(_sellAddr) == wmaticAddr; + convertMaticToWmatic(isMatic, _sellAddr, _slippageAmt); + approve(_sellAddr, address(swapRouter), _slippageAmt); + ISwapRouter.ExactOutputSingleParams memory params; + + { + params = ISwapRouter.ExactOutputSingleParams({ + tokenIn: buyData.sellAddr, + tokenOut: buyData.buyAddr, + fee: buyData.fee, + recipient: address(this), + deadline: block.timestamp + 1, + amountOut: _buyAmt, + amountInMaximum: _slippageAmt, //require(_sellAmt <= amountInMaximum) + sqrtPriceLimitX96: 0 + }); + } + + uint256 _sellAmt = swapRouter.exactOutputSingle(params); + require(_slippageAmt >= _sellAmt, "Too much slippage"); + + isMatic = address(_buyAddr) == wmaticAddr; + convertWmaticToMatic(isMatic, _buyAddr, _buyAmt); + + setUint(buyData.setId, _sellAmt); + + _eventName = "LogBuy(address,address,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode( + buyData.buyAddr, + buyData.sellAddr, + _buyAmt, + _sellAmt, + buyData.getId, + buyData.setId + ); + } + + /** + * @dev Sell Function + * @notice Swap token(sellAddr) with token(buyAddr), to get max buy tokens + * @param sellData Data input for the sell action + */ + function _sell( + SellInfo memory sellData + ) + internal + payable + returns (string memory _eventName, bytes memory _eventParam) + { + uint256 _sellAmt = getUint(sellData.getId, sellData.sellAmt); + ( + TokenInterface _buyAddr, + TokenInterface _sellAddr + ) = changeMaticAddress(sellData.buyAddr, sellData.sellAddr); + + if (_sellAmt == uint(-1)) { + _sellAmt = sellData.sellAddr == maticAddr + ? address(this).balance + : _sellAddr.balanceOf(address(this)); + } + + uint _slippageAmt = convert18ToDec(_buyAddr.decimals(), + wmul(sellData.unitAmt, convertTo18(_sellAddr.decimals(), _sellAmt)) + ); + + bool isMatic = address(_sellAddr) == wmaticAddr; + convertMaticToWmatic(isMatic, _sellAddr, _sellAmt); + approve(_sellAddr, address(swapRouter), _sellAmt); + ISwapRouter.ExactInputSingleParams memory params; + + { + params = ISwapRouter.ExactInputSingleParams({ + tokenIn: sellData.sellAddr, + tokenOut: sellData.buyAddr, + fee: sellData.fee, + recipient: address(this), + deadline: block.timestamp + 1, + amountIn: _sellAmt, + amountOutMinimum: _slippageAmt, //require(_buyAmt >= amountOutMinimum) + sqrtPriceLimitX96: 0 + }); + } + + uint256 _buyAmt = swapRouter.exactInputSingle(params); + require(_slippageAmt <= _buyAmt, "Too much slippage"); + + isMatic = address(_buyAddr) == wmaticAddr; + convertWmaticToMatic(isMatic, _buyAddr, _buyAmt); + + setUint(sellData.setId, _buyAmt); + + _eventName = "LogSell(address,address,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode( + sellData.buyAddr, + sellData.sellAddr, + _buyAmt, + _sellAmt, + sellData.getId, + sellData.setId + ); + } } \ No newline at end of file diff --git a/contracts/polygon/connectors/uniswap/v3_swap/main.sol b/contracts/polygon/connectors/uniswap/v3_swap/main.sol index ab134d91..00cea452 100644 --- a/contracts/polygon/connectors/uniswap/v3_swap/main.sol +++ b/contracts/polygon/connectors/uniswap/v3_swap/main.sol @@ -10,132 +10,71 @@ pragma abicoder v2; import {TokenInterface} from "../../../common/interfaces.sol"; import {Helpers} from "./helpers.sol"; import {Events} from "./events.sol"; -import "./interface.sol"; abstract contract UniswapResolver is Helpers, Events { /** - * @dev Buy Function - * @notice Swap token(sellAddr) with token(buyAddr), buy token with minimum sell token - * @param buyData Data input for the buy action - */ - function buy( - BuyInfo memory buyData - ) - external - payable - returns (string memory _eventName, bytes memory _eventParam) - { - uint256 _buyAmt = getUint(buyData.getId, buyData.buyAmt); - ( - TokenInterface _buyAddr, - TokenInterface _sellAddr - ) = changeMaticAddress(buyData.buyAddr, buyData.sellAddr); - - uint _slippageAmt = convert18ToDec(_sellAddr.decimals(), - wmul(buyData.unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt)) - ); - - bool isMatic = address(_sellAddr) == wmaticAddr; - convertMaticToWmatic(isMatic, _sellAddr, _slippageAmt); - approve(_sellAddr, address(swapRouter), _slippageAmt); - ISwapRouter.ExactOutputSingleParams memory params; - - { - params = ISwapRouter.ExactOutputSingleParams({ - tokenIn: buyData.sellAddr, - tokenOut: buyData.buyAddr, - fee: buyData.fee, - recipient: address(this), - deadline: block.timestamp + 1, - amountOut: _buyAmt, - amountInMaximum: _slippageAmt, //require(_sellAmt <= amountInMaximum) - sqrtPriceLimitX96: 0 - }); - } - - uint256 _sellAmt = swapRouter.exactOutputSingle(params); - require(_slippageAmt >= _sellAmt, "Too much slippage"); - - isMatic = address(_buyAddr) == wmaticAddr; - convertWmaticToMatic(isMatic, _buyAddr, _buyAmt); - - setUint(buyData.setId, _sellAmt); - - _eventName = "LogBuy(address,address,uint256,uint256,uint256,uint256)"; - _eventParam = abi.encode( - buyData.buyAddr, - buyData.sellAddr, - _buyAmt, - _sellAmt, - buyData.getId, - buyData.setId - ); + * @dev Buy Function + * @notice Swap token(sellAddr) with token(buyAddr), buy token with minimum sell token + * @param buyAddr token to be bought + * @param sellAddr token to be sold + * @param fee pool fees for buyAddr-sellAddr token pair + * @param unitAmt The unit amount of sellAmt/buyAmt with slippage + * @param buyAmt amount of token to be bought + * @param getId Id to get buyAmt + * @param setId Id to store sellAmt + */ + function buy( + address _buyAddr, + address _sellAddr, + uint24 _fee, + uint256 _unitAmt, + uint256 _buyAmt, + uint256 _getId, + uint256 _setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + BuyInfo memory buyData = BuyInfo({ + buyAddr: _buyAddr, + sellAddr: _sellAddr, + fee: _fee, + unitAmt: _unitAmt, + buyAmt: _buyAmt, + getId: _getId, + setId: _setId + }); + return _buy(buyData); } /** - * @dev Sell Function - * @notice Swap token(sellAddr) with token(buyAddr), to get max buy tokens - * @param sellData Data input for the buy action - */ - function sell( - SellInfo memory sellData - ) - external - payable - returns (string memory _eventName, bytes memory _eventParam) - { - uint256 _sellAmt = getUint(sellData.getId, sellData.sellAmt); - ( - TokenInterface _buyAddr, - TokenInterface _sellAddr - ) = changeMaticAddress(sellData.buyAddr, sellData.sellAddr); - - if (_sellAmt == uint(-1)) { - _sellAmt = sellData.sellAddr == maticAddr - ? address(this).balance - : _sellAddr.balanceOf(address(this)); - } - - uint _slippageAmt = convert18ToDec(_buyAddr.decimals(), - wmul(sellData.unitAmt, convertTo18(_sellAddr.decimals(), _sellAmt)) - ); - - bool isMatic = address(_sellAddr) == wmaticAddr; - convertMaticToWmatic(isMatic, _sellAddr, _sellAmt); - approve(_sellAddr, address(swapRouter), _sellAmt); - ISwapRouter.ExactInputSingleParams memory params; - - { - params = ISwapRouter.ExactInputSingleParams({ - tokenIn: sellData.sellAddr, - tokenOut: sellData.buyAddr, - fee: sellData.fee, - recipient: address(this), - deadline: block.timestamp + 1, - amountIn: _sellAmt, - amountOutMinimum: _slippageAmt, //require(_buyAmt >= amountOutMinimum) - sqrtPriceLimitX96: 0 - }); - } - - uint256 _buyAmt = swapRouter.exactInputSingle(params); - require(_slippageAmt <= _buyAmt, "Too much slippage"); - - isMatic = address(_buyAddr) == wmaticAddr; - convertWmaticToMatic(isMatic, _buyAddr, _buyAmt); - - setUint(sellData.setId, _buyAmt); - - _eventName = "LogSell(address,address,uint256,uint256,uint256,uint256)"; - _eventParam = abi.encode( - sellData.buyAddr, - sellData.sellAddr, - _buyAmt, - _sellAmt, - sellData.getId, - sellData.setId - ); + * @dev Sell Function + * @notice Swap token(sellAddr) with token(buyAddr), buy token with minimum sell token + * @param buyAddr token to be bought + * @param sellAddr token to be sold + * @param fee pool fees for buyAddr-sellAddr token pair + * @param unitAmt The unit amount of buyAmt/sellAmt with slippage + * @param sellAmt amount of token to be sold + * @param getId Id to get sellAmt + * @param setId Id to store buyAmt + */ + function sell( + address _buyAddr, + address _sellAddr, + uint24 _fee, + uint256 _unitAmt, + uint256 _sellAmt, + uint256 _getId, + uint256 _setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + return _sell(SellInfo({ + buyAddr: _buyAddr, + sellAddr: _sellAddr, + fee: _fee, + unitAmt: _unitAmt, + sellAmt: _sellAmt, + getId: _getId, + setId: _setId + })); } + } contract ConnectV2UniswapV3Polygon is UniswapResolver {