wp-evm-v4-core 0.1.14

Pure data + quote + plan for v4-family DEXes — no async dependencies
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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
//! Native V4 action sequencer — byte-identical replacement for
//! `uniswap_v4_sdk::utils::v4_planner::V4Planner`.
//!
//! ## Why this lives here, not in `wp-evm-v4-interfaces`
//!
//! The interfaces crate is pure ABI codegen — no encoding policy. The
//! SDK's `V4Planner` bakes in two policies that this module mirrors:
//!
//! 1. **Per-action encoding mode.** V4 periphery decodes the `params[i]`
//!    bytes blob with a different shape per action. Liquidity / settle /
//!    take actions use `abi.decode(params, (uint256, uint256, ..., bytes))`
//!    — i.e. tuple-flattened — while swap actions use
//!    `abi.decode(params, (StructType))` — i.e. struct-wrapped. The SDK
//!    encodes each `*Params` struct via `abi_encode_params()` or
//!    `abi_encode()` accordingly. Each `add_*` method below picks the
//!    matching encoder so calldata bytes are identical to the SDK output.
//!
//! 2. **Final unlockData layout.** `finalize()` emits
//!    `abi.encode(actions, params)` (params-form encoding of
//!    `ActionsParams`) — what the V4 PositionManager sees as `unlockData`.
//!
//! Slice C migrated `plan.rs` onto this planner (R2c fork-parity 0 bps);
//! Slice F retired `uniswap-v4-sdk` from the workspace entirely. The
//! tests below are now standalone golden-vector locks against `hex!()`
//! constants captured from SDK output before its removal.

use alloy_primitives::{address, Address, Bytes, U256};
use alloy_sol_types::{SolCall, SolValue};
use wp_evm_v4_interfaces::periphery::position_manager::{
    Actions, ActionsParams, BurnPositionParams, DecreaseLiquidityParams, IPositionManager,
    IncreaseLiquidityParams, MintPositionParams, SettleAllParams, SettlePairParams, SettleParams,
    SwapExactInParams, SwapExactInSingleParams, SwapExactOutParams, SwapExactOutSingleParams,
    SweepParams, TakeAllParams, TakePairParams, TakeParams, TakePortionParams,
};

/// `MSG_SENDER` recipient sentinel used by V4 PositionManager / Router
/// to mean "the transaction sender". V4 periphery treats this address
/// specially when resolving `recipient` parameters.
pub const MSG_SENDER: Address = address!("0000000000000000000000000000000000000001");

/// Indicates "use the open delta" when unwrapping WETH.
pub const OPEN_DELTA: U256 = U256::ZERO;

/// Native V4 action sequencer. Mirror of `uniswap_v4_sdk::utils::
/// v4_planner::V4Planner` but built over `wp-evm-v4-interfaces` types
/// with no transitive `uniswap-sdk-core` / `uniswap-v3-sdk` deps.
///
/// Fields are crate-private — callers compose flows through the typed
/// `add_*` helpers and read out via [`V4Planner::finalize`]. The SDK's
/// equivalent fields are `pub`, but we are retiring that shape and
/// don't want to inherit it as a public-API hazard under our 0.x
/// breaking-in-minor policy.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct V4Planner {
    /// Concatenation of 1-byte action discriminators (see [`Actions`]).
    pub(crate) actions: Vec<u8>,
    /// Per-action ABI-encoded params, parallel-indexed to `actions`.
    pub(crate) params: Vec<Bytes>,
}

impl V4Planner {
    /// Empty planner, ready for `add_*` calls.
    pub fn new() -> Self {
        Self::default()
    }

    // ---- Liquidity actions (params-form encoding) -----------------------

    /// Append `INCREASE_LIQUIDITY (0x00)` with `IncreaseLiquidityParams`.
    pub fn add_increase_liquidity(&mut self, params: IncreaseLiquidityParams) -> &mut Self {
        self.actions.push(Actions::INCREASE_LIQUIDITY);
        self.params.push(params.abi_encode_params().into());
        self
    }

    /// Append `DECREASE_LIQUIDITY (0x01)` with `DecreaseLiquidityParams`.
    pub fn add_decrease_liquidity(&mut self, params: DecreaseLiquidityParams) -> &mut Self {
        self.actions.push(Actions::DECREASE_LIQUIDITY);
        self.params.push(params.abi_encode_params().into());
        self
    }

    /// Append `MINT_POSITION (0x02)` with `MintPositionParams`.
    pub fn add_mint_position(&mut self, params: MintPositionParams) -> &mut Self {
        self.actions.push(Actions::MINT_POSITION);
        self.params.push(params.abi_encode_params().into());
        self
    }

    /// Append `BURN_POSITION (0x03)` with `BurnPositionParams`.
    pub fn add_burn_position(&mut self, params: BurnPositionParams) -> &mut Self {
        self.actions.push(Actions::BURN_POSITION);
        self.params.push(params.abi_encode_params().into());
        self
    }

    // ---- Swap actions (struct-form encoding) ----------------------------

    /// Append `SWAP_EXACT_IN_SINGLE (0x06)` with `SwapExactInSingleParams`.
    pub fn add_swap_exact_in_single(&mut self, params: SwapExactInSingleParams) -> &mut Self {
        self.actions.push(Actions::SWAP_EXACT_IN_SINGLE);
        self.params.push(params.abi_encode().into());
        self
    }

    /// Append `SWAP_EXACT_IN (0x07)` with `SwapExactInParams`.
    pub fn add_swap_exact_in(&mut self, params: SwapExactInParams) -> &mut Self {
        self.actions.push(Actions::SWAP_EXACT_IN);
        self.params.push(params.abi_encode().into());
        self
    }

    /// Append `SWAP_EXACT_OUT_SINGLE (0x08)` with `SwapExactOutSingleParams`.
    pub fn add_swap_exact_out_single(&mut self, params: SwapExactOutSingleParams) -> &mut Self {
        self.actions.push(Actions::SWAP_EXACT_OUT_SINGLE);
        self.params.push(params.abi_encode().into());
        self
    }

    /// Append `SWAP_EXACT_OUT (0x09)` with `SwapExactOutParams`.
    pub fn add_swap_exact_out(&mut self, params: SwapExactOutParams) -> &mut Self {
        self.actions.push(Actions::SWAP_EXACT_OUT);
        self.params.push(params.abi_encode().into());
        self
    }

    // ---- Settle actions (params-form encoding) --------------------------

    /// Append `SETTLE (0x0b)` with `SettleParams`.
    pub fn add_settle(&mut self, params: SettleParams) -> &mut Self {
        self.actions.push(Actions::SETTLE);
        self.params.push(params.abi_encode_params().into());
        self
    }

    /// Append `SETTLE_ALL (0x0c)` with `SettleAllParams`.
    pub fn add_settle_all(&mut self, params: SettleAllParams) -> &mut Self {
        self.actions.push(Actions::SETTLE_ALL);
        self.params.push(params.abi_encode_params().into());
        self
    }

    /// Append `SETTLE_PAIR (0x0d)` with `SettlePairParams`.
    pub fn add_settle_pair(&mut self, params: SettlePairParams) -> &mut Self {
        self.actions.push(Actions::SETTLE_PAIR);
        self.params.push(params.abi_encode_params().into());
        self
    }

    // ---- Take actions (params-form encoding) ----------------------------

    /// Append `TAKE (0x0e)` with `TakeParams`.
    pub fn add_take(&mut self, params: TakeParams) -> &mut Self {
        self.actions.push(Actions::TAKE);
        self.params.push(params.abi_encode_params().into());
        self
    }

    /// Append `TAKE_ALL (0x0f)` with `TakeAllParams`.
    pub fn add_take_all(&mut self, params: TakeAllParams) -> &mut Self {
        self.actions.push(Actions::TAKE_ALL);
        self.params.push(params.abi_encode_params().into());
        self
    }

    /// Append `TAKE_PORTION (0x10)` with `TakePortionParams`.
    pub fn add_take_portion(&mut self, params: TakePortionParams) -> &mut Self {
        self.actions.push(Actions::TAKE_PORTION);
        self.params.push(params.abi_encode_params().into());
        self
    }

    /// Append `TAKE_PAIR (0x11)` with `TakePairParams`.
    pub fn add_take_pair(&mut self, params: TakePairParams) -> &mut Self {
        self.actions.push(Actions::TAKE_PAIR);
        self.params.push(params.abi_encode_params().into());
        self
    }

    // ---- Other actions --------------------------------------------------

    /// Append `CLOSE_CURRENCY (0x12)` with the currency address. Encoded
    /// as a bare `address` (struct-form).
    pub fn add_close_currency(&mut self, currency: Address) -> &mut Self {
        self.actions.push(Actions::CLOSE_CURRENCY);
        self.params.push(currency.abi_encode().into());
        self
    }

    /// Append `SWEEP (0x14)` with `SweepParams`.
    pub fn add_sweep(&mut self, params: SweepParams) -> &mut Self {
        self.actions.push(Actions::SWEEP);
        self.params.push(params.abi_encode_params().into());
        self
    }

    /// Append `UNWRAP (0x16)` with the WETH amount. Encoded as a bare
    /// `uint256` (struct-form).
    pub fn add_unwrap(&mut self, amount: U256) -> &mut Self {
        self.actions.push(Actions::UNWRAP);
        self.params.push(amount.abi_encode().into());
        self
    }

    // ---- Finalization ---------------------------------------------------

    /// Encode the action sequence as a V4 `unlockData` blob —
    /// `abi.encode(actions, params)` in Solidity terms. Pass the result
    /// to [`encode_modify_liquidities`] to wrap in a PositionManager
    /// `modifyLiquidities` calldata.
    #[must_use]
    pub fn finalize(self) -> Bytes {
        ActionsParams { actions: self.actions.into(), params: self.params }
            .abi_encode_params()
            .into()
    }
}

/// Build calldata for `IPositionManager.modifyLiquidities(unlockData,
/// deadline)`. Mirror of `uniswap_v4_sdk::position_manager::
/// encode_modify_liquidities`.
#[must_use]
pub fn encode_modify_liquidities(unlock_data: Bytes, deadline: U256) -> Bytes {
    IPositionManager::modifyLiquiditiesCall { unlockData: unlock_data, deadline }
        .abi_encode()
        .into()
}

#[cfg(test)]
mod tests {
    //! Golden-vector tests for the native V4 action sequencer. Each
    //! test builds an action sequence through `V4Planner` and asserts
    //! byte-equality on `finalize()` against a hex constant captured
    //! from `uniswap_v4_sdk`'s `V4Planner` while the SDK was still in
    //! the dep tree (see PR #141 / #142 byte-equality parity tests).
    //! R17-pre Slice F retired the SDK dep; these constants now serve
    //! as the regression lock for the V4Planner encoding.
    //!
    //! Naming: `golden_<sequence>` for hex-constant byte-equality
    //! tests, `native_<flavor>` for native-only behavioural checks.

    use super::*;
    use alloy_primitives::{
        address,
        aliases::{I24, U24},
        hex, Address, U256,
    };
    use wp_evm_v4_interfaces::pool::PoolKey;

    // -- Reusable fixtures ----------------------------------------------

    fn pool_key_native() -> PoolKey {
        PoolKey {
            currency0: address!("4200000000000000000000000000000000000006"),
            currency1: address!("833589fcd6edb6e08f4c7c32d4f71b54bda02913"),
            fee: U24::from(500u32),
            tickSpacing: I24::try_from(10i32).unwrap(),
            hooks: Address::ZERO,
        }
    }

    const HOLDER: Address = address!("00000000219ab540356cBB839Cbe05303d7705Fa");
    const TICK_LOWER: i32 = -887_270;
    const TICK_UPPER: i32 = 887_270;

    // -- Golden-vector tests --------------------------------------------

    /// add_liquidity flow: MINT_POSITION + SETTLE_PAIR.
    #[test]
    fn golden_mint_position_then_settle_pair() {
        let mut native = V4Planner::new();
        native.add_mint_position(MintPositionParams {
            poolKey: pool_key_native(),
            tickLower: I24::try_from(TICK_LOWER).unwrap(),
            tickUpper: I24::try_from(TICK_UPPER).unwrap(),
            liquidity: U256::from(1_000_000u128),
            amount0Max: 1_000u128,
            amount1Max: 2_000u128,
            owner: HOLDER,
            hookData: Bytes::default(),
        });
        native.add_settle_pair(SettlePairParams {
            currency0: address!("4200000000000000000000000000000000000006"),
            currency1: address!("833589fcd6edb6e08f4c7c32d4f71b54bda02913"),
        });

        assert_eq!(
            native.finalize(),
            hex!(
                "0000000000000000000000000000000000000000000000000000000000000040"
                "0000000000000000000000000000000000000000000000000000000000000080"
                "0000000000000000000000000000000000000000000000000000000000000002"
                "020d000000000000000000000000000000000000000000000000000000000000"
                "0000000000000000000000000000000000000000000000000000000000000002"
                "0000000000000000000000000000000000000000000000000000000000000040"
                "0000000000000000000000000000000000000000000000000000000000000200"
                "00000000000000000000000000000000000000000000000000000000000001a0"
                "0000000000000000000000004200000000000000000000000000000000000006"
                "000000000000000000000000833589fcd6edb6e08f4c7c32d4f71b54bda02913"
                "00000000000000000000000000000000000000000000000000000000000001f4"
                "000000000000000000000000000000000000000000000000000000000000000a"
                "0000000000000000000000000000000000000000000000000000000000000000"
                "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff2761a"
                "00000000000000000000000000000000000000000000000000000000000d89e6"
                "00000000000000000000000000000000000000000000000000000000000f4240"
                "00000000000000000000000000000000000000000000000000000000000003e8"
                "00000000000000000000000000000000000000000000000000000000000007d0"
                "00000000000000000000000000000000219ab540356cbb839cbe05303d7705fa"
                "0000000000000000000000000000000000000000000000000000000000000180"
                "0000000000000000000000000000000000000000000000000000000000000000"
                "0000000000000000000000000000000000000000000000000000000000000040"
                "0000000000000000000000004200000000000000000000000000000000000006"
                "000000000000000000000000833589fcd6edb6e08f4c7c32d4f71b54bda02913"
            )[..],
        );
    }

    /// burn flow: BURN_POSITION + TAKE_PAIR.
    #[test]
    fn golden_burn_position_then_take_pair() {
        let mut native = V4Planner::new();
        native.add_burn_position(BurnPositionParams {
            tokenId: U256::from(42u64),
            amount0Min: 0u128,
            amount1Min: 0u128,
            hookData: Bytes::default(),
        });
        native.add_take_pair(TakePairParams {
            currency0: address!("4200000000000000000000000000000000000006"),
            currency1: address!("833589fcd6edb6e08f4c7c32d4f71b54bda02913"),
            recipient: MSG_SENDER,
        });

        assert_eq!(
            native.finalize(),
            hex!(
                "0000000000000000000000000000000000000000000000000000000000000040"
                "0000000000000000000000000000000000000000000000000000000000000080"
                "0000000000000000000000000000000000000000000000000000000000000002"
                "0311000000000000000000000000000000000000000000000000000000000000"
                "0000000000000000000000000000000000000000000000000000000000000002"
                "0000000000000000000000000000000000000000000000000000000000000040"
                "0000000000000000000000000000000000000000000000000000000000000100"
                "00000000000000000000000000000000000000000000000000000000000000a0"
                "000000000000000000000000000000000000000000000000000000000000002a"
                "0000000000000000000000000000000000000000000000000000000000000000"
                "0000000000000000000000000000000000000000000000000000000000000000"
                "0000000000000000000000000000000000000000000000000000000000000080"
                "0000000000000000000000000000000000000000000000000000000000000000"
                "0000000000000000000000000000000000000000000000000000000000000060"
                "0000000000000000000000004200000000000000000000000000000000000006"
                "000000000000000000000000833589fcd6edb6e08f4c7c32d4f71b54bda02913"
                "0000000000000000000000000000000000000000000000000000000000000001"
            )[..],
        );
    }

    /// partial-decrease + collect_fees flow: DECREASE_LIQUIDITY + TAKE_PAIR.
    #[test]
    fn golden_decrease_liquidity_then_take_pair() {
        let mut native = V4Planner::new();
        native.add_decrease_liquidity(DecreaseLiquidityParams {
            tokenId: U256::from(99u64),
            liquidity: U256::from(500_000u128),
            amount0Min: 1u128,
            amount1Min: 2u128,
            hookData: Bytes::default(),
        });
        native.add_take_pair(TakePairParams {
            currency0: address!("4200000000000000000000000000000000000006"),
            currency1: address!("833589fcd6edb6e08f4c7c32d4f71b54bda02913"),
            recipient: MSG_SENDER,
        });

        assert_eq!(
            native.finalize(),
            hex!(
                "0000000000000000000000000000000000000000000000000000000000000040"
                "0000000000000000000000000000000000000000000000000000000000000080"
                "0000000000000000000000000000000000000000000000000000000000000002"
                "0111000000000000000000000000000000000000000000000000000000000000"
                "0000000000000000000000000000000000000000000000000000000000000002"
                "0000000000000000000000000000000000000000000000000000000000000040"
                "0000000000000000000000000000000000000000000000000000000000000120"
                "00000000000000000000000000000000000000000000000000000000000000c0"
                "0000000000000000000000000000000000000000000000000000000000000063"
                "000000000000000000000000000000000000000000000000000000000007a120"
                "0000000000000000000000000000000000000000000000000000000000000001"
                "0000000000000000000000000000000000000000000000000000000000000002"
                "00000000000000000000000000000000000000000000000000000000000000a0"
                "0000000000000000000000000000000000000000000000000000000000000000"
                "0000000000000000000000000000000000000000000000000000000000000060"
                "0000000000000000000000004200000000000000000000000000000000000006"
                "000000000000000000000000833589fcd6edb6e08f4c7c32d4f71b54bda02913"
                "0000000000000000000000000000000000000000000000000000000000000001"
            )[..],
        );
    }

    /// swap_exact_in flow: SWAP_EXACT_IN_SINGLE + SETTLE_ALL + TAKE_ALL.
    /// Exercises the struct-form encoding path (swap action) alongside
    /// the params-form encoding path (settle/take) in the same blob.
    #[test]
    fn golden_swap_exact_in_single_then_settle_all_take_all() {
        let mut native = V4Planner::new();
        native.add_swap_exact_in_single(SwapExactInSingleParams {
            poolKey: pool_key_native(),
            zeroForOne: true,
            amountIn: 1_000_000u128,
            amountOutMinimum: 950_000u128,
            hookData: Bytes::default(),
        });
        native.add_settle_all(SettleAllParams {
            currency: address!("4200000000000000000000000000000000000006"),
            maxAmount: U256::from(1_000_000u128),
        });
        native.add_take_all(TakeAllParams {
            currency: address!("833589fcd6edb6e08f4c7c32d4f71b54bda02913"),
            minAmount: U256::from(950_000u128),
        });

        assert_eq!(
            native.finalize(),
            hex!(
                "0000000000000000000000000000000000000000000000000000000000000040"
                "0000000000000000000000000000000000000000000000000000000000000080"
                "0000000000000000000000000000000000000000000000000000000000000003"
                "060c0f0000000000000000000000000000000000000000000000000000000000"
                "0000000000000000000000000000000000000000000000000000000000000003"
                "0000000000000000000000000000000000000000000000000000000000000060"
                "00000000000000000000000000000000000000000000000000000000000001e0"
                "0000000000000000000000000000000000000000000000000000000000000240"
                "0000000000000000000000000000000000000000000000000000000000000160"
                "0000000000000000000000000000000000000000000000000000000000000020"
                "0000000000000000000000004200000000000000000000000000000000000006"
                "000000000000000000000000833589fcd6edb6e08f4c7c32d4f71b54bda02913"
                "00000000000000000000000000000000000000000000000000000000000001f4"
                "000000000000000000000000000000000000000000000000000000000000000a"
                "0000000000000000000000000000000000000000000000000000000000000000"
                "0000000000000000000000000000000000000000000000000000000000000001"
                "00000000000000000000000000000000000000000000000000000000000f4240"
                "00000000000000000000000000000000000000000000000000000000000e7ef0"
                "0000000000000000000000000000000000000000000000000000000000000120"
                "0000000000000000000000000000000000000000000000000000000000000000"
                "0000000000000000000000000000000000000000000000000000000000000040"
                "0000000000000000000000004200000000000000000000000000000000000006"
                "00000000000000000000000000000000000000000000000000000000000f4240"
                "0000000000000000000000000000000000000000000000000000000000000040"
                "000000000000000000000000833589fcd6edb6e08f4c7c32d4f71b54bda02913"
                "00000000000000000000000000000000000000000000000000000000000e7ef0"
            )[..],
        );
    }

    /// Full PositionManager calldata: native `encode_modify_liquidities`
    /// over a minimal BURN_POSITION sequence with deadline=1_800_000_000.
    /// Validates the outer `modifyLiquidities(unlockData, deadline)`
    /// envelope in addition to the unlockData payload, and pins the
    /// modifyLiquidities selector (0xdd46508f).
    #[test]
    fn golden_encode_modify_liquidities_full_calldata() {
        let mut native = V4Planner::new();
        native.add_burn_position(BurnPositionParams {
            tokenId: U256::from(7u64),
            amount0Min: 0u128,
            amount1Min: 0u128,
            hookData: Bytes::default(),
        });
        let unlock_native = native.finalize();
        let deadline = U256::from(1_800_000_000u64);
        let calldata_native = encode_modify_liquidities(unlock_native, deadline);

        assert_eq!(
            &calldata_native[..],
            &hex!(
                "dd46508f00000000000000000000000000000000000000000000000000000000"
                "0000004000000000000000000000000000000000000000000000000000000000"
                "6b49d20000000000000000000000000000000000000000000000000000000000"
                "0000018000000000000000000000000000000000000000000000000000000000"
                "0000004000000000000000000000000000000000000000000000000000000000"
                "0000008000000000000000000000000000000000000000000000000000000000"
                "0000000103000000000000000000000000000000000000000000000000000000"
                "0000000000000000000000000000000000000000000000000000000000000000"
                "0000000100000000000000000000000000000000000000000000000000000000"
                "0000002000000000000000000000000000000000000000000000000000000000"
                "000000a000000000000000000000000000000000000000000000000000000000"
                "0000000700000000000000000000000000000000000000000000000000000000"
                "0000000000000000000000000000000000000000000000000000000000000000"
                "0000000000000000000000000000000000000000000000000000000000000000"
                "0000008000000000000000000000000000000000000000000000000000000000"
                "00000000"
            )[..],
        );

        // Selector lock — first 4 bytes are 0xdd46508f for
        // modifyLiquidities(bytes,uint256).
        assert_eq!(&calldata_native[..4], &hex!("dd46508f"));
    }

    // -- Native-only checks ---------------------------------------------

    /// MSG_SENDER constant must match the V4 periphery sentinel
    /// (`address(1)`).
    #[test]
    fn native_msg_sender_matches_periphery_sentinel() {
        assert_eq!(MSG_SENDER, address!("0000000000000000000000000000000000000001"));
    }

    /// Empty planner finalizes to a four-word `abi.encode((bytes,
    /// bytes[]))` envelope: two pointer-head slots + two zero-length
    /// count words = 4 × 32 = 128 bytes. Pinning the exact bytes
    /// catches regressions that happen to preserve the byte count.
    #[test]
    fn golden_empty_planner_envelope() {
        let native = V4Planner::new().finalize();
        assert_eq!(
            &native[..],
            &hex!(
                "0000000000000000000000000000000000000000000000000000000000000040"
                "0000000000000000000000000000000000000000000000000000000000000060"
                "0000000000000000000000000000000000000000000000000000000000000000"
                "0000000000000000000000000000000000000000000000000000000000000000"
            )[..],
        );
        assert_eq!(native.len(), 128);
    }

    // -- Encoding-class boundary tests ----------------------------------
    //
    // The four swap arms share one encoding policy and the
    // `golden_swap_exact_in_single_*` test above exercises it. The
    // tests below cover the *other* struct-form arms — multi-hop swap
    // (still struct-form but with dynamic array fields), bare-`address`
    // CLOSE_CURRENCY, bare-`uint256` UNWRAP — so a wrong-encoder edit
    // in any of them is caught here rather than at runtime against
    // a deployed PositionManager.

    /// Multi-hop swap: SWAP_EXACT_IN with non-empty `path` and
    /// `maxHopSlippage`. Struct-form encoding with dynamic array
    /// payload — distinct calldata shape from SWAP_EXACT_IN_SINGLE.
    #[test]
    fn golden_swap_exact_in_multi_hop() {
        let path = vec![
            wp_evm_v4_interfaces::periphery::position_manager::PathKey {
                intermediateCurrency: address!("833589fcd6edb6e08f4c7c32d4f71b54bda02913"),
                fee: U256::from(500u32),
                tickSpacing: I24::try_from(10i32).unwrap(),
                hooks: Address::ZERO,
                hookData: Bytes::default(),
            },
            wp_evm_v4_interfaces::periphery::position_manager::PathKey {
                intermediateCurrency: address!("aaaaaaaa17b8b6e08f4c7c32d4f71b54bda02913"),
                fee: U256::from(3_000u32),
                tickSpacing: I24::try_from(60i32).unwrap(),
                hooks: Address::ZERO,
                hookData: Bytes::default(),
            },
        ];
        let mut native = V4Planner::new();
        native.add_swap_exact_in(SwapExactInParams {
            currencyIn: address!("4200000000000000000000000000000000000006"),
            path,
            maxHopSlippage: vec![U256::from(50u64), U256::from(50u64)],
            amountIn: 1_000_000u128,
            amountOutMinimum: 950_000u128,
        });

        assert_eq!(
            native.finalize(),
            hex!(
                "0000000000000000000000000000000000000000000000000000000000000040"
                "0000000000000000000000000000000000000000000000000000000000000080"
                "0000000000000000000000000000000000000000000000000000000000000001"
                "0700000000000000000000000000000000000000000000000000000000000000"
                "0000000000000000000000000000000000000000000000000000000000000001"
                "0000000000000000000000000000000000000000000000000000000000000020"
                "0000000000000000000000000000000000000000000000000000000000000300"
                "0000000000000000000000000000000000000000000000000000000000000020"
                "0000000000000000000000004200000000000000000000000000000000000006"
                "00000000000000000000000000000000000000000000000000000000000000a0"
                "0000000000000000000000000000000000000000000000000000000000000280"
                "00000000000000000000000000000000000000000000000000000000000f4240"
                "00000000000000000000000000000000000000000000000000000000000e7ef0"
                "0000000000000000000000000000000000000000000000000000000000000002"
                "0000000000000000000000000000000000000000000000000000000000000040"
                "0000000000000000000000000000000000000000000000000000000000000100"
                "000000000000000000000000833589fcd6edb6e08f4c7c32d4f71b54bda02913"
                "00000000000000000000000000000000000000000000000000000000000001f4"
                "000000000000000000000000000000000000000000000000000000000000000a"
                "0000000000000000000000000000000000000000000000000000000000000000"
                "00000000000000000000000000000000000000000000000000000000000000a0"
                "0000000000000000000000000000000000000000000000000000000000000000"
                "000000000000000000000000aaaaaaaa17b8b6e08f4c7c32d4f71b54bda02913"
                "0000000000000000000000000000000000000000000000000000000000000bb8"
                "000000000000000000000000000000000000000000000000000000000000003c"
                "0000000000000000000000000000000000000000000000000000000000000000"
                "00000000000000000000000000000000000000000000000000000000000000a0"
                "0000000000000000000000000000000000000000000000000000000000000000"
                "0000000000000000000000000000000000000000000000000000000000000002"
                "0000000000000000000000000000000000000000000000000000000000000032"
                "0000000000000000000000000000000000000000000000000000000000000032"
            )[..],
        );
    }

    /// CLOSE_CURRENCY (0x12): bare `address` payload, struct-form
    /// encoding. A wrong-encoder regression in `add_close_currency`
    /// would slip past the params-form-only tests above.
    #[test]
    fn golden_close_currency() {
        let mut native = V4Planner::new();
        native.add_close_currency(address!("4200000000000000000000000000000000000006"));

        assert_eq!(
            native.finalize(),
            hex!(
                "0000000000000000000000000000000000000000000000000000000000000040"
                "0000000000000000000000000000000000000000000000000000000000000080"
                "0000000000000000000000000000000000000000000000000000000000000001"
                "1200000000000000000000000000000000000000000000000000000000000000"
                "0000000000000000000000000000000000000000000000000000000000000001"
                "0000000000000000000000000000000000000000000000000000000000000020"
                "0000000000000000000000000000000000000000000000000000000000000020"
                "0000000000000000000000004200000000000000000000000000000000000006"
            )[..],
        );
    }

    /// UNWRAP (0x16): bare `uint256` payload, struct-form encoding.
    #[test]
    fn golden_unwrap() {
        let mut native = V4Planner::new();
        native.add_unwrap(U256::from(1_500_000u128));

        assert_eq!(
            native.finalize(),
            hex!(
                "0000000000000000000000000000000000000000000000000000000000000040"
                "0000000000000000000000000000000000000000000000000000000000000080"
                "0000000000000000000000000000000000000000000000000000000000000001"
                "1600000000000000000000000000000000000000000000000000000000000000"
                "0000000000000000000000000000000000000000000000000000000000000001"
                "0000000000000000000000000000000000000000000000000000000000000020"
                "0000000000000000000000000000000000000000000000000000000000000020"
                "000000000000000000000000000000000000000000000000000000000016e360"
            )[..],
        );
    }
}