From 128fff7488d622f0e042993ba00a0ee0b73b53f2 Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Tue, 2 Aug 2022 23:25:37 +0530 Subject: [PATCH 1/3] fixed isEth logic Signed-off-by: pradyuman-verma --- contracts/arbitrum/connectors/uniswap/v3_swap/helpers.sol | 4 ++-- contracts/mainnet/connectors/uniswap/v3_swap/helpers.sol | 4 ++-- contracts/optimism/connectors/uniswap/v3_swap/helpers.sol | 4 ++-- contracts/polygon/connectors/uniswap/v3_swap/helpers.sol | 4 ++-- contracts/polygon/connectors/uniswap/v3_swap/main.sol | 2 +- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/contracts/arbitrum/connectors/uniswap/v3_swap/helpers.sol b/contracts/arbitrum/connectors/uniswap/v3_swap/helpers.sol index fc4ead42..1a7a1366 100644 --- a/contracts/arbitrum/connectors/uniswap/v3_swap/helpers.sol +++ b/contracts/arbitrum/connectors/uniswap/v3_swap/helpers.sol @@ -53,7 +53,7 @@ abstract contract Helpers is DSMath, Basic { _sellAddr.decimals(), wmul(buyData.unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt)) ); - bool isEth = address(_sellAddr) == wethAddr; + bool isEth = address(_sellAddr) == ethAddr; convertEthToWeth(isEth, _sellAddr, _slippageAmt); approve(_sellAddr, address(swapRouter), _slippageAmt); @@ -115,7 +115,7 @@ abstract contract Helpers is DSMath, Basic { wmul(sellData.unitAmt, convertTo18(_sellAddr.decimals(), _sellAmt)) ); - bool isEth = address(_sellAddr) == wethAddr; + bool isEth = address(_sellAddr) == ethAddr; convertEthToWeth(isEth, _sellAddr, _sellAmt); approve(_sellAddr, address(swapRouter), _sellAmt); ExactInputSingleParams memory params = ExactInputSingleParams({ diff --git a/contracts/mainnet/connectors/uniswap/v3_swap/helpers.sol b/contracts/mainnet/connectors/uniswap/v3_swap/helpers.sol index 27e7cb88..a949c8fa 100644 --- a/contracts/mainnet/connectors/uniswap/v3_swap/helpers.sol +++ b/contracts/mainnet/connectors/uniswap/v3_swap/helpers.sol @@ -53,7 +53,7 @@ abstract contract Helpers is DSMath, Basic { _sellAddr.decimals(), wmul(buyData.unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt)) ); - bool isEth = address(_sellAddr) == wethAddr; + bool isEth = address(_sellAddr) == ethAddr; convertEthToWeth(isEth, _sellAddr, _slippageAmt); approve(_sellAddr, address(swapRouter), _slippageAmt); @@ -114,7 +114,7 @@ abstract contract Helpers is DSMath, Basic { wmul(sellData.unitAmt, convertTo18(_sellAddr.decimals(), _sellAmt)) ); - bool isEth = address(_sellAddr) == wethAddr; + bool isEth = address(_sellAddr) == ethAddr; convertEthToWeth(isEth, _sellAddr, _sellAmt); approve(_sellAddr, address(swapRouter), _sellAmt); ExactInputSingleParams memory params = ExactInputSingleParams({ diff --git a/contracts/optimism/connectors/uniswap/v3_swap/helpers.sol b/contracts/optimism/connectors/uniswap/v3_swap/helpers.sol index 622a338c..a6a27230 100644 --- a/contracts/optimism/connectors/uniswap/v3_swap/helpers.sol +++ b/contracts/optimism/connectors/uniswap/v3_swap/helpers.sol @@ -53,7 +53,7 @@ abstract contract Helpers is DSMath, Basic { _sellAddr.decimals(), wmul(buyData.unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt)) ); - bool isEth = address(_sellAddr) == wethAddr; + bool isEth = address(_sellAddr) == ethAddr; convertEthToWeth(isEth, _sellAddr, _slippageAmt); approve(_sellAddr, address(swapRouter), _slippageAmt); @@ -115,7 +115,7 @@ abstract contract Helpers is DSMath, Basic { wmul(sellData.unitAmt, convertTo18(_sellAddr.decimals(), _sellAmt)) ); - bool isEth = address(_sellAddr) == wethAddr; + bool isEth = address(_sellAddr) == ethAddr; convertEthToWeth(isEth, _sellAddr, _sellAmt); approve(_sellAddr, address(swapRouter), _sellAmt); ExactInputSingleParams memory params = ExactInputSingleParams({ diff --git a/contracts/polygon/connectors/uniswap/v3_swap/helpers.sol b/contracts/polygon/connectors/uniswap/v3_swap/helpers.sol index 30911e8e..f2535230 100644 --- a/contracts/polygon/connectors/uniswap/v3_swap/helpers.sol +++ b/contracts/polygon/connectors/uniswap/v3_swap/helpers.sol @@ -53,7 +53,7 @@ abstract contract Helpers is DSMath, Basic { wmul(buyData.unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt)) ); - bool isMatic = address(_sellAddr) == wmaticAddr; + bool isMatic = address(_sellAddr) == maticAddr; convertMaticToWmatic(isMatic, _sellAddr, _slippageAmt); approve(_sellAddr, address(swapRouter), _slippageAmt); ExactOutputSingleParams memory params = ExactOutputSingleParams({ @@ -114,7 +114,7 @@ abstract contract Helpers is DSMath, Basic { wmul(sellData.unitAmt, convertTo18(_sellAddr.decimals(), _sellAmt)) ); - bool isMatic = address(_sellAddr) == wmaticAddr; + bool isMatic = address(_sellAddr) == maticAddr; convertMaticToWmatic(isMatic, _sellAddr, _sellAmt); approve(_sellAddr, address(swapRouter), _sellAmt); ExactInputSingleParams memory params = ExactInputSingleParams({ diff --git a/contracts/polygon/connectors/uniswap/v3_swap/main.sol b/contracts/polygon/connectors/uniswap/v3_swap/main.sol index 5d88bfa5..9a0809f8 100644 --- a/contracts/polygon/connectors/uniswap/v3_swap/main.sol +++ b/contracts/polygon/connectors/uniswap/v3_swap/main.sol @@ -90,4 +90,4 @@ abstract contract UniswapResolver is Helpers, Events { contract ConnectV2UniswapV3SwapPolygon is UniswapResolver { string public constant name = "UniswapV3-Swap-v1"; -} \ No newline at end of file +} From 3b280ee38e0d8a5778bb310ad571b74c76b78b32 Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Tue, 2 Aug 2022 23:46:52 +0530 Subject: [PATCH 2/3] fixes Signed-off-by: pradyuman-verma --- .../arbitrum/connectors/uniswap/v3_swap/helpers.sol | 12 ++++++------ .../mainnet/connectors/uniswap/v3_swap/helpers.sol | 10 +++++----- .../optimism/connectors/uniswap/v3_swap/helpers.sol | 12 ++++++------ .../polygon/connectors/uniswap/v3_swap/helpers.sol | 10 +++++----- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/contracts/arbitrum/connectors/uniswap/v3_swap/helpers.sol b/contracts/arbitrum/connectors/uniswap/v3_swap/helpers.sol index 1a7a1366..cd1ec99a 100644 --- a/contracts/arbitrum/connectors/uniswap/v3_swap/helpers.sol +++ b/contracts/arbitrum/connectors/uniswap/v3_swap/helpers.sol @@ -53,7 +53,7 @@ abstract contract Helpers is DSMath, Basic { _sellAddr.decimals(), wmul(buyData.unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt)) ); - bool isEth = address(_sellAddr) == ethAddr; + bool isEth = address(buyData.sellAddr) == ethAddr; convertEthToWeth(isEth, _sellAddr, _slippageAmt); approve(_sellAddr, address(swapRouter), _slippageAmt); @@ -70,8 +70,8 @@ abstract contract Helpers is DSMath, Basic { uint256 _sellAmt = swapRouter.exactOutputSingle(params); require(_slippageAmt >= _sellAmt, "Too much slippage"); - isEth = address(_buyAddr) == wethAddr; - convertWethToEth(isEth, _buyAddr, _buyAmt); + isEth = address(buyData.buyAddr) == wethAddr; + convertEthToWeth(isEth, _buyAddr, _slippageAmt); setUint(setId, _sellAmt); @@ -115,7 +115,7 @@ abstract contract Helpers is DSMath, Basic { wmul(sellData.unitAmt, convertTo18(_sellAddr.decimals(), _sellAmt)) ); - bool isEth = address(_sellAddr) == ethAddr; + bool isEth = address(sellData.sellAddr) == ethAddr; convertEthToWeth(isEth, _sellAddr, _sellAmt); approve(_sellAddr, address(swapRouter), _sellAmt); ExactInputSingleParams memory params = ExactInputSingleParams({ @@ -131,8 +131,8 @@ abstract contract Helpers is DSMath, Basic { uint256 _buyAmt = swapRouter.exactInputSingle(params); require(_slippageAmt <= _buyAmt, "Too much slippage"); - isEth = address(_buyAddr) == wethAddr; - convertWethToEth(isEth, _buyAddr, _buyAmt); + isEth = address(sellData.buyAddr) == wethAddr; + convertEthToWeth(isEth, _buyAddr, _sellAmt); setUint(setId, _buyAmt); diff --git a/contracts/mainnet/connectors/uniswap/v3_swap/helpers.sol b/contracts/mainnet/connectors/uniswap/v3_swap/helpers.sol index a949c8fa..419466ad 100644 --- a/contracts/mainnet/connectors/uniswap/v3_swap/helpers.sol +++ b/contracts/mainnet/connectors/uniswap/v3_swap/helpers.sol @@ -53,7 +53,7 @@ abstract contract Helpers is DSMath, Basic { _sellAddr.decimals(), wmul(buyData.unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt)) ); - bool isEth = address(_sellAddr) == ethAddr; + bool isEth = address(buyData.sellAddr) == ethAddr; convertEthToWeth(isEth, _sellAddr, _slippageAmt); approve(_sellAddr, address(swapRouter), _slippageAmt); @@ -69,8 +69,8 @@ abstract contract Helpers is DSMath, Basic { uint256 _sellAmt = swapRouter.exactOutputSingle(params); require(_slippageAmt >= _sellAmt, "Too much slippage"); - isEth = address(_buyAddr) == wethAddr; - convertWethToEth(isEth, _buyAddr, _buyAmt); + isEth = address(buyData.buyAddr) == wethAddr; + convertEthToWeth(isEth, _buyAddr, _slippageAmt); setUint(setId, _sellAmt); @@ -114,7 +114,7 @@ abstract contract Helpers is DSMath, Basic { wmul(sellData.unitAmt, convertTo18(_sellAddr.decimals(), _sellAmt)) ); - bool isEth = address(_sellAddr) == ethAddr; + bool isEth = address(sellData.sellAddr) == ethAddr; convertEthToWeth(isEth, _sellAddr, _sellAmt); approve(_sellAddr, address(swapRouter), _sellAmt); ExactInputSingleParams memory params = ExactInputSingleParams({ @@ -129,7 +129,7 @@ abstract contract Helpers is DSMath, Basic { uint256 _buyAmt = swapRouter.exactInputSingle(params); require(_slippageAmt <= _buyAmt, "Too much slippage"); - isEth = address(_buyAddr) == wethAddr; + isEth = address(sellData.buyAddr) == wethAddr; convertWethToEth(isEth, _buyAddr, _buyAmt); setUint(setId, _buyAmt); diff --git a/contracts/optimism/connectors/uniswap/v3_swap/helpers.sol b/contracts/optimism/connectors/uniswap/v3_swap/helpers.sol index a6a27230..9c6cac04 100644 --- a/contracts/optimism/connectors/uniswap/v3_swap/helpers.sol +++ b/contracts/optimism/connectors/uniswap/v3_swap/helpers.sol @@ -53,7 +53,7 @@ abstract contract Helpers is DSMath, Basic { _sellAddr.decimals(), wmul(buyData.unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt)) ); - bool isEth = address(_sellAddr) == ethAddr; + bool isEth = address(buyData.sellAddr) == ethAddr; convertEthToWeth(isEth, _sellAddr, _slippageAmt); approve(_sellAddr, address(swapRouter), _slippageAmt); @@ -70,8 +70,8 @@ abstract contract Helpers is DSMath, Basic { uint256 _sellAmt = swapRouter.exactOutputSingle(params); require(_slippageAmt >= _sellAmt, "Too much slippage"); - isEth = address(_buyAddr) == wethAddr; - convertWethToEth(isEth, _buyAddr, _buyAmt); + isEth = address(buyData.buyAddr) == ethAddr; + convertEthToWeth(isEth, _buyAddr, _slippageAmt); setUint(setId, _sellAmt); @@ -115,7 +115,7 @@ abstract contract Helpers is DSMath, Basic { wmul(sellData.unitAmt, convertTo18(_sellAddr.decimals(), _sellAmt)) ); - bool isEth = address(_sellAddr) == ethAddr; + bool isEth = address(sellData.sellAddr) == ethAddr; convertEthToWeth(isEth, _sellAddr, _sellAmt); approve(_sellAddr, address(swapRouter), _sellAmt); ExactInputSingleParams memory params = ExactInputSingleParams({ @@ -131,8 +131,8 @@ abstract contract Helpers is DSMath, Basic { uint256 _buyAmt = swapRouter.exactInputSingle(params); require(_slippageAmt <= _buyAmt, "Too much slippage"); - isEth = address(_buyAddr) == wethAddr; - convertWethToEth(isEth, _buyAddr, _buyAmt); + isEth = address(sellData.buyAddr) == ethAddr; + convertEthToWeth(isEth, _buyAddr, _sellAmt); setUint(setId, _buyAmt); diff --git a/contracts/polygon/connectors/uniswap/v3_swap/helpers.sol b/contracts/polygon/connectors/uniswap/v3_swap/helpers.sol index f2535230..1f160e17 100644 --- a/contracts/polygon/connectors/uniswap/v3_swap/helpers.sol +++ b/contracts/polygon/connectors/uniswap/v3_swap/helpers.sol @@ -53,7 +53,7 @@ abstract contract Helpers is DSMath, Basic { wmul(buyData.unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt)) ); - bool isMatic = address(_sellAddr) == maticAddr; + bool isMatic = address(buyData.sellAddr) == maticAddr; convertMaticToWmatic(isMatic, _sellAddr, _slippageAmt); approve(_sellAddr, address(swapRouter), _slippageAmt); ExactOutputSingleParams memory params = ExactOutputSingleParams({ @@ -69,8 +69,8 @@ abstract contract Helpers is DSMath, Basic { uint256 _sellAmt = swapRouter.exactOutputSingle(params); require(_slippageAmt >= _sellAmt, "Too much slippage"); - isMatic = address(_buyAddr) == wmaticAddr; - convertWmaticToMatic(isMatic, _buyAddr, _buyAmt); + isMatic = address(buyData.buyAddr) == maticAddr; + convertMaticToWmatic(isMatic, _buyAddr, _slippageAmt); setUint(setId, _sellAmt); @@ -114,7 +114,7 @@ abstract contract Helpers is DSMath, Basic { wmul(sellData.unitAmt, convertTo18(_sellAddr.decimals(), _sellAmt)) ); - bool isMatic = address(_sellAddr) == maticAddr; + bool isMatic = address(buyData.sellAddr) == maticAddr; convertMaticToWmatic(isMatic, _sellAddr, _sellAmt); approve(_sellAddr, address(swapRouter), _sellAmt); ExactInputSingleParams memory params = ExactInputSingleParams({ @@ -130,7 +130,7 @@ abstract contract Helpers is DSMath, Basic { uint256 _buyAmt = swapRouter.exactInputSingle(params); require(_slippageAmt <= _buyAmt, "Too much slippage"); - isMatic = address(_buyAddr) == wmaticAddr; + isMatic = address(sellData.buyAddr) == wmaticAddr; convertWmaticToMatic(isMatic, _buyAddr, _buyAmt); setUint(setId, _buyAmt); From 1a8c6aea4f04fae8872975df4b21283eab8e36f6 Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Wed, 3 Aug 2022 00:25:36 +0530 Subject: [PATCH 3/3] fixes + rest amount conversion Signed-off-by: pradyuman-verma --- .../arbitrum/connectors/uniswap/v3_swap/helpers.sol | 12 ++++++++---- .../mainnet/connectors/uniswap/v3_swap/helpers.sol | 10 +++++++--- .../optimism/connectors/uniswap/v3_swap/helpers.sol | 8 ++++++-- .../polygon/connectors/uniswap/v3_swap/helpers.sol | 11 ++++++++--- 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/contracts/arbitrum/connectors/uniswap/v3_swap/helpers.sol b/contracts/arbitrum/connectors/uniswap/v3_swap/helpers.sol index cd1ec99a..90485806 100644 --- a/contracts/arbitrum/connectors/uniswap/v3_swap/helpers.sol +++ b/contracts/arbitrum/connectors/uniswap/v3_swap/helpers.sol @@ -70,8 +70,12 @@ abstract contract Helpers is DSMath, Basic { uint256 _sellAmt = swapRouter.exactOutputSingle(params); require(_slippageAmt >= _sellAmt, "Too much slippage"); - isEth = address(buyData.buyAddr) == wethAddr; - convertEthToWeth(isEth, _buyAddr, _slippageAmt); + if (_slippageAmt > _sellAmt) { + convertEthToWeth(isEth, _sellAddr, _slippageAmt - _sellAmt); + approve(_sellAddr, address(swapRouter), 0); + } + isEth = address(buyData.buyAddr) == ethAddr; + convertWethToEth(isEth, _buyAddr, _buyAmt); setUint(setId, _sellAmt); @@ -131,8 +135,8 @@ abstract contract Helpers is DSMath, Basic { uint256 _buyAmt = swapRouter.exactInputSingle(params); require(_slippageAmt <= _buyAmt, "Too much slippage"); - isEth = address(sellData.buyAddr) == wethAddr; - convertEthToWeth(isEth, _buyAddr, _sellAmt); + isEth = address(sellData.buyAddr) == ethAddr; + convertWethToEth(isEth, _buyAddr, _buyAmt); setUint(setId, _buyAmt); diff --git a/contracts/mainnet/connectors/uniswap/v3_swap/helpers.sol b/contracts/mainnet/connectors/uniswap/v3_swap/helpers.sol index 419466ad..27b69132 100644 --- a/contracts/mainnet/connectors/uniswap/v3_swap/helpers.sol +++ b/contracts/mainnet/connectors/uniswap/v3_swap/helpers.sol @@ -69,8 +69,12 @@ abstract contract Helpers is DSMath, Basic { uint256 _sellAmt = swapRouter.exactOutputSingle(params); require(_slippageAmt >= _sellAmt, "Too much slippage"); - isEth = address(buyData.buyAddr) == wethAddr; - convertEthToWeth(isEth, _buyAddr, _slippageAmt); + if (_slippageAmt > _sellAmt) { + convertEthToWeth(isEth, _sellAddr, _slippageAmt - _sellAmt); + approve(_sellAddr, address(swapRouter), 0); + } + isEth = address(buyData.buyAddr) == ethAddr; + convertWethToEth(isEth, _buyAddr, _buyAmt); setUint(setId, _sellAmt); @@ -129,7 +133,7 @@ abstract contract Helpers is DSMath, Basic { uint256 _buyAmt = swapRouter.exactInputSingle(params); require(_slippageAmt <= _buyAmt, "Too much slippage"); - isEth = address(sellData.buyAddr) == wethAddr; + isEth = address(sellData.buyAddr) == ethAddr; convertWethToEth(isEth, _buyAddr, _buyAmt); setUint(setId, _buyAmt); diff --git a/contracts/optimism/connectors/uniswap/v3_swap/helpers.sol b/contracts/optimism/connectors/uniswap/v3_swap/helpers.sol index 9c6cac04..5b8504b3 100644 --- a/contracts/optimism/connectors/uniswap/v3_swap/helpers.sol +++ b/contracts/optimism/connectors/uniswap/v3_swap/helpers.sol @@ -70,8 +70,12 @@ abstract contract Helpers is DSMath, Basic { uint256 _sellAmt = swapRouter.exactOutputSingle(params); require(_slippageAmt >= _sellAmt, "Too much slippage"); + if (_slippageAmt > _sellAmt) { + convertEthToWeth(isEth, _sellAddr, _slippageAmt - _sellAmt); + approve(_sellAddr, address(swapRouter), 0); + } isEth = address(buyData.buyAddr) == ethAddr; - convertEthToWeth(isEth, _buyAddr, _slippageAmt); + convertWethToEth(isEth, _buyAddr, _buyAmt); setUint(setId, _sellAmt); @@ -132,7 +136,7 @@ abstract contract Helpers is DSMath, Basic { require(_slippageAmt <= _buyAmt, "Too much slippage"); isEth = address(sellData.buyAddr) == ethAddr; - convertEthToWeth(isEth, _buyAddr, _sellAmt); + convertWethToEth(isEth, _buyAddr, _buyAmt); setUint(setId, _buyAmt); diff --git a/contracts/polygon/connectors/uniswap/v3_swap/helpers.sol b/contracts/polygon/connectors/uniswap/v3_swap/helpers.sol index 1f160e17..7276a050 100644 --- a/contracts/polygon/connectors/uniswap/v3_swap/helpers.sol +++ b/contracts/polygon/connectors/uniswap/v3_swap/helpers.sol @@ -69,8 +69,13 @@ abstract contract Helpers is DSMath, Basic { uint256 _sellAmt = swapRouter.exactOutputSingle(params); require(_slippageAmt >= _sellAmt, "Too much slippage"); + if (_slippageAmt > _sellAmt) { + convertMaticToWmatic(isMatic, _sellAddr, _slippageAmt - _sellAmt); + approve(_sellAddr, address(swapRouter), 0); + } + isMatic = address(buyData.buyAddr) == maticAddr; - convertMaticToWmatic(isMatic, _buyAddr, _slippageAmt); + convertWmaticToMatic(isMatic, _buyAddr, _buyAmt); setUint(setId, _sellAmt); @@ -114,7 +119,7 @@ abstract contract Helpers is DSMath, Basic { wmul(sellData.unitAmt, convertTo18(_sellAddr.decimals(), _sellAmt)) ); - bool isMatic = address(buyData.sellAddr) == maticAddr; + bool isMatic = address(sellData.sellAddr) == maticAddr; convertMaticToWmatic(isMatic, _sellAddr, _sellAmt); approve(_sellAddr, address(swapRouter), _sellAmt); ExactInputSingleParams memory params = ExactInputSingleParams({ @@ -130,7 +135,7 @@ abstract contract Helpers is DSMath, Basic { uint256 _buyAmt = swapRouter.exactInputSingle(params); require(_slippageAmt <= _buyAmt, "Too much slippage"); - isMatic = address(sellData.buyAddr) == wmaticAddr; + isMatic = address(sellData.buyAddr) == maticAddr; convertWmaticToMatic(isMatic, _buyAddr, _buyAmt); setUint(setId, _buyAmt);