From 219ec18ae7c0781d4ec2bc8b77b0b3812d93932e Mon Sep 17 00:00:00 2001 From: Shriya Tyagi <47134275+shriyatyagii@users.noreply.github.com> Date: Fri, 4 Mar 2022 02:51:30 +0530 Subject: [PATCH] Limit order connector added --- .../polygon/connectors/limitOrder/events.sol | 24 ++++ .../polygon/connectors/limitOrder/helpers.sol | 86 +++++++++++++++ .../connectors/limitOrder/interface.sol | 43 ++++++++ .../polygon/connectors/limitOrder/main.sol | 103 ++++++++++++++++++ 4 files changed, 256 insertions(+) create mode 100644 contracts/polygon/connectors/limitOrder/events.sol create mode 100644 contracts/polygon/connectors/limitOrder/helpers.sol create mode 100644 contracts/polygon/connectors/limitOrder/interface.sol create mode 100644 contracts/polygon/connectors/limitOrder/main.sol diff --git a/contracts/polygon/connectors/limitOrder/events.sol b/contracts/polygon/connectors/limitOrder/events.sol new file mode 100644 index 00000000..f7e1767d --- /dev/null +++ b/contracts/polygon/connectors/limitOrder/events.sol @@ -0,0 +1,24 @@ +pragma solidity ^0.8.1; +// SPDX-License-Identifier: MIT + +contract Events { + event LogCreate( + uint256 indexed tokenId, + uint256 liquidity, + uint256 amount, + int24 tickLower, + int24 tickUpper + ); + + event LogWithdrawMid( + uint256 indexed tokenId, + uint256 liquidity, + uint256 amountA, + uint256 amountB + ); + + event LogWithdrawFull( + uint256 indexed tokenId, + uint256 liquidity + ); +} diff --git a/contracts/polygon/connectors/limitOrder/helpers.sol b/contracts/polygon/connectors/limitOrder/helpers.sol new file mode 100644 index 00000000..a4f38188 --- /dev/null +++ b/contracts/polygon/connectors/limitOrder/helpers.sol @@ -0,0 +1,86 @@ +pragma solidity ^0.8.1; +// SPDX-License-Identifier: MIT + +import { Basic } from "../../common/basic.sol"; +import "./interface.sol"; + +contract Helpers is Basic { + + UniLimitOrder limitCon_ = UniLimitOrder(0xfC428E6535dC5Fee30fb57cFc93EBB1D92fdCf6e); + + function sortTokenAddress(address _token0, address _token1) + internal + pure + returns (address token0, address token1) + { + if (_token0 > _token1) { + (token0, token1) = (_token1, _token0); + } else { + (token0, token1) = (_token0, _token1); + } + } + + struct MintParams { + address token0; + address token1; + uint24 fee; + int24 tickLower; + int24 tickUpper; + uint256 amount; + bool token0to1; + } + + /** + * @dev Mint function which interact with Uniswap v3 + */ + function _createPosition (MintParams memory params_) + internal + returns ( + uint256 tokenId_, + uint128 liquidity_, + uint256 mintAmount_ + ) + { + uint256 amountSend_; + + (TokenInterface token0_, TokenInterface token1_) = changeMaticAddress( + params_.token0, + params_.token1 + ); + + if(params_.token0to1){ + amountSend_ = params_.amount == type(uint128).max ? getTokenBal(TokenInterface(params_.token0)) : params_.amount; + convertMaticToWmatic(address(token0_) == wmaticAddr, token0_, amountSend_); + approve(TokenInterface(token0_), address(limitCon_), amountSend_); + } else { + amountSend_ = params_.amount == type(uint128).max ? getTokenBal(TokenInterface(params_.token1)) : params_.amount; + convertMaticToWmatic(address(token1_) == wmaticAddr, token1_, amountSend_); + approve(TokenInterface(token1_), address(limitCon_), amountSend_); + } + + { + (address token0, ) = sortTokenAddress( + address(token0_), + address(token1_) + ); + + if (token0 != address(token0_)) { + (token0_, token1_) = (token1_, token0_); + } + } + + UniLimitOrder.MintParams memory parameter = + UniLimitOrder.MintParams( + address(token0_), + address(token1_), + params_.fee, + params_.tickLower, + params_.tickUpper, + amountSend_, + params_.token0to1 + ); + + (tokenId_, liquidity_, mintAmount_) = limitCon_.createPosition(parameter); + + } +} \ No newline at end of file diff --git a/contracts/polygon/connectors/limitOrder/interface.sol b/contracts/polygon/connectors/limitOrder/interface.sol new file mode 100644 index 00000000..402961f2 --- /dev/null +++ b/contracts/polygon/connectors/limitOrder/interface.sol @@ -0,0 +1,43 @@ +pragma solidity ^0.8.1; +// SPDX-License-Identifier: MIT + +interface UniLimitOrder { + + function NftToOwner(uint256) external view returns (address); + function token0to1(uint256) external view returns (bool); + + struct MintParams { + address token0; + address token1; + uint24 fee; + int24 tickLower; + int24 tickUpper; + uint256 amount; + bool token0To1; + } + + function createPosition( + MintParams memory params_ + ) external + returns ( + uint256 tokenId_, + uint128 liquidity_, + uint256 mintAmount_ + ); + + + function closeMidPosition( + uint256 tokenId_, + uint256 amount0Min_, + uint256 amount1Min_ + ) + external + returns (uint128 liquidity_, uint256 amount0_, uint256 amount1_); + + function closeFullPosition( + uint256 tokenId_ + ) + external + returns (uint128 liquidity_); + +} \ No newline at end of file diff --git a/contracts/polygon/connectors/limitOrder/main.sol b/contracts/polygon/connectors/limitOrder/main.sol new file mode 100644 index 00000000..2ca96ab5 --- /dev/null +++ b/contracts/polygon/connectors/limitOrder/main.sol @@ -0,0 +1,103 @@ +pragma solidity ^0.8.1; +// SPDX-License-Identifier: MIT + +import {Helpers} from "./helpers.sol"; +import {UniLimitOrder} from "./interface.sol"; +import {TokenInterface} from "../../common/interfaces.sol"; + +/** + * @title LimitOrderConnector. + * @dev Connector for Limit Order Swap on Uni V3. + */ +contract LimitOrderConnector is Helpers { + + function createPosition( + address token0_, + address token1_, + uint24 fee_, + int24 tickLower_, + int24 tickUpper_, + uint256 amount_, + bool token0to1_, + uint256 getId_, + uint256 setId_ + ) + external + payable + returns (string memory eventName_, bytes memory eventParam_) + { + + MintParams memory params_ = MintParams( + token0_, + token1_, + fee_, + tickLower_, + tickUpper_, + amount_, + token0to1_ + ); + + params_.amount = getUint(getId_, amount_); + + ( + uint256 tokenId_, + uint256 liquidity_, + uint256 minAmount_ + ) = _createPosition(params_); + + setUint(setId_, liquidity_); + + eventName_ = "LogCreate(uint256,uint256,uint256,int24,int24)"; + _eventParam = abi.encode( + tokenId_, + liquidity_, + minAmount_, + params_.tickLower, + params_.tickUpper + ); + } + + + function withdrawMid( + uint256 tokenId_, + uint256 amountAMin_, + uint256 amountBMin_, + uint256[] calldata setIds_ + ) + external + payable + returns (string memory eventName_, bytes memory eventParam_) + { + + (uint128 liquidity_, uint256 amount0, uint256 amount1) = limitCon_.closeMidPosition(tokenId_, amountAMin_, amountBMin_); + + setUint(setIds_[0], amount0); + setUint(setIds_[1], amount1); + + eventName_ = "LogWithdrawMid(uint256,uint256,uint256,uint256)"; + eventParam_ = abi.encode(tokenId_, liquidity_, amount0, amount1); + } + + + function withdrawFull( + uint256 tokenId_, + uint256 setId_ + ) + external + payable + returns (string memory eventName_, bytes memory eventParam_) + { + + (uint256 closeAmount_) = limitCon_.closeFullPosition(tokenId_); + + setUint(setId_, closeAmount_); + + eventName_ = "LogWithdrawFull(uint256,uint256)"; + eventParam_ = abi.encode(tokenId_, closeAmount_); + } + +} + +contract ConnectV2LimitOrder is LimitOrderConnector { + string public constant name = "Limit-Order-Connector"; +} \ No newline at end of file