From 7f5f5f185b8ff067e7b7756d561bf37a553a2839 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Fri, 29 Apr 2022 00:30:32 +0530 Subject: [PATCH] uniswapv3-swap connector-polygon --- .../polygon/connectors/uniswap/v3/events.sol | 18 +++ .../polygon/connectors/uniswap/v3/main.sol | 118 ++++++++++++++++++ 2 files changed, 136 insertions(+) diff --git a/contracts/polygon/connectors/uniswap/v3/events.sol b/contracts/polygon/connectors/uniswap/v3/events.sol index f3c9b3b8..adb88e4e 100644 --- a/contracts/polygon/connectors/uniswap/v3/events.sol +++ b/contracts/polygon/connectors/uniswap/v3/events.sol @@ -40,4 +40,22 @@ contract Events { ); event LogBurnPosition(uint256 tokenId); + + event LogBuy( + address indexed buyToken, + address indexed sellToken, + uint256 buyAmt, + uint256 sellAmt, + uint256 getId, + uint256 setId + ); + + event LogSell( + address indexed buyToken, + address indexed sellToken, + uint256 buyAmt, + uint256 sellAmt, + uint256 getId, + uint256 setId + ); } diff --git a/contracts/polygon/connectors/uniswap/v3/main.sol b/contracts/polygon/connectors/uniswap/v3/main.sol index f249da57..f85ea8d5 100644 --- a/contracts/polygon/connectors/uniswap/v3/main.sol +++ b/contracts/polygon/connectors/uniswap/v3/main.sol @@ -10,6 +10,7 @@ pragma abicoder v2; import {TokenInterface} from "../../../common/interfaces.sol"; import {Helpers} from "./helpers.sol"; import {Events} from "./events.sol"; +import "./interface.sol"; abstract contract UniswapResolver is Helpers, Events { @@ -241,6 +242,123 @@ abstract contract UniswapResolver is Helpers, Events { _eventName = "LogBurnPosition(uint256)"; _eventParam = abi.encode(tokenId); } + + /** + * @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 + */ + function buy( + address buyAddr, + address sellAddr, + uint24 fee, + uint256 buyAmt, + uint256 unitAmt, + uint256 getId, + uint256 setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + uint _buyAmt = getUint(getId, buyAmt); + (TokenInterface _buyAddr, TokenInterface _sellAddr) = changeMaticAddress(buyAddr, sellAddr); + + // uint _slippageAmt = convert18ToDec(_sellAddr.decimals(), + // wmul(unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt)) + // ); + + bool isMatic = address(_sellAddr) == wmaticAddr; + convertMaticToWmatic(isMatic, _sellAddr, uint256(-1)); + approve(_sellAddr, address(swapRouter), uint256(-1)); + ISwapRouter.ExactOutputSingleParams memory params; + + { + params = ISwapRouter.ExactOutputSingleParams({ + tokenIn: sellAddr, + tokenOut: buyAddr, + fee: fee, + recipient: address(this), + deadline: block.timestamp + 1, + amountOut: _buyAmt, + amountInMaximum: uint256(-1), + sqrtPriceLimitX96: 0 + }); + } + + uint _sellAmt = swapRouter.exactOutputSingle(params); + + isMatic = address(_buyAddr) == wmaticAddr; + convertWmaticToMatic(isMatic, _buyAddr, _buyAmt); + + setUint(setId, _sellAmt); + + _eventName = "LogBuy(address,address,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode(buyAddr, sellAddr, _buyAmt, _sellAmt, getId, setId); + } + + /** + * @dev Sell Function + * @notice Swap token(sellAddr) with token(buyAddr), sell token to get maximum amount of buy token + * @param buyAddr token to be bought + * @param sellAddr token to be sold + * @param fee pool fees for buyAddr-sellAddr token pair + * @param sellAmt amount of token to be sold + * @param getId Id to get sellAmount + * @param setId Id to store buyAmount + */ + function sell( + address buyAddr, + address sellAddr, + uint24 fee, + uint256 sellAmt, + uint256 unitAmt, + uint256 getId, + uint256 setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + uint _sellAmt = getUint(getId, sellAmt); + (TokenInterface _buyAddr, TokenInterface _sellAddr) = changeMaticAddress(buyAddr, sellAddr); + + if (_sellAmt == uint(-1)) { + _sellAmt = sellAddr == maticAddr ? + address(this).balance : + _sellAddr.balanceOf(address(this)); + } + + // uint _slippageAmt = convert18ToDec(_buyAddr.decimals(), + // wmul(unitAmt, convertTo18(_sellAddr.decimals(), _sellAmt)) + // ); + // require(_slippageAmt <= _expectedAmt, "Too much slippage"); + + bool isMatic = address(_sellAddr) == wmaticAddr; + convertMaticToWmatic(isMatic, _sellAddr, _sellAmt); + approve(_sellAddr, address(swapRouter), _sellAmt); + ISwapRouter.ExactInputSingleParams memory params; + + { + params = ISwapRouter.ExactInputSingleParams({ + tokenIn: sellAddr, + tokenOut: buyAddr, + fee: fee, + recipient: address(this), + deadline: block.timestamp + 1, + amountIn: _sellAmt, + amountOutMinimum: 0, + sqrtPriceLimitX96: 0 + }); + } + + uint _buyAmt = swapRouter.exactInputSingle(params); + + isMatic = address(_buyAddr) == wmaticAddr; + convertWmaticToMatic(isMatic, _buyAddr, _buyAmt); + + setUint(setId, _buyAmt); + + _eventName = "LogSell(address,address,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode(buyAddr, sellAddr, _buyAmt, _sellAmt, getId, setId); + } } contract ConnectV2UniswapV3Polygon is UniswapResolver {