hl_ranger 0.6.0-ranger.20

Fork of Hyperliquid Rust SDK with unsigned transaction support for external signing
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
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
use ethers::signers::{LocalWallet, Signer};
use ethers::types::{Signature, H256};
use hl_ranger::exchange::ExchangeClient;
use hl_ranger::prelude::Result;
use hl_ranger::{
    BaseUrl, ClientCancelRequest, ClientLimit, ClientModifyRequest, ClientOrder,
    ClientOrderRequest, ExchangeDataStatus, ExchangeResponseStatus, UnsignedTransactionBuilder,
    UnsignedTransactionComponents,
};
use log::{error, info};
use std::str::FromStr;
use std::{env, thread, time};
use uuid::Uuid;

// Load private key from environment variable
fn get_test_private_key() -> Result<String> {
    // Load .env file if it exists (for development)
    dotenv::dotenv().ok();

    env::var("TEST_PRIVATE_KEY")
        .map_err(|_| hl_ranger::Error::GenericRequest(
            "TEST_PRIVATE_KEY environment variable not found. Please set it in your .env file or environment.".to_string()
        ))
}

// Helper function to sign a pre-computed hash (digest)
// This function is a simplified version of what's in the SDK's signature module
// It's exposed here for direct use in testing the signing of UnsignedTransactionComponents.
fn sign_digest(hash: H256, wallet: &LocalWallet) -> Result<Signature> {
    // The Hyperliquid SDK uses Sha256Proxy for signing, which effectively means it signs the H256 directly.
    // ethers::signers::Signer::sign_hash can be used if the hash is treated as a message hash.
    // However, Hyperliquid's EIP-712 signing process involves specific structures.
    // For L1 agent actions, the structure is `hl_ranger::signature::agent::l1::Agent`.
    // For other actions (like USDC transfer), it's the action struct itself (e.g., `hl_ranger::UsdSend`).
    // The `UnsignedTransactionBuilder` already provides the final `digest_to_sign`.
    // We just need to sign this H256 digest.
    // The `wallet.sign_hash(hash)` method is appropriate here.
    wallet
        .sign_hash(hash)
        .map_err(|e| hl_ranger::Error::SignatureFailure(e.to_string()))
}

async fn sign_and_post_transaction(
    components: UnsignedTransactionComponents,
    exchange_client: &ExchangeClient,
    wallet: &LocalWallet,
    agent_key: Option<&str>,
) -> Result<ExchangeResponseStatus> {
    info!("Digest to sign: {:?}", components.digest_to_sign);

    // Choose the correct wallet for signing based on transaction type
    let signing_wallet = if components.is_l1_agent_signature {
        if let Some(key) = agent_key {
            info!("Using agent key for L1 agent signature");
            LocalWallet::from_str(key)
                .map_err(|e| hl_ranger::Error::PrivateKeyParse(e.to_string()))?
        } else {
            return Err(hl_ranger::Error::GenericRequest(
                "L1 agent signature required but no agent key provided".to_string(),
            ));
        }
    } else {
        info!("Using main wallet for EIP-712 direct signature");
        wallet.clone()
    };

    let signature = sign_digest(components.digest_to_sign, &signing_wallet)?;
    info!(
        "Generated Signature: r: {}, s: {}, v: {}",
        signature.r, signature.s, signature.v
    );

    // The ExchangeClient's internal post method is not public.
    // We need to replicate its functionality or use a similar public method if available.
    // For now, let's use the existing `exchange_client.post` by making it pub(crate) or creating a helper.
    // Looking at `ExchangeClient`, the `post` method is private.
    // We will call it directly here.
    // To do this, we need to construct the `ExchangePayload` manually and serialize it.

    #[derive(serde::Serialize)]
    #[serde(rename_all = "camelCase")]
    struct ExchangePayloadInternal<'a> {
        action: &'a serde_json::Value,
        signature: Signature,
        nonce: u64,
        vault_address: Option<ethers::types::H160>,
    }

    let exchange_payload_internal = ExchangePayloadInternal {
        action: &components.action_payload_json,
        signature,
        nonce: components.nonce,
        vault_address: components.vault_address,
    };

    let payload_str = serde_json::to_string(&exchange_payload_internal)
        .map_err(|e| hl_ranger::Error::JsonParse(e.to_string()))?;

    info!("Sending payload: {}", payload_str);

    let response_str = exchange_client
        .http_client
        .post("/exchange", payload_str)
        .await
        .map_err(|e| {
            error!("HTTP request failed: {}", e);
            hl_ranger::Error::GenericRequest(e.to_string())
        })?;

    info!("Received response string: '{}'", response_str);
    info!("Response length: {} bytes", response_str.len());
    info!(
        "Response bytes (first 200): {:?}",
        response_str.chars().take(200).collect::<String>()
    );

    if response_str.is_empty() {
        error!("Server returned empty response!");
        return Err(hl_ranger::Error::GenericRequest(
            "Server returned empty response".to_string(),
        ));
    }

    // Try to parse the response, but provide more detailed error info if it fails
    serde_json::from_str(&response_str).map_err(|e| {
        error!("Failed to parse response JSON: {}", e);
        error!("Raw response was: '{}'", response_str);
        error!("Response as bytes: {:?}", response_str.as_bytes());
        error!("Is valid UTF-8: {}", response_str.is_ascii());

        // Try to identify what type of response this might be
        if response_str.starts_with('<') {
            error!("Response appears to be HTML (possibly an error page)");
        } else if response_str.starts_with('{') || response_str.starts_with('[') {
            error!("Response appears to be JSON but failed to parse");
        } else {
            error!("Response format is unknown");
        }

        hl_ranger::Error::JsonParse(format!(
            "Failed to parse response: {}. Raw response: '{}'",
            e, response_str
        ))
    })
}

#[tokio::main]
async fn main() -> Result<()> {
    env_logger::init();
    info!(
        "Starting signed transaction tests with key: {}",
        get_test_private_key()?
    );

    let wallet = LocalWallet::from_str(&get_test_private_key()?)
        .map_err(|e| hl_ranger::Error::Wallet(e.to_string()))?;
    info!("Wallet address: {:?}", wallet.address());

    let _unsigned_builder = UnsignedTransactionBuilder::new(
        None,
        Some(BaseUrl::Testnet),
        None,
        None, // No vault address for these general tests unless specified
    )
    .await?;
    info!("UnsignedTransactionBuilder initialized for Testnet.");

    let exchange_client =
        ExchangeClient::new(None, wallet.clone(), Some(BaseUrl::Testnet), None, None).await?;
    info!("ExchangeClient initialized for Testnet.");

    // Track all opened orders for cleanup
    let mut opened_orders: Vec<u64> = Vec::new();

    // Test 1: Main wallet direct orders (using ExchangeClient)
    info!("\n🔹 TEST 1: Main Wallet Direct Orders");
    match test_main_wallet_orders(&exchange_client, &wallet).await {
        Ok(oids) => {
            opened_orders.extend(oids);
            info!("✅ Main wallet test completed successfully");
        }
        Err(e) => {
            error!("❌ Main wallet test failed: {:?}", e);
        }
    }

    // Test 2: Agent wallet orders (using approved agent)
    info!("\n🔹 TEST 2: Agent Wallet Orders");
    match test_agent_wallet_orders(&exchange_client, &wallet).await {
        Ok(oids) => {
            opened_orders.extend(oids);
            info!("✅ Agent wallet test completed successfully");
        }
        Err(e) => {
            error!("❌ Agent wallet test failed: {:?}", e);
        }
    }

    // Clean up all opened orders
    if !opened_orders.is_empty() {
        info!(
            "\n🧹 CLEANUP: Canceling {} opened orders",
            opened_orders.len()
        );
        cleanup_orders(&exchange_client, &wallet, opened_orders).await;
    } else {
        info!("No orders to clean up.");
    }

    info!("\n✅ All tests completed!");
    Ok(())
}

async fn test_main_wallet_orders(
    exchange_client: &ExchangeClient,
    wallet: &LocalWallet,
) -> Result<Vec<u64>> {
    info!("📋 Testing main wallet direct orders (no agent approval needed)");
    let mut opened_orders = Vec::new();

    // Place a limit order using main wallet directly
    let cloid = Uuid::new_v4();
    let order_request = ClientOrderRequest {
        asset: "ETH".to_string(),
        is_buy: true,
        reduce_only: false,
        limit_px: 2500.0, // Below market price so it rests
        sz: 0.1,          // $250+ value, well above $10 minimum
        cloid: Some(cloid),
        order_type: ClientOrder::Limit(ClientLimit {
            tif: "Gtc".to_string(),
        }),
    };

    info!(
        "📤 Placing limit order with main wallet: {:?}",
        order_request
    );

    match exchange_client.order(order_request, Some(wallet)).await {
        Ok(response) => {
            info!("📥 Order response: {:?}", response);
            if let hl_ranger::ExchangeResponseStatus::Ok(response_data) = response {
                if let Some(data) = response_data.data {
                    if !data.statuses.is_empty() {
                        match &data.statuses[0] {
                            hl_ranger::ExchangeDataStatus::Resting(resting_order) => {
                                info!("✅ Order resting with OID: {}", resting_order.oid);
                                opened_orders.push(resting_order.oid);
                            }
                            hl_ranger::ExchangeDataStatus::Filled(filled_order) => {
                                info!("✅ Order filled with OID: {}", filled_order.oid);
                                // Filled orders don't need cleanup
                            }
                            hl_ranger::ExchangeDataStatus::Error(e) => {
                                error!("❌ Order error: {}", e);
                            }
                            other => {
                                info!("ℹ️ Unexpected order status: {:?}", other);
                            }
                        }
                    }
                }
            }
        }
        Err(e) => {
            error!("❌ Failed to place order with main wallet: {:?}", e);
            return Err(e);
        }
    }

    Ok(opened_orders)
}

async fn test_agent_wallet_orders(
    exchange_client: &ExchangeClient,
    wallet: &LocalWallet,
) -> Result<Vec<u64>> {
    info!("📋 Testing agent wallet orders (with agent approval)");
    let mut opened_orders = Vec::new();

    // Step 1: Approve agent
    info!("🔐 Approving L1 agent...");
    let (agent_key, approve_response) = exchange_client.approve_agent(Some(wallet)).await?;
    info!("🔑 Generated agent key: {}", agent_key);
    info!("📥 Agent approval response: {:?}", approve_response);

    // Wait for approval to be processed
    info!("⏳ Waiting 10 seconds for agent approval to be processed...");
    thread::sleep(time::Duration::from_secs(10));

    // Step 2: Place order using the approved agent
    let cloid = Uuid::new_v4();
    let order_request = ClientOrderRequest {
        asset: "ETH".to_string(),
        is_buy: true,
        reduce_only: false,
        limit_px: 2501.0, // Slightly different price from main wallet test
        sz: 0.1,
        cloid: Some(cloid),
        order_type: ClientOrder::Limit(ClientLimit {
            tif: "Gtc".to_string(),
        }),
    };

    info!(
        "📤 Placing limit order with agent wallet: {:?}",
        order_request
    );

    match exchange_client.order(order_request, Some(wallet)).await {
        Ok(response) => {
            info!("📥 Agent order response: {:?}", response);
            if let hl_ranger::ExchangeResponseStatus::Ok(response_data) = response {
                if let Some(data) = response_data.data {
                    if !data.statuses.is_empty() {
                        match &data.statuses[0] {
                            hl_ranger::ExchangeDataStatus::Resting(resting_order) => {
                                info!("✅ Agent order resting with OID: {}", resting_order.oid);
                                opened_orders.push(resting_order.oid);
                            }
                            hl_ranger::ExchangeDataStatus::Filled(filled_order) => {
                                info!("✅ Agent order filled with OID: {}", filled_order.oid);
                                // Filled orders don't need cleanup
                            }
                            hl_ranger::ExchangeDataStatus::Error(e) => {
                                error!("❌ Agent order error: {}", e);
                            }
                            other => {
                                info!("ℹ️ Unexpected agent order status: {:?}", other);
                            }
                        }
                    }
                }
            }
        }
        Err(e) => {
            error!("❌ Failed to place order with agent wallet: {:?}", e);
            return Err(e);
        }
    }

    Ok(opened_orders)
}

async fn cleanup_orders(exchange_client: &ExchangeClient, wallet: &LocalWallet, oids: Vec<u64>) {
    for oid in oids {
        info!("Canceling order OID: {}", oid);
        let cancel_request = ClientCancelRequest {
            asset: "ETH".to_string(),
            oid,
        };

        match exchange_client.cancel(cancel_request, Some(wallet)).await {
            Ok(response) => {
                info!(
                    "✅ Successfully canceled order OID: {} - {:?}",
                    oid, response
                );
            }
            Err(e) => {
                error!("❌ Failed to cancel order OID: {} - {:?}", oid, e);
            }
        }

        // Small delay between cancellations
        thread::sleep(time::Duration::from_millis(500));
    }
}

async fn test_signed_order_and_cancel(
    builder: &UnsignedTransactionBuilder,
    exchange_client: &ExchangeClient,
    wallet: &LocalWallet,
) -> Result<()> {
    info!("--- Testing Signed Order and Cancel ---");

    // 1. Prepare and post an order
    let cloid = Uuid::new_v4();
    let order_request = ClientOrderRequest {
        asset: "ETH".to_string(),
        is_buy: true,
        reduce_only: false,
        limit_px: 2500.0, // More realistic price close to current market ($2525)
        sz: 0.1, // Increased from 0.01 to 0.1 ETH (~$252 vs $25) to be well above $10 minimum
        cloid: Some(cloid),
        order_type: ClientOrder::Limit(ClientLimit {
            tif: "Gtc".to_string(),
        }),
    };
    info!("Preparing unsigned order: {:?}", order_request);
    let unsigned_order_components = builder
        .prepare_unsigned_order(
            order_request.clone(),
            Some("na".to_string()), // Use same grouping as ExchangeClient
        )
        .await?;

    info!(
        "Action for order: {}",
        serde_json::to_string_pretty(&unsigned_order_components.action_payload_json).unwrap()
    );

    let order_response =
        sign_and_post_transaction(unsigned_order_components, exchange_client, wallet, None).await;

    let mut oid_to_cancel: Option<u64> = None;
    match order_response {
        Ok(response_status) => {
            info!("Order Post Response: {:?}", response_status);
            if let ExchangeResponseStatus::Ok(response_data) = response_status {
                if let Some(data) = response_data.data {
                    if !data.statuses.is_empty() {
                        match &data.statuses[0] {
                            ExchangeDataStatus::Resting(resting_order) => {
                                info!("Order Resting with OID: {}", resting_order.oid);
                                oid_to_cancel = Some(resting_order.oid);
                            }
                            ExchangeDataStatus::Filled(filled_order) => {
                                info!(
                                    "Order Filled with OID: {}. Cannot cancel.",
                                    filled_order.oid
                                );
                            }
                            ExchangeDataStatus::Error(e) => {
                                error!("Order Post Error in status: {}", e);
                            }
                            other => {
                                info!("Order Post - Unexpected status: {:?}", other);
                            }
                        }
                    } else {
                        info!("Order Post - No statuses in response data.");
                    }
                } else {
                    info!("Order Post - No data in response.");
                }
            } else if let ExchangeResponseStatus::Err(e) = response_status {
                error!("Order Post Error: {}", e);
            }
        }
        Err(e) => {
            error!("Failed to post order: {:?}", e);
        }
    }

    if let Some(oid) = oid_to_cancel {
        info!(
            "Waiting a few seconds before attempting to cancel order OID: {}",
            oid
        );
        thread::sleep(time::Duration::from_secs(5));

        // 2. Prepare and post a cancel for that order
        let cancel_request = ClientCancelRequest {
            asset: "ETH".to_string(),
            oid,
        };
        info!(
            "Preparing unsigned cancel for OID {}: {:?}",
            oid, cancel_request
        );
        let unsigned_cancel_components = builder.prepare_unsigned_cancel(cancel_request).await?;
        info!(
            "Action for cancel: {}",
            serde_json::to_string_pretty(&unsigned_cancel_components.action_payload_json).unwrap()
        );

        let cancel_response =
            sign_and_post_transaction(unsigned_cancel_components, exchange_client, wallet, None)
                .await;

        match cancel_response {
            Ok(response_status) => {
                info!("Cancel Post Response: {:?}", response_status);
                if let ExchangeResponseStatus::Ok(response_data) = response_status {
                    if let Some(data) = response_data.data {
                        if !data.statuses.is_empty() {
                            match &data.statuses[0] {
                                ExchangeDataStatus::Success => {
                                    info!("Successfully cancelled OID: {}", oid);
                                }
                                ExchangeDataStatus::Error(e) => {
                                    error!("Cancel Post Error in status for OID {}: {}", oid, e);
                                }
                                other => {
                                    info!(
                                        "Cancel Post - Unexpected status for OID {}: {:?}",
                                        oid, other
                                    );
                                }
                            }
                        }
                    }
                } else if let ExchangeResponseStatus::Err(e) = response_status {
                    error!("Cancel Post Error for OID {}: {}", oid, e);
                }
            }
            Err(e) => {
                error!("Failed to post cancel for OID {}: {:?}", oid, e);
            }
        }
    } else {
        info!("No resting order OID obtained, skipping cancel test.");
    }

    info!("--- Finished Signed Order and Cancel Test ---");
    Ok(())
}

async fn test_signed_usdc_transfer(
    builder: &UnsignedTransactionBuilder,
    exchange_client: &ExchangeClient,
    wallet: &LocalWallet,
) -> Result<()> {
    info!("--- Testing Signed USDC Transfer ---");
    let amount = "1.0"; // 1 USDC
                        // Using a common test address, replace if needed, but for testnet this might not matter beyond format.
    let destination = "0x0D1d9635D0640821d15e323ac8AdADfA9c111414";

    info!(
        "Preparing unsigned USDC transfer: amount {}, destination {}",
        amount, destination
    );
    let components = builder
        .prepare_unsigned_usdc_transfer(amount, destination)
        .await?;

    info!(
        "Action for USDC transfer: {}",
        serde_json::to_string_pretty(&components.action_payload_json).unwrap()
    );

    let response = sign_and_post_transaction(components, exchange_client, wallet, None).await;

    match response {
        Ok(response_status) => {
            info!("USDC Transfer Post Response: {:?}", response_status);
            // Further checks can be added based on expected success/error messages
        }
        Err(e) => {
            error!("Failed to post USDC transfer: {:?}", e);
        }
    }

    info!("--- Finished Signed USDC Transfer Test ---");
    Ok(())
}

async fn test_signed_withdraw(
    builder: &UnsignedTransactionBuilder,
    exchange_client: &ExchangeClient,
    wallet: &LocalWallet,
) -> Result<()> {
    info!("--- Testing Signed Withdraw ---");
    let amount = "0.5"; // 0.5 USD
    let destination = "0x0D1d9635D0640821d15e323ac8AdADfA9c111414";

    info!(
        "Preparing unsigned withdraw: amount {}, destination {}",
        amount, destination
    );
    let components = builder
        .prepare_unsigned_withdraw(amount, destination)
        .await?;

    info!(
        "Action for withdraw: {}",
        serde_json::to_string_pretty(&components.action_payload_json).unwrap()
    );

    let response = sign_and_post_transaction(components, exchange_client, wallet, None).await;

    match response {
        Ok(response_status) => {
            info!("Withdraw Post Response: {:?}", response_status);
            // Further checks can be added based on expected success/error messages
        }
        Err(e) => {
            error!("Failed to post withdraw: {:?}", e);
        }
    }

    info!("--- Finished Signed Withdraw Test ---");
    Ok(())
}

async fn test_signed_modify_order(
    builder: &UnsignedTransactionBuilder,
    exchange_client: &ExchangeClient,
    wallet: &LocalWallet,
) -> Result<()> {
    info!("--- Testing Signed Modify Order ---");

    // 1. Place an order to get an OID
    let initial_cloid = Uuid::new_v4();
    let initial_order_request = ClientOrderRequest {
        asset: "ETH".to_string(),
        is_buy: true,
        reduce_only: false,
        limit_px: 1000.0, // Low price to ensure it rests
        sz: 0.01,
        cloid: Some(initial_cloid),
        order_type: ClientOrder::Limit(ClientLimit {
            tif: "Gtc".to_string(),
        }),
    };
    info!(
        "Preparing initial unsigned order for modification test: {:?}",
        initial_order_request
    );
    let unsigned_initial_order_components = builder
        .prepare_unsigned_order(
            initial_order_request.clone(),
            Some("test_modify_group".to_string()),
        )
        .await?;

    let initial_order_response = sign_and_post_transaction(
        unsigned_initial_order_components,
        exchange_client,
        wallet,
        None,
    )
    .await;

    let mut oid_to_modify: Option<u64> = None;
    match initial_order_response {
        Ok(response_status) => {
            info!(
                "Initial Order Post Response (for modify test): {:?}",
                response_status
            );
            if let ExchangeResponseStatus::Ok(response_data) = response_status {
                if let Some(data) = response_data.data {
                    if !data.statuses.is_empty() {
                        match &data.statuses[0] {
                            ExchangeDataStatus::Resting(resting_order) => {
                                info!("Initial Order Resting with OID: {}", resting_order.oid);
                                oid_to_modify = Some(resting_order.oid);
                            }
                            ExchangeDataStatus::Filled(filled_order) => {
                                info!("Initial Order Filled with OID: {}. Cannot modify if fully filled immediately.", filled_order.oid);
                            }
                            ExchangeDataStatus::Error(e) => {
                                error!("Initial Order Post Error in status: {}", e);
                            }
                            _ => {}
                        }
                    }
                }
            }
        }
        Err(e) => {
            error!("Failed to post initial order for modify test: {:?}", e);
        }
    }

    if let Some(oid) = oid_to_modify {
        info!(
            "Waiting a few seconds before attempting to modify order OID: {}",
            oid
        );
        thread::sleep(time::Duration::from_secs(3));

        // 2. Prepare and post a modify request for that order
        let modified_cloid = Uuid::new_v4();
        let modify_order_details = ClientOrderRequest {
            asset: "ETH".to_string(), // Must match original asset
            is_buy: true,             // Usually matches original, unless flipping side (complex)
            reduce_only: false,
            limit_px: 1001.0, // New price
            sz: 0.012,        // New size
            cloid: Some(modified_cloid),
            order_type: ClientOrder::Limit(ClientLimit {
                tif: "Gtc".to_string(),
            }),
        };
        let modify_request = ClientModifyRequest {
            oid,
            order: modify_order_details,
        };

        info!(
            "Preparing unsigned modify for OID {}: {:?}",
            oid, modify_request
        );
        let unsigned_modify_components = builder
            .prepare_unsigned_modify_order(modify_request)
            .await?;
        info!(
            "Action for modify: {}",
            serde_json::to_string_pretty(&unsigned_modify_components.action_payload_json).unwrap()
        );

        let modify_response =
            sign_and_post_transaction(unsigned_modify_components, exchange_client, wallet, None)
                .await;

        match modify_response {
            Ok(response_status) => {
                info!(
                    "Modify Post Response for OID {}: {:?}",
                    oid, response_status
                );
                // Similar status checking as order/cancel
            }
            Err(e) => {
                error!("Failed to post modify for OID {}: {:?}", oid, e);
            }
        }
        // Optional: Attempt to cancel the modified order to clean up
        info!(
            "Attempting to cancel modified order OID: {} (best effort)",
            oid
        );
        let cancel_req = ClientCancelRequest {
            asset: "ETH".to_string(),
            oid,
        };
        if let Ok(cancel_comps) = builder.prepare_unsigned_cancel(cancel_req).await {
            let _ = sign_and_post_transaction(cancel_comps, exchange_client, wallet, None).await;
            info!("Sent cancel for modified order OID: {}", oid);
        }
    } else {
        info!("No resting order OID obtained, skipping modify test.");
    }

    info!("--- Finished Signed Modify Order Test ---");
    Ok(())
}

async fn test_signed_update_leverage(
    builder: &UnsignedTransactionBuilder,
    exchange_client: &ExchangeClient,
    wallet: &LocalWallet,
) -> Result<()> {
    info!("--- Testing Signed Update Leverage ---");
    let asset = "ETH";
    let leverage = 5;
    let is_cross = false; // Or true, depending on what you want to test

    info!(
        "Preparing unsigned update leverage: asset {}, leverage {}, is_cross {}",
        asset, leverage, is_cross
    );
    let components = builder
        .prepare_unsigned_update_leverage(leverage, asset, is_cross)
        .await?;

    info!(
        "Action for update leverage: {}",
        serde_json::to_string_pretty(&components.action_payload_json).unwrap()
    );

    let response = sign_and_post_transaction(components, exchange_client, wallet, None).await;

    match response {
        Ok(response_status) => {
            info!("Update Leverage Post Response: {:?}", response_status);
            // Check for success or specific errors
        }
        Err(e) => {
            error!("Failed to post update leverage: {:?}", e);
        }
    }

    info!("--- Finished Signed Update Leverage Test ---");
    Ok(())
}

async fn test_signed_spot_transfer(
    builder: &UnsignedTransactionBuilder,
    exchange_client: &ExchangeClient,
    wallet: &LocalWallet,
) -> Result<()> {
    info!("--- Testing Signed Spot Transfer ---");
    let amount = "1"; // Amount of the token
    let destination = "0x0D1d9635D0640821d15e323ac8AdADfA9c111414";
    // Use a valid spot token for testnet, e.g., from spot_order.rs example or docs
    // This is an example token format. Ensure it exists on testnet.
    let token = "PURR:0xc4bf3f870c0e9465323c0b6ed28096c2";

    info!(
        "Preparing unsigned spot transfer: amount {}, destination {}, token {}",
        amount, destination, token
    );
    let components = builder
        .prepare_unsigned_spot_transfer(amount, destination, token)
        .await?;
    info!(
        "Action for spot transfer: {}",
        serde_json::to_string_pretty(&components.action_payload_json).unwrap()
    );

    let response = sign_and_post_transaction(components, exchange_client, wallet, None).await;

    match response {
        Ok(response_status) => {
            info!("Spot Transfer Post Response: {:?}", response_status);
            // Check for success or specific errors.
            // This might fail if the account doesn't hold the spot asset.
        }
        Err(e) => {
            error!("Failed to post spot transfer: {:?}", e);
        }
    }

    info!("--- Finished Signed Spot Transfer Test ---");
    Ok(())
}

async fn test_signed_vault_transfer(
    builder: &UnsignedTransactionBuilder,
    exchange_client: &ExchangeClient,
    wallet: &LocalWallet,
) -> Result<()> {
    info!("--- Testing Signed Vault Transfer ---");
    let is_deposit = true;
    let usd_amount = 5_000_000; // Example: 5 USD (represented as u64, check SDK for exact unit)
                                // The UnsignedTransactionBuilder expects u64 for usd.
                                // Using the vault address from the `vault_transfer.rs` example bin.
    let vault_address_str = "0x1962905b0a2d0ce7907ae1a0d17f3e4a1f63dfb7";
    let vault_h160 = ethers::types::H160::from_str(vault_address_str)
        .map_err(|e| hl_ranger::Error::GenericParse(format!("Invalid vault address: {}", e)))?;

    // Note: The UnsignedTransactionBuilder has vault_address as an Option<H160> in its own state.
    // The prepare_unsigned_vault_transfer also takes an Option<H160>.
    // If the builder was initialized with a vault_address, that one is used by default for L1 agent hashing.
    // The vault_address parameter in prepare_unsigned_vault_transfer is specifically for the VaultTransfer action payload.

    info!(
        "Preparing unsigned vault transfer: is_deposit {}, usd_amount {}, vault_address {}",
        is_deposit, usd_amount, vault_address_str
    );
    let components = builder
        .prepare_unsigned_vault_transfer(is_deposit, usd_amount, Some(vault_h160))
        .await?;
    info!(
        "Action for vault transfer: {}",
        serde_json::to_string_pretty(&components.action_payload_json).unwrap()
    );

    let response = sign_and_post_transaction(components, exchange_client, wallet, None).await;

    match response {
        Ok(response_status) => {
            info!("Vault Transfer Post Response: {:?}", response_status);
            // Check for success. This might fail if the user is not whitelisted or vault doesn't exist.
        }
        Err(e) => {
            error!("Failed to post vault transfer: {:?}", e);
        }
    }

    info!("--- Finished Signed Vault Transfer Test ---");
    Ok(())
}

async fn test_signed_bulk_cancel(
    builder: &UnsignedTransactionBuilder,
    exchange_client: &ExchangeClient,
    wallet: &LocalWallet,
) -> Result<()> {
    info!("--- Testing Signed Bulk Cancel ---");

    // 1. Place a couple of orders to get OIDs
    let mut oids_to_cancel: Vec<ClientCancelRequest> = Vec::new();
    info!("Placing orders for bulk cancel test...");

    for i in 0..2 {
        let cloid = Uuid::new_v4();
        let order_request = ClientOrderRequest {
            asset: "ETH".to_string(),
            is_buy: true,
            reduce_only: false,
            limit_px: 1000.0 + i as f64, // Slightly different prices
            sz: 0.01,
            cloid: Some(cloid),
            order_type: ClientOrder::Limit(ClientLimit {
                tif: "Gtc".to_string(),
            }),
        };
        let unsigned_order_components = builder
            .prepare_unsigned_order(
                order_request.clone(),
                Some(format!("bulk_cancel_setup_{}", i)),
            )
            .await?;

        let order_response =
            sign_and_post_transaction(unsigned_order_components, exchange_client, wallet, None)
                .await;
        match order_response {
            Ok(ExchangeResponseStatus::Ok(res_data)) => {
                if let Some(data) = res_data.data {
                    if !data.statuses.is_empty() {
                        if let ExchangeDataStatus::Resting(resting) = &data.statuses[0] {
                            info!("Order {} placed for bulk cancel, OID: {}", i, resting.oid);
                            oids_to_cancel.push(ClientCancelRequest {
                                asset: "ETH".to_string(),
                                oid: resting.oid,
                            });
                        } else {
                            info!("Order {} did not rest: {:?}", i, data.statuses[0]);
                        }
                    }
                }
            }
            Ok(ExchangeResponseStatus::Err(e)) => error!("Error placing order {}: {}", i, e),
            Err(e) => error!("Failed to post order {}: {:?}", i, e),
        }
        thread::sleep(time::Duration::from_millis(500)); // Small delay between posts
    }

    if oids_to_cancel.len() < 2 {
        error!(
            "Could not place enough orders for bulk cancel test. Placed: {}. Skipping.",
            oids_to_cancel.len()
        );
        return Ok(());
    }

    info!(
        "Waiting a few seconds before attempting bulk cancel for OIDs: {:?}",
        oids_to_cancel.iter().map(|c| c.oid).collect::<Vec<_>>()
    );
    thread::sleep(time::Duration::from_secs(5));

    // 2. Prepare and post a bulk cancel for these orders
    info!(
        "Preparing unsigned bulk cancel for {} orders",
        oids_to_cancel.len()
    );
    let components = builder.prepare_unsigned_bulk_cancel(oids_to_cancel).await?;
    info!(
        "Action for bulk cancel: {}",
        serde_json::to_string_pretty(&components.action_payload_json).unwrap()
    );

    let response = sign_and_post_transaction(components, exchange_client, wallet, None).await;

    match response {
        Ok(response_status) => {
            info!("Bulk Cancel Post Response: {:?}", response_status);
            // Check statuses for each cancelled order if needed
        }
        Err(e) => {
            error!("Failed to post bulk cancel: {:?}", e);
        }
    }

    info!("--- Finished Signed Bulk Cancel Test ---");
    Ok(())
}

async fn test_exchange_client_order_with_agent(
    exchange_client: &ExchangeClient,
    wallet: &LocalWallet,
) -> Result<()> {
    info!("--- Testing ExchangeClient Order Method Directly ---");

    // 1. Approve the wallet as an L1 agent using ExchangeClient directly
    info!("Approving L1 agent using ExchangeClient...");
    let (agent_key, approve_response) = exchange_client.approve_agent(Some(wallet)).await?;
    info!("Generated agent key: {}", agent_key);
    info!("L1 Agent Approval Response: {:?}", approve_response);

    // Wait a moment for the approval to be processed
    info!("Waiting for agent approval to be processed...");
    thread::sleep(time::Duration::from_secs(10));

    // 2. Now try to place an order using ExchangeClient's order method directly
    let cloid = Uuid::new_v4();
    let order_request = ClientOrderRequest {
        asset: "ETH".to_string(),
        is_buy: true,
        reduce_only: false,
        limit_px: 2500.0, // More realistic price close to current market ($2525)
        sz: 0.1, // Increased from 0.01 to 0.1 ETH (~$252 vs $25) to be well above $10 minimum
        cloid: Some(cloid),
        order_type: ClientOrder::Limit(ClientLimit {
            tif: "Gtc".to_string(),
        }),
    };
    info!(
        "Placing order using ExchangeClient.order() method: {:?}",
        order_request
    );

    // Use ExchangeClient's order method directly
    let order_response = exchange_client
        .order(order_request.clone(), Some(wallet))
        .await;

    match order_response {
        Ok(response_status) => {
            info!("ExchangeClient Order Response: {:?}", response_status);
            if let ExchangeResponseStatus::Ok(response_data) = response_status {
                if let Some(data) = response_data.data {
                    if !data.statuses.is_empty() {
                        match &data.statuses[0] {
                            ExchangeDataStatus::Resting(resting_order) => {
                                info!("Order Resting with OID: {}", resting_order.oid);
                                // Try to cancel it
                                let cancel_request = ClientCancelRequest {
                                    asset: "ETH".to_string(),
                                    oid: resting_order.oid,
                                };
                                thread::sleep(time::Duration::from_secs(3));
                                let cancel_response =
                                    exchange_client.cancel(cancel_request, Some(wallet)).await;
                                info!("Cancel Response: {:?}", cancel_response);
                            }
                            ExchangeDataStatus::Filled(filled_order) => {
                                info!(
                                    "Order Filled with OID: {}. Position opened!",
                                    filled_order.oid
                                );
                            }
                            ExchangeDataStatus::Error(e) => {
                                error!("Order Post Error in status: {}", e);
                            }
                            other => {
                                info!("Order Post - Unexpected status: {:?}", other);
                            }
                        }
                    } else {
                        info!("Order Post - No statuses in response data.");
                    }
                } else {
                    info!("Order Post - No data in response.");
                }
            } else if let ExchangeResponseStatus::Err(e) = response_status {
                error!("Order Post Error: {}", e);
            }
        }
        Err(e) => {
            error!("Failed to post order using ExchangeClient: {:?}", e);
        }
    }

    info!("--- Finished ExchangeClient Order Method Test ---");
    Ok(())
}

async fn test_compare_payloads(
    builder: &UnsignedTransactionBuilder,
    exchange_client: &ExchangeClient,
    wallet: &LocalWallet,
) -> Result<()> {
    info!("--- Comparing Our Payload vs ExchangeClient Payload ---");

    // Test with USDC transfer since it was working before
    let amount = "1.0";
    let destination = "0x0D1d9635D0640821d15e323ac8AdADfA9c111414";

    // 1. Our implementation
    info!("Testing our UnsignedTransactionBuilder implementation...");
    let components = builder
        .prepare_unsigned_usdc_transfer(amount, destination)
        .await?;

    let response = sign_and_post_transaction(components, exchange_client, wallet, None).await;
    info!("Our implementation result: {:?}", response);

    // 2. ExchangeClient implementation
    info!("Testing ExchangeClient implementation...");
    let exchange_response = exchange_client
        .usdc_transfer(amount, destination, Some(wallet))
        .await;
    info!("ExchangeClient result: {:?}", exchange_response);

    info!("--- Finished Payload Comparison ---");
    Ok(())
}

async fn test_open_and_close_position(
    exchange_client: &ExchangeClient,
    wallet: &LocalWallet,
) -> Result<()> {
    info!("--- Testing Open and Close Position with Market Orders ---");

    // 1. Approve the wallet as an L1 agent using ExchangeClient directly
    info!("Approving L1 agent using ExchangeClient...");
    let (agent_key, approve_response) = exchange_client.approve_agent(Some(wallet)).await?;
    info!("Generated agent key: {}", agent_key);
    info!("L1 Agent Approval Response: {:?}", approve_response);

    // Wait a moment for the approval to be processed
    info!("Waiting for agent approval to be processed...");
    thread::sleep(time::Duration::from_secs(10));

    // 2. Open position with market order (should fill immediately)
    let cloid = Uuid::new_v4();
    let market_order_request = ClientOrderRequest {
        asset: "ETH".to_string(),
        is_buy: true,
        reduce_only: false,
        limit_px: 2600.0, // Above market price to ensure immediate fill
        sz: 0.1,          // $250+ value, well above $10 minimum
        cloid: Some(cloid),
        order_type: ClientOrder::Limit(ClientLimit {
            tif: "Ioc".to_string(), // Immediate or Cancel - acts like market order
        }),
    };
    info!(
        "Opening position with market-like order: {:?}",
        market_order_request
    );

    let open_response = exchange_client
        .order(market_order_request.clone(), Some(wallet))
        .await;

    let mut position_opened = false;
    match open_response {
        Ok(response_status) => {
            info!("Open Position Response: {:?}", response_status);
            if let ExchangeResponseStatus::Ok(response_data) = response_status {
                if let Some(data) = response_data.data {
                    if !data.statuses.is_empty() {
                        match &data.statuses[0] {
                            ExchangeDataStatus::Filled(filled_order) => {
                                info!(
                                    "🎉 POSITION OPENED! Order Filled with OID: {}",
                                    filled_order.oid
                                );
                                position_opened = true;
                            }
                            ExchangeDataStatus::Resting(resting_order) => {
                                info!(
                                    "Order Resting with OID: {} (not filled yet)",
                                    resting_order.oid
                                );
                                // Cancel the resting order
                                let cancel_request = ClientCancelRequest {
                                    asset: "ETH".to_string(),
                                    oid: resting_order.oid,
                                };
                                let _ = exchange_client.cancel(cancel_request, Some(wallet)).await;
                                info!("Canceled resting order");
                            }
                            ExchangeDataStatus::Error(e) => {
                                error!("Order Error: {}", e);
                            }
                            other => {
                                info!("Unexpected order status: {:?}", other);
                            }
                        }
                    }
                }
            }
        }
        Err(e) => {
            error!("Failed to place market order: {:?}", e);
        }
    }

    if position_opened {
        info!("Position opened successfully! Waiting 5 seconds before closing...");
        thread::sleep(time::Duration::from_secs(5));

        // 3. Close position with market order
        let close_cloid = Uuid::new_v4();
        let close_order_request = ClientOrderRequest {
            asset: "ETH".to_string(),
            is_buy: false,     // Sell to close long position
            reduce_only: true, // This ensures we're closing, not opening new position
            limit_px: 2400.0,  // Below market price to ensure immediate fill
            sz: 0.1,           // Same size as opened position
            cloid: Some(close_cloid),
            order_type: ClientOrder::Limit(ClientLimit {
                tif: "Ioc".to_string(), // Immediate or Cancel
            }),
        };
        info!(
            "Closing position with market-like order: {:?}",
            close_order_request
        );

        let close_response = exchange_client
            .order(close_order_request, Some(wallet))
            .await;

        match close_response {
            Ok(response_status) => {
                info!("Close Position Response: {:?}", response_status);
                if let ExchangeResponseStatus::Ok(response_data) = response_status {
                    if let Some(data) = response_data.data {
                        if !data.statuses.is_empty() {
                            match &data.statuses[0] {
                                ExchangeDataStatus::Filled(filled_order) => {
                                    info!(
                                        "🎉 POSITION CLOSED! Order Filled with OID: {}",
                                        filled_order.oid
                                    );
                                }
                                ExchangeDataStatus::Error(e) => {
                                    error!("Close Order Error: {}", e);
                                }
                                other => {
                                    info!("Unexpected close status: {:?}", other);
                                }
                            }
                        }
                    }
                }
            }
            Err(e) => {
                error!("Failed to close position: {:?}", e);
            }
        }
    } else {
        info!("No position was opened, skipping close test.");
    }

    info!("--- Finished Open and Close Position Test ---");
    Ok(())
}

async fn test_simple_limit_orders_with_exchange_client(
    exchange_client: &ExchangeClient,
    wallet: &LocalWallet,
) -> Result<()> {
    info!("--- Testing Simple Limit Orders with ExchangeClient ---");

    // 1. Approve agent (required for orders)
    info!("Approving L1 agent...");
    let (agent_key, approve_response) = exchange_client.approve_agent(Some(wallet)).await?;
    info!("Generated agent key: {}", agent_key);
    info!("L1 Agent Approval Response: {:?}", approve_response);

    // Wait for approval to be processed
    info!("Waiting for agent approval to be processed...");
    thread::sleep(time::Duration::from_secs(10));

    // 2. Place a limit order using ExchangeClient directly
    let cloid = Uuid::new_v4();
    let order_request = ClientOrderRequest {
        asset: "ETH".to_string(),
        is_buy: true,
        reduce_only: false,
        limit_px: 2500.0, // Below market price so it rests
        sz: 0.1,          // $250+ value, well above $10 minimum
        cloid: Some(cloid),
        order_type: ClientOrder::Limit(ClientLimit {
            tif: "Gtc".to_string(),
        }),
    };
    info!(
        "Placing limit order using ExchangeClient: {:?}",
        order_request
    );

    let order_response = exchange_client.order(order_request, Some(wallet)).await;

    let mut oid_to_cancel: Option<u64> = None;
    match order_response {
        Ok(response_status) => {
            info!("Limit Order Response: {:?}", response_status);
            if let ExchangeResponseStatus::Ok(response_data) = response_status {
                if let Some(data) = response_data.data {
                    if !data.statuses.is_empty() {
                        match &data.statuses[0] {
                            ExchangeDataStatus::Resting(resting_order) => {
                                info!(
                                    "🎉 SUCCESS! Limit order resting with OID: {}",
                                    resting_order.oid
                                );
                                oid_to_cancel = Some(resting_order.oid);
                            }
                            ExchangeDataStatus::Filled(filled_order) => {
                                info!(
                                    "🎉 SUCCESS! Limit order filled with OID: {}",
                                    filled_order.oid
                                );
                            }
                            ExchangeDataStatus::Error(e) => {
                                error!("Limit order error: {}", e);
                            }
                            other => {
                                info!("Limit order unexpected status: {:?}", other);
                            }
                        }
                    }
                }
            }
        }
        Err(e) => {
            error!("Failed to place limit order: {:?}", e);
        }
    }

    // 3. Cancel the order if it's resting
    if let Some(oid) = oid_to_cancel {
        info!("Waiting 3 seconds before canceling order OID: {}", oid);
        thread::sleep(time::Duration::from_secs(3));

        let cancel_request = ClientCancelRequest {
            asset: "ETH".to_string(),
            oid,
        };
        info!("Canceling order OID: {}", oid);

        let cancel_response = exchange_client.cancel(cancel_request, Some(wallet)).await;

        match cancel_response {
            Ok(response_status) => {
                info!("Cancel Response: {:?}", response_status);
                if let ExchangeResponseStatus::Ok(response_data) = response_status {
                    if let Some(data) = response_data.data {
                        if !data.statuses.is_empty() {
                            match &data.statuses[0] {
                                ExchangeDataStatus::Success => {
                                    info!("✅ Successfully cancelled OID: {}", oid);
                                }
                                ExchangeDataStatus::Error(e) => {
                                    error!("Cancel error for OID {}: {}", oid, e);
                                }
                                other => {
                                    info!("Cancel unexpected status for OID {}: {:?}", oid, other);
                                }
                            }
                        }
                    }
                }
            }
            Err(e) => {
                error!("Failed to cancel OID {}: {:?}", oid, e);
            }
        }
    } else {
        info!("No resting order to cancel.");
    }

    info!("--- Finished Simple Limit Orders with ExchangeClient Test ---");
    Ok(())
}