From e1eecad8988520b496132a1fd82dee758e0d597e Mon Sep 17 00:00:00 2001 From: Tim Olson <> Date: Fri, 27 Oct 2023 16:07:02 -0400 Subject: [PATCH] uniswap execution fixes, logs, and tests --- src/UniswapSwapper.sol | 29 +++++++++++++++++++++++------ src/VaultAddress.sol | 2 +- test/TestOrder.sol | 24 +++++++++++++++++++++++- 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/src/UniswapSwapper.sol b/src/UniswapSwapper.sol index ba1cdab..90cc8a4 100644 --- a/src/UniswapSwapper.sol +++ b/src/UniswapSwapper.sol @@ -44,18 +44,20 @@ library UniswapSwapper { console2.log(uint(params.sqrtPriceLimitX96)); console2.log(address(Constants.uniswapV3SwapRouter)); - if (params.sqrtPriceLimitX96 == 0) - params.sqrtPriceLimitX96 = params.tokenIn < params.tokenOut ? TickMath.MIN_SQRT_RATIO+1 : TickMath.MAX_SQRT_RATIO-1; TransferHelper.safeApprove(params.tokenIn, address(Constants.uniswapV3SwapRouter), params.amount); +// if (params.sqrtPriceLimitX96 == 0) +// params.sqrtPriceLimitX96 = params.tokenIn < params.tokenOut ? TickMath.MIN_SQRT_RATIO+1 : TickMath.MAX_SQRT_RATIO-1; + console2.log('splx96'); console2.log(uint(params.sqrtPriceLimitX96)); + amountOut = Constants.uniswapV3SwapRouter.exactInputSingle(ISwapRouter.ExactInputSingleParams({ tokenIn: params.tokenIn, tokenOut: params.tokenOut, fee: params.fee, recipient: params.recipient, - deadline: block.timestamp, amountIn: params.amount, amountOutMinimum: 0, sqrtPriceLimitX96: params.sqrtPriceLimitX96 + deadline: block.timestamp, amountIn: params.amount, amountOutMinimum: 1, sqrtPriceLimitX96: params.sqrtPriceLimitX96 })); console2.log('swapped'); console2.log(amountOut); - IERC20(params.tokenIn).approve(address(Constants.uniswapV3SwapRouter), 0); + TransferHelper.safeApprove(params.tokenIn, address(Constants.uniswapV3SwapRouter), 0); } function swapExactOutput(SwapParams memory params) internal returns (uint256 amountIn) @@ -79,19 +81,34 @@ library UniswapSwapper { revert('IIA'); } uint256 maxAmountIn = balance; + console2.log('swapExactOutput approve...'); - TransferHelper.safeApprove(params.tokenIn, address(Constants.uniswapV3SwapRouter), maxAmountIn); + console2.log(address(this)); console2.log(params.tokenIn); + console2.log(params.tokenOut); + console2.log(uint(params.fee)); + console2.log(address(params.recipient)); + console2.log(params.amount); + console2.log(uint(params.sqrtPriceLimitX96)); console2.log(address(Constants.uniswapV3SwapRouter)); + console2.log('approve'); console2.log(maxAmountIn); + + TransferHelper.safeApprove(params.tokenIn, address(Constants.uniswapV3SwapRouter), maxAmountIn); + +// if (params.sqrtPriceLimitX96 == 0) +// params.sqrtPriceLimitX96 = params.tokenIn < params.tokenOut ? TickMath.MIN_SQRT_RATIO+1 : TickMath.MAX_SQRT_RATIO-1; + amountIn = Constants.uniswapV3SwapRouter.exactOutputSingle(ISwapRouter.ExactOutputSingleParams({ tokenIn: params.tokenIn, tokenOut: params.tokenOut, fee: params.fee, recipient: params.recipient, deadline: block.timestamp, amountOut: params.amount, amountInMaximum: maxAmountIn, sqrtPriceLimitX96: params.sqrtPriceLimitX96 })); + console2.log('swapped'); console2.log(amountIn); - IERC20(params.tokenIn).approve(address(Constants.uniswapV3SwapRouter), 0); + + TransferHelper.safeApprove(params.tokenIn, address(Constants.uniswapV3SwapRouter), 0); } } diff --git a/src/VaultAddress.sol b/src/VaultAddress.sol index 6a8c3fb..b394ace 100644 --- a/src/VaultAddress.sol +++ b/src/VaultAddress.sol @@ -11,7 +11,7 @@ library VaultAddress { // keccak-256 hash of the Vault's bytecode (not the deployed bytecode but the initialization bytecode) // can paste into: // https://emn178.github.io/online-tools/keccak_256.html - bytes32 public constant VAULT_INIT_CODE_HASH = 0xaa3457854b70ea8d66f3b73269f0cf34c7e2212e4b4bd8176e8388ff223a2bd0; + bytes32 public constant VAULT_INIT_CODE_HASH = 0x5548eccbb8c4c38711944ab3e79a6f320ae2eb4a6bb16c64215f13e7732f182c; // the contract being constructed must not have any constructor arguments or the determinism will be broken. instead, use a callback to // get construction arguments diff --git a/test/TestOrder.sol b/test/TestOrder.sol index 2960e60..6a8e5da 100644 --- a/test/TestOrder.sol +++ b/test/TestOrder.sol @@ -47,7 +47,29 @@ contract TestOrder is MockEnv, Test { vault.placeOrder(order); } - function testExecuteOrder() public { + function testExecuteOrderExactOutput() public { + OrderLib.Tranche[] memory tranches = new OrderLib.Tranche[](1); + OrderLib.Constraint[] memory constraints1 = new OrderLib.Constraint[](1); + constraints1[0] = OrderLib.Constraint(OrderLib.ConstraintMode.Time, bytes(hex"0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000046500")); + tranches[0] = OrderLib.Tranche(type(uint16).max,constraints1); + uint256 amount = 3*10**USD.decimals() / 10; // 0.3 USD + COIN.mint(address(vault), amount); // create COIN to sell + OrderLib.SwapOrder memory order = OrderLib.SwapOrder( + address(COIN), address(USD), // sell COIN for USD + OrderLib.Route(OrderLib.Exchange.UniswapV3, 500), amount, false, false, + OrderLib.NO_CHAIN, tranches + ); + uint64 orderIndex = vault.numSwapOrders(); + vault.placeOrder(order); + console2.log('placed order'); + console2.log(uint(orderIndex)); + string memory result; + vault.execute(orderIndex, 0, OrderLib.PriceProof(0)); + console2.log('executed'); + } + + + function testExecuteOrderExactInput() public { OrderLib.Tranche[] memory tranches = new OrderLib.Tranche[](1); OrderLib.Constraint[] memory constraints1 = new OrderLib.Constraint[](1); constraints1[0] = OrderLib.Constraint(OrderLib.ConstraintMode.Time, bytes(hex"0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000046500"));