wp-evm-algebra-core 0.1.14

Pure data + quote + plan for Algebra-family CL DEXes
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
650
651
652
653
654
655
656
657
658
//! Pure plan functions for the Algebra family (V1.9 variant).
//!
//! Inputs: hydrated PoolState + computed Quote + params + slippage + deadline + config.
//! Output: a PlanFragment (calls + declared approvals + value).
//!
//! No I/O.
//!
//! # Calldata shape — swap
//!
//! AlgebraV1.9 `exactInputSingle` has **7 fields** and **no `fee` field**.
//! Function selector: `0xbc651188`.
//!
//! Compare to Uniswap V3's `exactInputSingle` which has **8 fields** including
//! a `uint24 fee` at position 2.
//!
//! # Calldata shape — liquidity management
//!
//! Task 4.5b NFPM research outcome (AlgebraV1.9 vs Uniswap V3):
//! - `mint(MintParams)`:          Algebra omits `fee` field → 10 fields vs 11.
//!   New `AlgebraMintParams` sol! binding required.
//! - `decreaseLiquidity(params)`: **identical** 5-field struct → reuse encoding.
//! - `collect(params)`:           **identical** 4-field struct → reuse encoding.
//! - `positions(tokenId)` return: Algebra omits `fee` → 11 fields vs 12.
//!
//! Source: `cryptoalgebra/AlgebraV1.9` `INonfungiblePositionManager.sol`.

use crate::data::{
    AddLiquidityParams, CollectFeesParams, ExactInParams, PlanFragment, PoolState, Quote,
    RemoveAndCollectParams, RemoveLiquidityParams,
};
use alloy_primitives::{aliases::U160, Address, U256};
use alloy_sol_types::{sol, SolCall};
use wp_evm_base::types::{Call, SlippageBps, TokenApproval};
use wp_evm_v3_core::plan::apply_slippage_min;

pub use wp_evm_v3_core::plan::{collectCall, decreaseLiquidityCall, increaseLiquidityCall};

sol! {
    /// AlgebraV1.9 exactInputSingle params — 7 fields, no fee.
    /// Selector: 0xbc651188
    #[derive(Debug)]
    struct AlgebraExactInputSingleParams {
        address tokenIn;
        address tokenOut;
        address recipient;
        uint256 deadline;
        uint256 amountIn;
        uint256 amountOutMinimum;
        uint160 limitSqrtPrice;
    }

    function exactInputSingle(AlgebraExactInputSingleParams params)
        external payable returns (uint256 amountOut);
}

/// Build an AlgebraV1.9 router `exactInputSingle` plan fragment.
///
/// Pure. No chain interaction. The caller must supply the deadline as an
/// absolute unix timestamp.
///
/// The emitted calldata has **no fee field** — fee is determined dynamically
/// by the pool at execution time. The selector is `0xbc651188`.
pub fn swap_exact_in(
    _state: &PoolState,
    quote: &Quote,
    params: &ExactInParams,
    slippage: SlippageBps,
    deadline: u64,
    router: Address,
) -> PlanFragment {
    let amount_out_min = apply_slippage_min(quote.amount_out, slippage);

    let call_params = AlgebraExactInputSingleParams {
        tokenIn: params.token_in,
        tokenOut: params.token_out,
        recipient: params.recipient,
        deadline: U256::from(deadline),
        amountIn: params.amount_in,
        amountOutMinimum: amount_out_min,
        limitSqrtPrice: U160::ZERO,
    };

    let calldata = exactInputSingleCall { params: call_params }.abi_encode().into();

    PlanFragment {
        calls: vec![Call { target: router, calldata, value: U256::ZERO }],
        approvals: vec![TokenApproval {
            token: params.token_in,
            spender: router,
            min_amount: params.amount_in,
        }],
        value: U256::ZERO,
    }
}

sol! {
    /// Algebra V1.9 `mint` MintParams — **10 fields, no `fee`**.
    ///
    /// Uniswap V3 has 11 fields with `uint24 fee` at position 2.
    /// Algebra V1.9 omits `fee` entirely because pool identity is
    /// `(token0, token1)` only — there is one pool per pair.
    #[derive(Debug)]
    struct AlgebraMintParams {
        address token0;
        address token1;
        int24   tickLower;
        int24   tickUpper;
        uint256 amount0Desired;
        uint256 amount1Desired;
        uint256 amount0Min;
        uint256 amount1Min;
        address recipient;
        uint256 deadline;
    }
    function mint(AlgebraMintParams params) external payable
        returns (uint256 tokenId, uint128 liquidity, uint256 amount0, uint256 amount1);
}

/// Build an Algebra V1.9 NFPM `mint` plan fragment (add liquidity).
///
/// Declares approvals for both tokens against the position manager.
/// `amount0Min` / `amount1Min` are derived from `slippage` applied to
/// `amount0_desired` / `amount1_desired`.
///
/// The emitted calldata uses `AlgebraMintParams` (10 fields, no `fee`).
/// Using a wp-evm-v3-core `MintParams` here would produce incorrect calldata
/// because Uniswap V3's struct has 11 fields (includes `uint24 fee`).
pub fn add_liquidity(
    params: &AddLiquidityParams,
    slippage: SlippageBps,
    deadline: u64,
    position_manager: Address,
) -> PlanFragment {
    let mint_params = AlgebraMintParams {
        token0: params.token0,
        token1: params.token1,
        tickLower: alloy_primitives::aliases::I24::try_from(params.tick_lower)
            .expect("tick_lower within i24 range"),
        tickUpper: alloy_primitives::aliases::I24::try_from(params.tick_upper)
            .expect("tick_upper within i24 range"),
        amount0Desired: params.amount0_desired,
        amount1Desired: params.amount1_desired,
        amount0Min: apply_slippage_min(params.amount0_desired, slippage),
        amount1Min: apply_slippage_min(params.amount1_desired, slippage),
        recipient: params.recipient,
        deadline: U256::from(deadline),
    };
    let calldata = mintCall { params: mint_params }.abi_encode().into();
    PlanFragment {
        calls: vec![Call { target: position_manager, calldata, value: U256::ZERO }],
        approvals: vec![
            TokenApproval {
                token: params.token0,
                spender: position_manager,
                min_amount: params.amount0_desired,
            },
            TokenApproval {
                token: params.token1,
                spender: position_manager,
                min_amount: params.amount1_desired,
            },
        ],
        value: U256::ZERO,
    }
}

/// Build an `increaseLiquidity` plan fragment for an existing Algebra NFT
/// position. Mirrors `wp_evm_v3_core::plan::increase_liquidity` shape —
/// Algebra's `IncreaseLiquidityParams` struct is byte-identical to V3's
/// (6 fields, no `fee`).
///
/// `token0` / `token1` are needed for the approval declaration and must
/// match the position's stored pair (the function does not validate this
/// — caller's responsibility, typically by hydrating the position first).
///
/// Approvals: token0 + token1 to NFPM (same as mint).
#[allow(clippy::too_many_arguments)]
pub fn increase_liquidity(
    token_id: U256,
    token0: Address,
    token1: Address,
    amount0_desired: U256,
    amount1_desired: U256,
    slippage: SlippageBps,
    deadline: u64,
    position_manager: Address,
) -> PlanFragment {
    wp_evm_v3_core::plan::increase_liquidity(
        token_id,
        token0,
        token1,
        amount0_desired,
        amount1_desired,
        slippage,
        deadline,
        position_manager,
    )
}

/// Build an Algebra V1.9 NFPM `decreaseLiquidity` plan fragment.
///
/// Struct is ABI-identical to Uniswap V3's `DecreaseLiquidityParams`
/// (same 5 fields, same order).
pub fn remove_liquidity(
    params: &RemoveLiquidityParams,
    deadline: u64,
    position_manager: Address,
) -> PlanFragment {
    wp_evm_v3_core::plan::remove_liquidity(params, deadline, position_manager)
}

/// Build an Algebra V1.9 NFPM `collect` plan fragment.
///
/// Uses `u128::MAX` for both `amount0Max` and `amount1Max` — the standard
/// "collect everything" sentinel. Struct is ABI-identical to Uniswap V3's
/// `CollectParams` (same 4 fields, same order).
pub fn collect_fees(params: &CollectFeesParams, position_manager: Address) -> PlanFragment {
    wp_evm_v3_core::plan::collect_fees(params, position_manager)
}

/// Build an atomic `multicall(decreaseLiquidity, collect)` plan fragment
/// for an Algebra V1.9 NFPM position.
///
/// `deadline` threads through `decreaseLiquidity`'s deadline param.
/// `collect`'s `amount0Max` / `amount1Max` are set to `u128::MAX` (collect
/// everything in `tokensOwed`).
///
/// Delegates straight to `wp_evm_v3_core::plan::remove_liquidity_and_collect`:
/// Algebra V1.9's `decreaseLiquidity`, `collect`, and outer
/// `multicall(bytes[])` ABIs are byte-identical to V3. The Algebra-specific
/// divergence is fee-less minting / dynamic swap fees, not these liquidity
/// removal records.
///
/// **Native unwrap is NOT done here** — this helper is pure encoding.
/// Facade-layer `plan_remove_liquidity_and_collect` composes
/// `unwrapWNativeToken` + `sweepToken` tails on top via
/// `wp_evm_v3_provider::plan::compose_native_remove_collect_multicall`.
pub fn remove_liquidity_and_collect(
    params: &RemoveAndCollectParams,
    deadline: u64,
    position_manager: Address,
) -> PlanFragment {
    wp_evm_v3_core::plan::remove_liquidity_and_collect(params, deadline, position_manager)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::data::AlgebraProtocolConfig;
    use alloy_primitives::{address, b256, Address};

    const TEST_CFG: AlgebraProtocolConfig = AlgebraProtocolConfig {
        factory: address!("0x411b0fAcC3489691f28ad58c47006AF5E3Ab3A28"),
        // Placeholder — this fixture's CREATE2 path is never exercised.
        pool_deployer: address!("0x1111111111111111111111111111111111111112"),
        router: address!("0xf5b509bB0909a69B1c207E495f687a596C168E12"),
        position_mgr: address!("0x8eF88E4c7CfbbaC1C163f7eddd4B578792201de6"),
        // Placeholder — visibly distinct from the canonical V1 hash
        // (`0x6ec6c9c8...294a4` in `wp-evm-quickswap/src/lib.rs`) so a
        // grep doesn't surface two near-identical values.
        init_code_hash: b256!("0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
        multicall: address!("0xcA11bde05977b3631167028862bE2a173976CA11"),
        quoter: None,
        farming_center: None,
    };

    fn dummy_pool_state() -> PoolState {
        PoolState {
            token0: address!("0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174"), // USDC.e
            token1: address!("0x7ceB23fD6bC0adD59E62ac25578270cFf1b9f619"), // WETH
            fee: 500,
            tick_spacing: 60,
            sqrt_price_x96: U256::from(1u64) << 96,
            liquidity: 0,
            tick: 0,
            ticks: vec![],
        }
    }

    fn dummy_quote(amount_in: U256, amount_out: U256) -> Quote {
        Quote {
            amount_in,
            amount_out,
            sqrt_price_x96_after: U256::from(1u64) << 96,
            price_impact_bps: 0,
            effective_fee_pips: 500,
        }
    }

    /// The AlgebraV1.9 `exactInputSingle` selector must be `0xbc651188`.
    ///
    /// This is derived from keccak256("exactInputSingle((address,address,address,uint256,uint256,uint256,uint160))")
    /// and must NOT match the Uniswap V3 selector `0x414bf389` which includes
    /// a `uint24 fee` field, making the tuple 8 fields instead of 7.
    #[test]
    fn selector_is_0xbc651188() {
        let expected: [u8; 4] = [0xbc, 0x65, 0x11, 0x88];
        assert_eq!(
            exactInputSingleCall::SELECTOR,
            expected,
            "AlgebraV1.9 selector mismatch — check that ExactInputSingleParams has 7 fields and no fee"
        );
    }

    /// Decode the calldata produced by `swap_exact_in` and verify that the
    /// struct has exactly 7 fields and no fee field.
    ///
    /// The decoded `AlgebraExactInputSingleParams` has no `fee` member —
    /// if it did, the struct definition would have 8 fields and the selector
    /// would not match `0xbc651188`.
    #[test]
    fn calldata_decodes_without_fee_field() {
        let s = dummy_pool_state();
        let token_in = s.token0;
        let token_out = s.token1;
        let amount_in = U256::from(1_000_000u64);
        let q = dummy_quote(amount_in, U256::from(500_000_000_000_000u64));
        let p = ExactInParams {
            token_in,
            token_out,
            amount_in,
            recipient: address!("0x0000000000000000000000000000000000000099"),
        };

        let frag = swap_exact_in(&s, &q, &p, SlippageBps::new(50), 9_999_999_999, TEST_CFG.router);
        assert_eq!(frag.calls.len(), 1);

        // Decode back and verify struct fields — no `fee` field present.
        let decoded = exactInputSingleCall::abi_decode(&frag.calls[0].calldata)
            .expect("calldata should round-trip through abi_decode");

        assert_eq!(decoded.params.tokenIn, token_in);
        assert_eq!(decoded.params.tokenOut, token_out);
        assert_eq!(decoded.params.recipient, p.recipient);
        assert_eq!(decoded.params.amountIn, amount_in);
        // limitSqrtPrice must be zero (no price limit)
        assert_eq!(decoded.params.limitSqrtPrice, U160::ZERO);

        // amount_out_minimum: 1% slippage of 500_000_000_000_000 = 497_500_000_000_000
        // with 50 bps: 500_000_000_000_000 * 9950 / 10000 = 497_500_000_000_000
        let expected_min = U256::from(497_500_000_000_000u64);
        assert_eq!(decoded.params.amountOutMinimum, expected_min);
    }

    #[test]
    fn swap_exact_in_calldata_round_trips_all_fields() {
        let s = dummy_pool_state();
        let token_in = s.token0;
        let token_out = s.token1;
        let amount_in = U256::from(1_000_000u64);
        let amount_out = U256::from(500_000_000_000_000u64);
        let q = dummy_quote(amount_in, amount_out);
        let recipient = address!("0000000000000000000000000000000000000099");
        let p = ExactInParams { token_in, token_out, amount_in, recipient };
        let deadline = 1_700_000_000u64;

        let frag = swap_exact_in(&s, &q, &p, SlippageBps::new(50), deadline, TEST_CFG.router);

        let decoded = exactInputSingleCall::abi_decode(&frag.calls[0].calldata)
            .expect("calldata should decode ok");
        let dp = decoded.params;
        assert_eq!(dp.tokenIn, token_in);
        assert_eq!(dp.tokenOut, token_out);
        assert_eq!(dp.recipient, recipient);
        assert_eq!(dp.deadline, U256::from(deadline));
        assert_eq!(dp.amountIn, amount_in);
        // amountOutMinimum = amount_out * (10000 - 50) / 10000
        let expected_min = amount_out * U256::from(9950u64) / U256::from(10000u64);
        assert_eq!(dp.amountOutMinimum, expected_min);
        assert_eq!(dp.limitSqrtPrice, U160::ZERO);
    }

    #[test]
    fn plan_targets_router_with_correct_approval() {
        let s = dummy_pool_state();
        let amount_in = U256::from(1_000_000u64);
        let q = dummy_quote(amount_in, U256::from(500_000_000_000_000u64));
        let p = ExactInParams {
            token_in: s.token0,
            token_out: s.token1,
            amount_in,
            recipient: address!("0x0000000000000000000000000000000000000099"),
        };

        let frag = swap_exact_in(&s, &q, &p, SlippageBps::new(50), 9_999_999_999, TEST_CFG.router);

        assert_eq!(frag.calls[0].target, TEST_CFG.router);
        assert_eq!(frag.approvals.len(), 1);
        assert_eq!(frag.approvals[0].token, s.token0);
        assert_eq!(frag.approvals[0].spender, TEST_CFG.router);
        assert_eq!(frag.approvals[0].min_amount, amount_in);
        assert_eq!(frag.value, U256::ZERO);
    }

    /// `mint` selector for Algebra V1.9 NFPM (10-field MintParams, no `fee`).
    ///
    /// keccak256("mint((address,address,int24,int24,uint256,uint256,uint256,uint256,address,uint256))")
    /// This must differ from the Uniswap V3 `mint` selector (11 fields, has `fee`).
    #[test]
    fn algebra_mint_selector_differs_from_uniswap_v3() {
        // Uniswap V3 mint: keccak256("mint((address,address,uint24,int24,int24,uint256,uint256,uint256,uint256,address,uint256))")
        // = 0x88316456
        let uniswap_v3_mint: [u8; 4] = [0x88, 0x31, 0x64, 0x56];
        assert_ne!(
            mintCall::SELECTOR,
            uniswap_v3_mint,
            "Algebra mint selector should differ from Uniswap V3 because Algebra MintParams has no fee field"
        );
    }

    #[test]
    fn plan_add_liquidity_targets_position_manager() {
        let p = AddLiquidityParams {
            token0: address!("0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174"),
            token1: address!("0x7ceB23fD6bC0adD59E62ac25578270cFf1b9f619"),
            tick_lower: -887_272,
            tick_upper: 887_272,
            amount0_desired: U256::from(1_000_000u64),
            amount1_desired: U256::from(500_000_000_000_000u64),
            recipient: address!("0x0000000000000000000000000000000000000099"),
        };
        let frag = add_liquidity(&p, SlippageBps::new(50), 9_999_999_999, TEST_CFG.position_mgr);
        assert_eq!(frag.calls.len(), 1);
        assert_eq!(frag.calls[0].target, TEST_CFG.position_mgr);
        assert_eq!(frag.approvals.len(), 2);
        assert_eq!(frag.approvals[0].token, p.token0);
        assert_eq!(frag.approvals[0].spender, TEST_CFG.position_mgr);
        assert_eq!(frag.approvals[1].token, p.token1);
        assert_eq!(frag.approvals[1].spender, TEST_CFG.position_mgr);
        assert_eq!(frag.value, U256::ZERO);
    }

    #[test]
    fn add_liquidity_calldata_round_trips_all_fields() {
        let p = AddLiquidityParams {
            token0: address!("0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174"),
            token1: address!("0x7ceB23fD6bC0adD59E62ac25578270cFf1b9f619"),
            tick_lower: -887_272,
            tick_upper: 887_272,
            amount0_desired: U256::from(1_000_000u64),
            amount1_desired: U256::from(500_000_000_000_000u64),
            recipient: address!("0x0000000000000000000000000000000000000099"),
        };
        let deadline = 1_700_000_000u64;
        let frag = add_liquidity(&p, SlippageBps::new(100), deadline, TEST_CFG.position_mgr);

        let decoded = mintCall::abi_decode(&frag.calls[0].calldata).expect("decode ok");
        let mp = decoded.params;
        assert_eq!(mp.token0, p.token0);
        assert_eq!(mp.token1, p.token1);
        assert_eq!(mp.tickLower, alloy_primitives::aliases::I24::try_from(-887_272i32).unwrap());
        assert_eq!(mp.tickUpper, alloy_primitives::aliases::I24::try_from(887_272i32).unwrap());
        assert_eq!(mp.amount0Desired, p.amount0_desired);
        assert_eq!(mp.amount1Desired, p.amount1_desired);
        // amount0Min = amount0_desired * 9900 / 10000 (100 bps = 1%)
        let expected_min0 = p.amount0_desired * U256::from(9900u64) / U256::from(10000u64);
        assert_eq!(mp.amount0Min, expected_min0);
        let expected_min1 = p.amount1_desired * U256::from(9900u64) / U256::from(10000u64);
        assert_eq!(mp.amount1Min, expected_min1);
        assert_eq!(mp.recipient, p.recipient);
        assert_eq!(mp.deadline, U256::from(deadline));
    }

    #[test]
    fn plan_increase_liquidity_targets_position_manager_two_approvals() {
        let token0 = address!("0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174");
        let token1 = address!("0x7ceB23fD6bC0adD59E62ac25578270cFf1b9f619");
        let frag = increase_liquidity(
            U256::from(123_456u64),
            token0,
            token1,
            U256::from(1_000_000u64),
            U256::from(500_000_000_000_000u64),
            SlippageBps::new(50),
            9_999_999_999,
            TEST_CFG.position_mgr,
        );
        assert_eq!(frag.calls.len(), 1);
        assert_eq!(frag.calls[0].target, TEST_CFG.position_mgr);
        assert_eq!(frag.approvals.len(), 2);
        assert_eq!(frag.approvals[0].token, token0);
        assert_eq!(frag.approvals[1].token, token1);
        assert_eq!(frag.value, U256::ZERO);
    }

    #[test]
    fn increase_liquidity_delegation_matches_v3_core_bytewise() {
        let token0 = address!("0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174");
        let token1 = address!("0x7ceB23fD6bC0adD59E62ac25578270cFf1b9f619");
        let token_id = U256::from(987_654u64);
        let amount0 = U256::from(2_000_000u64);
        let amount1 = U256::from(750_000_000_000_000u64);
        let slippage = SlippageBps::new(100);
        let deadline = 1_700_000_000u64;

        let ours = increase_liquidity(
            token_id,
            token0,
            token1,
            amount0,
            amount1,
            slippage,
            deadline,
            TEST_CFG.position_mgr,
        );
        let core = wp_evm_v3_core::plan::increase_liquidity(
            token_id,
            token0,
            token1,
            amount0,
            amount1,
            slippage,
            deadline,
            TEST_CFG.position_mgr,
        );

        assert_eq!(ours.calls[0].calldata, core.calls[0].calldata);
        assert_eq!(ours.calls[0].target, core.calls[0].target);
        assert_eq!(ours.approvals, core.approvals);
    }

    #[test]
    fn plan_remove_liquidity_targets_position_manager_no_approvals() {
        let p = RemoveLiquidityParams {
            token_id: U256::from(42u64),
            liquidity: 1_000_000_000_000u128,
            amount0_min: None,
            amount1_min: None,
        };
        let frag = remove_liquidity(&p, 9_999_999_999, TEST_CFG.position_mgr);
        assert_eq!(frag.calls.len(), 1);
        assert_eq!(frag.calls[0].target, TEST_CFG.position_mgr);
        assert!(frag.approvals.is_empty());
        assert_eq!(frag.value, U256::ZERO);
    }

    #[test]
    fn remove_liquidity_delegation_matches_v3_core_bytewise() {
        let p = RemoveLiquidityParams {
            token_id: U256::from(42u64),
            liquidity: 1_000_000_000_000u128,
            amount0_min: Some(U256::from(500_000u64)),
            amount1_min: Some(U256::from(1_000_000_000u64)),
        };
        let ours = remove_liquidity(&p, 9_999_999_999, TEST_CFG.position_mgr);
        let core = wp_evm_v3_core::plan::remove_liquidity(&p, 9_999_999_999, TEST_CFG.position_mgr);
        assert_eq!(ours.calls[0].calldata, core.calls[0].calldata);
        assert_eq!(ours.calls[0].target, core.calls[0].target);
    }

    #[test]
    fn remove_liquidity_default_mins_delegation_matches_v3_core_bytewise() {
        let p = RemoveLiquidityParams {
            token_id: U256::from(42u64),
            liquidity: 1_000_000_000_000u128,
            amount0_min: None,
            amount1_min: None,
        };
        let ours = remove_liquidity(&p, 9_999_999_999, TEST_CFG.position_mgr);
        let core = wp_evm_v3_core::plan::remove_liquidity(&p, 9_999_999_999, TEST_CFG.position_mgr);
        assert_eq!(ours.calls[0].calldata, core.calls[0].calldata);
        assert_eq!(ours.calls[0].target, core.calls[0].target);
    }

    #[test]
    fn plan_collect_fees_targets_position_manager_no_approvals() {
        let p = CollectFeesParams {
            token_id: U256::from(42u64),
            recipient: address!("0x0000000000000000000000000000000000000099"),
            token0: Address::ZERO,
            token1: Address::ZERO,
            caller: Address::ZERO,
        };
        let frag = collect_fees(&p, TEST_CFG.position_mgr);
        assert_eq!(frag.calls.len(), 1);
        assert_eq!(frag.calls[0].target, TEST_CFG.position_mgr);
        assert!(frag.approvals.is_empty());
        assert_eq!(frag.value, U256::ZERO);
    }

    #[test]
    fn collect_fees_delegation_matches_v3_core_bytewise() {
        let p = CollectFeesParams {
            token_id: U256::from(42u64),
            recipient: address!("0000000000000000000000000000000000000099"),
            token0: Address::ZERO,
            token1: Address::ZERO,
            caller: Address::ZERO,
        };
        let ours = collect_fees(&p, TEST_CFG.position_mgr);
        let core = wp_evm_v3_core::plan::collect_fees(&p, TEST_CFG.position_mgr);

        assert_eq!(ours.calls[0].calldata, core.calls[0].calldata);
        assert_eq!(ours.calls[0].target, core.calls[0].target);
    }

    fn fixture_remove_and_collect_params() -> RemoveAndCollectParams {
        RemoveAndCollectParams {
            token_id: U256::from(42u64),
            liquidity: 1_000_000_000_000u128,
            amount0_min: Some(U256::from(500_000u64)),
            amount1_min: Some(U256::from(1_000_000_000u64)),
            recipient: address!("0000000000000000000000000000000000000099"),
            token0: address!("0000000000000000000000000000000000000001"),
            token1: address!("0000000000000000000000000000000000000002"),
            caller: Address::ZERO,
            burn: false,
        }
    }

    #[test]
    fn plan_remove_liquidity_and_collect_targets_nfpm_with_outer_multicall() {
        // Outer call must be `multicall(bytes[])` — universal selector
        // `0xac9650d8` shared by V3 / Algebra / Ramses / Velodrome.
        let frag = remove_liquidity_and_collect(
            &fixture_remove_and_collect_params(),
            9_999_999_999,
            TEST_CFG.position_mgr,
        );
        assert_eq!(frag.calls.len(), 1);
        assert_eq!(frag.calls[0].target, TEST_CFG.position_mgr);
        assert_eq!(
            &frag.calls[0].calldata[..4],
            &[0xac, 0x96, 0x50, 0xd8],
            "outer selector must be multicall(bytes[])"
        );
        assert!(frag.approvals.is_empty());
        assert_eq!(frag.value, U256::ZERO);
    }

    #[test]
    fn remove_liquidity_and_collect_delegation_matches_v3_core_bytewise() {
        let p = fixture_remove_and_collect_params();
        let ours = remove_liquidity_and_collect(&p, 9_999_999_999, TEST_CFG.position_mgr);
        let core = wp_evm_v3_core::plan::remove_liquidity_and_collect(
            &p,
            9_999_999_999,
            TEST_CFG.position_mgr,
        );
        assert_eq!(ours.calls[0].calldata, core.calls[0].calldata);
        assert_eq!(ours.calls[0].target, core.calls[0].target);
    }

    #[test]
    fn remove_liquidity_and_collect_default_mins_delegation_matches_v3_core_bytewise() {
        let mut p = fixture_remove_and_collect_params();
        p.amount0_min = None;
        p.amount1_min = None;
        let ours = remove_liquidity_and_collect(&p, 9_999_999_999, TEST_CFG.position_mgr);
        let core = wp_evm_v3_core::plan::remove_liquidity_and_collect(
            &p,
            9_999_999_999,
            TEST_CFG.position_mgr,
        );
        assert_eq!(ours.calls[0].calldata, core.calls[0].calldata);
        assert_eq!(ours.calls[0].target, core.calls[0].target);
    }
}