From 84a39b1582c9ffbeb889ab16294e2ecba4877256 Mon Sep 17 00:00:00 2001 From: bhavik-m Date: Sun, 27 Feb 2022 19:04:29 +0530 Subject: [PATCH] added connector on opt --- .../uniswap/v3_auto_router/events.sol | 12 +++ .../uniswap/v3_auto_router/helpers.sol | 80 +++++++++++++++++++ .../uniswap/v3_auto_router/interface.sol | 12 +++ .../uniswap/v3_auto_router/main.sol | 64 +++++++++++++++ 4 files changed, 168 insertions(+) create mode 100644 contracts/optimism/connectors/uniswap/v3_auto_router/events.sol create mode 100644 contracts/optimism/connectors/uniswap/v3_auto_router/helpers.sol create mode 100644 contracts/optimism/connectors/uniswap/v3_auto_router/interface.sol create mode 100644 contracts/optimism/connectors/uniswap/v3_auto_router/main.sol diff --git a/contracts/optimism/connectors/uniswap/v3_auto_router/events.sol b/contracts/optimism/connectors/uniswap/v3_auto_router/events.sol new file mode 100644 index 00000000..12a4fd3c --- /dev/null +++ b/contracts/optimism/connectors/uniswap/v3_auto_router/events.sol @@ -0,0 +1,12 @@ +pragma solidity ^0.7.0; + +contract Events { + event LogSwap( + address indexed buyToken, + address indexed sellToken, + uint256 buyAmt, + uint256 sellAmt, + uint256 getId, + uint256 setId + ); +} diff --git a/contracts/optimism/connectors/uniswap/v3_auto_router/helpers.sol b/contracts/optimism/connectors/uniswap/v3_auto_router/helpers.sol new file mode 100644 index 00000000..3e73bc68 --- /dev/null +++ b/contracts/optimism/connectors/uniswap/v3_auto_router/helpers.sol @@ -0,0 +1,80 @@ +pragma solidity ^0.7.0; + +import { TokenInterface } from "../../../common/interfaces.sol"; +import { DSMath } from "../../../common/math.sol"; +import { Basic } from "../../../common/basic.sol"; +import { SwapData } from "./interface.sol"; + +abstract contract Helpers is DSMath, Basic { + /** + * @dev UniswapV3 Swap Router Address + */ + address internal constant V3_SWAP_ROUTER_ADDRESS = + 0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45; + + /** + * @dev UniswapV3 swapHelper + * @param swapData - Struct defined in interfaces.sol + */ + function _swapHelper(SwapData memory swapData) + internal + returns (uint256 buyAmt) + { + (uint256 _buyDec, uint256 _sellDec) = getTokensDec( + swapData.buyToken, + swapData.sellToken + ); + uint256 _sellAmt18 = convertTo18(_sellDec, swapData._sellAmt); + uint256 _slippageAmt = convert18ToDec( + _buyDec, + wmul(swapData.unitAmt, _sellAmt18) + ); + + uint256 initalBal = getTokenBal(swapData.buyToken); + + // solium-disable-next-line security/no-call-value + (bool success, ) = V3_SWAP_ROUTER_ADDRESS.call(swapData.callData); + if (!success) revert("uniswapV3-swap-failed"); + + uint256 finalBal = getTokenBal(swapData.buyToken); + + buyAmt = sub(finalBal, initalBal); + require(_slippageAmt <= buyAmt, "Too much slippage"); + } + + /** + * @dev Gets the swapping data from auto router sdk + * @param swapData Struct with multiple swap data defined in interfaces.sol + * @param setId Set token amount at this ID in `InstaMemory` Contract. + */ + function _swap(SwapData memory swapData, uint256 setId) + internal + returns (SwapData memory) + { + bool isEthSellToken = address(swapData.sellToken) == ethAddr; + bool isEthBuyToken = address(swapData.buyToken) == ethAddr; + + swapData.sellToken = isEthSellToken + ? TokenInterface(wethAddr) + : swapData.sellToken; + swapData.buyToken = isEthBuyToken + ? TokenInterface(wethAddr) + : swapData.buyToken; + + convertEthToWeth(isEthSellToken, swapData.sellToken, swapData._sellAmt); + + approve( + TokenInterface(swapData.sellToken), + V3_SWAP_ROUTER_ADDRESS, + swapData._sellAmt + ); + + swapData._buyAmt = _swapHelper(swapData); + + convertWethToEth(isEthBuyToken, swapData.buyToken, swapData._buyAmt); + + setUint(setId, swapData._buyAmt); + + return swapData; + } +} diff --git a/contracts/optimism/connectors/uniswap/v3_auto_router/interface.sol b/contracts/optimism/connectors/uniswap/v3_auto_router/interface.sol new file mode 100644 index 00000000..3482cdd3 --- /dev/null +++ b/contracts/optimism/connectors/uniswap/v3_auto_router/interface.sol @@ -0,0 +1,12 @@ +pragma solidity ^0.7.0; + +import { TokenInterface } from "../../../common/interfaces.sol"; + +struct SwapData { + TokenInterface sellToken; + TokenInterface buyToken; + uint256 _sellAmt; + uint256 _buyAmt; + uint256 unitAmt; + bytes callData; +} diff --git a/contracts/optimism/connectors/uniswap/v3_auto_router/main.sol b/contracts/optimism/connectors/uniswap/v3_auto_router/main.sol new file mode 100644 index 00000000..be7b8235 --- /dev/null +++ b/contracts/optimism/connectors/uniswap/v3_auto_router/main.sol @@ -0,0 +1,64 @@ +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; + +/** + * @title UniswapV3_autoRouter. + * @dev DEX. + */ + +// import files from common directory +import { TokenInterface, MemoryInterface } from "../../../common/interfaces.sol"; +import { Stores } from "../../../common/stores.sol"; +import { SwapData } from "./interface.sol"; +import { Helpers } from "./helpers.sol"; +import { Events } from "./events.sol"; + +abstract contract AutoRouter is Helpers, Events { + /** + * @dev Sell ETH/ERC20_Token using uniswap v3 auto router. + * @notice Swap tokens from getting an optimized trade routes + * @param buyAddr The address of the token to buy.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param sellAddr The address of the token to sell.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param sellAmt The amount of the token to sell. + * @param unitAmt The amount of buyAmt/sellAmt with slippage. + * @param callData Data from Uniswap V3 auto router SDK. + * @param setId ID stores the amount of token brought. + */ + function sell( + address buyAddr, + address sellAddr, + uint256 sellAmt, + uint256 unitAmt, + bytes calldata callData, + uint256 setId + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + SwapData memory swapData = SwapData({ + buyToken: TokenInterface(buyAddr), + sellToken: TokenInterface(sellAddr), + unitAmt: unitAmt, + callData: callData, + _sellAmt: sellAmt, + _buyAmt: 0 + }); + + swapData = _swap(swapData, setId); + + _eventName = "LogSwap(address,address,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode( + buyAddr, + sellAddr, + swapData._buyAmt, + swapData._sellAmt, + 0, + setId + ); + } +} + +contract ConnectV2UniswapV3AutoRouterOptimism is AutoRouter { + string public name = "UniswapV3-Auto-Router-v1"; +}