updated functions to take non-struct params

This commit is contained in:
Richa-iitr 2022-05-02 01:57:10 +05:30
parent ce3e50b585
commit 296f7d929c
8 changed files with 726 additions and 480 deletions

View File

@ -33,4 +33,128 @@ abstract contract Helpers is DSMath, Basic {
uint256 getId; //Id to get buyAmt
uint256 setId; //Id to store sellAmt
}
/**
* @dev Buy Function
* @notice Swap token(sellAddr) with token(buyAddr), buy token with minimum sell token
* @param buyData Data input for the buy action
*/
function _buy(
BuyInfo memory buyData
)
internal
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _buyAmt = getUint(buyData.getId, buyData.buyAmt);
ISwapRouter.ExactOutputSingleParams memory params;
(TokenInterface _buyAddr, TokenInterface _sellAddr) = changeEthAddress(
buyData.buyAddr,
buyData.sellAddr
);
uint _slippageAmt = convert18ToDec(_sellAddr.decimals(),
wmul(buyData.unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt))
);
bool isEth = address(_sellAddr) == wethAddr;
convertEthToWeth(isEth, _sellAddr, _slippageAmt);
approve(_sellAddr, address(swapRouter), _slippageAmt);
{
params = ISwapRouter.ExactOutputSingleParams({
tokenIn: buyData.sellAddr,
tokenOut: buyData.buyAddr,
fee: buyData.fee,
recipient: address(this),
deadline: block.timestamp + 1,
amountOut: _buyAmt,
amountInMaximum: _slippageAmt, //require(_sellAmt <= amountInMaximum)
sqrtPriceLimitX96: 0
});
}
uint256 _sellAmt = swapRouter.exactOutputSingle(params);
require(_slippageAmt >= _sellAmt, "Too much slippage");
isEth = address(_buyAddr) == wethAddr;
convertWethToEth(isEth, _buyAddr, _buyAmt);
setUint(buyData.setId, _sellAmt);
_eventName = "LogBuy(address,address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(
buyData.buyAddr,
buyData.sellAddr,
_buyAmt,
_sellAmt,
buyData.getId,
buyData.setId
);
}
/**
* @dev Sell Function
* @notice Swap token(sellAddr) with token(buyAddr), to get max buy tokens
* @param sellData Data input for the sell action
*/
function _sell(
SellInfo memory sellData
)
internal
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _sellAmt = getUint(sellData.getId, sellData.sellAmt);
(TokenInterface _buyAddr, TokenInterface _sellAddr) = changeEthAddress(
sellData.buyAddr,
sellData.sellAddr
);
if (_sellAmt == uint(-1)) {
_sellAmt = sellData.sellAddr == ethAddr
? address(this).balance
: _sellAddr.balanceOf(address(this));
}
uint _slippageAmt = convert18ToDec(_buyAddr.decimals(),
wmul(sellData.unitAmt, convertTo18(_sellAddr.decimals(), _sellAmt))
);
bool isEth = address(_sellAddr) == wethAddr;
convertEthToWeth(isEth, _sellAddr, _sellAmt);
approve(_sellAddr, address(swapRouter), _sellAmt);
ISwapRouter.ExactInputSingleParams memory params;
{
params = ISwapRouter.ExactInputSingleParams({
tokenIn: sellData.sellAddr,
tokenOut: sellData.buyAddr,
fee: sellData.fee,
recipient: address(this),
deadline: block.timestamp + 1,
amountIn: _sellAmt,
amountOutMinimum: _slippageAmt, //require(_buyAmt >= amountOutMinimum)
sqrtPriceLimitX96: 0
});
}
uint256 _buyAmt = swapRouter.exactInputSingle(params);
require(_slippageAmt <= _buyAmt, "Too much slippage");
isEth = address(_buyAddr) == wethAddr;
convertWethToEth(isEth, _buyAddr, _buyAmt);
setUint(sellData.setId, _buyAmt);
_eventName = "LogSell(address,address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(
sellData.buyAddr,
sellData.sellAddr,
_buyAmt,
_sellAmt,
sellData.getId,
sellData.setId
);
}
}

View File

@ -13,129 +13,68 @@ import {Events} from "./events.sol";
import "./interface.sol";
abstract contract UniswapResolver is Helpers, Events {
/**
* @dev Buy Function
* @notice Swap token(sellAddr) with token(buyAddr), buy token with minimum sell token
* @param buyData Data input for the buy action
*/
function buy(
BuyInfo memory buyData
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _buyAmt = getUint(buyData.getId, buyData.buyAmt);
ISwapRouter.ExactOutputSingleParams memory params;
(TokenInterface _buyAddr, TokenInterface _sellAddr) = changeEthAddress(
buyData.buyAddr,
buyData.sellAddr
);
uint _slippageAmt = convert18ToDec(_sellAddr.decimals(),
wmul(buyData.unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt))
);
bool isEth = address(_sellAddr) == wethAddr;
convertEthToWeth(isEth, _sellAddr, _slippageAmt);
approve(_sellAddr, address(swapRouter), _slippageAmt);
{
params = ISwapRouter.ExactOutputSingleParams({
tokenIn: buyData.sellAddr,
tokenOut: buyData.buyAddr,
fee: buyData.fee,
recipient: address(this),
deadline: block.timestamp + 1,
amountOut: _buyAmt,
amountInMaximum: _slippageAmt, //require(_sellAmt <= amountInMaximum)
sqrtPriceLimitX96: 0
});
}
uint256 _sellAmt = swapRouter.exactOutputSingle(params);
require(_slippageAmt >= _sellAmt, "Too much slippage");
isEth = address(_buyAddr) == wethAddr;
convertWethToEth(isEth, _buyAddr, _buyAmt);
setUint(buyData.setId, _sellAmt);
_eventName = "LogBuy(address,address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(
buyData.buyAddr,
buyData.sellAddr,
_buyAmt,
_sellAmt,
buyData.getId,
buyData.setId
);
/**
* @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 unitAmt The unit amount of sellAmt/buyAmt with slippage
* @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 _unitAmt,
uint256 _buyAmt,
uint256 _getId,
uint256 _setId
) external payable returns (string memory _eventName, bytes memory _eventParam) {
BuyInfo memory buyData = BuyInfo({
buyAddr: _buyAddr,
sellAddr: _sellAddr,
fee: _fee,
unitAmt: _unitAmt,
buyAmt: _buyAmt,
getId: _getId,
setId: _setId
});
return _buy(buyData);
}
/**
* @dev Sell Function
* @notice Swap token(sellAddr) with token(buyAddr), to get max buy tokens
* @param sellData Data input for the buy action
*/
function sell(
SellInfo memory sellData
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _sellAmt = getUint(sellData.getId, sellData.sellAmt);
(TokenInterface _buyAddr, TokenInterface _sellAddr) = changeEthAddress(
sellData.buyAddr,
sellData.sellAddr
);
if (_sellAmt == uint(-1)) {
_sellAmt = sellData.sellAddr == ethAddr
? address(this).balance
: _sellAddr.balanceOf(address(this));
}
uint _slippageAmt = convert18ToDec(_buyAddr.decimals(),
wmul(sellData.unitAmt, convertTo18(_sellAddr.decimals(), _sellAmt))
);
bool isEth = address(_sellAddr) == wethAddr;
convertEthToWeth(isEth, _sellAddr, _sellAmt);
approve(_sellAddr, address(swapRouter), _sellAmt);
ISwapRouter.ExactInputSingleParams memory params;
{
params = ISwapRouter.ExactInputSingleParams({
tokenIn: sellData.sellAddr,
tokenOut: sellData.buyAddr,
fee: sellData.fee,
recipient: address(this),
deadline: block.timestamp + 1,
amountIn: _sellAmt,
amountOutMinimum: _slippageAmt, //require(_buyAmt >= amountOutMinimum)
sqrtPriceLimitX96: 0
});
}
uint256 _buyAmt = swapRouter.exactInputSingle(params);
require(_slippageAmt <= _buyAmt, "Too much slippage");
isEth = address(_buyAddr) == wethAddr;
convertWethToEth(isEth, _buyAddr, _buyAmt);
setUint(sellData.setId, _buyAmt);
_eventName = "LogSell(address,address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(
sellData.buyAddr,
sellData.sellAddr,
_buyAmt,
_sellAmt,
sellData.getId,
sellData.setId
);
}
* @dev Sell 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 unitAmt The unit amount of buyAmt/sellAmt with slippage
* @param sellAmt amount of token to be sold
* @param getId Id to get sellAmt
* @param setId Id to store buyAmt
*/
function sell(
address _buyAddr,
address _sellAddr,
uint24 _fee,
uint256 _unitAmt,
uint256 _sellAmt,
uint256 _getId,
uint256 _setId
) external payable returns (string memory _eventName, bytes memory _eventParam) {
return _sell(SellInfo({
buyAddr: _buyAddr,
sellAddr: _sellAddr,
fee: _fee,
unitAmt: _unitAmt,
sellAmt: _sellAmt,
getId: _getId,
setId: _setId
}));
}
}
contract ConnectV2UniswapV3Arbitrum is UniswapResolver {

View File

@ -33,4 +33,128 @@ abstract contract Helpers is DSMath, Basic {
uint256 getId; //Id to get buyAmt
uint256 setId; //Id to store sellAmt
}
/**
* @dev Buy Function
* @notice Swap token(sellAddr) with token(buyAddr), buy token with minimum sell token
* @param buyData Data input for the buy action
*/
function _buy(
BuyInfo memory buyData
)
internal
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _buyAmt = getUint(buyData.getId, buyData.buyAmt);
ISwapRouter.ExactOutputSingleParams memory params;
(TokenInterface _buyAddr, TokenInterface _sellAddr) = changeEthAddress(
buyData.buyAddr,
buyData.sellAddr
);
uint _slippageAmt = convert18ToDec(_sellAddr.decimals(),
wmul(buyData.unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt))
);
bool isEth = address(_sellAddr) == wethAddr;
convertEthToWeth(isEth, _sellAddr, _slippageAmt);
approve(_sellAddr, address(swapRouter), _slippageAmt);
{
params = ISwapRouter.ExactOutputSingleParams({
tokenIn: buyData.sellAddr,
tokenOut: buyData.buyAddr,
fee: buyData.fee,
recipient: address(this),
deadline: block.timestamp + 1,
amountOut: _buyAmt,
amountInMaximum: _slippageAmt, //require(_sellAmt <= amountInMaximum)
sqrtPriceLimitX96: 0
});
}
uint256 _sellAmt = swapRouter.exactOutputSingle(params);
require(_slippageAmt >= _sellAmt, "Too much slippage");
isEth = address(_buyAddr) == wethAddr;
convertWethToEth(isEth, _buyAddr, _buyAmt);
setUint(buyData.setId, _sellAmt);
_eventName = "LogBuy(address,address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(
buyData.buyAddr,
buyData.sellAddr,
_buyAmt,
_sellAmt,
buyData.getId,
buyData.setId
);
}
/**
* @dev Sell Function
* @notice Swap token(sellAddr) with token(buyAddr), to get max buy tokens
* @param sellData Data input for the sell action
*/
function _sell(
SellInfo memory sellData
)
internal
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _sellAmt = getUint(sellData.getId, sellData.sellAmt);
(TokenInterface _buyAddr, TokenInterface _sellAddr) = changeEthAddress(
sellData.buyAddr,
sellData.sellAddr
);
if (_sellAmt == uint(-1)) {
_sellAmt = sellData.sellAddr == ethAddr
? address(this).balance
: _sellAddr.balanceOf(address(this));
}
uint _slippageAmt = convert18ToDec(_buyAddr.decimals(),
wmul(sellData.unitAmt, convertTo18(_sellAddr.decimals(), _sellAmt))
);
bool isEth = address(_sellAddr) == wethAddr;
convertEthToWeth(isEth, _sellAddr, _sellAmt);
approve(_sellAddr, address(swapRouter), _sellAmt);
ISwapRouter.ExactInputSingleParams memory params;
{
params = ISwapRouter.ExactInputSingleParams({
tokenIn: sellData.sellAddr,
tokenOut: sellData.buyAddr,
fee: sellData.fee,
recipient: address(this),
deadline: block.timestamp + 1,
amountIn: _sellAmt,
amountOutMinimum: _slippageAmt, //require(_buyAmt >= amountOutMinimum)
sqrtPriceLimitX96: 0
});
}
uint256 _buyAmt = swapRouter.exactInputSingle(params);
require(_slippageAmt <= _buyAmt, "Too much slippage");
isEth = address(_buyAddr) == wethAddr;
convertWethToEth(isEth, _buyAddr, _buyAmt);
setUint(sellData.setId, _buyAmt);
_eventName = "LogSell(address,address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(
sellData.buyAddr,
sellData.sellAddr,
_buyAmt,
_sellAmt,
sellData.getId,
sellData.setId
);
}
}

View File

@ -14,129 +14,65 @@ import "./interface.sol";
abstract contract UniswapResolver is Helpers, Events {
/**
* @dev Buy Function
* @notice Swap token(sellAddr) with token(buyAddr), buy token with minimum sell token
* @param buyData Data input for the buy action
*/
function buy(
BuyInfo memory buyData
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _buyAmt = getUint(buyData.getId, buyData.buyAmt);
uint _slippageAmt;
bool isEth;
ISwapRouter.ExactOutputSingleParams memory params;
(TokenInterface _buyAddr, TokenInterface _sellAddr) = changeEthAddress(
buyData.buyAddr,
buyData.sellAddr
);
_slippageAmt = convert18ToDec(_sellAddr.decimals(),
wmul(buyData.unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt))
);
isEth = address(_sellAddr) == wethAddr;
convertEthToWeth(isEth, _sellAddr, _slippageAmt);
approve(_sellAddr, address(swapRouter), _slippageAmt);
{
params = ISwapRouter.ExactOutputSingleParams({
tokenIn: buyData.sellAddr,
tokenOut: buyData.buyAddr,
fee: buyData.fee,
recipient: address(this),
deadline: block.timestamp + 1,
amountOut: _buyAmt,
amountInMaximum: _slippageAmt, //require(_sellAmt <= amountInMaximum)
sqrtPriceLimitX96: 0
});
}
uint256 _sellAmt = swapRouter.exactOutputSingle(params);
require(_slippageAmt >= _sellAmt, "Too much slippage");
isEth = address(_buyAddr) == wethAddr;
convertWethToEth(isEth, _buyAddr, _buyAmt);
setUint(buyData.setId, _sellAmt);
_eventName = "LogBuy(address,address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(
buyData.buyAddr,
buyData.sellAddr,
_buyAmt,
_sellAmt,
buyData.getId,
buyData.setId
);
* @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 unitAmt The unit amount of sellAmt/buyAmt with slippage
* @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 _unitAmt,
uint256 _buyAmt,
uint256 _getId,
uint256 _setId
) external payable returns (string memory _eventName, bytes memory _eventParam) {
return _buy(BuyInfo({
buyAddr: _buyAddr,
sellAddr: _sellAddr,
fee: _fee,
unitAmt: _unitAmt,
buyAmt: _buyAmt,
getId: _getId,
setId: _setId
}));
}
/**
* @dev Sell Function
* @notice Swap token(sellAddr) with token(buyAddr), to get max buy tokens
* @param sellData Data input for the buy action
*/
function sell(
SellInfo memory sellData
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _sellAmt = getUint(sellData.getId, sellData.sellAmt);
(TokenInterface _buyAddr, TokenInterface _sellAddr) = changeEthAddress(
sellData.buyAddr,
sellData.sellAddr
);
if (_sellAmt == uint(-1)) {
_sellAmt = sellData.sellAddr == ethAddr
? address(this).balance
: _sellAddr.balanceOf(address(this));
}
uint _slippageAmt = convert18ToDec(_buyAddr.decimals(),
wmul(sellData.unitAmt, convertTo18(_sellAddr.decimals(), _sellAmt))
);
bool isEth = address(_sellAddr) == wethAddr;
convertEthToWeth(isEth, _sellAddr, _sellAmt);
approve(_sellAddr, address(swapRouter), _sellAmt);
ISwapRouter.ExactInputSingleParams memory params;
{
params = ISwapRouter.ExactInputSingleParams({
tokenIn: sellData.sellAddr,
tokenOut: sellData.buyAddr,
fee: sellData.fee,
recipient: address(this),
deadline: block.timestamp + 1,
amountIn: _sellAmt,
amountOutMinimum: _slippageAmt, //require(_buyAmt >= amountOutMinimum)
sqrtPriceLimitX96: 0
});
}
uint256 _buyAmt = swapRouter.exactInputSingle(params);
require(_slippageAmt <= _buyAmt, "Too much slippage");
isEth = address(_buyAddr) == wethAddr;
convertWethToEth(isEth, _buyAddr, _buyAmt);
setUint(sellData.setId, _buyAmt);
_eventName = "LogSell(address,address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(
sellData.buyAddr,
sellData.sellAddr,
_buyAmt,
_sellAmt,
sellData.getId,
sellData.setId
);
* @dev Sell 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 unitAmt The unit amount of buyAmt/sellAmt with slippage
* @param sellAmt amount of token to be sold
* @param getId Id to get sellAmt
* @param setId Id to store buyAmt
*/
function sell(
address _buyAddr,
address _sellAddr,
uint24 _fee,
uint256 _unitAmt,
uint256 _sellAmt,
uint256 _getId,
uint256 _setId
) external payable returns (string memory _eventName, bytes memory _eventParam) {
return _sell(SellInfo({
buyAddr: _buyAddr,
sellAddr: _sellAddr,
fee: _fee,
unitAmt: _unitAmt,
sellAmt: _sellAmt,
getId: _getId,
setId: _setId
}));
}
}

View File

@ -33,4 +33,128 @@ abstract contract Helpers is DSMath, Basic {
uint256 getId; //Id to get buyAmt
uint256 setId; //Id to store sellAmt
}
/**
* @dev Buy Function
* @notice Swap token(sellAddr) with token(buyAddr), buy token with minimum sell token
* @param buyData Data input for the buy action
*/
function _buy(
BuyInfo memory buyData
)
internal
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _buyAmt = getUint(buyData.getId, buyData.buyAmt);
ISwapRouter.ExactOutputSingleParams memory params;
(TokenInterface _buyAddr, TokenInterface _sellAddr) = changeEthAddress(
buyData.buyAddr,
buyData.sellAddr
);
uint _slippageAmt = convert18ToDec(_sellAddr.decimals(),
wmul(buyData.unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt))
);
bool isEth = address(_sellAddr) == wethAddr;
convertEthToWeth(isEth, _sellAddr, _slippageAmt);
approve(_sellAddr, address(swapRouter), _slippageAmt);
{
params = ISwapRouter.ExactOutputSingleParams({
tokenIn: buyData.sellAddr,
tokenOut: buyData.buyAddr,
fee: buyData.fee,
recipient: address(this),
deadline: block.timestamp + 1,
amountOut: _buyAmt,
amountInMaximum: _slippageAmt, //require(_sellAmt <= amountInMaximum)
sqrtPriceLimitX96: 0
});
}
uint256 _sellAmt = swapRouter.exactOutputSingle(params);
require(_slippageAmt >= _sellAmt, "Too much slippage");
isEth = address(_buyAddr) == wethAddr;
convertWethToEth(isEth, _buyAddr, _buyAmt);
setUint(buyData.setId, _sellAmt);
_eventName = "LogBuy(address,address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(
buyData.buyAddr,
buyData.sellAddr,
_buyAmt,
_sellAmt,
buyData.getId,
buyData.setId
);
}
/**
* @dev Sell Function
* @notice Swap token(sellAddr) with token(buyAddr), to get max buy tokens
* @param sellData Data input for the sell action
*/
function _sell(
SellInfo memory sellData
)
internal
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _sellAmt = getUint(sellData.getId, sellData.sellAmt);
(TokenInterface _buyAddr, TokenInterface _sellAddr) = changeEthAddress(
sellData.buyAddr,
sellData.sellAddr
);
if (_sellAmt == uint(-1)) {
_sellAmt = sellData.sellAddr == ethAddr
? address(this).balance
: _sellAddr.balanceOf(address(this));
}
uint _slippageAmt = convert18ToDec(_buyAddr.decimals(),
wmul(sellData.unitAmt, convertTo18(_sellAddr.decimals(), _sellAmt))
);
bool isEth = address(_sellAddr) == wethAddr;
convertEthToWeth(isEth, _sellAddr, _sellAmt);
approve(_sellAddr, address(swapRouter), _sellAmt);
ISwapRouter.ExactInputSingleParams memory params;
{
params = ISwapRouter.ExactInputSingleParams({
tokenIn: sellData.sellAddr,
tokenOut: sellData.buyAddr,
fee: sellData.fee,
recipient: address(this),
deadline: block.timestamp + 1,
amountIn: _sellAmt,
amountOutMinimum: _slippageAmt, //require(_buyAmt >= amountOutMinimum)
sqrtPriceLimitX96: 0
});
}
uint256 _buyAmt = swapRouter.exactInputSingle(params);
require(_slippageAmt <= _buyAmt, "Too much slippage");
isEth = address(_buyAddr) == wethAddr;
convertWethToEth(isEth, _buyAddr, _buyAmt);
setUint(sellData.setId, _buyAmt);
_eventName = "LogSell(address,address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(
sellData.buyAddr,
sellData.sellAddr,
_buyAmt,
_sellAmt,
sellData.getId,
sellData.setId
);
}
}

View File

@ -14,129 +14,65 @@ import "./interface.sol";
abstract contract UniswapResolver is Helpers, Events {
/**
* @dev Buy Function
* @notice Swap token(sellAddr) with token(buyAddr), buy token with minimum sell token
* @param buyData Data input for the buy action
*/
function buy(
BuyInfo memory buyData
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _buyAmt = getUint(buyData.getId, buyData.buyAmt);
uint _slippageAmt;
bool isEth;
ISwapRouter.ExactOutputSingleParams memory params;
(TokenInterface _buyAddr, TokenInterface _sellAddr) = changeEthAddress(
buyData.buyAddr,
buyData.sellAddr
);
_slippageAmt = convert18ToDec(_sellAddr.decimals(),
wmul(buyData.unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt))
);
isEth = address(_sellAddr) == wethAddr;
convertEthToWeth(isEth, _sellAddr, _slippageAmt);
approve(_sellAddr, address(swapRouter), _slippageAmt);
{
params = ISwapRouter.ExactOutputSingleParams({
tokenIn: buyData.sellAddr,
tokenOut: buyData.buyAddr,
fee: buyData.fee,
recipient: address(this),
deadline: block.timestamp + 1,
amountOut: _buyAmt,
amountInMaximum: _slippageAmt, //require(_sellAmt <= amountInMaximum)
sqrtPriceLimitX96: 0
});
}
uint256 _sellAmt = swapRouter.exactOutputSingle(params);
require(_slippageAmt >= _sellAmt, "Too much slippage");
isEth = address(_buyAddr) == wethAddr;
convertWethToEth(isEth, _buyAddr, _buyAmt);
setUint(buyData.setId, _sellAmt);
_eventName = "LogBuy(address,address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(
buyData.buyAddr,
buyData.sellAddr,
_buyAmt,
_sellAmt,
buyData.getId,
buyData.setId
);
* @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 unitAmt The unit amount of sellAmt/buyAmt with slippage
* @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 _unitAmt,
uint256 _buyAmt,
uint256 _getId,
uint256 _setId
) external payable returns (string memory _eventName, bytes memory _eventParam) {
return _buy(BuyInfo({
buyAddr: _buyAddr,
sellAddr: _sellAddr,
fee: _fee,
unitAmt: _unitAmt,
buyAmt: _buyAmt,
getId: _getId,
setId: _setId
}));
}
/**
* @dev Sell Function
* @notice Swap token(sellAddr) with token(buyAddr), to get max buy tokens
* @param sellData Data input for the buy action
*/
function sell(
SellInfo memory sellData
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _sellAmt = getUint(sellData.getId, sellData.sellAmt);
(TokenInterface _buyAddr, TokenInterface _sellAddr) = changeEthAddress(
sellData.buyAddr,
sellData.sellAddr
);
if (_sellAmt == uint(-1)) {
_sellAmt = sellData.sellAddr == ethAddr
? address(this).balance
: _sellAddr.balanceOf(address(this));
}
uint _slippageAmt = convert18ToDec(_buyAddr.decimals(),
wmul(sellData.unitAmt, convertTo18(_sellAddr.decimals(), _sellAmt))
);
bool isEth = address(_sellAddr) == wethAddr;
convertEthToWeth(isEth, _sellAddr, _sellAmt);
approve(_sellAddr, address(swapRouter), _sellAmt);
ISwapRouter.ExactInputSingleParams memory params;
{
params = ISwapRouter.ExactInputSingleParams({
tokenIn: sellData.sellAddr,
tokenOut: sellData.buyAddr,
fee: sellData.fee,
recipient: address(this),
deadline: block.timestamp + 1,
amountIn: _sellAmt,
amountOutMinimum: _slippageAmt, //require(_buyAmt >= amountOutMinimum)
sqrtPriceLimitX96: 0
});
}
uint256 _buyAmt = swapRouter.exactInputSingle(params);
require(_slippageAmt <= _buyAmt, "Too much slippage");
isEth = address(_buyAddr) == wethAddr;
convertWethToEth(isEth, _buyAddr, _buyAmt);
setUint(sellData.setId, _buyAmt);
_eventName = "LogSell(address,address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(
sellData.buyAddr,
sellData.sellAddr,
_buyAmt,
_sellAmt,
sellData.getId,
sellData.setId
);
* @dev Sell 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 unitAmt The unit amount of buyAmt/sellAmt with slippage
* @param sellAmt amount of token to be sold
* @param getId Id to get sellAmt
* @param setId Id to store buyAmt
*/
function sell(
address _buyAddr,
address _sellAddr,
uint24 _fee,
uint256 _unitAmt,
uint256 _sellAmt,
uint256 _getId,
uint256 _setId
) external payable returns (string memory _eventName, bytes memory _eventParam) {
return _sell(SellInfo({
buyAddr: _buyAddr,
sellAddr: _sellAddr,
fee: _fee,
unitAmt: _unitAmt,
sellAmt: _sellAmt,
getId: _getId,
setId: _setId
}));
}
}

View File

@ -28,9 +28,133 @@ abstract contract Helpers is DSMath, Basic {
address buyAddr; //token to be bought
address sellAddr; //token to be sold
uint24 fee; //pool fees for buyAddr-sellAddr token pair
uint256 unitAmt; //The unit amount of sellAmt/buyAmt with slippage
uint256 unitAmt; //The unit amount of buyAmt/sellAmt with slippage
uint256 sellAmt; //amount of token to be bought
uint256 getId; //Id to get buyAmt
uint256 setId; //Id to store sellAmt
}
/**
* @dev Buy Function
* @notice Swap token(sellAddr) with token(buyAddr), buy token with minimum sell token
* @param buyData Data input for the buy action
*/
function _buy(
BuyInfo memory buyData
)
internal
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _buyAmt = getUint(buyData.getId, buyData.buyAmt);
(
TokenInterface _buyAddr,
TokenInterface _sellAddr
) = changeMaticAddress(buyData.buyAddr, buyData.sellAddr);
uint _slippageAmt = convert18ToDec(_sellAddr.decimals(),
wmul(buyData.unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt))
);
bool isMatic = address(_sellAddr) == wmaticAddr;
convertMaticToWmatic(isMatic, _sellAddr, _slippageAmt);
approve(_sellAddr, address(swapRouter), _slippageAmt);
ISwapRouter.ExactOutputSingleParams memory params;
{
params = ISwapRouter.ExactOutputSingleParams({
tokenIn: buyData.sellAddr,
tokenOut: buyData.buyAddr,
fee: buyData.fee,
recipient: address(this),
deadline: block.timestamp + 1,
amountOut: _buyAmt,
amountInMaximum: _slippageAmt, //require(_sellAmt <= amountInMaximum)
sqrtPriceLimitX96: 0
});
}
uint256 _sellAmt = swapRouter.exactOutputSingle(params);
require(_slippageAmt >= _sellAmt, "Too much slippage");
isMatic = address(_buyAddr) == wmaticAddr;
convertWmaticToMatic(isMatic, _buyAddr, _buyAmt);
setUint(buyData.setId, _sellAmt);
_eventName = "LogBuy(address,address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(
buyData.buyAddr,
buyData.sellAddr,
_buyAmt,
_sellAmt,
buyData.getId,
buyData.setId
);
}
/**
* @dev Sell Function
* @notice Swap token(sellAddr) with token(buyAddr), to get max buy tokens
* @param sellData Data input for the sell action
*/
function _sell(
SellInfo memory sellData
)
internal
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _sellAmt = getUint(sellData.getId, sellData.sellAmt);
(
TokenInterface _buyAddr,
TokenInterface _sellAddr
) = changeMaticAddress(sellData.buyAddr, sellData.sellAddr);
if (_sellAmt == uint(-1)) {
_sellAmt = sellData.sellAddr == maticAddr
? address(this).balance
: _sellAddr.balanceOf(address(this));
}
uint _slippageAmt = convert18ToDec(_buyAddr.decimals(),
wmul(sellData.unitAmt, convertTo18(_sellAddr.decimals(), _sellAmt))
);
bool isMatic = address(_sellAddr) == wmaticAddr;
convertMaticToWmatic(isMatic, _sellAddr, _sellAmt);
approve(_sellAddr, address(swapRouter), _sellAmt);
ISwapRouter.ExactInputSingleParams memory params;
{
params = ISwapRouter.ExactInputSingleParams({
tokenIn: sellData.sellAddr,
tokenOut: sellData.buyAddr,
fee: sellData.fee,
recipient: address(this),
deadline: block.timestamp + 1,
amountIn: _sellAmt,
amountOutMinimum: _slippageAmt, //require(_buyAmt >= amountOutMinimum)
sqrtPriceLimitX96: 0
});
}
uint256 _buyAmt = swapRouter.exactInputSingle(params);
require(_slippageAmt <= _buyAmt, "Too much slippage");
isMatic = address(_buyAddr) == wmaticAddr;
convertWmaticToMatic(isMatic, _buyAddr, _buyAmt);
setUint(sellData.setId, _buyAmt);
_eventName = "LogSell(address,address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(
sellData.buyAddr,
sellData.sellAddr,
_buyAmt,
_sellAmt,
sellData.getId,
sellData.setId
);
}
}

View File

@ -10,132 +10,71 @@ 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 {
/**
* @dev Buy Function
* @notice Swap token(sellAddr) with token(buyAddr), buy token with minimum sell token
* @param buyData Data input for the buy action
*/
function buy(
BuyInfo memory buyData
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _buyAmt = getUint(buyData.getId, buyData.buyAmt);
(
TokenInterface _buyAddr,
TokenInterface _sellAddr
) = changeMaticAddress(buyData.buyAddr, buyData.sellAddr);
uint _slippageAmt = convert18ToDec(_sellAddr.decimals(),
wmul(buyData.unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt))
);
bool isMatic = address(_sellAddr) == wmaticAddr;
convertMaticToWmatic(isMatic, _sellAddr, _slippageAmt);
approve(_sellAddr, address(swapRouter), _slippageAmt);
ISwapRouter.ExactOutputSingleParams memory params;
{
params = ISwapRouter.ExactOutputSingleParams({
tokenIn: buyData.sellAddr,
tokenOut: buyData.buyAddr,
fee: buyData.fee,
recipient: address(this),
deadline: block.timestamp + 1,
amountOut: _buyAmt,
amountInMaximum: _slippageAmt, //require(_sellAmt <= amountInMaximum)
sqrtPriceLimitX96: 0
});
}
uint256 _sellAmt = swapRouter.exactOutputSingle(params);
require(_slippageAmt >= _sellAmt, "Too much slippage");
isMatic = address(_buyAddr) == wmaticAddr;
convertWmaticToMatic(isMatic, _buyAddr, _buyAmt);
setUint(buyData.setId, _sellAmt);
_eventName = "LogBuy(address,address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(
buyData.buyAddr,
buyData.sellAddr,
_buyAmt,
_sellAmt,
buyData.getId,
buyData.setId
);
* @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 unitAmt The unit amount of sellAmt/buyAmt with slippage
* @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 _unitAmt,
uint256 _buyAmt,
uint256 _getId,
uint256 _setId
) external payable returns (string memory _eventName, bytes memory _eventParam) {
BuyInfo memory buyData = BuyInfo({
buyAddr: _buyAddr,
sellAddr: _sellAddr,
fee: _fee,
unitAmt: _unitAmt,
buyAmt: _buyAmt,
getId: _getId,
setId: _setId
});
return _buy(buyData);
}
/**
* @dev Sell Function
* @notice Swap token(sellAddr) with token(buyAddr), to get max buy tokens
* @param sellData Data input for the buy action
*/
function sell(
SellInfo memory sellData
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _sellAmt = getUint(sellData.getId, sellData.sellAmt);
(
TokenInterface _buyAddr,
TokenInterface _sellAddr
) = changeMaticAddress(sellData.buyAddr, sellData.sellAddr);
if (_sellAmt == uint(-1)) {
_sellAmt = sellData.sellAddr == maticAddr
? address(this).balance
: _sellAddr.balanceOf(address(this));
}
uint _slippageAmt = convert18ToDec(_buyAddr.decimals(),
wmul(sellData.unitAmt, convertTo18(_sellAddr.decimals(), _sellAmt))
);
bool isMatic = address(_sellAddr) == wmaticAddr;
convertMaticToWmatic(isMatic, _sellAddr, _sellAmt);
approve(_sellAddr, address(swapRouter), _sellAmt);
ISwapRouter.ExactInputSingleParams memory params;
{
params = ISwapRouter.ExactInputSingleParams({
tokenIn: sellData.sellAddr,
tokenOut: sellData.buyAddr,
fee: sellData.fee,
recipient: address(this),
deadline: block.timestamp + 1,
amountIn: _sellAmt,
amountOutMinimum: _slippageAmt, //require(_buyAmt >= amountOutMinimum)
sqrtPriceLimitX96: 0
});
}
uint256 _buyAmt = swapRouter.exactInputSingle(params);
require(_slippageAmt <= _buyAmt, "Too much slippage");
isMatic = address(_buyAddr) == wmaticAddr;
convertWmaticToMatic(isMatic, _buyAddr, _buyAmt);
setUint(sellData.setId, _buyAmt);
_eventName = "LogSell(address,address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(
sellData.buyAddr,
sellData.sellAddr,
_buyAmt,
_sellAmt,
sellData.getId,
sellData.setId
);
* @dev Sell 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 unitAmt The unit amount of buyAmt/sellAmt with slippage
* @param sellAmt amount of token to be sold
* @param getId Id to get sellAmt
* @param setId Id to store buyAmt
*/
function sell(
address _buyAddr,
address _sellAddr,
uint24 _fee,
uint256 _unitAmt,
uint256 _sellAmt,
uint256 _getId,
uint256 _setId
) external payable returns (string memory _eventName, bytes memory _eventParam) {
return _sell(SellInfo({
buyAddr: _buyAddr,
sellAddr: _sellAddr,
fee: _fee,
unitAmt: _unitAmt,
sellAmt: _sellAmt,
getId: _getId,
setId: _setId
}));
}
}
contract ConnectV2UniswapV3Polygon is UniswapResolver {