added uniswap v3 swap router connector

This commit is contained in:
bhavik-m 2022-01-13 00:03:33 +05:30
parent a7cd775056
commit 442d6fe0bd
4 changed files with 144 additions and 0 deletions

View File

@ -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
);
}

View File

@ -0,0 +1,67 @@
pragma solidity ^0.7.0;
import { TokenInterface } from "../../../common/interfaces.sol";
import { DSMath } from "../../../common/math.sol";
import { Basic } from "../../../common/basic.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
* @param ethAmt - Eth to swap for .value()
*/
function _SwapHelper(
SwapData memory swapData,
uint ethAmt
) internal returns (uint buyAmt) {
TokenInterface buyToken = swapData.buyToken;
(uint _buyDec, uint _sellDec) = getTokensDec(buyToken, swapData.sellToken);
uint _sellAmt18 = convertTo18(_sellDec, swapData._sellAmt);
uint _slippageAmt = convert18ToDec(_buyDec, wmul(swapData.unitAmt, _sellAmt18));
uint initalBal = getTokenBal(buyToken);
// solium-disable-next-line security/no-call-value
(bool success, ) = V3_SWAP_ROUTER_ADDRESS.call{value: ethAmt}(swapData.callData);
if (!success) revert("uniswapV3-swap-failed");
uint finalBal = getTokenBal(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,
uint setId
) internal returns (SwapData memory) {
TokenInterface _sellAddr = swapData.sellToken;
uint ethAmt;
if (address(_sellAddr) == ethAddr) {
ethAmt = swapData._sellAmt;
} else {
approve(TokenInterface(_sellAddr), V3_SWAP_ROUTER_ADDRESS, swapData._sellAmt);
}
swapData._buyAmt = _SwapHelper(swapData, ethAmt);
setUint(setId, swapData._buyAmt);
return swapData;
}
}

View File

@ -0,0 +1,12 @@
pragma solidity ^0.7.0;
import { TokenInterface } from "../../../common/interfaces.sol";
struct SwapData {
TokenInterface sellToken;
TokenInterface buyToken;
uint _sellAmt;
uint _buyAmt;
uint unitAmt;
bytes callData;
}

View File

@ -0,0 +1,53 @@
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,
uint sellAmt,
uint unitAmt,
bytes calldata callData,
uint 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 ConnectV2UniswapV3AutoRouter is AutoRouter {
string public name = "UniswapV3-Auto-Router-v1";
}