|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.26; |
| 3 | + |
| 4 | +import {Hooks} from "@uniswap/v4-core/src/libraries/Hooks.sol"; |
| 5 | +import {IHooks} from "@uniswap/v4-core/src/interfaces/IHooks.sol"; |
| 6 | +import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol"; |
| 7 | +import {Currency} from "@uniswap/v4-core/src/types/Currency.sol"; |
| 8 | +import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol"; |
| 9 | +import {IERC20Minimal} from "@uniswap/v4-core/src/interfaces/external/IERC20Minimal.sol"; |
| 10 | +// Internal imports |
| 11 | +import {LimitOrderHook, OrderIdLibrary} from "src/general/LimitOrderHook.sol"; |
| 12 | +import {LimitOrderHookMock} from "src/mocks/general/LimitOrderHookMock.sol"; |
| 13 | +import {CurrencySettler} from "src/utils/CurrencySettler.sol"; |
| 14 | +import {HookTest} from "../utils/HookTest.sol"; |
| 15 | + |
| 16 | +/// @dev Calls {CurrencySettler-settle} directly, to reach the library from outside a hook. |
| 17 | +contract CurrencySettlerMock { |
| 18 | + function settleNative(IPoolManager poolManager, address payer, uint256 amount) external { |
| 19 | + CurrencySettler.settle(Currency.wrap(address(0)), poolManager, payer, amount, false); |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +contract LimitOrderHookNativeTest is HookTest { |
| 24 | + LimitOrderHookMock hook; |
| 25 | + |
| 26 | + address user = makeAddr("user"); |
| 27 | + address swapper = makeAddr("swapper"); |
| 28 | + address attacker = makeAddr("attacker"); |
| 29 | + |
| 30 | + int24 tickSpacing; |
| 31 | + |
| 32 | + function setUp() public { |
| 33 | + deployFreshManagerAndRouters(); |
| 34 | + deployMintAndApprove2Currencies(); |
| 35 | + |
| 36 | + hook = LimitOrderHookMock(address(uint160(Hooks.AFTER_INITIALIZE_FLAG | Hooks.AFTER_SWAP_FLAG))); |
| 37 | + deployCodeTo( |
| 38 | + "src/mocks/general/LimitOrderHookMock.sol:LimitOrderHookMock", abi.encode(address(manager)), address(hook) |
| 39 | + ); |
| 40 | + |
| 41 | + (nativeKey,) = initPool(Currency.wrap(address(0)), currency1, IHooks(address(hook)), 3000, SQRT_PRICE_1_1); |
| 42 | + tickSpacing = nativeKey.tickSpacing; |
| 43 | + |
| 44 | + address[3] memory holders = [user, swapper, attacker]; |
| 45 | + for (uint256 i = 0; i < holders.length; i++) { |
| 46 | + deal(holders[i], 1e24); |
| 47 | + IERC20Minimal(Currency.unwrap(currency1)).transfer(holders[i], 1e30); |
| 48 | + |
| 49 | + vm.startPrank(holders[i]); |
| 50 | + IERC20Minimal(Currency.unwrap(currency1)).approve(address(hook), type(uint256).max); |
| 51 | + IERC20Minimal(Currency.unwrap(currency1)).approve(address(swapRouter), type(uint256).max); |
| 52 | + IERC20Minimal(Currency.unwrap(currency1)).approve(address(modifyLiquidityRouter), type(uint256).max); |
| 53 | + vm.stopPrank(); |
| 54 | + } |
| 55 | + |
| 56 | + deal(address(this), 1e24); |
| 57 | + } |
| 58 | + |
| 59 | + /// @dev The placer settles the currency their order sells, and the native currency is paid from the |
| 60 | + /// hook's balance rather than theirs. The hook serves no pool holding it, in either direction, so |
| 61 | + /// whatever balance it holds stays where it is. |
| 62 | + function test_placeOrder_native_reverts() public { |
| 63 | + uint256 seeded = 5 ether; |
| 64 | + vm.deal(address(hook), seeded); |
| 65 | + |
| 66 | + vm.prank(attacker); |
| 67 | + vm.expectRevert(LimitOrderHook.NativeCurrencyUnsupported.selector); |
| 68 | + hook.placeOrder(nativeKey, tickSpacing, true, 1e18); |
| 69 | + |
| 70 | + vm.prank(attacker); |
| 71 | + vm.expectRevert(LimitOrderHook.NativeCurrencyUnsupported.selector); |
| 72 | + hook.placeOrder(nativeKey, -tickSpacing, false, 1e18); |
| 73 | + |
| 74 | + assertEq(address(hook).balance, seeded, "no placement should reach the hook's balance"); |
| 75 | + assertEq(rawOrderIdOf(nativeKey, tickSpacing, true), 0, "no sell order should exist"); |
| 76 | + assertEq(rawOrderIdOf(nativeKey, -tickSpacing, false), 0, "no buy order should exist"); |
| 77 | + } |
| 78 | + |
| 79 | + /// @dev The library pays the native currency from the balance of the calling contract, so settling it |
| 80 | + /// for anyone else would spend a balance that payer never provided. |
| 81 | + function test_settle_nativeForAnotherPayer_reverts() public { |
| 82 | + CurrencySettlerMock settler = new CurrencySettlerMock(); |
| 83 | + |
| 84 | + vm.expectRevert(abi.encodeWithSelector(CurrencySettler.InvalidNativePayer.selector, attacker)); |
| 85 | + settler.settleNative(manager, attacker, 1 ether); |
| 86 | + } |
| 87 | + |
| 88 | + function rawOrderIdOf(PoolKey memory poolKey, int24 tickLower, bool zeroForOne) internal view returns (uint232) { |
| 89 | + return OrderIdLibrary.OrderId.unwrap(hook.getOrderId(poolKey, tickLower, zeroForOne)); |
| 90 | + } |
| 91 | +} |
0 commit comments