2021-11-19 12:59:55 +00:00
|
|
|
pragma solidity ^0.7.6;
|
|
|
|
pragma abicoder v2;
|
|
|
|
|
|
|
|
import "./interface.sol";
|
|
|
|
import {SqrtPriceMath} from "./libraries/SqrtPriceMath.sol";
|
|
|
|
import "./libraries/TransferHelper.sol";
|
|
|
|
|
2021-11-19 14:28:35 +00:00
|
|
|
contract Helpers is ISwapRouter {
|
|
|
|
ISwapRouter constant public router =
|
2021-11-19 12:59:55 +00:00
|
|
|
ISwapRouter(0xE592427A0AEce92De3Edee1F18E0157C05861564);
|
|
|
|
|
2021-11-19 14:28:35 +00:00
|
|
|
UniswapV3Pool constant public state =
|
2021-11-19 12:59:55 +00:00
|
|
|
UniswapV3Pool(0xCEda10b4d3bdE429DdA3A6daB87b38360313CBdB);
|
|
|
|
|
|
|
|
function getPriceLimit(uint256 amountIn, bool zeroForOne)
|
|
|
|
public
|
|
|
|
returns (uint160)
|
|
|
|
{
|
|
|
|
return (
|
|
|
|
SqrtPriceMath.getNextSqrtPriceFromInput(
|
|
|
|
state.slot0().sqrtPriceX96,
|
|
|
|
state.liquidity(),
|
|
|
|
amountIn,
|
|
|
|
zeroForOne
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getParams(
|
|
|
|
address tokenIn,
|
|
|
|
address tokenOut,
|
|
|
|
address recipient,
|
|
|
|
uint24 fee,
|
|
|
|
uint256 amountIn,
|
|
|
|
uint256 amountOutMinimum,
|
|
|
|
bool zeroForOne
|
|
|
|
) public returns (ISwapRouter.ExactInputSingleParams memory params) {
|
|
|
|
params = ISwapRouter.ExactInputSingleParams({
|
|
|
|
tokenIn: tokenIn,
|
|
|
|
tokenOut: tokenOut,
|
|
|
|
fee: fee,
|
|
|
|
recipient: recipient,
|
|
|
|
deadline: block.timestamp + 1,
|
|
|
|
amountIn: amountIn,
|
|
|
|
amountOutMinimum: amountOutMinimum,
|
|
|
|
sqrtPriceLimitX96: getPriceLimit(amountIn, zeroForOne)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function approveTransfer(
|
|
|
|
address tokenIn,
|
|
|
|
address sender,
|
|
|
|
address recipient,
|
|
|
|
uint256 amountIn
|
|
|
|
) public {
|
|
|
|
TransferHelper.safeTransferFrom(tokenIn, sender, recipient, amountIn);
|
|
|
|
|
|
|
|
TransferHelper.safeApprove(tokenIn, address(router), amountIn);
|
|
|
|
}
|
|
|
|
|
2021-11-19 14:22:34 +00:00
|
|
|
function swapSingleInput(ISwapRouter.ExactInputSingleParams memory params)
|
2021-11-19 12:59:55 +00:00
|
|
|
public
|
|
|
|
returns (uint256)
|
|
|
|
{
|
|
|
|
return (uint256(router.exactInputSingle(params)));
|
|
|
|
}
|
|
|
|
}
|