SwapOrder.inverted bugfixes
This commit is contained in:
@@ -3,6 +3,7 @@ pragma solidity 0.8.26;
|
|||||||
|
|
||||||
import "@forge-std/console2.sol";
|
import "@forge-std/console2.sol";
|
||||||
import {IUniswapV3Pool} from "@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol";
|
import {IUniswapV3Pool} from "@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol";
|
||||||
|
import {FullMath} from "@uniswap/v3-core/contracts/libraries/FullMath.sol";
|
||||||
import {Util} from "./Util.sol";
|
import {Util} from "./Util.sol";
|
||||||
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
|
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
|
||||||
import {IEEE754, float} from "./IEEE754.sol";
|
import {IEEE754, float} from "./IEEE754.sol";
|
||||||
@@ -239,11 +240,15 @@ library OrderLib {
|
|||||||
// console2.log('slippage');
|
// console2.log('slippage');
|
||||||
uint256 protectedPrice = router.protectedPrice(order.route.exchange, order.tokenIn,
|
uint256 protectedPrice = router.protectedPrice(order.route.exchange, order.tokenIn,
|
||||||
order.tokenOut, order.route.fee, order.inverted);
|
order.tokenOut, order.route.fee, order.inverted);
|
||||||
// minIntercept is interpreted as the slippage ratio
|
// minLine.intercept is interpreted as the slippage ratio
|
||||||
uint256 slippage = uint256(tranche.minLine.intercept.toFixed(96));
|
uint256 slippage = uint256(tranche.minLine.intercept.toFixed(96));
|
||||||
v.limit = protectedPrice * 2**96 / (2**96+slippage);
|
bool buy = (order.tokenIn > order.tokenOut) != order.inverted;
|
||||||
|
v.limit = buy ?
|
||||||
|
FullMath.mulDiv( protectedPrice, 2**96+slippage, 2**96) :
|
||||||
|
FullMath.mulDiv( protectedPrice, 2**96, 2**96+slippage);
|
||||||
// console2.log(protectedPrice);
|
// console2.log(protectedPrice);
|
||||||
// console2.log(slippage);
|
// console2.log(slippage);
|
||||||
|
// console2.log(buy);
|
||||||
// console2.log(v.limit);
|
// console2.log(v.limit);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -70,9 +70,8 @@ contract UniswapV3Swapper {
|
|||||||
function _univ3_swap(IRouter.SwapParams memory params) internal
|
function _univ3_swap(IRouter.SwapParams memory params) internal
|
||||||
returns (uint256 amountIn, uint256 amountOut) {
|
returns (uint256 amountIn, uint256 amountOut) {
|
||||||
if( params.limitPriceX96 != 0 ) {
|
if( params.limitPriceX96 != 0 ) {
|
||||||
// convert to output/input which is what the _univ3_* methods expect
|
// convert to the standard tokenOut/tokenIn which is what the _univ3_* methods expect
|
||||||
bool inputInverted = params.tokenIn > params.tokenOut;
|
if (params.inverted) {
|
||||||
if (params.inverted!=inputInverted) {
|
|
||||||
// console2.log('inverting params.limitPriceX96');
|
// console2.log('inverting params.limitPriceX96');
|
||||||
// console2.log(params.limitPriceX96);
|
// console2.log(params.limitPriceX96);
|
||||||
params.limitPriceX96 = Util.invertX96(params.limitPriceX96);
|
params.limitPriceX96 = Util.invertX96(params.limitPriceX96);
|
||||||
@@ -111,13 +110,13 @@ contract UniswapV3Swapper {
|
|||||||
// console2.log(address(swapRouter));
|
// console2.log(address(swapRouter));
|
||||||
|
|
||||||
amountIn = params.amount;
|
amountIn = params.amount;
|
||||||
uint256 balance = IERC20(params.tokenIn).balanceOf(address(this));
|
uint256 startingBalanceIn = IERC20(params.tokenIn).balanceOf(address(this));
|
||||||
// console2.log('amountIn balance');
|
// console2.log('amountIn balance');
|
||||||
// console2.log(balance);
|
// console2.log(balance);
|
||||||
if( balance == 0 || balance < params.minAmount ) // minAmount is units of input token
|
if( startingBalanceIn == 0 || startingBalanceIn < params.minAmount ) // minAmount is units of input token
|
||||||
revert('IIA');
|
revert('IIA');
|
||||||
if( balance < amountIn )
|
if( startingBalanceIn < amountIn )
|
||||||
amountIn = balance;
|
amountIn = startingBalanceIn;
|
||||||
|
|
||||||
TransferHelper.safeApprove(params.tokenIn, address(swapRouter), amountIn);
|
TransferHelper.safeApprove(params.tokenIn, address(swapRouter), amountIn);
|
||||||
// if (params.sqrtPriceLimitX96 == 0)
|
// if (params.sqrtPriceLimitX96 == 0)
|
||||||
@@ -132,6 +131,8 @@ contract UniswapV3Swapper {
|
|||||||
tokenIn: params.tokenIn, tokenOut: params.tokenOut, fee: params.maxFee, recipient: params.recipient,
|
tokenIn: params.tokenIn, tokenOut: params.tokenOut, fee: params.maxFee, recipient: params.recipient,
|
||||||
deadline: block.timestamp, amountIn: amountIn, amountOutMinimum: 1, sqrtPriceLimitX96: sqrtPriceLimitX96
|
deadline: block.timestamp, amountIn: amountIn, amountOutMinimum: 1, sqrtPriceLimitX96: sqrtPriceLimitX96
|
||||||
}));
|
}));
|
||||||
|
uint256 endingBalanceIn = IERC20(params.tokenIn).balanceOf(address(this));
|
||||||
|
amountIn = startingBalanceIn - endingBalanceIn;
|
||||||
// console2.log('swapped');
|
// console2.log('swapped');
|
||||||
// console2.log(amountOut);
|
// console2.log(amountOut);
|
||||||
TransferHelper.safeApprove(params.tokenIn, address(swapRouter), 0);
|
TransferHelper.safeApprove(params.tokenIn, address(swapRouter), 0);
|
||||||
@@ -151,10 +152,11 @@ contract UniswapV3Swapper {
|
|||||||
// uint256 amountInMaximum;
|
// uint256 amountInMaximum;
|
||||||
// uint160 sqrtPriceLimitX96;
|
// uint160 sqrtPriceLimitX96;
|
||||||
// }
|
// }
|
||||||
uint256 balance = IERC20(params.tokenIn).balanceOf(address(this));
|
uint256 startingBalanceIn = IERC20(params.tokenIn).balanceOf(address(this));
|
||||||
if( balance == 0 )
|
if( startingBalanceIn == 0 )
|
||||||
revert('IIA');
|
revert('IIA');
|
||||||
uint256 maxAmountIn = balance;
|
uint256 maxAmountIn = startingBalanceIn;
|
||||||
|
uint256 startingBalanceOut = IERC20(params.tokenOut).balanceOf(address(this));
|
||||||
|
|
||||||
// console2.log('swapExactOutput');
|
// console2.log('swapExactOutput');
|
||||||
// console2.log(address(this));
|
// console2.log(address(this));
|
||||||
@@ -181,7 +183,8 @@ contract UniswapV3Swapper {
|
|||||||
sqrtPriceLimitX96: sqrtPriceLimitX96
|
sqrtPriceLimitX96: sqrtPriceLimitX96
|
||||||
})) returns (uint256 amtIn) {
|
})) returns (uint256 amtIn) {
|
||||||
amountIn = amtIn;
|
amountIn = amtIn;
|
||||||
amountOut = params.amount;
|
uint256 endingBalanceOut = IERC20(params.tokenOut).balanceOf(address(this));
|
||||||
|
amountOut = endingBalanceOut - startingBalanceOut;
|
||||||
}
|
}
|
||||||
catch Error( string memory reason ) {
|
catch Error( string memory reason ) {
|
||||||
// todo check reason before trying exactinput
|
// todo check reason before trying exactinput
|
||||||
@@ -190,7 +193,8 @@ contract UniswapV3Swapper {
|
|||||||
tokenIn: params.tokenIn, tokenOut: params.tokenOut, fee: params.maxFee, recipient: params.recipient,
|
tokenIn: params.tokenIn, tokenOut: params.tokenOut, fee: params.maxFee, recipient: params.recipient,
|
||||||
deadline: block.timestamp, amountIn: maxAmountIn, amountOutMinimum: 1, sqrtPriceLimitX96: sqrtPriceLimitX96
|
deadline: block.timestamp, amountIn: maxAmountIn, amountOutMinimum: 1, sqrtPriceLimitX96: sqrtPriceLimitX96
|
||||||
})) returns (uint256 amtOut) {
|
})) returns (uint256 amtOut) {
|
||||||
amountIn = maxAmountIn;
|
uint256 endingBalanceIn = IERC20(params.tokenIn).balanceOf(address(this));
|
||||||
|
amountIn = startingBalanceIn - endingBalanceIn;
|
||||||
amountOut = amtOut;
|
amountOut = amtOut;
|
||||||
}
|
}
|
||||||
catch Error( string memory ) {
|
catch Error( string memory ) {
|
||||||
|
|||||||
Reference in New Issue
Block a user