added connector on opt

This commit is contained in:
bhavik-m 2022-02-27 19:04:29 +05:30
parent e90507f82f
commit 84a39b1582
4 changed files with 168 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,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;
}
}

View File

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

View File

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