rustrade-execution 0.2.1

Stream private account data from financial venues, and execute (live or mock) orders.
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
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
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
//! Hyperliquid Execution Client Integration Tests
//!
//! These tests require a funded Hyperliquid testnet account.
//!
//! # Prerequisites
//!
//! 1. Hyperliquid testnet account with mock USDC (claim at app.hyperliquid-testnet.xyz/drip)
//! 2. Environment variables set (see .env.template):
//!    - HYPERLIQUID_PRIVATE_KEY: Wallet private key (hex)
//!    - HYPERLIQUID_TESTNET=true
//!
//! # Running
//!
//! ```bash
//! # Load env vars from .env (optional, or export manually)
//! source .env
//!
//! # Run all Hyperliquid integration tests
//! cargo test --test hyperliquid_execution --features hyperliquid -- --ignored
//!
//! # Run specific test
//! cargo test --test hyperliquid_execution --features hyperliquid test_connection -- --ignored
//! ```
//!
//! Tests are marked `#[ignore]` to avoid CI failures without testnet connectivity.
//!
//! # Mainnet Read-Only Test
//!
//! `test_mainnet_authentication` verifies mainnet connectivity using read-only operations.
//! Safe to run even with zero mainnet balance.

#![cfg(feature = "hyperliquid")]
#![allow(clippy::unwrap_used, clippy::expect_used)]

use rust_decimal_macros::dec;
use rustrade_execution::{
    client::{
        ExecutionClient,
        hyperliquid::{HyperliquidClient, config::HyperliquidConfig},
    },
    order::{
        OrderKey, OrderKind, TimeInForce,
        id::{ClientOrderId, StrategyId},
        request::RequestOpen,
        state::{ActiveOrderState, OrderState},
    },
};
use rustrade_instrument::{
    Side, asset::name::AssetNameExchange, exchange::ExchangeId,
    instrument::name::InstrumentNameExchange,
};
use std::time::Duration;
use tokio_stream::StreamExt;
use tracing_subscriber::{EnvFilter, fmt};

fn init_logging() {
    let _ = fmt()
        .with_env_filter(
            EnvFilter::builder()
                .with_default_directive(tracing::Level::DEBUG.into())
                .from_env_lossy(),
        )
        .try_init();
}

fn test_config() -> HyperliquidConfig {
    HyperliquidConfig::from_env().expect(
        "HYPERLIQUID_PRIVATE_KEY env var required. Set HYPERLIQUID_TESTNET=true for testnet.",
    )
}

fn btc_instrument() -> InstrumentNameExchange {
    "BTC-USD-PERP".into()
}

fn eth_instrument() -> InstrumentNameExchange {
    "ETH-USD-PERP".into()
}

// ============================================================================
// Connection Tests
// ============================================================================

#[tokio::test]
#[ignore]
async fn test_connection() {
    init_logging();

    let config = test_config();
    assert!(config.testnet, "Integration tests must run on testnet");

    println!("Wallet address: {}", config.wallet_address_hex());

    let client = HyperliquidClient::connect(config)
        .await
        .expect("Failed to connect");

    assert_eq!(HyperliquidClient::EXCHANGE, ExchangeId::HyperliquidPerp);
    println!("Client wallet: {}", client.wallet_address());
    println!("Client created successfully");
}

/// Test mainnet authentication with read-only operations.
/// Uses the same wallet as testnet (which has zero mainnet balance).
/// Verifies EIP-712 signing works against mainnet API.
#[tokio::test]
#[ignore]
async fn test_mainnet_authentication() {
    init_logging();

    // Create mainnet config (override testnet setting)
    let private_key =
        std::env::var("HYPERLIQUID_PRIVATE_KEY").expect("HYPERLIQUID_PRIVATE_KEY env var required");
    let config =
        HyperliquidConfig::from_private_key(&private_key, false).expect("Invalid private key");

    assert!(!config.testnet, "This test must run on mainnet");
    println!("Mainnet wallet address: {}", config.wallet_address_hex());

    let client = HyperliquidClient::connect(config)
        .await
        .expect("Failed to connect to mainnet");

    // Read-only: fetch balances (will be 0 for test wallet)
    let assets: Vec<AssetNameExchange> = vec![];
    let result = client.fetch_balances(&assets).await;

    assert!(
        result.is_ok(),
        "Mainnet fetch_balances failed: {:?}",
        result.err()
    );

    let balances = result.unwrap();
    println!("Mainnet balances: {} asset(s)", balances.len());
    for balance in &balances {
        println!(
            "  {}: total={}, free={}",
            balance.asset, balance.balance.total, balance.balance.free
        );
    }

    println!("Mainnet authentication successful (read-only)");
}

// ============================================================================
// Account Snapshot Tests
// ============================================================================

#[tokio::test]
#[ignore]
async fn test_fetch_balances() {
    init_logging();

    let config = test_config();
    let client = HyperliquidClient::connect(config)
        .await
        .expect("Failed to connect");

    let assets: Vec<AssetNameExchange> = vec![];
    let result = client.fetch_balances(&assets).await;

    assert!(result.is_ok(), "fetch_balances failed: {:?}", result.err());

    let balances = result.unwrap();
    println!("Fetched {} balance(s)", balances.len());
    for balance in &balances {
        println!(
            "  {}: total={}, free={}",
            balance.asset, balance.balance.total, balance.balance.free
        );
    }

    assert!(!balances.is_empty(), "Expected at least USDC balance");
}

#[tokio::test]
#[ignore]
async fn test_account_snapshot() {
    init_logging();

    let config = test_config();
    let client = HyperliquidClient::connect(config)
        .await
        .expect("Failed to connect");

    let assets: Vec<AssetNameExchange> = vec![];
    let instruments: Vec<InstrumentNameExchange> = vec![];

    let result = client.account_snapshot(&assets, &instruments).await;

    assert!(
        result.is_ok(),
        "account_snapshot failed: {:?}",
        result.err()
    );

    let snapshot = result.unwrap();
    println!("Exchange: {:?}", snapshot.exchange);
    println!("Balances: {}", snapshot.balances.len());
    for balance in &snapshot.balances {
        println!(
            "  {}: total={}, free={}",
            balance.asset, balance.balance.total, balance.balance.free
        );
    }
    println!(
        "Instruments with positions/orders: {}",
        snapshot.instruments.len()
    );
    for inst in &snapshot.instruments {
        println!(
            "  {}: orders={}, position={:?}",
            inst.instrument,
            inst.orders.len(),
            inst.position.as_ref().map(|p| p.quantity)
        );
    }
}

#[tokio::test]
#[ignore]
async fn test_account_snapshot_filtered() {
    init_logging();

    let config = test_config();
    let client = HyperliquidClient::connect(config)
        .await
        .expect("Failed to connect");

    let assets: Vec<AssetNameExchange> = vec![];
    let instruments = vec![btc_instrument(), eth_instrument()];

    let result = client.account_snapshot(&assets, &instruments).await;

    assert!(
        result.is_ok(),
        "account_snapshot failed: {:?}",
        result.err()
    );

    let snapshot = result.unwrap();
    println!("Filtered snapshot for BTC and ETH perps");
    println!("Instruments returned: {}", snapshot.instruments.len());
}

// ============================================================================
// Open Orders Tests
// ============================================================================

#[tokio::test]
#[ignore]
async fn test_fetch_open_orders() {
    init_logging();

    let config = test_config();
    let client = HyperliquidClient::connect(config)
        .await
        .expect("Failed to connect");

    let instruments: Vec<InstrumentNameExchange> = vec![];
    let result = client.fetch_open_orders(&instruments).await;

    assert!(
        result.is_ok(),
        "fetch_open_orders failed: {:?}",
        result.err()
    );

    let orders = result.unwrap();
    println!("Open orders: {}", orders.len());
    for order in &orders {
        println!(
            "  {:?} {} {} @ {:?}",
            order.side, order.quantity, order.key.instrument, order.price
        );
    }
}

// ============================================================================
// Historical Trades Tests
// ============================================================================

#[tokio::test]
#[ignore]
async fn test_fetch_trades() {
    init_logging();

    let config = test_config();
    let client = HyperliquidClient::connect(config)
        .await
        .expect("Failed to connect");

    let since = chrono::Utc::now() - chrono::Duration::days(7);
    let instruments: Vec<InstrumentNameExchange> = vec![];

    let result = client.fetch_trades(since, &instruments).await;

    assert!(result.is_ok(), "fetch_trades failed: {:?}", result.err());

    let trades = result.unwrap();
    println!("Trades in last 7 days: {}", trades.len());
    for trade in trades.iter().take(10) {
        println!(
            "  {} {:?} {} {} @ {} (fee: {})",
            trade.time_exchange.format("%Y-%m-%d %H:%M:%S"),
            trade.side,
            trade.quantity,
            trade.instrument,
            trade.price,
            trade.fees.fees
        );
    }
}

// ============================================================================
// Order Lifecycle Tests
// ============================================================================

#[tokio::test]
#[ignore]
async fn test_place_and_cancel_limit_order() {
    init_logging();

    let config = test_config();
    assert!(config.testnet, "This test MUST run on testnet only!");

    let client = HyperliquidClient::connect(config)
        .await
        .expect("Failed to connect");

    let instrument = btc_instrument();
    let strategy = StrategyId::new("test-strategy");
    let order_cid = ClientOrderId::new(format!("test-{}", chrono::Utc::now().timestamp_millis()));

    let order_key = OrderKey {
        exchange: ExchangeId::HyperliquidPerp,
        instrument: &instrument,
        strategy: strategy.clone(),
        cid: order_cid.clone(),
    };

    // Place a limit buy below market price (won't fill, but within 80% of market)
    // BTC ~$95k as of 2026, so $50k is ~47% below - within Hyperliquid's 80% limit
    let request_open = RequestOpen {
        side: Side::Buy,
        price: Some(dec!(50000.0)),
        quantity: dec!(0.001), // Minimum size
        kind: OrderKind::Limit,
        time_in_force: TimeInForce::GoodUntilCancelled { post_only: false },
        position_id: None,
        reduce_only: false,
    };

    let open_request = rustrade_execution::order::OrderEvent {
        key: order_key.clone(),
        state: request_open,
    };

    println!("Placing limit order: BUY 0.001 BTC-USD-PERP @ $50,000 (won't fill)");

    let response = client.open_order(open_request).await;

    assert!(response.is_some(), "Expected order response");
    let response = response.unwrap();

    match &response.state {
        OrderState::Active(ActiveOrderState::Open(open_state)) => {
            println!("Order placed successfully!");
            println!("  Client Order ID: {}", response.key.cid);
            println!("  Exchange Order ID: {}", open_state.id);

            // Wait a moment for order to be fully processed
            tokio::time::sleep(Duration::from_millis(500)).await;

            // Cancel the order
            let cancel_key = OrderKey {
                exchange: ExchangeId::HyperliquidPerp,
                instrument: &instrument,
                strategy: response.key.strategy.clone(),
                cid: response.key.cid.clone(),
            };

            let cancel_request = rustrade_execution::order::OrderEvent {
                key: cancel_key,
                state: rustrade_execution::order::request::RequestCancel {
                    id: Some(open_state.id.clone()),
                },
            };

            println!("Canceling order...");
            let cancel_response = client.cancel_order(cancel_request).await;

            assert!(cancel_response.is_some(), "Expected cancel response");
            let cancel_response = cancel_response.unwrap();

            match &cancel_response.state {
                Ok(cancelled) => {
                    println!("Order canceled successfully!");
                    println!("  Cancelled at: {}", cancelled.time_exchange);
                }
                Err(e) => {
                    panic!("Cancel rejected: {:?}", e);
                }
            }
        }
        OrderState::Inactive(e) => {
            panic!("Order rejected: {:?}", e);
        }
        other => {
            panic!("Unexpected order state: {:?}", other);
        }
    }
}

// ============================================================================
// Conditional Order Tests
// ============================================================================

/// Test that Stop orders require UUID-format client order ID.
#[tokio::test]
#[ignore]
async fn test_stop_order_requires_uuid_cid() {
    init_logging();

    let config = test_config();
    assert!(config.testnet, "This test MUST run on testnet only!");

    let client = HyperliquidClient::connect(config)
        .await
        .expect("Failed to connect");

    let instrument = btc_instrument();
    let strategy = StrategyId::new("test-strategy");

    // Use a non-UUID cid - should be rejected
    let non_uuid_cid =
        ClientOrderId::new(format!("test-{}", chrono::Utc::now().timestamp_millis()));

    let order_key = OrderKey {
        exchange: ExchangeId::HyperliquidPerp,
        instrument: &instrument,
        strategy: strategy.clone(),
        cid: non_uuid_cid.clone(),
    };

    // Place a Stop order with non-UUID cid
    let request_open = RequestOpen {
        side: Side::Sell,
        price: None, // Market trigger - no limit price
        quantity: dec!(0.001),
        kind: OrderKind::Stop {
            trigger_price: dec!(80000.0), // Stop below current price
        },
        time_in_force: TimeInForce::GoodUntilCancelled { post_only: false },
        position_id: None,
        reduce_only: false,
    };

    let open_request = rustrade_execution::order::OrderEvent {
        key: order_key,
        state: request_open,
    };

    println!("Placing Stop order with non-UUID cid (should be rejected)...");

    let response = client.open_order(open_request).await;
    assert!(response.is_some(), "Expected order response");
    let response = response.unwrap();

    match &response.state {
        OrderState::Inactive(rustrade_execution::order::state::InactiveOrderState::OpenFailed(
            rustrade_execution::error::OrderError::Rejected(err),
        )) => {
            println!("Stop order correctly rejected: {:?}", err);
            assert!(
                err.to_string().contains("UUID"),
                "Error message should mention UUID requirement"
            );
        }
        other => {
            panic!("Expected rejection for non-UUID cid, got: {:?}", other);
        }
    }
}

/// Test placing and canceling a Stop order with proper UUID cid.
#[tokio::test]
#[ignore]
async fn test_place_and_cancel_stop_order() {
    init_logging();

    let config = test_config();
    assert!(config.testnet, "This test MUST run on testnet only!");

    let client = HyperliquidClient::connect(config)
        .await
        .expect("Failed to connect");

    let instrument = btc_instrument();
    let strategy = StrategyId::new("test-strategy");

    // Use UUID cid as required for trigger orders
    let uuid_cid = ClientOrderId::uuid();
    println!("Using UUID cid: {}", uuid_cid);

    let order_key = OrderKey {
        exchange: ExchangeId::HyperliquidPerp,
        instrument: &instrument,
        strategy: strategy.clone(),
        cid: uuid_cid.clone(),
    };

    // Place a Stop order (sell stop below market to avoid triggering)
    // BTC ~$95k, so $50k stop is well below market
    let request_open = RequestOpen {
        side: Side::Sell,
        price: None, // Market trigger
        quantity: dec!(0.001),
        kind: OrderKind::Stop {
            trigger_price: dec!(50000.0),
        },
        time_in_force: TimeInForce::GoodUntilCancelled { post_only: false },
        position_id: None,
        reduce_only: false,
    };

    let open_request = rustrade_execution::order::OrderEvent {
        key: order_key.clone(),
        state: request_open,
    };

    println!("Placing Stop order: SELL 0.001 BTC-USD-PERP @ stop $50,000");

    let response = client.open_order(open_request).await;
    assert!(response.is_some(), "Expected order response");
    let response = response.unwrap();

    match &response.state {
        OrderState::Active(ActiveOrderState::Open(open_state)) => {
            println!("Stop order placed successfully!");
            println!("  Client Order ID (UUID): {}", response.key.cid);
            println!("  Order ID: {}", open_state.id);

            // Note: Exchange may return numeric OID (Resting) or UUID (WaitingForTrigger)
            // depending on the trigger order type. Both should work for cancellation.
            let is_uuid_format = open_state.id.0.contains('-');
            println!(
                "  Order ID format: {}",
                if is_uuid_format {
                    "UUID (cloid)"
                } else {
                    "numeric (OID)"
                }
            );

            tokio::time::sleep(Duration::from_millis(500)).await;

            // Cancel using the order ID (works for both OID and cloid)
            let cancel_key = OrderKey {
                exchange: ExchangeId::HyperliquidPerp,
                instrument: &instrument,
                strategy: response.key.strategy.clone(),
                cid: response.key.cid.clone(),
            };

            let cancel_request = rustrade_execution::order::OrderEvent {
                key: cancel_key,
                state: rustrade_execution::order::request::RequestCancel {
                    id: Some(open_state.id.clone()),
                },
            };

            println!("Canceling Stop order...");
            let cancel_response = client.cancel_order(cancel_request).await;

            assert!(cancel_response.is_some(), "Expected cancel response");
            let cancel_response = cancel_response.unwrap();

            match &cancel_response.state {
                Ok(cancelled) => {
                    println!("Stop order canceled successfully!");
                    println!("  Cancelled at: {}", cancelled.time_exchange);
                }
                Err(e) => {
                    panic!("Cancel rejected: {:?}", e);
                }
            }
        }
        OrderState::Inactive(e) => {
            panic!("Stop order rejected: {:?}", e);
        }
        other => {
            panic!("Unexpected order state: {:?}", other);
        }
    }
}

/// Test placing and canceling a TakeProfit order.
#[tokio::test]
#[ignore]
async fn test_place_and_cancel_take_profit_order() {
    init_logging();

    let config = test_config();
    assert!(config.testnet, "This test MUST run on testnet only!");

    let client = HyperliquidClient::connect(config)
        .await
        .expect("Failed to connect");

    let instrument = btc_instrument();
    let strategy = StrategyId::new("test-strategy");
    let uuid_cid = ClientOrderId::uuid();

    let order_key = OrderKey {
        exchange: ExchangeId::HyperliquidPerp,
        instrument: &instrument,
        strategy: strategy.clone(),
        cid: uuid_cid.clone(),
    };

    // Place a TakeProfit order (sell TP above market to avoid triggering)
    // BTC ~$95k, so $150k TP is well above market
    let request_open = RequestOpen {
        side: Side::Sell,
        price: None, // Market trigger
        quantity: dec!(0.001),
        kind: OrderKind::TakeProfit {
            trigger_price: dec!(150000.0),
        },
        time_in_force: TimeInForce::GoodUntilCancelled { post_only: false },
        position_id: None,
        reduce_only: false,
    };

    let open_request = rustrade_execution::order::OrderEvent {
        key: order_key,
        state: request_open,
    };

    println!("Placing TakeProfit order: SELL 0.001 BTC-USD-PERP @ TP $150,000");

    let response = client.open_order(open_request).await;
    assert!(response.is_some(), "Expected order response");
    let response = response.unwrap();

    match &response.state {
        OrderState::Active(ActiveOrderState::Open(open_state)) => {
            println!("TakeProfit order placed successfully!");
            println!("  Order ID (cloid): {}", open_state.id);

            tokio::time::sleep(Duration::from_millis(500)).await;

            // Cancel
            let cancel_key = OrderKey {
                exchange: ExchangeId::HyperliquidPerp,
                instrument: &response.key.instrument,
                strategy: response.key.strategy.clone(),
                cid: response.key.cid.clone(),
            };

            let cancel_request = rustrade_execution::order::OrderEvent {
                key: cancel_key,
                state: rustrade_execution::order::request::RequestCancel {
                    id: Some(open_state.id.clone()),
                },
            };

            println!("Canceling TakeProfit order...");
            let cancel_response = client.cancel_order(cancel_request).await;
            assert!(cancel_response.is_some());

            match &cancel_response.unwrap().state {
                Ok(_) => println!("TakeProfit order canceled successfully!"),
                Err(e) => panic!("Cancel rejected: {:?}", e),
            }
        }
        OrderState::Inactive(e) => {
            panic!("TakeProfit order rejected: {:?}", e);
        }
        other => {
            panic!("Unexpected order state: {:?}", other);
        }
    }
}

/// Test that TrailingStop orders are rejected (unsupported).
#[tokio::test]
#[ignore]
async fn test_trailing_stop_unsupported() {
    init_logging();

    let config = test_config();
    assert!(config.testnet, "This test MUST run on testnet only!");

    let client = HyperliquidClient::connect(config)
        .await
        .expect("Failed to connect");

    let instrument = btc_instrument();
    let order_key = OrderKey {
        exchange: ExchangeId::HyperliquidPerp,
        instrument: &instrument,
        strategy: StrategyId::new("test"),
        cid: ClientOrderId::uuid(),
    };

    let request_open = RequestOpen {
        side: Side::Sell,
        price: None,
        quantity: dec!(0.001),
        kind: OrderKind::TrailingStop {
            offset: dec!(100.0),
            offset_type: rustrade_execution::order::TrailingOffsetType::Absolute,
        },
        time_in_force: TimeInForce::GoodUntilCancelled { post_only: false },
        position_id: None,
        reduce_only: false,
    };

    let open_request = rustrade_execution::order::OrderEvent {
        key: order_key,
        state: request_open,
    };

    println!("Placing TrailingStop order (should be rejected as unsupported)...");

    let response = client.open_order(open_request).await;
    assert!(response.is_some());

    match &response.unwrap().state {
        OrderState::Inactive(rustrade_execution::order::state::InactiveOrderState::OpenFailed(
            rustrade_execution::error::OrderError::UnsupportedOrderType(msg),
        )) => {
            println!("TrailingStop correctly rejected: {}", msg);
            assert!(msg.to_lowercase().contains("trailing"));
        }
        other => {
            panic!("Expected UnsupportedOrderType, got: {:?}", other);
        }
    }
}

// ============================================================================
// Account Stream Tests
// ============================================================================

#[tokio::test]
#[ignore]
async fn test_account_stream() {
    init_logging();

    let config = test_config();
    let client = HyperliquidClient::connect(config)
        .await
        .expect("Failed to connect");

    let assets: Vec<AssetNameExchange> = vec![];
    let instruments: Vec<InstrumentNameExchange> = vec![];

    let stream_result = client.account_stream(&assets, &instruments).await;

    assert!(
        stream_result.is_ok(),
        "account_stream failed: {:?}",
        stream_result.err()
    );

    let mut stream = stream_result.unwrap();

    println!("Account stream started. Waiting for events (10 second timeout)...");
    println!(
        "(Place/cancel an order manually to see events, or run test_account_stream_with_order)"
    );

    let timeout = tokio::time::timeout(Duration::from_secs(10), async {
        let mut count = 0;
        while let Some(event) = stream.next().await {
            println!("Event: {:?}", event.kind);
            count += 1;
            if count >= 3 {
                break;
            }
        }
        count
    })
    .await;

    match timeout {
        Ok(count) => println!("Received {} events", count),
        Err(_) => println!("Timeout reached (this is normal if no orders are active)"),
    }
}

#[tokio::test]
#[ignore]
async fn test_account_stream_with_order() {
    init_logging();

    let config = test_config();
    assert!(config.testnet, "This test MUST run on testnet only!");

    let client = HyperliquidClient::connect(config)
        .await
        .expect("Failed to connect");

    // Start account stream
    let assets: Vec<AssetNameExchange> = vec![];
    let instruments: Vec<InstrumentNameExchange> = vec![];

    let stream_result = client.account_stream(&assets, &instruments).await;
    assert!(
        stream_result.is_ok(),
        "account_stream failed: {:?}",
        stream_result.err()
    );
    let mut stream = stream_result.unwrap();

    println!("Account stream started, placing order to trigger events...");

    // Give WebSocket time to fully connect
    tokio::time::sleep(Duration::from_millis(500)).await;

    // Place an order to trigger stream events
    let instrument = eth_instrument();
    let strategy = StrategyId::new("stream-test");
    let order_cid = ClientOrderId::new(format!("stream-{}", chrono::Utc::now().timestamp_millis()));

    let order_key = OrderKey {
        exchange: ExchangeId::HyperliquidPerp,
        instrument: &instrument,
        strategy: strategy.clone(),
        cid: order_cid.clone(),
    };

    // ETH ~$3500 as of 2026, so $2000 is ~43% below - within 80% limit
    let request_open = RequestOpen {
        side: Side::Buy,
        price: Some(dec!(2000.0)),
        quantity: dec!(0.01),
        kind: OrderKind::Limit,
        time_in_force: TimeInForce::GoodUntilCancelled { post_only: false },
        position_id: None,
        reduce_only: false,
    };

    let open_request = rustrade_execution::order::OrderEvent {
        key: order_key,
        state: request_open,
    };

    let response = client.open_order(open_request).await;
    assert!(response.is_some());
    let response = response.unwrap();

    let order_id = match &response.state {
        OrderState::Active(ActiveOrderState::Open(open_state)) => {
            println!("Order placed: {}", open_state.id);
            Some(open_state.id.clone())
        }
        other => {
            println!("Order failed (may still get stream events): {:?}", other);
            None
        }
    };

    // Collect stream events
    println!("Collecting stream events...");
    let events = tokio::time::timeout(Duration::from_secs(5), async {
        let mut events = Vec::new();
        while let Some(event) = stream.next().await {
            println!("  Stream event: {:?}", event.kind);
            events.push(event);
            if events.len() >= 5 {
                break;
            }
        }
        events
    })
    .await;

    match events {
        Ok(events) => println!("Received {} stream events", events.len()),
        Err(_) => println!("Stream timeout (events may have been received)"),
    }

    // Cleanup: cancel the order if it was placed
    if let Some(oid) = order_id {
        let cancel_key = OrderKey {
            exchange: ExchangeId::HyperliquidPerp,
            instrument: &instrument,
            strategy,
            cid: order_cid,
        };
        let cancel_request = rustrade_execution::order::OrderEvent {
            key: cancel_key,
            state: rustrade_execution::order::request::RequestCancel { id: Some(oid) },
        };
        let _ = client.cancel_order(cancel_request).await;
        println!("Cleanup: order cancelled");
    }
}

// ============================================================================
// Edge Case Tests
// ============================================================================

#[tokio::test]
#[ignore]
async fn test_cancel_nonexistent_order() {
    init_logging();

    let config = test_config();
    let client = HyperliquidClient::connect(config)
        .await
        .expect("Failed to connect");

    let instrument = btc_instrument();
    let strategy = StrategyId::new("test-strategy");
    let order_cid = ClientOrderId::new("nonexistent-order");

    let cancel_key = OrderKey {
        exchange: ExchangeId::HyperliquidPerp,
        instrument: &instrument,
        strategy,
        cid: order_cid,
    };

    let cancel_request = rustrade_execution::order::OrderEvent {
        key: cancel_key,
        state: rustrade_execution::order::request::RequestCancel {
            id: Some(rustrade_execution::order::id::OrderId::new("999999999")),
        },
    };

    let response = client.cancel_order(cancel_request).await;

    assert!(response.is_some());
    let response = response.unwrap();

    // Hyperliquid may return success or error for nonexistent orders
    println!("Cancel nonexistent order result: {:?}", response.state);
}

#[tokio::test]
#[ignore]
async fn test_cancel_without_order_id() {
    init_logging();

    let config = test_config();
    let client = HyperliquidClient::connect(config)
        .await
        .expect("Failed to connect");

    let instrument = btc_instrument();
    let strategy = StrategyId::new("test-strategy");
    let order_cid = ClientOrderId::new("no-id-order");

    let cancel_key = OrderKey {
        exchange: ExchangeId::HyperliquidPerp,
        instrument: &instrument,
        strategy,
        cid: order_cid,
    };

    let cancel_request = rustrade_execution::order::OrderEvent {
        key: cancel_key,
        state: rustrade_execution::order::request::RequestCancel { id: None },
    };

    let response = client.cancel_order(cancel_request).await;

    assert!(response.is_some());
    let response = response.unwrap();

    assert!(
        response.state.is_err(),
        "Expected rejection when order ID is missing"
    );
    println!("Cancel correctly rejected: {:?}", response.state.err());
}