tycho-execution 0.302.1

Provides tools for encoding and executing swaps against Tycho router and protocol executors.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.26;

import {IExecutor} from "@interfaces/IExecutor.sol";
import {ICallback} from "@interfaces/ICallback.sol";
import {ETH_ADDRESS} from "../../lib/NativeETH.sol";
import {
    IERC20,
    SafeERC20
} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {SwapParams} from "@uniswap/v4-core/src/types/PoolOperation.sol";
import {
    Currency,
    CurrencyLibrary
} from "@uniswap/v4-core/src/types/Currency.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {BalanceDelta} from "@uniswap/v4-core/src/types/BalanceDelta.sol";
import {TickMath} from "@uniswap/v4-core/src/libraries/TickMath.sol";
import {IHooks} from "@uniswap/v4-core/src/interfaces/IHooks.sol";
import {PathKey} from "@uniswap/v4-periphery/src/libraries/PathKey.sol";
import {
    IUnlockCallback
} from "@uniswap/v4-core/src/interfaces/callback/IUnlockCallback.sol";
import {
    SafeCast as V4SafeCast
} from "@uniswap/v4-core/src/libraries/SafeCast.sol";
import {
    TransientStateLibrary
} from "@uniswap/v4-core/src/libraries/TransientStateLibrary.sol";
import {TransferManager} from "../TransferManager.sol";
import {Address} from "@openzeppelin/contracts/utils/Address.sol";
import {
    LibPrefixLengthEncodedByteArray
} from "../../lib/bytes/LibPrefixLengthEncodedByteArray.sol";

error UniswapV4Executor__InvalidDataLength();
error UniswapV4Executor__NotPoolManager();
error UniswapV4Executor__UnknownCallback(bytes4 selector);
error UniswapV4Executor__DeltaNotPositive(Currency currency);
error UniswapV4Executor__DeltaNotNegative(Currency currency);
error UniswapV4Executor__V4TooMuchRequested(
    uint256 maxAmountInRequested, uint256 amountRequested
);
error UniswapV4Executor__InvalidAngstromAttestationDataLength(uint256 length);
error UniswapV4Executor__ZeroAddressAngstromHook();

contract UniswapV4Executor is IExecutor, ICallback {
    using SafeERC20 for IERC20;
    using CurrencyLibrary for Currency;
    using V4SafeCast for *;
    using TransientStateLibrary for IPoolManager;
    using LibPrefixLengthEncodedByteArray for bytes;

    bytes4 private immutable _swapExactInputSelector;
    bytes4 private immutable _swapExactInputSingleSelector;

    IPoolManager public immutable poolManager;
    address private immutable _angstromHookAddress;
    address private immutable _self;

    struct UniswapV4Pool {
        address intermediaryToken;
        uint24 fee;
        int24 tickSpacing;
        address hook;
        bytes hookData;
    }

    constructor(IPoolManager poolManager_, address angstromHook) {
        if (angstromHook == address(0)) {
            revert UniswapV4Executor__ZeroAddressAngstromHook();
        }
        poolManager = poolManager_;
        _angstromHookAddress = angstromHook;
        _self = address(this);
        _swapExactInputSelector = this.swapExactInput.selector;
        _swapExactInputSingleSelector = this.swapExactInputSingle.selector;
    }

    function fundsExpectedAddress(bytes calldata data)
        external
        view
        returns (address receiver)
    {
        bool skipUnlock = data[41] != 0;
        if (skipUnlock) {
            return address(poolManager);
        }
        return msg.sender;
    }

    /**
     * @dev Modifier to restrict access to only the pool manager.
     */
    modifier poolManagerOnly() virtual {
        if (msg.sender != address(poolManager)) {
            revert UniswapV4Executor__NotPoolManager();
        }
        _;
    }

    function swap(uint256 amountIn, bytes calldata data, address receiver)
        external
        payable
    {
        address tokenIn;
        address tokenOut;
        bool zeroForOne;
        bool skipUnlock;
        UniswapV4Executor.UniswapV4Pool[] memory pools;
        (tokenIn, tokenOut, zeroForOne, skipUnlock, pools) = _decodeData(data);
        // When skipUnlock=true, output must land at the router so the
        // Dispatcher can measure the balance diff and forward via
        // _transferOut (outputToRouter=true in getTransferData).
        address swapReceiver = skipUnlock ? address(this) : receiver;
        bytes memory swapData;
        bytes4 selector;
        if (pools.length == 1) {
            PoolKey memory key = PoolKey({
                currency0: Currency.wrap(zeroForOne ? tokenIn : tokenOut),
                currency1: Currency.wrap(zeroForOne ? tokenOut : tokenIn),
                fee: pools[0].fee,
                tickSpacing: pools[0].tickSpacing,
                hooks: IHooks(pools[0].hook)
            });
            selector = _swapExactInputSingleSelector;
            swapData = abi.encodeWithSelector(
                selector,
                key,
                zeroForOne,
                amountIn,
                swapReceiver,
                pools[0].hookData
            );
        } else {
            PathKey[] memory path = new PathKey[](pools.length);
            for (uint256 i = 0; i < pools.length; i++) {
                path[i] = PathKey({
                    intermediateCurrency: Currency.wrap(
                        pools[i].intermediaryToken
                    ),
                    fee: pools[i].fee,
                    tickSpacing: pools[i].tickSpacing,
                    hooks: IHooks(pools[i].hook),
                    hookData: pools[i].hookData
                });
            }

            Currency currencyIn = Currency.wrap(tokenIn);
            swapData = abi.encodeWithSelector(
                this.swapExactInput.selector,
                currencyIn,
                amountIn,
                swapReceiver,
                path
            );
        }
        if (skipUnlock) {
            // Caller must have called poolManager.sync(tokenIn)
            // and the Dispatcher transferred tokens to PM already.
            // This delegatecall is safe because the swapData signature is constructed
            // in this method.
            // slither-disable-next-line low-level-calls
            (bool success, bytes memory returnData) =
                _self.delegatecall(swapData);
            if (!success) {
                revert(
                    string(
                        returnData.length > 0
                            ? returnData
                            : abi.encodePacked(
                                "Uniswap v4 swap without unlock failed"
                            )
                    )
                );
            }
            // Snapshot PM's tokenOut balance so the Dispatcher's _transferOut (which
            // forwards output to PM for the next sequential hop) is detected by the
            // next settle(). This is safe to do even if there is no next hop.
            poolManager.sync(Currency.wrap(tokenOut));
        } else {
            poolManager.sync(Currency.wrap(tokenIn));
            // slither-disable-next-line unused-return
            poolManager.unlock(swapData);
        }
    }

    /// @dev Swap data uses ETH_ADDRESS for native ETH; translate to address(0) for V4 protocol interaction.
    function _toV4Token(address token) internal pure returns (address) {
        return token == ETH_ADDRESS ? address(0) : token;
    }

    // slither-disable-next-line dead-code
    function _decodeData(bytes calldata data)
        internal
        view
        virtual
        returns (
            address tokenIn,
            address tokenOut,
            bool zeroForOne,
            bool skipUnlock,
            UniswapV4Pool[] memory pools
        )
    {
        if (data.length < 90) {
            revert UniswapV4Executor__InvalidDataLength();
        }

        tokenIn = _toV4Token(address(bytes20(data[0:20])));
        tokenOut = _toV4Token(address(bytes20(data[20:40])));
        zeroForOne = data[40] != 0;
        skipUnlock = data[41] != 0;

        bytes calldata remaining = data[42:];

        // Decode first pool with hook data
        if (remaining.length < 48) {
            // 20 + 3 + 3 + 20 + 2 = 48 minimum
            revert UniswapV4Executor__InvalidDataLength();
        }

        address firstToken = _toV4Token(address(bytes20(remaining[0:20])));
        uint24 firstFee = uint24(bytes3(remaining[20:23]));
        int24 firstTickSpacing = int24(uint24(bytes3(remaining[23:26])));
        address firstHook = address(bytes20(remaining[26:46]));
        uint16 firstHookDataLength = uint16(bytes2(remaining[46:48]));

        uint256 firstPoolTotalLength = 48 + firstHookDataLength;
        if (remaining.length < firstPoolTotalLength) {
            revert UniswapV4Executor__InvalidDataLength();
        }

        bytes memory firstHookData;
        if (firstHook == _angstromHookAddress) {
            // Select attestation from first pool's hook data
            // Convert calldata to memory since _selectAttestation requires bytes memory
            firstHookData = _selectAttestation(
                bytes(remaining[48:48 + firstHookDataLength])
            );
        } else {
            firstHookData = bytes(remaining[48:48 + firstHookDataLength]);
        }

        // Remaining after first pool are ple encoded
        bytes[] memory encodedPools = LibPrefixLengthEncodedByteArray.toArray(
            remaining[firstPoolTotalLength:]
        );

        pools = new UniswapV4Pool[](1 + encodedPools.length);
        pools[0] = UniswapV4Pool(
            firstToken, firstFee, firstTickSpacing, firstHook, firstHookData
        );

        // Decode subsequent pools
        for (uint256 i = 0; i < encodedPools.length; i++) {
            bytes memory poolData = encodedPools[i];

            address intermediaryToken;
            uint24 fee;
            int24 tickSpacing;
            address hook;
            uint16 hookDataLength;

            // slither-disable-next-line assembly
            assembly {
                let dataPtr := add(poolData, 0x20)
                intermediaryToken := shr(96, mload(dataPtr))
                fee := and(shr(232, mload(add(dataPtr, 20))), 0xffffff)
                tickSpacing := and(shr(208, mload(add(dataPtr, 20))), 0xffffff)
                hook := shr(96, mload(add(dataPtr, 26)))
                hookDataLength := and(shr(240, mload(add(dataPtr, 46))), 0xffff)
            }

            intermediaryToken = _toV4Token(intermediaryToken);

            if (poolData.length < 48 + hookDataLength) {
                revert UniswapV4Executor__InvalidDataLength();
            }

            // Extract hookData bytes for this pool
            // We copy byte-by-byte because we cannot slice bytes memory in Solidity
            bytes memory rawHookData = new bytes(hookDataLength);
            for (uint256 j = 0; j < hookDataLength; j++) {
                rawHookData[j] = poolData[48 + j];
            }

            bytes memory hookData;
            if (hook == _angstromHookAddress) {
                // Select attestation from hookData
                hookData = _selectAttestation(rawHookData);
            } else {
                hookData = rawHookData;
            }

            pools[i + 1] = UniswapV4Pool(
                intermediaryToken, fee, tickSpacing, hook, hookData
            );
        }
    }

    /**
     * @notice Handles the callback from the pool manager. This is used for callbacks from the router.
     */
    function handleCallback(bytes calldata data)
        external
        returns (bytes memory)
    {
        bytes calldata stripped = data[68:];
        verifyCallback(stripped);
        // Our general callback logic returns a not ABI encoded result.
        // However, the pool manager expects the result to be ABI encoded. That is why we need to encode it here again.
        return abi.encode(_unlockCallback(stripped));
    }

    function verifyCallback(bytes calldata) public view poolManagerOnly {}

    /**
     * @dev Internal function to handle the unlock callback.
     */
    function _unlockCallback(bytes calldata data)
        internal
        returns (bytes memory)
    {
        bytes4 selector = bytes4(data[:4]);
        if (
            selector != _swapExactInputSelector
                && selector != _swapExactInputSingleSelector
        ) {
            revert UniswapV4Executor__UnknownCallback(selector);
        }

        // here we expect to call either `swapExactInputSingle` or `swapExactInput`. See `swap` to see how we encode the selector and the calldata
        // slither-disable-next-line low-level-calls
        (bool success, bytes memory returnData) = _self.delegatecall(data);
        if (!success) {
            revert(
                string(
                    returnData.length > 0
                        ? returnData
                        : abi.encodePacked("Uniswap v4 Callback failed")
                )
            );
        }
        return returnData;
    }

    /**
     * @notice Performs an exact input single swap. It settles and takes the tokens after the swap.
     * @param poolKey The key of the pool to swap in.
     * @param zeroForOne Whether the swap is from token0 to token1 (true) or vice versa (false).
     * @param amountIn The amount of tokens to swap in.
     * @param receiver The address of the receiver.
     * @param hookData Additional data for hook contracts.
     */
    function swapExactInputSingle(
        PoolKey memory poolKey,
        bool zeroForOne,
        uint128 amountIn,
        address receiver,
        bytes calldata hookData
    ) external {
        Currency currencyIn = zeroForOne ? poolKey.currency0 : poolKey.currency1;
        _settle(currencyIn, amountIn);

        uint256 swapAmountIn = _getFullCredit(currencyIn);

        uint128 amountOut = _swap(
                poolKey, zeroForOne, -int256(swapAmountIn), hookData
            ).toUint128();

        Currency currencyOut =
            zeroForOne ? poolKey.currency1 : poolKey.currency0;

        _take(currencyOut, receiver, _mapTakeAmount(amountOut, currencyOut));
    }

    /**
     * @notice Performs an exact input swap along a path. It settles and takes the tokens after the swap.
     * @param currencyIn The currency of the input token.
     * @param amountIn The amount of tokens to swap in.
     * @param receiver The address of the receiver.
     * @param path The path to swap along.
     */
    function swapExactInput(
        Currency currencyIn,
        uint128 amountIn,
        address receiver,
        PathKey[] calldata path
    ) external {
        uint128 amountOut = 0;
        Currency swapCurrencyIn = currencyIn;
        _settle(currencyIn, amountIn);

        uint256 swapAmountIn = _getFullCredit(currencyIn);

        unchecked {
            uint256 pathLength = path.length;
            PathKey calldata pathKey;

            for (uint256 i = 0; i < pathLength; i++) {
                pathKey = path[i];
                (PoolKey memory poolKey, bool zeroForOne) =
                    pathKey.getPoolAndSwapDirection(swapCurrencyIn);
                amountOut = _swap(
                        poolKey,
                        zeroForOne,
                        -int256(uint256(swapAmountIn)),
                        pathKey.hookData
                    ).toUint128();

                swapAmountIn = amountOut;
                swapCurrencyIn = pathKey.intermediateCurrency;
            }
        }

        _take(
            swapCurrencyIn, receiver, _mapTakeAmount(amountOut, swapCurrencyIn)
        );
    }

    function _swap(
        PoolKey memory poolKey,
        bool zeroForOne,
        int256 amountSpecified,
        bytes calldata hookData
    ) private returns (int128 reciprocalAmount) {
        unchecked {
            // slither-disable-next-line calls-loop
            BalanceDelta delta = poolManager.swap(
                poolKey,
                SwapParams(
                    zeroForOne,
                    amountSpecified,
                    zeroForOne
                        ? TickMath.MIN_SQRT_PRICE + 1
                        : TickMath.MAX_SQRT_PRICE - 1
                ),
                hookData
            );

            reciprocalAmount = (zeroForOne == amountSpecified < 0)
                ? delta.amount1()
                : delta.amount0();
        }
    }

    /**
     * @notice Obtains the full amount owed by this contract (negative delta).
     * @param currency The currency to get the delta for.
     * @return amount The amount owed by this contract.
     */
    function _getFullCredit(Currency currency)
        internal
        view
        returns (uint256 amount)
    {
        int256 _amount = poolManager.currencyDelta(address(this), currency);
        // If the amount is negative, it should be settled not taken.
        if (_amount < 0) revert UniswapV4Executor__DeltaNotPositive(currency);
        amount = uint256(_amount);
    }

    /**
     * @notice Pays and settles a currency to the pool manager.
     * @dev The implementing contract must ensure that the `payer` is a secure address.
     * @param currency The currency to settle.
     * @param amount The amount to send.
     * @dev Returns early if the amount is 0.
     */
    function _settle(Currency currency, uint256 amount) internal {
        if (amount == 0) return;
        if (currency.isAddressZero()) {
            // slither-disable-next-line unused-return
            poolManager.settle{value: amount}();
        } else {
            // slither-disable-next-line unused-return
            poolManager.settle();
        }
    }

    /**
     * @notice Takes an amount of currency out of the pool manager.
     * @param currency The currency to take.
     * @param recipient The address to receive the currency.
     * @param amount The amount to take.
     * @dev Returns early if the amount is 0.
     */
    function _take(Currency currency, address recipient, uint256 amount)
        internal
    {
        if (amount == 0) return;
        poolManager.take(currency, recipient, amount);
    }

    function _mapTakeAmount(uint256 amount, Currency currency)
        internal
        view
        returns (uint256)
    {
        if (amount == 0) {
            return _getFullCredit(currency);
        } else {
            return amount;
        }
    }

    /// @notice Selects the appropriate attestation for the current block number
    /// @dev Each attestation is exactly 93 bytes: 8 bytes blockNumber + 85 bytes attestation
    /// @param attestationData Raw bytes of encoded attestations for several blocks
    /// @return The attestation bytes for the current block, or empty bytes if no attestation found
    function _selectAttestation(bytes memory attestationData)
        internal
        view
        returns (bytes memory)
    {
        uint256 totalLength = 93;
        bytes memory attestation = new bytes(85);

        // Calculate number of attestations from data length
        if (attestationData.length % totalLength != 0) {
            revert UniswapV4Executor__InvalidAngstromAttestationDataLength(attestationData.length);
        }

        uint256 attestationCount = attestationData.length / totalLength;

        for (uint256 i = 0; i < attestationCount; i++) {
            uint256 offset = i * totalLength;

            // Assembly is used because attestationData is bytes memory
            uint64 blockNumber;
            // slither-disable-next-line assembly
            assembly {
                // Load block number (8 bytes) - shift right to get the first 8 bytes
                blockNumber := shr(
                    192,
                    mload(add(add(attestationData, 0x20), offset))
                )

                // Copy attestation (85 bytes)
                let src := add(add(attestationData, 0x20), add(offset, 8))
                let dst := add(attestation, 0x20)

                // Copy 85 bytes (2 full words + 21 bytes)
                mstore(dst, mload(src)) // Copy first 32 bytes
                mstore(add(dst, 0x20), mload(add(src, 0x20))) // Copy next 32 bytes
                mstore(add(dst, 0x40), mload(add(src, 0x40))) // Copy remaining 21 bytes (loads 32, but we only need 21)
            }

            // If we find the attestation for the current block, stop decoding early
            // and return the attestation.
            // slither-disable-next-line incorrect-equality
            if (blockNumber == block.number) {
                return attestation;
            }
        }

        // All attestations decoded and no attestation found for the current block.
        // Return empty bytes instead of reverting
        return "";
    }

    function getTransferData(bytes calldata data)
        external
        view
        returns (
            TransferManager.TransferType transferType,
            address receiver,
            address tokenIn,
            address tokenOut,
            bool outputToRouter
        )
    {
        tokenIn = address(bytes20(data[0:20]));
        tokenOut = address(bytes20(data[20:40]));
        bool skipUnlock = data[41] != 0;

        // In the case where the unlock is skipped - the callback is not performed.
        if (skipUnlock) {
            if (tokenIn == ETH_ADDRESS) {
                return (
                    TransferManager.TransferType.TransferNativeInExecutor,
                    address(0),
                    tokenIn,
                    tokenOut,
                    true
                );
            } else {
                return (
                    TransferManager.TransferType.Transfer,
                    address(poolManager),
                    tokenIn,
                    tokenOut,
                    true
                );
            }
        } else {
            return (
                TransferManager.TransferType.None,
                address(0),
                tokenIn,
                tokenOut,
                false
            );
        }
    }

    function getCallbackTransferData(
        bytes calldata, /* data */
        address tokenIn,
        address /* caller */
    )
        external
        view
        returns (TransferManager.TransferType transferType, address receiver)
    {
        receiver = address(poolManager);
        if (tokenIn == ETH_ADDRESS) {
            transferType = TransferManager.TransferType.TransferNativeInExecutor;
        } else {
            transferType = TransferManager.TransferType.Transfer;
        }
    }
}