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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
use std::time::{SystemTime, UNIX_EPOCH};

use crate::account::CliAccount;
use crate::cli::{SigningArgs, SwapTokenCli, SwapVenueCli, TokenCommand, TokenSwapArgs};
use crate::common::{confirm_submission, resolve_account, resolve_address, resolve_network};
use aleph_sdk::credit::{EthereumConfig, format_token_amount, parse_token_amount};
use aleph_sdk::swap::cow::CowApi;
use aleph_sdk::swap::cow::chains::cow_chain;
use aleph_sdk::swap::cow::ethflow::create_eth_order;
use aleph_sdk::swap::cow::order::{AppData, VAULT_RELAYER};
use aleph_sdk::swap::uniswap::chains::uniswap_chain;
use aleph_sdk::swap::uniswap::router::execute_swap;
use aleph_sdk::swap::uniswap::{self, UniswapQuote};
use aleph_sdk::swap::{SwapQuote, SwapRequest, SwapToken, cow, ensure_allowance};
use aleph_types::account::EvmAccount;
use alloy_network::EthereumWallet;
use alloy_primitives::Address;
use alloy_provider::{Provider, ProviderBuilder};
use alloy_signer_local::PrivateKeySigner;
use anyhow::{Result, anyhow};

/// Decimals for ALEPH token (ERC-20, 18 decimals like most Ethereum tokens).
const ALEPH_DECIMALS: u8 = 18;

impl From<SwapTokenCli> for SwapToken {
    fn from(v: SwapTokenCli) -> Self {
        match v {
            SwapTokenCli::Eth => SwapToken::Eth,
            SwapTokenCli::Usdc => SwapToken::Usdc,
        }
    }
}

pub async fn handle_token_command(
    json: bool,
    command: TokenCommand,
    cli_network: Option<&str>,
) -> Result<()> {
    match command {
        TokenCommand::Swap(args) => handle_swap(json, args, cli_network).await,
    }
}

/// Validate that `percent` is in the range `0.0..=50.0` (inclusive) and is
/// not NaN. Returns the fractional form (`percent / 100`) on success.
fn validate_slippage(percent: f64) -> Result<f64> {
    if percent.is_nan() || !(0.0..=50.0).contains(&percent) {
        return Err(anyhow!(
            "--slippage must be between 0 and 50 (percent); got {}",
            percent
        ));
    }
    Ok(percent / 100.0)
}

fn build_swap_provider(
    evm_account: &EvmAccount,
    rpc_url: &str,
) -> Result<(impl Provider, Address, PrivateKeySigner)> {
    let signer = PrivateKeySigner::from_signing_key(evm_account.signing_key().clone());
    let address = signer.address();
    let url = rpc_url
        .parse()
        .map_err(|e| anyhow!("invalid RPC URL: {e}"))?;
    let provider = ProviderBuilder::new()
        .wallet(EthereumWallet::from(signer.clone()))
        .connect_http(url);
    Ok((provider, address, signer))
}

fn print_swap_quote(sell_token: SwapToken, quote: &SwapQuote) {
    let sell_display = format_token_amount(quote.sell_amount, sell_token.decimals());
    let buy_display = format_token_amount(quote.buy_amount, ALEPH_DECIMALS);
    let min_display = format_token_amount(quote.min_buy_amount, ALEPH_DECIMALS);
    let fee_display = format_token_amount(quote.fee_amount, sell_token.decimals());
    eprintln!(
        "Swapping {} {} for ALEPH via CoW Swap",
        sell_display,
        sell_token.symbol()
    );
    eprintln!("  Expected:     ~{buy_display} ALEPH");
    eprintln!("  Min received: {min_display} ALEPH");
    eprintln!(
        "  Fee:          {fee_display} {} (informational, taken from sell amount)",
        sell_token.symbol()
    );
}

fn print_uniswap_quote(sell_token: SwapToken, quote: &UniswapQuote) {
    let sell_display = format_token_amount(quote.sell_amount, sell_token.decimals());
    let buy_display = format_token_amount(quote.buy_amount, ALEPH_DECIMALS);
    let min_display = format_token_amount(quote.min_buy_amount, ALEPH_DECIMALS);
    eprintln!(
        "Swapping {} {} for ALEPH via Uniswap",
        sell_display,
        sell_token.symbol()
    );
    eprintln!("  Expected:     ~{buy_display} ALEPH");
    eprintln!("  Min received: {min_display} ALEPH");
    eprintln!(
        "  Pool fee:     {} (taken in the pool price; gas paid separately)",
        quote.route.fee_display()
    );
}

async fn handle_swap(json: bool, args: TokenSwapArgs, cli_network: Option<&str>) -> Result<()> {
    let slippage_frac = validate_slippage(args.slippage)?;

    let evm_account = resolve_swap_evm_account(&args.signing)?;
    let network = resolve_network(cli_network)?;
    let ethereum = network.ethereum.ok_or_else(|| {
        anyhow!(
            "network '{}' has no ethereum settlement config; \
             run: aleph config network set --network {} --rpc-url <URL> --credit-contract <ADDR> \
                  --aleph-token <ADDR> --usdc-token <ADDR> --price-source <coingecko|fixed:N|none>",
            network.name,
            network.name
        )
    })?;

    let rpc_url = args.rpc_url.as_deref().unwrap_or(&ethereum.rpc_url);

    let sell_token: SwapToken = args.sell_token.into();
    let sell_amount_raw = parse_token_amount(&args.amount, sell_token.decimals())
        .map_err(|e| anyhow!("invalid amount: {e}"))?;

    let (provider, owner, signer) = build_swap_provider(&evm_account, rpc_url)?;

    let receiver: Address = match &args.receiver {
        Some(r) => {
            let aleph_addr = resolve_address(r)?;
            aleph_addr
                .to_string()
                .parse::<Address>()
                .map_err(|e| anyhow!("invalid receiver address '{}': {e} (the receiver must be an EVM 0x... address)", r))?
        }
        None => owner,
    };

    let chain_id = provider
        .get_chain_id()
        .await
        .map_err(|e| anyhow!("failed to get chain ID: {e}"))?;

    let req = SwapRequest {
        sell_token,
        sell_amount: sell_amount_raw,
        buy_token: ethereum.aleph_token,
        receiver,
        from: owner,
        slippage: slippage_frac,
        valid_for_secs: args.valid_for,
    };

    match args.venue {
        SwapVenueCli::Cow => {
            swap_via_cow(
                json,
                &args,
                &network.name,
                &ethereum,
                chain_id,
                &provider,
                owner,
                receiver,
                &signer,
                &req,
            )
            .await
        }
        SwapVenueCli::Uniswap => {
            swap_via_uniswap(
                json,
                &args,
                &network.name,
                &ethereum,
                chain_id,
                &provider,
                owner,
                receiver,
                &req,
            )
            .await
        }
        SwapVenueCli::Ophis => {
            swap_via_ophis(
                json,
                &args,
                &network.name,
                &ethereum,
                chain_id,
                &provider,
                owner,
                receiver,
                &signer,
                &req,
            )
            .await
        }
    }
}

#[allow(clippy::too_many_arguments)]
async fn swap_via_cow(
    json: bool,
    args: &TokenSwapArgs,
    network_name: &str,
    ethereum: &EthereumConfig,
    chain_id: u64,
    provider: &impl Provider,
    owner: Address,
    receiver: Address,
    signer: &PrivateKeySigner,
    req: &SwapRequest,
) -> Result<()> {
    let sell_token = req.sell_token;

    let chain = cow_chain(chain_id).ok_or_else(|| {
        anyhow!(
            "CoW Swap is not available on chainId {} (network '{}')",
            chain_id,
            network_name
        )
    })?;

    let api = CowApi::new(chain_id).map_err(|e| anyhow!("failed to build CoW API client: {e}"))?;

    let (quote, resp) = match sell_token {
        SwapToken::Usdc => cow::quote_usdc(&api, ethereum.usdc_token, req)
            .await
            .map_err(|e| anyhow!("failed to fetch CoW quote: {e}"))?,
        SwapToken::Eth => cow::quote_eth(&api, chain.weth, req)
            .await
            .map_err(|e| anyhow!("failed to fetch CoW quote: {e}"))?,
    };

    // Print quote summary to stderr (human mode).
    if !json {
        print_swap_quote(sell_token, &quote);
    }

    // Dry-run: stop after printing the quote.
    if args.signing.dry_run {
        if json {
            let mut output = quote_json(sell_token, &quote);
            output["dry_run"] = serde_json::Value::Bool(true);
            println!("{}", serde_json::to_string_pretty(&output)?);
        } else {
            eprintln!("\nDry run - order not submitted.");
        }
        return Ok(());
    }

    // Confirmation: only prompt in human mode (mirror credit.rs: no prompt when --json).
    if !json && !args.yes && !confirm_submission("Proceed?")? {
        eprintln!("Cancelled.");
        return Ok(());
    }

    // Submit the order.
    match sell_token {
        SwapToken::Usdc => {
            ensure_allowance(
                provider,
                ethereum.usdc_token,
                owner,
                VAULT_RELAYER,
                quote.sell_amount,
            )
            .await
            .map_err(|e| anyhow!("failed to ensure USDC allowance: {e}"))?;
            let order_uid = cow::place_usdc_order(
                &api,
                chain_id,
                ethereum.usdc_token,
                &AppData::cow(),
                req,
                &quote,
                &resp,
                signer,
            )
            .await
            .map_err(|e| anyhow!("failed to place CoW order: {e}"))?;

            if json {
                let output = result_json_usdc(sell_token, &quote, &order_uid);
                println!("{}", serde_json::to_string_pretty(&output)?);
            } else {
                eprintln!("Order submitted: {order_uid}");
                eprintln!("https://explorer.cow.fi/orders/{order_uid}");
            }
        }
        SwapToken::Eth => {
            let tx_hash = create_eth_order(
                provider,
                chain.ethflow,
                ethereum.aleph_token,
                receiver,
                AppData::cow().hash,
                quote.sell_amount,
                quote.min_buy_amount,
                resp.quote.valid_to,
                resp.id.unwrap_or(0),
            )
            .await
            .map_err(|e| anyhow!("failed to submit ETH-flow order: {e}"))?;
            let tx_hash_str = format!("{:#x}", tx_hash);

            if json {
                let output = result_json_eth(sell_token, &quote, &tx_hash_str);
                println!("{}", serde_json::to_string_pretty(&output)?);
            } else {
                eprintln!("Transaction submitted: {tx_hash_str}");
                eprintln!("https://explorer.cow.fi/tx/{tx_hash_str}");
            }
        }
    }

    Ok(())
}

#[allow(clippy::too_many_arguments)]
async fn swap_via_uniswap(
    json: bool,
    args: &TokenSwapArgs,
    network_name: &str,
    ethereum: &EthereumConfig,
    chain_id: u64,
    provider: &impl Provider,
    owner: Address,
    receiver: Address,
    req: &SwapRequest,
) -> Result<()> {
    let sell_token = req.sell_token;

    let chain = uniswap_chain(chain_id).ok_or_else(|| {
        anyhow!(
            "Uniswap is not available on chainId {} (network '{}')",
            chain_id,
            network_name
        )
    })?;

    let quote = match sell_token {
        SwapToken::Eth => uniswap::quote_eth(provider, &chain, req)
            .await
            .map_err(|e| anyhow!("failed to fetch Uniswap quote: {e}"))?,
        SwapToken::Usdc => uniswap::quote_usdc(provider, &chain, ethereum.usdc_token, req)
            .await
            .map_err(|e| anyhow!("failed to fetch Uniswap quote: {e}"))?,
    };

    // Print quote summary to stderr (human mode).
    if !json {
        print_uniswap_quote(sell_token, &quote);
    }

    // Dry-run: stop after printing the quote.
    if args.signing.dry_run {
        if json {
            let mut output = quote_json_uniswap(sell_token, &quote);
            output["dry_run"] = serde_json::Value::Bool(true);
            println!("{}", serde_json::to_string_pretty(&output)?);
        } else {
            eprintln!("\nDry run - swap not submitted.");
        }
        return Ok(());
    }

    // Confirmation: only prompt in human mode (mirror credit.rs: no prompt when --json).
    if !json && !args.yes && !confirm_submission("Proceed?")? {
        eprintln!("Cancelled.");
        return Ok(());
    }

    let deadline_secs = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_err(|e| anyhow!("system clock is before the unix epoch: {e}"))?
        .as_secs()
        + u64::from(args.valid_for);

    // Native ETH rides along as msg.value; USDC needs a router allowance.
    let native_sell = match sell_token {
        SwapToken::Eth => true,
        SwapToken::Usdc => {
            ensure_allowance(
                provider,
                ethereum.usdc_token,
                owner,
                chain.swap_router02,
                quote.sell_amount,
            )
            .await
            .map_err(|e| anyhow!("failed to ensure USDC allowance: {e}"))?;
            false
        }
    };

    let tx_hash = execute_swap(
        provider,
        chain.swap_router02,
        &quote.route,
        quote.sell_amount,
        quote.min_buy_amount,
        receiver,
        deadline_secs,
        native_sell,
    )
    .await
    .map_err(|e| anyhow!("failed to submit Uniswap swap: {e}"))?;
    let tx_hash_str = format!("{:#x}", tx_hash);

    if json {
        let output = result_json_uniswap(sell_token, &quote, &tx_hash_str);
        println!("{}", serde_json::to_string_pretty(&output)?);
    } else {
        eprintln!("Transaction submitted: {tx_hash_str}");
        if let Some(base) = &ethereum.explorer_tx_base {
            eprintln!("{base}{tx_hash_str}");
        }
    }

    Ok(())
}

fn print_ophis_quote(sell_token: SwapToken, quote: &SwapQuote) {
    let sell_display = format_token_amount(quote.sell_amount, sell_token.decimals());
    let buy_display = format_token_amount(quote.buy_amount, ALEPH_DECIMALS);
    let min_display = format_token_amount(quote.min_buy_amount, ALEPH_DECIMALS);
    eprintln!(
        "Swapping {} {} for ALEPH via Ophis",
        sell_display,
        sell_token.symbol()
    );
    eprintln!("  Expected:     ~{buy_display} ALEPH (before partner fee)");
    eprintln!("  Min received: {min_display} ALEPH");
    eprintln!("  Partner fee:  0.10% (Ophis), taken on settlement");
}

/// Swap via Ophis: a CoW order whose appData carries Ophis's partner fee.
/// Same mainnet orderbook, settlement and contracts as the `cow` venue; the
/// only difference is the appData document (and the native-ETH path must
/// register that document so the orderbook can resolve the fee).
#[allow(clippy::too_many_arguments)]
async fn swap_via_ophis(
    json: bool,
    args: &TokenSwapArgs,
    network_name: &str,
    ethereum: &EthereumConfig,
    chain_id: u64,
    provider: &impl Provider,
    owner: Address,
    receiver: Address,
    signer: &PrivateKeySigner,
    req: &SwapRequest,
) -> Result<()> {
    let sell_token = req.sell_token;
    let app_data = AppData::ophis();

    let chain = cow_chain(chain_id).ok_or_else(|| {
        anyhow!(
            "Ophis is not available on chainId {} (network '{}')",
            chain_id,
            network_name
        )
    })?;

    let api =
        CowApi::new(chain_id).map_err(|e| anyhow!("failed to build Ophis API client: {e}"))?;

    let (quote, resp) = match sell_token {
        SwapToken::Usdc => cow::quote_usdc(&api, ethereum.usdc_token, req).await,
        SwapToken::Eth => cow::quote_eth(&api, chain.weth, req).await,
    }
    .map_err(|e| anyhow!("failed to fetch Ophis quote: {e}"))?;

    // Print quote summary to stderr (human mode).
    if !json {
        print_ophis_quote(sell_token, &quote);
    }

    // Dry-run: stop after printing the quote.
    if args.signing.dry_run {
        if json {
            let mut output = quote_json_ophis(sell_token, &quote);
            output["dry_run"] = serde_json::Value::Bool(true);
            println!("{}", serde_json::to_string_pretty(&output)?);
        } else {
            eprintln!("\nDry run - order not submitted.");
        }
        return Ok(());
    }

    // Confirmation: only prompt in human mode (mirror credit.rs: no prompt when --json).
    if !json && !args.yes && !confirm_submission("Proceed?")? {
        eprintln!("Cancelled.");
        return Ok(());
    }

    match sell_token {
        SwapToken::Usdc => {
            ensure_allowance(
                provider,
                ethereum.usdc_token,
                owner,
                VAULT_RELAYER,
                quote.sell_amount,
            )
            .await
            .map_err(|e| anyhow!("failed to ensure USDC allowance: {e}"))?;
            let order_uid = cow::place_usdc_order(
                &api,
                chain_id,
                ethereum.usdc_token,
                &app_data,
                req,
                &quote,
                &resp,
                signer,
            )
            .await
            .map_err(|e| anyhow!("failed to place Ophis order: {e}"))?;

            if json {
                let output = result_json_ophis_usdc(sell_token, &quote, &order_uid);
                println!("{}", serde_json::to_string_pretty(&output)?);
            } else {
                eprintln!("Order submitted: {order_uid}");
                eprintln!("https://explorer.cow.fi/orders/{order_uid}");
            }
        }
        SwapToken::Eth => {
            // EthFlow carries only the appData hash on-chain; register the full
            // document first so the orderbook can resolve the partner fee.
            let hash_hex = format!("{:#x}", app_data.hash);
            api.put_app_data(&hash_hex, app_data.json)
                .await
                .map_err(|e| anyhow!("failed to register Ophis appData: {e}"))?;
            let tx_hash = create_eth_order(
                provider,
                chain.ethflow,
                ethereum.aleph_token,
                receiver,
                app_data.hash,
                quote.sell_amount,
                quote.min_buy_amount,
                resp.quote.valid_to,
                resp.id.unwrap_or(0),
            )
            .await
            .map_err(|e| anyhow!("failed to submit Ophis ETH-flow order: {e}"))?;
            let tx_hash_str = format!("{:#x}", tx_hash);

            if json {
                let output = result_json_ophis_eth(sell_token, &quote, &tx_hash_str);
                println!("{}", serde_json::to_string_pretty(&output)?);
            } else {
                eprintln!("Transaction submitted: {tx_hash_str}");
                eprintln!("https://explorer.cow.fi/tx/{tx_hash_str}");
            }
        }
    }

    Ok(())
}

fn resolve_swap_evm_account(signing: &SigningArgs) -> Result<EvmAccount> {
    match resolve_account(&signing.identity)? {
        CliAccount::Evm(a) => Ok(a),
        CliAccount::LedgerEvm(_) => Err(anyhow!(
            "Ledger accounts are not yet supported for swaps. Use a local account."
        )),
        CliAccount::Sol(_) => Err(anyhow!("swaps require an EVM account (got Solana)")),
    }
}

/// Common quote fields shared by the dry-run and result JSON outputs.
fn quote_json(sell_token: SwapToken, quote: &SwapQuote) -> serde_json::Value {
    serde_json::json!({
        "venue": "cow",
        "sell_token": sell_token.symbol(),
        "sell_amount": format_token_amount(quote.sell_amount, sell_token.decimals()),
        "expected_aleph": format_token_amount(quote.buy_amount, ALEPH_DECIMALS),
        "min_aleph": format_token_amount(quote.min_buy_amount, ALEPH_DECIMALS),
        "fee": format_token_amount(quote.fee_amount, sell_token.decimals()),
    })
}

fn result_json_usdc(
    sell_token: SwapToken,
    quote: &SwapQuote,
    order_uid: &str,
) -> serde_json::Value {
    let mut v = quote_json(sell_token, quote);
    v["order_id"] = order_uid.into();
    v
}

fn result_json_eth(sell_token: SwapToken, quote: &SwapQuote, tx_hash: &str) -> serde_json::Value {
    let mut v = quote_json(sell_token, quote);
    v["tx_hash"] = tx_hash.into();
    v
}

/// Common quote fields for the Uniswap venue. No `fee` amount: the pool fee
/// is embedded in the price, so `pool_fees` names the tiers instead.
fn quote_json_uniswap(sell_token: SwapToken, quote: &UniswapQuote) -> serde_json::Value {
    serde_json::json!({
        "venue": "uniswap",
        "sell_token": sell_token.symbol(),
        "sell_amount": format_token_amount(quote.sell_amount, sell_token.decimals()),
        "expected_aleph": format_token_amount(quote.buy_amount, ALEPH_DECIMALS),
        "min_aleph": format_token_amount(quote.min_buy_amount, ALEPH_DECIMALS),
        "pool_fees": quote.route.fees_percent(),
    })
}

fn result_json_uniswap(
    sell_token: SwapToken,
    quote: &UniswapQuote,
    tx_hash: &str,
) -> serde_json::Value {
    let mut v = quote_json_uniswap(sell_token, quote);
    v["tx_hash"] = tx_hash.into();
    v
}

/// Common quote fields for the Ophis venue: the CoW fields plus the partner
/// fee taken on settlement (`partner_fee_bps`, 10 == 0.10%).
fn quote_json_ophis(sell_token: SwapToken, quote: &SwapQuote) -> serde_json::Value {
    serde_json::json!({
        "venue": "ophis",
        "sell_token": sell_token.symbol(),
        "sell_amount": format_token_amount(quote.sell_amount, sell_token.decimals()),
        "expected_aleph": format_token_amount(quote.buy_amount, ALEPH_DECIMALS),
        "min_aleph": format_token_amount(quote.min_buy_amount, ALEPH_DECIMALS),
        "fee": format_token_amount(quote.fee_amount, sell_token.decimals()),
        "partner_fee_bps": 10,
    })
}

fn result_json_ophis_usdc(
    sell_token: SwapToken,
    quote: &SwapQuote,
    order_uid: &str,
) -> serde_json::Value {
    let mut v = quote_json_ophis(sell_token, quote);
    v["order_id"] = order_uid.into();
    v
}

fn result_json_ophis_eth(
    sell_token: SwapToken,
    quote: &SwapQuote,
    tx_hash: &str,
) -> serde_json::Value {
    let mut v = quote_json_ophis(sell_token, quote);
    v["tx_hash"] = tx_hash.into();
    v
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloy_primitives::U256;

    fn sample_quote() -> SwapQuote {
        SwapQuote {
            sell_amount: U256::from(50_000_000u64),
            buy_amount: U256::from(1_000_000_000_000_000_000u128), // 1e18
            min_buy_amount: U256::from(995_000_000_000_000_000u128), // 0.995e18
            fee_amount: U256::from(100_000u64),                    // 0.1 USDC (6 dec)
        }
    }

    fn sample_uniswap_quote() -> UniswapQuote {
        use aleph_sdk::swap::uniswap::route::{FEE_1_PERCENT, FEE_005_PERCENT, UniswapRoute};
        use alloy_primitives::address;
        UniswapQuote {
            sell_amount: U256::from(50_000_000u64),
            buy_amount: U256::from(1_000_000_000_000_000_000u128), // 1e18
            min_buy_amount: U256::from(995_000_000_000_000_000u128), // 0.995e18
            route: UniswapRoute::new(
                vec![
                    address!("A0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"), // USDC
                    address!("C02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"), // WETH
                    address!("27702a26126e0B3702af63Ee09aC4d1A084EF628"), // ALEPH
                ],
                vec![FEE_005_PERCENT, FEE_1_PERCENT],
            ),
        }
    }

    #[test]
    fn swap_token_cli_maps_to_sdk_enum() {
        assert!(matches!(SwapToken::from(SwapTokenCli::Eth), SwapToken::Eth));
        assert!(matches!(
            SwapToken::from(SwapTokenCli::Usdc),
            SwapToken::Usdc
        ));
    }

    // --- validate_slippage ---

    #[test]
    fn validate_slippage_accepts_zero() {
        let frac = validate_slippage(0.0).unwrap();
        assert_eq!(frac, 0.0);
    }

    #[test]
    fn validate_slippage_accepts_midrange() {
        let frac = validate_slippage(0.5).unwrap();
        assert!((frac - 0.005).abs() < 1e-12);
    }

    #[test]
    fn validate_slippage_accepts_fifty() {
        let frac = validate_slippage(50.0).unwrap();
        assert!((frac - 0.5).abs() < 1e-12);
    }

    #[test]
    fn validate_slippage_rejects_negative() {
        assert!(validate_slippage(-0.1).is_err());
    }

    #[test]
    fn validate_slippage_rejects_above_fifty() {
        assert!(validate_slippage(50.1).is_err());
    }

    #[test]
    fn validate_slippage_rejects_nan() {
        assert!(validate_slippage(f64::NAN).is_err());
    }

    // --- result_json_usdc ---

    #[test]
    fn result_json_usdc_shape() {
        let v = result_json_usdc(SwapToken::Usdc, &sample_quote(), "0xUID");
        assert_eq!(v["venue"], "cow");
        assert_eq!(v["sell_token"], "USDC");
        // sell_amount: 50_000_000 atoms / 10^6 = 50 USDC
        assert_eq!(v["sell_amount"], "50");
        assert_eq!(v["expected_aleph"], "1");
        assert_eq!(v["min_aleph"], "0.995");
        assert_eq!(v["order_id"], "0xUID");
        // fee: 100_000 / 10^6 = 0.1 USDC
        assert_eq!(v["fee"], "0.1");
        assert!(
            v.get("tx_hash").is_none(),
            "USDC result must not have tx_hash"
        );
    }

    // --- result_json_eth ---

    #[test]
    fn result_json_eth_shape() {
        let v = result_json_eth(SwapToken::Eth, &sample_quote(), "0xdeadbeef");
        assert_eq!(v["venue"], "cow");
        assert_eq!(v["sell_token"], "ETH");
        // sell_amount: 50_000_000 atoms / 10^18 = 0.00000000005 ETH
        assert_eq!(v["sell_amount"], "0.00000000005");
        assert_eq!(v["tx_hash"], "0xdeadbeef");
        assert!(
            v.get("order_id").is_none(),
            "ETH result must not have order_id"
        );
    }

    // --- quote_json ---

    #[test]
    fn quote_json_has_fee_and_no_order_id() {
        let v = quote_json(SwapToken::Usdc, &sample_quote());
        assert_eq!(v["venue"], "cow");
        assert_eq!(v["sell_token"], "USDC");
        // sell_amount: 50_000_000 atoms / 10^6 = 50 USDC
        assert_eq!(v["sell_amount"], "50");
        assert_eq!(v["expected_aleph"], "1");
        assert_eq!(v["min_aleph"], "0.995");
        assert_eq!(v["fee"], "0.1");
        assert!(v.get("order_id").is_none());
        assert!(v.get("tx_hash").is_none());
        assert!(v.get("dry_run").is_none(), "dry_run only set by caller");
    }

    #[test]
    fn quote_json_dry_run_flag_set_by_caller() {
        let mut v = quote_json(SwapToken::Usdc, &sample_quote());
        v["dry_run"] = serde_json::Value::Bool(true);
        assert_eq!(v["dry_run"], true);
    }

    // --- Uniswap JSON helpers ---

    #[test]
    fn quote_json_uniswap_shape() {
        let v = quote_json_uniswap(SwapToken::Usdc, &sample_uniswap_quote());
        assert_eq!(v["venue"], "uniswap");
        assert_eq!(v["sell_token"], "USDC");
        assert_eq!(v["sell_amount"], "50");
        assert_eq!(v["expected_aleph"], "1");
        assert_eq!(v["min_aleph"], "0.995");
        assert_eq!(v["pool_fees"], serde_json::json!(["0.05%", "1%"]));
        assert!(v.get("fee").is_none(), "uniswap has no separate fee amount");
        assert!(v.get("tx_hash").is_none());
        assert!(v.get("order_id").is_none());
    }

    #[test]
    fn result_json_uniswap_shape() {
        let v = result_json_uniswap(SwapToken::Usdc, &sample_uniswap_quote(), "0xdeadbeef");
        assert_eq!(v["venue"], "uniswap");
        assert_eq!(v["tx_hash"], "0xdeadbeef");
        assert!(v.get("order_id").is_none());
    }

    #[test]
    fn result_json_uniswap_snapshot() {
        insta::assert_json_snapshot!(result_json_uniswap(
            SwapToken::Usdc,
            &sample_uniswap_quote(),
            "0xfeed"
        ));
    }

    #[test]
    fn quote_json_uniswap_snapshot() {
        insta::assert_json_snapshot!(quote_json_uniswap(SwapToken::Usdc, &sample_uniswap_quote()));
    }

    // --- Snapshot tests ---

    #[test]
    fn result_json_usdc_snapshot() {
        insta::assert_json_snapshot!(result_json_usdc(SwapToken::Usdc, &sample_quote(), "0xUID"));
    }

    #[test]
    fn result_json_eth_snapshot() {
        insta::assert_json_snapshot!(result_json_eth(SwapToken::Eth, &sample_quote(), "0xfeed"));
    }

    #[test]
    fn quote_json_snapshot() {
        insta::assert_json_snapshot!(quote_json(SwapToken::Eth, &sample_quote()));
    }

    // --- Ophis JSON helpers ---

    #[test]
    fn quote_json_ophis_has_partner_fee() {
        let v = quote_json_ophis(SwapToken::Usdc, &sample_quote());
        assert_eq!(v["venue"], "ophis");
        assert_eq!(v["sell_token"], "USDC");
        assert_eq!(v["sell_amount"], "50");
        assert_eq!(v["expected_aleph"], "1");
        assert_eq!(v["min_aleph"], "0.995");
        assert_eq!(v["fee"], "0.1");
        assert_eq!(v["partner_fee_bps"], 10);
        assert!(v.get("order_id").is_none());
        assert!(v.get("tx_hash").is_none());
    }

    #[test]
    fn result_json_ophis_usdc_has_order_id() {
        let v = result_json_ophis_usdc(SwapToken::Usdc, &sample_quote(), "0xUID");
        assert_eq!(v["venue"], "ophis");
        assert_eq!(v["order_id"], "0xUID");
        assert_eq!(v["partner_fee_bps"], 10);
        assert!(v.get("tx_hash").is_none());
    }

    #[test]
    fn result_json_ophis_eth_has_tx_hash() {
        let v = result_json_ophis_eth(SwapToken::Eth, &sample_quote(), "0xdeadbeef");
        assert_eq!(v["venue"], "ophis");
        assert_eq!(v["tx_hash"], "0xdeadbeef");
        assert!(v.get("order_id").is_none());
    }

    #[test]
    fn quote_json_ophis_snapshot() {
        insta::assert_json_snapshot!(quote_json_ophis(SwapToken::Usdc, &sample_quote()));
    }

    #[test]
    fn result_json_ophis_eth_snapshot() {
        insta::assert_json_snapshot!(result_json_ophis_eth(
            SwapToken::Eth,
            &sample_quote(),
            "0xfeed"
        ));
    }
}