From b0980aebb407a30140cc40d04855c6bd9f2f7576 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Sat, 30 Apr 2022 16:59:30 +0530 Subject: [PATCH] refactored code --- .../connectors/uniswap/v3_swap/helpers.sol | 11 ++++ .../connectors/uniswap/v3_swap/main.sol | 49 ++++++++---------- .../connectors/uniswap/v3_swap/helpers.sol | 11 ++++ .../connectors/uniswap/v3_swap/main.sol | 50 ++++++++----------- .../connectors/uniswap/v3_swap/helpers.sol | 11 ++++ .../connectors/uniswap/v3_swap/main.sol | 50 ++++++++----------- .../connectors/uniswap/v3_swap/helpers.sol | 11 ++++ .../connectors/uniswap/v3_swap/main.sol | 41 +++++++-------- 8 files changed, 125 insertions(+), 109 deletions(-) diff --git a/contracts/arbitrum/connectors/uniswap/v3_swap/helpers.sol b/contracts/arbitrum/connectors/uniswap/v3_swap/helpers.sol index 554cb719..2950f6a1 100644 --- a/contracts/arbitrum/connectors/uniswap/v3_swap/helpers.sol +++ b/contracts/arbitrum/connectors/uniswap/v3_swap/helpers.sol @@ -13,4 +13,15 @@ abstract contract Helpers is DSMath, Basic { */ ISwapRouter constant swapRouter = ISwapRouter(0xE592427A0AEce92De3Edee1F18E0157C05861564); + + struct BuyInfo { + 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 expectedAmt; //The amount that is expected to be returned after the swap + uint256 buyAmt; //amount of token to be bought + uint256 getId; //Id to get buyAmt + uint256 setId; //Id to store sellAmt + } } \ 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 b02b6d93..ae4ce130 100644 --- a/contracts/arbitrum/connectors/uniswap/v3_swap/main.sol +++ b/contracts/arbitrum/connectors/uniswap/v3_swap/main.sol @@ -16,34 +16,27 @@ abstract contract UniswapResolver is Helpers, Events { /** * @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 buyAmt amount of token to be bought - * @param getId Id to get buyAmt - * @param setId Id to store sellAmt + * @param buyData Data input for the buy action */ function buy( - address buyAddr, - address sellAddr, - uint24 fee, - uint256 buyAmt, - uint256 getId, - uint256 setId + BuyInfo memory buyData ) external payable returns (string memory _eventName, bytes memory _eventParam) { - uint256 _buyAmt = getUint(getId, buyAmt); + uint256 _buyAmt = getUint(buyData.getId, buyData.buyAmt); (TokenInterface _buyAddr, TokenInterface _sellAddr) = changeEthAddress( - buyAddr, - sellAddr + buyData.buyAddr, + buyData.sellAddr ); - - // uint _slippageAmt = convert18ToDec(_sellAddr.decimals(), - // wmul(unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt)) - // ); + + { + uint _slippageAmt = convert18ToDec(_sellAddr.decimals(), + wmul(buyData.unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt)) + ); + require(_slippageAmt >= buyData.expectedAmt, "Too much slippage"); + } bool isEth = address(_sellAddr) == wethAddr; convertEthToWeth(isEth, _sellAddr, uint256(-1)); @@ -52,13 +45,13 @@ abstract contract UniswapResolver is Helpers, Events { { params = ISwapRouter.ExactOutputSingleParams({ - tokenIn: sellAddr, - tokenOut: buyAddr, - fee: fee, + tokenIn: buyData.sellAddr, + tokenOut: buyData.buyAddr, + fee: buyData.fee, recipient: address(this), deadline: block.timestamp + 1, amountOut: _buyAmt, - amountInMaximum: uint256(-1), + amountInMaximum: buyData.expectedAmt, sqrtPriceLimitX96: 0 }); } @@ -68,16 +61,16 @@ abstract contract UniswapResolver is Helpers, Events { isEth = address(_buyAddr) == wethAddr; convertWethToEth(isEth, _buyAddr, _buyAmt); - setUint(setId, _sellAmt); + setUint(buyData.setId, _sellAmt); _eventName = "LogBuy(address,address,uint256,uint256,uint256,uint256)"; _eventParam = abi.encode( - buyAddr, - sellAddr, + buyData.buyAddr, + buyData.sellAddr, _buyAmt, _sellAmt, - getId, - setId + buyData.getId, + buyData.setId ); } diff --git a/contracts/mainnet/connectors/uniswap/v3_swap/helpers.sol b/contracts/mainnet/connectors/uniswap/v3_swap/helpers.sol index 554cb719..6f4a7905 100644 --- a/contracts/mainnet/connectors/uniswap/v3_swap/helpers.sol +++ b/contracts/mainnet/connectors/uniswap/v3_swap/helpers.sol @@ -13,4 +13,15 @@ abstract contract Helpers is DSMath, Basic { */ ISwapRouter constant swapRouter = ISwapRouter(0xE592427A0AEce92De3Edee1F18E0157C05861564); + + struct BuyInfo { + 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 expectedAmt; //The amount that is expected to be returned after the swap + uint256 buyAmt; //amount of token to be bought + uint256 getId; //Id to get buyAmt + uint256 setId; //Id to store sellAmt + } } \ 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 ddd14a75..9fce8eb2 100644 --- a/contracts/mainnet/connectors/uniswap/v3_swap/main.sol +++ b/contracts/mainnet/connectors/uniswap/v3_swap/main.sol @@ -13,38 +13,30 @@ 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 buyAddr token to be bought - * @param sellAddr token to be sold - * @param fee pool fees for buyAddr-sellAddr token pair - * @param buyAmt amount of token to be bought - * @param getId Id to get buyAmt - * @param setId Id to store sellAmt + * @param buyData Data input for the buy action */ function buy( - address buyAddr, - address sellAddr, - uint24 fee, - uint256 buyAmt, - uint256 getId, - uint256 setId + BuyInfo memory buyData ) external payable returns (string memory _eventName, bytes memory _eventParam) { - uint256 _buyAmt = getUint(getId, buyAmt); + uint256 _buyAmt = getUint(buyData.getId, buyData.buyAmt); (TokenInterface _buyAddr, TokenInterface _sellAddr) = changeEthAddress( - buyAddr, - sellAddr + buyData.buyAddr, + buyData.sellAddr ); - - // uint _slippageAmt = convert18ToDec(_sellAddr.decimals(), - // wmul(unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt)) - // ); + + { + uint _slippageAmt = convert18ToDec(_sellAddr.decimals(), + wmul(buyData.unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt)) + ); + require(_slippageAmt >= buyData.expectedAmt, "Too much slippage"); + } bool isEth = address(_sellAddr) == wethAddr; convertEthToWeth(isEth, _sellAddr, uint256(-1)); @@ -53,13 +45,13 @@ abstract contract UniswapResolver is Helpers, Events { { params = ISwapRouter.ExactOutputSingleParams({ - tokenIn: sellAddr, - tokenOut: buyAddr, - fee: fee, + tokenIn: buyData.sellAddr, + tokenOut: buyData.buyAddr, + fee: buyData.fee, recipient: address(this), deadline: block.timestamp + 1, amountOut: _buyAmt, - amountInMaximum: uint256(-1), + amountInMaximum: buyData.expectedAmt, sqrtPriceLimitX96: 0 }); } @@ -69,16 +61,16 @@ abstract contract UniswapResolver is Helpers, Events { isEth = address(_buyAddr) == wethAddr; convertWethToEth(isEth, _buyAddr, _buyAmt); - setUint(setId, _sellAmt); + setUint(buyData.setId, _sellAmt); _eventName = "LogBuy(address,address,uint256,uint256,uint256,uint256)"; _eventParam = abi.encode( - buyAddr, - sellAddr, + buyData.buyAddr, + buyData.sellAddr, _buyAmt, _sellAmt, - getId, - setId + buyData.getId, + buyData.setId ); } diff --git a/contracts/optimism/connectors/uniswap/v3_swap/helpers.sol b/contracts/optimism/connectors/uniswap/v3_swap/helpers.sol index 554cb719..2950f6a1 100644 --- a/contracts/optimism/connectors/uniswap/v3_swap/helpers.sol +++ b/contracts/optimism/connectors/uniswap/v3_swap/helpers.sol @@ -13,4 +13,15 @@ abstract contract Helpers is DSMath, Basic { */ ISwapRouter constant swapRouter = ISwapRouter(0xE592427A0AEce92De3Edee1F18E0157C05861564); + + struct BuyInfo { + 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 expectedAmt; //The amount that is expected to be returned after the swap + uint256 buyAmt; //amount of token to be bought + uint256 getId; //Id to get buyAmt + uint256 setId; //Id to store sellAmt + } } \ 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 9ae367d3..0d6e5a5f 100644 --- a/contracts/optimism/connectors/uniswap/v3_swap/main.sol +++ b/contracts/optimism/connectors/uniswap/v3_swap/main.sol @@ -13,38 +13,30 @@ 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 buyAddr token to be bought - * @param sellAddr token to be sold - * @param fee pool fees for buyAddr-sellAddr token pair - * @param buyAmt amount of token to be bought - * @param getId Id to get buyAmt - * @param setId Id to store sellAmt + * @param buyData Data input for the buy action */ function buy( - address buyAddr, - address sellAddr, - uint24 fee, - uint256 buyAmt, - uint256 getId, - uint256 setId + BuyInfo memory buyData ) external payable returns (string memory _eventName, bytes memory _eventParam) { - uint256 _buyAmt = getUint(getId, buyAmt); + uint256 _buyAmt = getUint(buyData.getId, buyData.buyAmt); (TokenInterface _buyAddr, TokenInterface _sellAddr) = changeEthAddress( - buyAddr, - sellAddr + buyData.buyAddr, + buyData.sellAddr ); - - // uint _slippageAmt = convert18ToDec(_sellAddr.decimals(), - // wmul(unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt)) - // ); + + { + uint _slippageAmt = convert18ToDec(_sellAddr.decimals(), + wmul(buyData.unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt)) + ); + require(_slippageAmt >= buyData.expectedAmt, "Too much slippage"); + } bool isEth = address(_sellAddr) == wethAddr; convertEthToWeth(isEth, _sellAddr, uint256(-1)); @@ -53,13 +45,13 @@ abstract contract UniswapResolver is Helpers, Events { { params = ISwapRouter.ExactOutputSingleParams({ - tokenIn: sellAddr, - tokenOut: buyAddr, - fee: fee, + tokenIn: buyData.sellAddr, + tokenOut: buyData.buyAddr, + fee: buyData.fee, recipient: address(this), deadline: block.timestamp + 1, amountOut: _buyAmt, - amountInMaximum: uint256(-1), + amountInMaximum: buyData.expectedAmt, sqrtPriceLimitX96: 0 }); } @@ -69,16 +61,16 @@ abstract contract UniswapResolver is Helpers, Events { isEth = address(_buyAddr) == wethAddr; convertWethToEth(isEth, _buyAddr, _buyAmt); - setUint(setId, _sellAmt); + setUint(buyData.setId, _sellAmt); _eventName = "LogBuy(address,address,uint256,uint256,uint256,uint256)"; _eventParam = abi.encode( - buyAddr, - sellAddr, + buyData.buyAddr, + buyData.sellAddr, _buyAmt, _sellAmt, - getId, - setId + buyData.getId, + buyData.setId ); } diff --git a/contracts/polygon/connectors/uniswap/v3_swap/helpers.sol b/contracts/polygon/connectors/uniswap/v3_swap/helpers.sol index c7527145..5794ae22 100644 --- a/contracts/polygon/connectors/uniswap/v3_swap/helpers.sol +++ b/contracts/polygon/connectors/uniswap/v3_swap/helpers.sol @@ -13,4 +13,15 @@ abstract contract Helpers is DSMath, Basic { */ ISwapRouter constant swapRouter = ISwapRouter(0xE592427A0AEce92De3Edee1F18E0157C05861564); + + struct BuyInfo { + 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 expectedAmt; //The amount that is expected to be returned after the swap + uint256 buyAmt; //amount of token to be bought + uint256 getId; //Id to get buyAmt + uint256 setId; //Id to store sellAmt + } } \ 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 2b9efdff..5c128bb9 100644 --- a/contracts/polygon/connectors/uniswap/v3_swap/main.sol +++ b/contracts/polygon/connectors/uniswap/v3_swap/main.sol @@ -16,30 +16,25 @@ abstract contract UniswapResolver is Helpers, Events { /** * @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 buyAmt amount of token to be bought - * @param getId Id to get buyAmt - * @param setId Id to store sellAmt + * @param buyData Data input for the buy action */ function buy( - address buyAddr, - address sellAddr, - uint24 fee, - uint256 buyAmt, - uint256 getId, - uint256 setId + BuyInfo memory buyData ) external payable returns (string memory _eventName, bytes memory _eventParam) { - uint256 _buyAmt = getUint(getId, buyAmt); + uint256 _buyAmt = getUint(buyData.getId, buyData.buyAmt); ( TokenInterface _buyAddr, TokenInterface _sellAddr - ) = changeMaticAddress(buyAddr, sellAddr); + ) = changeMaticAddress(buyData.buyAddr, buyData.sellAddr); + + uint _slippageAmt = convert18ToDec(_sellAddr.decimals(), + wmul(buyData.unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt)) + ); + require(_slippageAmt >= buyData.expectedAmt, "Too much slippage"); bool isMatic = address(_sellAddr) == wmaticAddr; convertMaticToWmatic(isMatic, _sellAddr, uint256(-1)); @@ -48,13 +43,13 @@ abstract contract UniswapResolver is Helpers, Events { { params = ISwapRouter.ExactOutputSingleParams({ - tokenIn: sellAddr, - tokenOut: buyAddr, - fee: fee, + tokenIn: buyData.sellAddr, + tokenOut: buyData.buyAddr, + fee: buyData.fee, recipient: address(this), deadline: block.timestamp + 1, amountOut: _buyAmt, - amountInMaximum: uint256(-1), + amountInMaximum: buyData.expectedAmt, sqrtPriceLimitX96: 0 }); } @@ -64,16 +59,16 @@ abstract contract UniswapResolver is Helpers, Events { isMatic = address(_buyAddr) == wmaticAddr; convertWmaticToMatic(isMatic, _buyAddr, _buyAmt); - setUint(setId, _sellAmt); + setUint(buyData.setId, _sellAmt); _eventName = "LogBuy(address,address,uint256,uint256,uint256,uint256)"; _eventParam = abi.encode( - buyAddr, - sellAddr, + buyData.buyAddr, + buyData.sellAddr, _buyAmt, _sellAmt, - getId, - setId + buyData.getId, + buyData.setId ); }