|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity 0.8.26; |
| 3 | + |
| 4 | +import {RoutingTestHelpers} from "../shared/RoutingTestHelpers.sol"; |
| 5 | +import {Planner} from "../shared/Planner.sol"; |
| 6 | +import {Actions} from "../../src/libraries/Actions.sol"; |
| 7 | +import {ActionConstants} from "../../src/libraries/ActionConstants.sol"; |
| 8 | +import {IV4Router} from "../../src/interfaces/IV4Router.sol"; |
| 9 | +import {Currency} from "@uniswap/v4-core/src/types/Currency.sol"; |
| 10 | +import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol"; |
| 11 | +import {IHooks} from "@uniswap/v4-core/src/interfaces/IHooks.sol"; |
| 12 | +import {MockERC20} from "solmate/src/test/utils/mocks/MockERC20.sol"; |
| 13 | +import {ModifyLiquidityParams} from "@uniswap/v4-core/src/types/PoolOperation.sol"; |
| 14 | + |
| 15 | +/// @notice Exact output is all-or-nothing. A v4 swap can partial-fill when a pool runs out of |
| 16 | +/// liquidity before the price limit; the router must revert `V4ExactOutputUnfilled` rather |
| 17 | +/// than silently deliver less than the requested `amountOut`. These drive real exact-output |
| 18 | +/// swaps through the router against a deliberately thin pool. |
| 19 | +contract V4RouterExactOutputUnfilledTest is RoutingTestHelpers { |
| 20 | + PoolKey internal thinKey; |
| 21 | + |
| 22 | + function setUp() public { |
| 23 | + setupRouterCurrenciesAndPoolsWithLiquidity(); |
| 24 | + plan = Planner.init(); |
| 25 | + // liquidity sits in a single tick-spacing band, so a 1e18 exact-output request cannot fully |
| 26 | + // fill before the price leaves the band |
| 27 | + thinKey = _createThinPool(currency0, currency1, 500, 0, 60); |
| 28 | + } |
| 29 | + |
| 30 | + function test_exactOutputSingle_revertsOnUnderfill() public { |
| 31 | + // buy currency0 by selling currency1 (oneForZero); the thin band cannot deliver 1e18 out |
| 32 | + IV4Router.ExactOutputSingleParams memory params = |
| 33 | + IV4Router.ExactOutputSingleParams(thinKey, false, 1 ether, type(uint128).max, 0, bytes("")); |
| 34 | + plan = plan.add(Actions.SWAP_EXACT_OUT_SINGLE, abi.encode(params)); |
| 35 | + bytes memory data = plan.finalizeSwap(currency1, currency0, ActionConstants.MSG_SENDER); |
| 36 | + |
| 37 | + vm.expectPartialRevert(IV4Router.V4ExactOutputUnfilled.selector); |
| 38 | + router.executeActions(data); |
| 39 | + } |
| 40 | + |
| 41 | + function test_exactOutput_multiHop_revertsOnUnderfill() public { |
| 42 | + // a 2-hop path currency2 -> currency1 -> currency0 whose final hop (currency1 -> currency0) |
| 43 | + // is the thin pool; the final hop cannot deliver 1e18 of currency0 |
| 44 | + tokenPath.push(currency2); |
| 45 | + tokenPath.push(currency1); |
| 46 | + tokenPath.push(currency0); |
| 47 | + IV4Router.ExactOutputParams memory params = _getExactOutputParams(tokenPath, 1 ether); |
| 48 | + // route the final hop through the thin pool by overriding its fee/tickSpacing in the path |
| 49 | + params.path[1].fee = 500; |
| 50 | + params.path[1].tickSpacing = 60; |
| 51 | + |
| 52 | + plan = plan.add(Actions.SWAP_EXACT_OUT, abi.encode(params)); |
| 53 | + bytes memory data = plan.finalizeSwap(currency2, currency0, ActionConstants.MSG_SENDER); |
| 54 | + |
| 55 | + vm.expectPartialRevert(IV4Router.V4ExactOutputUnfilled.selector); |
| 56 | + router.executeActions(data); |
| 57 | + } |
| 58 | + |
| 59 | + /// @notice An intermediate underfill cannot be left for settlement to catch when the path repeats |
| 60 | + /// a currency. Here the middle currency1 -> currency0 hop underfills, and because currency0 |
| 61 | + /// is also the route's input currency the shortfall merges into the input debt that |
| 62 | + /// SETTLE_ALL pays -- so no non-zero delta survives to fail settlement, and the |
| 63 | + /// post-loop amountInMaximum check only ever sees the last hop's own input. |
| 64 | + function test_exactOutput_revertsOnIntermediateUnderfill_repeatedCurrency() public { |
| 65 | + // deep pool for the final delivered hop, so only the middle hop is thin |
| 66 | + createPoolWithLiquidity(currency0, currency2, address(0)); |
| 67 | + |
| 68 | + // economic route currency0 -> currency1 -> currency0 -> currency2 |
| 69 | + tokenPath.push(currency0); |
| 70 | + tokenPath.push(currency1); |
| 71 | + tokenPath.push(currency0); |
| 72 | + tokenPath.push(currency2); |
| 73 | + IV4Router.ExactOutputParams memory params = _getExactOutputParams(tokenPath, 1 ether); |
| 74 | + // route the middle currency1 -> currency0 hop through the thin pool |
| 75 | + params.path[1].fee = 500; |
| 76 | + params.amountInMaximum = 0.8 ether; |
| 77 | + |
| 78 | + plan = plan.add(Actions.SWAP_EXACT_OUT, abi.encode(params)); |
| 79 | + bytes memory data = plan.finalizeSwap(currency0, currency2, ActionConstants.MSG_SENDER); |
| 80 | + |
| 81 | + vm.expectPartialRevert(IV4Router.V4ExactOutputUnfilled.selector); |
| 82 | + router.executeActions(data); |
| 83 | + } |
| 84 | + |
| 85 | + function test_exactOutputSingle_fullFillSucceeds() public { |
| 86 | + // the deep pool (key0) fully fills, unchanged by the new guard |
| 87 | + uint256 amountOut = 1 ether; |
| 88 | + IV4Router.ExactOutputSingleParams memory params = |
| 89 | + IV4Router.ExactOutputSingleParams(key0, true, uint128(amountOut), type(uint128).max, 0, bytes("")); |
| 90 | + plan = plan.add(Actions.SWAP_EXACT_OUT_SINGLE, abi.encode(params)); |
| 91 | + |
| 92 | + (, uint256 outputBalanceBefore,, uint256 outputBalanceAfter) = |
| 93 | + _finalizeAndExecuteSwap(currency0, currency1, 2 ether); |
| 94 | + |
| 95 | + assertEq(outputBalanceAfter - outputBalanceBefore, amountOut, "exact output delivered in full"); |
| 96 | + assertEq(currency0.balanceOf(address(router)), 0, "router holds no input residual"); |
| 97 | + assertEq(currency1.balanceOf(address(router)), 0, "router holds no output residual"); |
| 98 | + } |
| 99 | + |
| 100 | + function _createThinPool(Currency a, Currency b, uint24 fee, int24 lower, int24 upper) |
| 101 | + internal |
| 102 | + returns (PoolKey memory key) |
| 103 | + { |
| 104 | + if (Currency.unwrap(a) > Currency.unwrap(b)) (a, b) = (b, a); |
| 105 | + key = PoolKey(a, b, fee, 60, IHooks(address(0))); |
| 106 | + manager.initialize(key, SQRT_PRICE_1_1); |
| 107 | + MockERC20(Currency.unwrap(a)).approve(address(positionManager), type(uint256).max); |
| 108 | + MockERC20(Currency.unwrap(b)).approve(address(positionManager), type(uint256).max); |
| 109 | + positionManager.modifyLiquidity(key, ModifyLiquidityParams(lower, upper, 200 ether, 0), "0x"); |
| 110 | + } |
| 111 | +} |
0 commit comments