refactored code

This commit is contained in:
Richa-iitr 2022-04-30 16:59:30 +05:30
parent e904ed718d
commit b0980aebb4
8 changed files with 125 additions and 109 deletions

View File

@ -13,4 +13,15 @@ abstract contract Helpers is DSMath, Basic {
*/ */
ISwapRouter constant swapRouter = ISwapRouter constant swapRouter =
ISwapRouter(0xE592427A0AEce92De3Edee1F18E0157C05861564); ISwapRouter(0xE592427A0AEce92De3Edee1F18E0157C05861564);
struct BuyInfo {
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 expectedAmt; //The amount that is expected to be returned after the swap
uint256 buyAmt; //amount of token to be bought
uint256 getId; //Id to get buyAmt
uint256 setId; //Id to store sellAmt
}
} }

View File

@ -16,34 +16,27 @@ abstract contract UniswapResolver is Helpers, Events {
/** /**
* @dev Buy Function * @dev Buy Function
* @notice Swap token(sellAddr) with token(buyAddr), buy token with minimum sell token * @notice Swap token(sellAddr) with token(buyAddr), buy token with minimum sell token
* @param buyAddr token to be bought * @param buyData Data input for the buy action
* @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( function buy(
address buyAddr, BuyInfo memory buyData
address sellAddr,
uint24 fee,
uint256 buyAmt,
uint256 getId,
uint256 setId
) )
external external
payable payable
returns (string memory _eventName, bytes memory _eventParam) returns (string memory _eventName, bytes memory _eventParam)
{ {
uint256 _buyAmt = getUint(getId, buyAmt); uint256 _buyAmt = getUint(buyData.getId, buyData.buyAmt);
(TokenInterface _buyAddr, TokenInterface _sellAddr) = changeEthAddress( (TokenInterface _buyAddr, TokenInterface _sellAddr) = changeEthAddress(
buyAddr, buyData.buyAddr,
sellAddr buyData.sellAddr
); );
// uint _slippageAmt = convert18ToDec(_sellAddr.decimals(), {
// wmul(unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt)) uint _slippageAmt = convert18ToDec(_sellAddr.decimals(),
// ); wmul(buyData.unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt))
);
require(_slippageAmt >= buyData.expectedAmt, "Too much slippage");
}
bool isEth = address(_sellAddr) == wethAddr; bool isEth = address(_sellAddr) == wethAddr;
convertEthToWeth(isEth, _sellAddr, uint256(-1)); convertEthToWeth(isEth, _sellAddr, uint256(-1));
@ -52,13 +45,13 @@ abstract contract UniswapResolver is Helpers, Events {
{ {
params = ISwapRouter.ExactOutputSingleParams({ params = ISwapRouter.ExactOutputSingleParams({
tokenIn: sellAddr, tokenIn: buyData.sellAddr,
tokenOut: buyAddr, tokenOut: buyData.buyAddr,
fee: fee, fee: buyData.fee,
recipient: address(this), recipient: address(this),
deadline: block.timestamp + 1, deadline: block.timestamp + 1,
amountOut: _buyAmt, amountOut: _buyAmt,
amountInMaximum: uint256(-1), amountInMaximum: buyData.expectedAmt,
sqrtPriceLimitX96: 0 sqrtPriceLimitX96: 0
}); });
} }
@ -68,16 +61,16 @@ abstract contract UniswapResolver is Helpers, Events {
isEth = address(_buyAddr) == wethAddr; isEth = address(_buyAddr) == wethAddr;
convertWethToEth(isEth, _buyAddr, _buyAmt); convertWethToEth(isEth, _buyAddr, _buyAmt);
setUint(setId, _sellAmt); setUint(buyData.setId, _sellAmt);
_eventName = "LogBuy(address,address,uint256,uint256,uint256,uint256)"; _eventName = "LogBuy(address,address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode( _eventParam = abi.encode(
buyAddr, buyData.buyAddr,
sellAddr, buyData.sellAddr,
_buyAmt, _buyAmt,
_sellAmt, _sellAmt,
getId, buyData.getId,
setId buyData.setId
); );
} }

View File

@ -13,4 +13,15 @@ abstract contract Helpers is DSMath, Basic {
*/ */
ISwapRouter constant swapRouter = ISwapRouter constant swapRouter =
ISwapRouter(0xE592427A0AEce92De3Edee1F18E0157C05861564); ISwapRouter(0xE592427A0AEce92De3Edee1F18E0157C05861564);
struct BuyInfo {
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 expectedAmt; //The amount that is expected to be returned after the swap
uint256 buyAmt; //amount of token to be bought
uint256 getId; //Id to get buyAmt
uint256 setId; //Id to store sellAmt
}
} }

View File

@ -13,38 +13,30 @@ import {Events} from "./events.sol";
import "./interface.sol"; import "./interface.sol";
abstract contract UniswapResolver is Helpers, Events { abstract contract UniswapResolver is Helpers, Events {
/** /**
* @dev Buy Function * @dev Buy Function
* @notice Swap token(sellAddr) with token(buyAddr), buy token with minimum sell token * @notice Swap token(sellAddr) with token(buyAddr), buy token with minimum sell token
* @param buyAddr token to be bought * @param buyData Data input for the buy action
* @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( function buy(
address buyAddr, BuyInfo memory buyData
address sellAddr,
uint24 fee,
uint256 buyAmt,
uint256 getId,
uint256 setId
) )
external external
payable payable
returns (string memory _eventName, bytes memory _eventParam) returns (string memory _eventName, bytes memory _eventParam)
{ {
uint256 _buyAmt = getUint(getId, buyAmt); uint256 _buyAmt = getUint(buyData.getId, buyData.buyAmt);
(TokenInterface _buyAddr, TokenInterface _sellAddr) = changeEthAddress( (TokenInterface _buyAddr, TokenInterface _sellAddr) = changeEthAddress(
buyAddr, buyData.buyAddr,
sellAddr buyData.sellAddr
); );
// uint _slippageAmt = convert18ToDec(_sellAddr.decimals(), {
// wmul(unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt)) uint _slippageAmt = convert18ToDec(_sellAddr.decimals(),
// ); wmul(buyData.unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt))
);
require(_slippageAmt >= buyData.expectedAmt, "Too much slippage");
}
bool isEth = address(_sellAddr) == wethAddr; bool isEth = address(_sellAddr) == wethAddr;
convertEthToWeth(isEth, _sellAddr, uint256(-1)); convertEthToWeth(isEth, _sellAddr, uint256(-1));
@ -53,13 +45,13 @@ abstract contract UniswapResolver is Helpers, Events {
{ {
params = ISwapRouter.ExactOutputSingleParams({ params = ISwapRouter.ExactOutputSingleParams({
tokenIn: sellAddr, tokenIn: buyData.sellAddr,
tokenOut: buyAddr, tokenOut: buyData.buyAddr,
fee: fee, fee: buyData.fee,
recipient: address(this), recipient: address(this),
deadline: block.timestamp + 1, deadline: block.timestamp + 1,
amountOut: _buyAmt, amountOut: _buyAmt,
amountInMaximum: uint256(-1), amountInMaximum: buyData.expectedAmt,
sqrtPriceLimitX96: 0 sqrtPriceLimitX96: 0
}); });
} }
@ -69,16 +61,16 @@ abstract contract UniswapResolver is Helpers, Events {
isEth = address(_buyAddr) == wethAddr; isEth = address(_buyAddr) == wethAddr;
convertWethToEth(isEth, _buyAddr, _buyAmt); convertWethToEth(isEth, _buyAddr, _buyAmt);
setUint(setId, _sellAmt); setUint(buyData.setId, _sellAmt);
_eventName = "LogBuy(address,address,uint256,uint256,uint256,uint256)"; _eventName = "LogBuy(address,address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode( _eventParam = abi.encode(
buyAddr, buyData.buyAddr,
sellAddr, buyData.sellAddr,
_buyAmt, _buyAmt,
_sellAmt, _sellAmt,
getId, buyData.getId,
setId buyData.setId
); );
} }

View File

@ -13,4 +13,15 @@ abstract contract Helpers is DSMath, Basic {
*/ */
ISwapRouter constant swapRouter = ISwapRouter constant swapRouter =
ISwapRouter(0xE592427A0AEce92De3Edee1F18E0157C05861564); ISwapRouter(0xE592427A0AEce92De3Edee1F18E0157C05861564);
struct BuyInfo {
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 expectedAmt; //The amount that is expected to be returned after the swap
uint256 buyAmt; //amount of token to be bought
uint256 getId; //Id to get buyAmt
uint256 setId; //Id to store sellAmt
}
} }

View File

@ -13,38 +13,30 @@ import {Events} from "./events.sol";
import "./interface.sol"; import "./interface.sol";
abstract contract UniswapResolver is Helpers, Events { abstract contract UniswapResolver is Helpers, Events {
/** /**
* @dev Buy Function * @dev Buy Function
* @notice Swap token(sellAddr) with token(buyAddr), buy token with minimum sell token * @notice Swap token(sellAddr) with token(buyAddr), buy token with minimum sell token
* @param buyAddr token to be bought * @param buyData Data input for the buy action
* @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( function buy(
address buyAddr, BuyInfo memory buyData
address sellAddr,
uint24 fee,
uint256 buyAmt,
uint256 getId,
uint256 setId
) )
external external
payable payable
returns (string memory _eventName, bytes memory _eventParam) returns (string memory _eventName, bytes memory _eventParam)
{ {
uint256 _buyAmt = getUint(getId, buyAmt); uint256 _buyAmt = getUint(buyData.getId, buyData.buyAmt);
(TokenInterface _buyAddr, TokenInterface _sellAddr) = changeEthAddress( (TokenInterface _buyAddr, TokenInterface _sellAddr) = changeEthAddress(
buyAddr, buyData.buyAddr,
sellAddr buyData.sellAddr
); );
// uint _slippageAmt = convert18ToDec(_sellAddr.decimals(), {
// wmul(unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt)) uint _slippageAmt = convert18ToDec(_sellAddr.decimals(),
// ); wmul(buyData.unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt))
);
require(_slippageAmt >= buyData.expectedAmt, "Too much slippage");
}
bool isEth = address(_sellAddr) == wethAddr; bool isEth = address(_sellAddr) == wethAddr;
convertEthToWeth(isEth, _sellAddr, uint256(-1)); convertEthToWeth(isEth, _sellAddr, uint256(-1));
@ -53,13 +45,13 @@ abstract contract UniswapResolver is Helpers, Events {
{ {
params = ISwapRouter.ExactOutputSingleParams({ params = ISwapRouter.ExactOutputSingleParams({
tokenIn: sellAddr, tokenIn: buyData.sellAddr,
tokenOut: buyAddr, tokenOut: buyData.buyAddr,
fee: fee, fee: buyData.fee,
recipient: address(this), recipient: address(this),
deadline: block.timestamp + 1, deadline: block.timestamp + 1,
amountOut: _buyAmt, amountOut: _buyAmt,
amountInMaximum: uint256(-1), amountInMaximum: buyData.expectedAmt,
sqrtPriceLimitX96: 0 sqrtPriceLimitX96: 0
}); });
} }
@ -69,16 +61,16 @@ abstract contract UniswapResolver is Helpers, Events {
isEth = address(_buyAddr) == wethAddr; isEth = address(_buyAddr) == wethAddr;
convertWethToEth(isEth, _buyAddr, _buyAmt); convertWethToEth(isEth, _buyAddr, _buyAmt);
setUint(setId, _sellAmt); setUint(buyData.setId, _sellAmt);
_eventName = "LogBuy(address,address,uint256,uint256,uint256,uint256)"; _eventName = "LogBuy(address,address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode( _eventParam = abi.encode(
buyAddr, buyData.buyAddr,
sellAddr, buyData.sellAddr,
_buyAmt, _buyAmt,
_sellAmt, _sellAmt,
getId, buyData.getId,
setId buyData.setId
); );
} }

View File

@ -13,4 +13,15 @@ abstract contract Helpers is DSMath, Basic {
*/ */
ISwapRouter constant swapRouter = ISwapRouter constant swapRouter =
ISwapRouter(0xE592427A0AEce92De3Edee1F18E0157C05861564); ISwapRouter(0xE592427A0AEce92De3Edee1F18E0157C05861564);
struct BuyInfo {
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 expectedAmt; //The amount that is expected to be returned after the swap
uint256 buyAmt; //amount of token to be bought
uint256 getId; //Id to get buyAmt
uint256 setId; //Id to store sellAmt
}
} }

View File

@ -16,30 +16,25 @@ abstract contract UniswapResolver is Helpers, Events {
/** /**
* @dev Buy Function * @dev Buy Function
* @notice Swap token(sellAddr) with token(buyAddr), buy token with minimum sell token * @notice Swap token(sellAddr) with token(buyAddr), buy token with minimum sell token
* @param buyAddr token to be bought * @param buyData Data input for the buy action
* @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( function buy(
address buyAddr, BuyInfo memory buyData
address sellAddr,
uint24 fee,
uint256 buyAmt,
uint256 getId,
uint256 setId
) )
external external
payable payable
returns (string memory _eventName, bytes memory _eventParam) returns (string memory _eventName, bytes memory _eventParam)
{ {
uint256 _buyAmt = getUint(getId, buyAmt); uint256 _buyAmt = getUint(buyData.getId, buyData.buyAmt);
( (
TokenInterface _buyAddr, TokenInterface _buyAddr,
TokenInterface _sellAddr TokenInterface _sellAddr
) = changeMaticAddress(buyAddr, sellAddr); ) = changeMaticAddress(buyData.buyAddr, buyData.sellAddr);
uint _slippageAmt = convert18ToDec(_sellAddr.decimals(),
wmul(buyData.unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt))
);
require(_slippageAmt >= buyData.expectedAmt, "Too much slippage");
bool isMatic = address(_sellAddr) == wmaticAddr; bool isMatic = address(_sellAddr) == wmaticAddr;
convertMaticToWmatic(isMatic, _sellAddr, uint256(-1)); convertMaticToWmatic(isMatic, _sellAddr, uint256(-1));
@ -48,13 +43,13 @@ abstract contract UniswapResolver is Helpers, Events {
{ {
params = ISwapRouter.ExactOutputSingleParams({ params = ISwapRouter.ExactOutputSingleParams({
tokenIn: sellAddr, tokenIn: buyData.sellAddr,
tokenOut: buyAddr, tokenOut: buyData.buyAddr,
fee: fee, fee: buyData.fee,
recipient: address(this), recipient: address(this),
deadline: block.timestamp + 1, deadline: block.timestamp + 1,
amountOut: _buyAmt, amountOut: _buyAmt,
amountInMaximum: uint256(-1), amountInMaximum: buyData.expectedAmt,
sqrtPriceLimitX96: 0 sqrtPriceLimitX96: 0
}); });
} }
@ -64,16 +59,16 @@ abstract contract UniswapResolver is Helpers, Events {
isMatic = address(_buyAddr) == wmaticAddr; isMatic = address(_buyAddr) == wmaticAddr;
convertWmaticToMatic(isMatic, _buyAddr, _buyAmt); convertWmaticToMatic(isMatic, _buyAddr, _buyAmt);
setUint(setId, _sellAmt); setUint(buyData.setId, _sellAmt);
_eventName = "LogBuy(address,address,uint256,uint256,uint256,uint256)"; _eventName = "LogBuy(address,address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode( _eventParam = abi.encode(
buyAddr, buyData.buyAddr,
sellAddr, buyData.sellAddr,
_buyAmt, _buyAmt,
_sellAmt, _sellAmt,
getId, buyData.getId,
setId buyData.setId
); );
} }