From 1a8c6aea4f04fae8872975df4b21283eab8e36f6 Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Wed, 3 Aug 2022 00:25:36 +0530 Subject: [PATCH] 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);