nautilus-derive 0.58.0

Derive integration adapter for the Nautilus trading engine
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
// -------------------------------------------------------------------------------------------------
//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
//  https://nautechsystems.io
//
//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
//  You may not use this file except in compliance with the License.
//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// -------------------------------------------------------------------------------------------------

//! `reqwest`-backed REST client for the Derive API.
//!
//! [`DeriveHttpClient`] exposes typed `send_public` / `send_private`
//! dispatchers plus thin wrappers for the two endpoints that establish the
//! plumbing this crate needs to grow against: `public/get_instruments` and
//! `private/order`. Authenticated requests inject the EIP-191 session-key
//! headers built by [`crate::signing::auth`].

use std::{
    collections::HashMap,
    fmt::Debug,
    sync::{
        Arc,
        atomic::{AtomicU64, Ordering},
    },
};

use ahash::AHashMap;
use alloy::signers::local::PrivateKeySigner;
use nautilus_network::{
    http::{HttpClient, HttpClientError, HttpResponse},
    retry::{RetryConfig, RetryManager},
};
use serde::{Serialize, de::DeserializeOwned};
use serde_json::Value;

use crate::{
    common::{
        consts::{HEADER_LYRA_SIGNATURE, HEADER_LYRA_TIMESTAMP, HEADER_LYRA_WALLET, HTTP_TIMEOUT},
        enums::DeriveInstrumentType,
        rate_limit::{self, DERIVE_NON_MATCHING_RATE_KEY},
        retry::{http_retry_config, should_retry_http_error},
    },
    http::{
        error::{DeriveHttpError, Result},
        models::{
            DeriveEmptyResult, DeriveInstrument, DeriveOpenOrdersResult, DeriveOrder,
            DeriveOrderResult, DeriveOrdersResult, DerivePositionsResult, DerivePublicCandle,
            DerivePublicFundingRateHistoryResult, DerivePublicTradesResult, DeriveReplaceResult,
            DeriveSubaccount, DeriveTickerSnapshot, DeriveTickersResult, DeriveTradesResult,
            JsonRpcResponse,
        },
        query::{
            DeriveCancelAllParams, DeriveCancelByLabelParams, DeriveCancelParams,
            DeriveGetOpenOrdersParams, DeriveGetOrderHistoryParams, DeriveGetOrderParams,
            DeriveGetPositionsParams, DeriveGetSubaccountParams, DeriveGetTradeHistoryParams,
            DeriveGetTriggerOrdersParams, DeriveOrderParams, DeriveReplaceParams,
        },
    },
    signing::auth::{AuthHeaders, build_rest_auth_headers},
};

/// Credentials used to sign authenticated REST requests.
///
/// `Debug` is implemented manually so the session key never escapes through
/// loggers or Python `__repr__`.
#[derive(Clone)]
pub struct DeriveCredentials {
    /// Derive Chain smart-contract wallet address (`0x`-prefixed hex, 42 chars).
    pub wallet_address: String,
    /// secp256k1 session-key signer.
    pub signer: PrivateKeySigner,
}

impl DeriveCredentials {
    /// Constructs credentials by parsing `session_key_hex` into a signer.
    ///
    /// # Errors
    ///
    /// Returns [`DeriveHttpError::Auth`] when the session-key hex cannot be
    /// parsed.
    pub fn new(wallet_address: impl Into<String>, session_key_hex: &str) -> Result<Self> {
        let signer: PrivateKeySigner = session_key_hex
            .parse()
            .map_err(|e| DeriveHttpError::decode(format!("invalid session key: {e}")))?;
        Ok(Self {
            wallet_address: wallet_address.into(),
            signer,
        })
    }
}

impl Debug for DeriveCredentials {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct(stringify!(DeriveCredentials))
            .field("wallet_address", &self.wallet_address)
            .field("signer", &"***redacted***")
            .finish()
    }
}

/// HTTP client for the Derive REST API.
///
/// The client carries an atomic `id` counter so every request frame has a
/// unique correlator; the REST transport ships only `params` on the wire but
/// the id is preserved for logs and reused by the upcoming WebSocket client.
/// Each call routes through a [`RetryManager`] that re-signs auth headers on
/// every attempt, so retries never replay a stale `X-LYRATIMESTAMP`.
#[derive(Debug, Clone)]
pub struct DeriveHttpClient {
    client: HttpClient,
    base_url: String,
    credentials: Option<DeriveCredentials>,
    next_id: Arc<AtomicU64>,
    timeout_secs: u64,
    retry_manager: Arc<RetryManager<DeriveHttpError>>,
}

impl DeriveHttpClient {
    /// Creates a public-only client.
    ///
    /// `retry_config` defaults to [`http_retry_config(3, 100, 5_000)`] when `None`.
    ///
    /// # Errors
    ///
    /// Returns [`DeriveHttpError::Transport`] when the underlying HTTP client
    /// (proxy URL, TLS init) cannot be constructed.
    pub fn new(
        base_url: impl Into<String>,
        timeout_secs: Option<u64>,
        proxy_url: Option<String>,
        retry_config: Option<RetryConfig>,
    ) -> Result<Self> {
        let timeout_secs = timeout_secs.unwrap_or_else(|| HTTP_TIMEOUT.as_secs());
        let client = build_client(timeout_secs, proxy_url)?;
        let retry_config = retry_config.unwrap_or_else(|| http_retry_config(3, 100, 5_000));
        Ok(Self {
            client,
            base_url: trim_trailing_slash(base_url.into()),
            credentials: None,
            next_id: Arc::new(AtomicU64::new(1)),
            timeout_secs,
            retry_manager: Arc::new(RetryManager::new(retry_config)),
        })
    }

    /// Creates a client with credentials installed for `send_private` calls.
    ///
    /// # Errors
    ///
    /// Returns [`DeriveHttpError::Transport`] when the underlying HTTP client
    /// cannot be constructed.
    pub fn with_credentials(
        base_url: impl Into<String>,
        credentials: DeriveCredentials,
        timeout_secs: Option<u64>,
        proxy_url: Option<String>,
        retry_config: Option<RetryConfig>,
    ) -> Result<Self> {
        let mut client = Self::new(base_url, timeout_secs, proxy_url, retry_config)?;
        client.credentials = Some(credentials);
        Ok(client)
    }

    /// Returns the configured base URL (no trailing slash).
    #[must_use]
    pub fn base_url(&self) -> &str {
        &self.base_url
    }

    /// Returns `true` when credentials are installed.
    #[must_use]
    pub fn has_credentials(&self) -> bool {
        self.credentials.is_some()
    }

    /// Allocates the next correlator id.
    fn next_id(&self) -> u64 {
        self.next_id.fetch_add(1, Ordering::Relaxed)
    }

    /// Sends an unauthenticated request and decodes the JSON-RPC envelope.
    ///
    /// Public endpoints are idempotent reads; this path retries transient
    /// failures via the configured [`RetryManager`].
    ///
    /// # Errors
    ///
    /// Propagates transport, HTTP, and JSON-RPC errors. See [`DeriveHttpError`].
    pub async fn send_public<P, R>(&self, method: &str, params: &P) -> Result<R>
    where
        P: Serialize + ?Sized,
        R: DeserializeOwned,
    {
        let id = self.next_id();
        self.dispatch(method, params, id, false, true).await
    }

    /// Sends an authenticated idempotent request (private reads).
    ///
    /// Used for `private/get_*` endpoints whose responses are pure reads of
    /// venue state. Transient failures retry via the configured
    /// [`RetryManager`].
    ///
    /// # Errors
    ///
    /// Returns [`DeriveHttpError::MissingCredentials`] when the client was
    /// built without credentials. Other variants propagate from the transport
    /// or the venue.
    pub async fn send_private<P, R>(&self, method: &str, params: &P) -> Result<R>
    where
        P: Serialize + ?Sized,
        R: DeserializeOwned,
    {
        if self.credentials.is_none() {
            return Err(DeriveHttpError::MissingCredentials {
                method: method.to_owned(),
            });
        }
        let id = self.next_id();
        self.dispatch(method, params, id, true, true).await
    }

    /// Sends an authenticated request exactly once (no retry).
    ///
    /// Used for state-changing endpoints (`private/order`, `private/cancel`,
    /// `private/cancel_all`, `private/cancel_by_label`, `private/replace`)
    /// where a transport-level failure leaves the venue's view of the
    /// signed action ambiguous: the request may have been accepted before
    /// the network broke. Automatic replay would either double-submit (when
    /// the venue accepted) or trigger a duplicate-nonce rejection (which
    /// the caller would surface as `OrderRejected` even though the original
    /// is live). Callers are expected to resolve ambiguous outcomes via
    /// reconciliation rather than retry here.
    ///
    /// # Errors
    ///
    /// Returns [`DeriveHttpError::MissingCredentials`] when the client was
    /// built without credentials. Other variants propagate from the transport
    /// or the venue.
    pub async fn send_private_once<P, R>(&self, method: &str, params: &P) -> Result<R>
    where
        P: Serialize + ?Sized,
        R: DeserializeOwned,
    {
        if self.credentials.is_none() {
            return Err(DeriveHttpError::MissingCredentials {
                method: method.to_owned(),
            });
        }
        let id = self.next_id();
        self.dispatch(method, params, id, true, false).await
    }

    /// Fetches the venue's listed instruments.
    ///
    /// `currency` is the perpetual/option underlying (e.g. `"ETH"`). When
    /// `expired` is `true` the venue includes expired option strikes.
    ///
    /// # Errors
    ///
    /// Propagates [`DeriveHttpError`] for transport, HTTP, and JSON-RPC failures.
    pub async fn get_instruments(
        &self,
        currency: &str,
        instrument_type: DeriveInstrumentType,
        expired: bool,
    ) -> Result<Vec<DeriveInstrument>> {
        let params = serde_json::json!({
            "currency": currency,
            "instrument_type": instrument_type,
            "expired": expired,
        });
        self.send_public("public/get_instruments", &params).await
    }

    /// Fetches a single instrument definition by name.
    ///
    /// Mirrors `public/get_instrument`, which the venue documents as the
    /// per-asset variant of `public/get_instruments`. The returned record
    /// matches one row of the bulk endpoint.
    ///
    /// # Errors
    ///
    /// Propagates [`DeriveHttpError`] for transport, HTTP, and JSON-RPC failures.
    pub async fn get_instrument(&self, instrument_name: &str) -> Result<DeriveInstrument> {
        let params = serde_json::json!({
            "instrument_name": instrument_name,
        });
        self.send_public("public/get_instrument", &params).await
    }

    /// Fetches a page of public trade history for the instrument.
    ///
    /// `from_timestamp` / `to_timestamp` are UNIX milliseconds and bound the
    /// returned window. `page` is 1-indexed; `page_size` is capped by the venue
    /// at 1000.
    ///
    /// # Errors
    ///
    /// Propagates [`DeriveHttpError`] for transport, HTTP, and JSON-RPC failures.
    pub async fn get_trade_history(
        &self,
        instrument_name: &str,
        from_timestamp: Option<i64>,
        to_timestamp: Option<i64>,
        page: u32,
        page_size: u32,
    ) -> Result<DerivePublicTradesResult> {
        let mut params = serde_json::Map::new();
        params.insert("instrument_name".to_string(), instrument_name.into());
        params.insert("page".to_string(), page.into());
        params.insert("page_size".to_string(), page_size.into());
        if let Some(from) = from_timestamp {
            params.insert("from_timestamp".to_string(), from.into());
        }

        if let Some(to) = to_timestamp {
            params.insert("to_timestamp".to_string(), to.into());
        }

        self.send_public("public/get_trade_history", &Value::Object(params))
            .await
    }

    /// Fetches the public funding rate history for the instrument.
    ///
    /// `start_timestamp` / `end_timestamp` are UNIX milliseconds. `period`, if
    /// provided, selects the sample interval in seconds.
    ///
    /// # Errors
    ///
    /// Propagates [`DeriveHttpError`] for transport, HTTP, and JSON-RPC failures.
    pub async fn get_funding_rate_history(
        &self,
        instrument_name: &str,
        start_timestamp: Option<i64>,
        end_timestamp: Option<i64>,
        period: Option<u32>,
    ) -> Result<DerivePublicFundingRateHistoryResult> {
        let mut params = serde_json::Map::new();
        params.insert("instrument_name".to_string(), instrument_name.into());
        if let Some(start) = start_timestamp {
            params.insert("start_timestamp".to_string(), start.into());
        }

        if let Some(end) = end_timestamp {
            params.insert("end_timestamp".to_string(), end.into());
        }

        if let Some(period) = period {
            params.insert("period".to_string(), period.into());
        }

        self.send_public("public/get_funding_rate_history", &Value::Object(params))
            .await
    }

    /// Fetches OHLCV candles via `public/get_tradingview_chart_data`.
    ///
    /// `start_timestamp` / `end_timestamp` are UNIX **seconds** and bound the
    /// returned window. `period` is the bucket size in seconds; the venue
    /// accepts 60, 300, 900, 1800, 3600, 14400, 28800, 86400, and 604800.
    /// The venue ships `result` as a flat array; the client decodes it
    /// directly into `Vec<DerivePublicCandle>`.
    ///
    /// # Errors
    ///
    /// Propagates [`DeriveHttpError`] for transport, HTTP, and JSON-RPC failures.
    pub async fn get_candles(
        &self,
        instrument_name: &str,
        start_timestamp: i64,
        end_timestamp: i64,
        period: u32,
    ) -> Result<Vec<DerivePublicCandle>> {
        let params = serde_json::json!({
            "instrument_name": instrument_name,
            "start_timestamp": start_timestamp,
            "end_timestamp": end_timestamp,
            "period": period,
        });
        self.send_public("public/get_tradingview_chart_data", &params)
            .await
    }

    /// Fetches current ticker snapshots.
    ///
    /// `currency` is the underlying (`"ETH"`, `"BTC"`, etc.). Options require
    /// both `currency` and `expiry_date`; perps and ERC-20 spot pairs reject
    /// `expiry_date`.
    ///
    /// # Errors
    ///
    /// Propagates [`DeriveHttpError`] for transport, HTTP, and JSON-RPC failures.
    pub async fn get_tickers(
        &self,
        instrument_type: DeriveInstrumentType,
        currency: Option<&str>,
        expiry_date: Option<&str>,
    ) -> Result<DeriveTickersResult> {
        let mut params = serde_json::Map::new();
        params.insert(
            "instrument_type".to_string(),
            serde_json::to_value(instrument_type).map_err(DeriveHttpError::from)?,
        );

        if let Some(currency) = currency {
            params.insert("currency".to_string(), currency.into());
        }

        if let Some(expiry_date) = expiry_date {
            params.insert("expiry_date".to_string(), expiry_date.into());
        }

        self.send_public("public/get_tickers", &Value::Object(params))
            .await
    }

    /// Fetches the current ticker snapshot for one instrument.
    ///
    /// This is a single-instrument convenience wrapper over
    /// `public/get_tickers`, which replaced Derive's deprecated
    /// `public/get_ticker` RPC.
    ///
    /// # Errors
    ///
    /// Propagates [`DeriveHttpError`] for transport, HTTP, JSON-RPC failures,
    /// or when the response omits the requested instrument.
    pub async fn get_ticker(&self, instrument_name: &str) -> Result<DeriveTickerSnapshot> {
        let request = ticker_request(instrument_name)?;
        let result = self
            .get_tickers(
                request.instrument_type,
                Some(request.currency),
                request.expiry_date,
            )
            .await?;
        let mut ticker = result
            .tickers
            .get(instrument_name)
            .cloned()
            .ok_or_else(|| {
                DeriveHttpError::decode(format!(
                    "missing ticker `{instrument_name}` in public/get_tickers response"
                ))
            })?;
        ticker.instrument_name = instrument_name.into();
        Ok(ticker)
    }

    /// Submits a signed order to the venue.
    ///
    /// `params` must be the fully-built signed `private/order` body.
    ///
    /// # Errors
    ///
    /// Returns [`DeriveHttpError::MissingCredentials`] when no credentials
    /// were installed; otherwise propagates transport and venue errors.
    pub async fn submit_order(&self, params: &DeriveOrderParams) -> Result<DeriveOrder> {
        let result: DeriveOrderResult = self.send_private_once("private/order", params).await?;
        Ok(result.order)
    }

    /// Cancels a single order by venue order id.
    ///
    /// # Errors
    ///
    /// Returns [`DeriveHttpError::MissingCredentials`] when no credentials
    /// were installed; otherwise propagates transport and venue errors.
    pub async fn cancel_order(&self, params: &DeriveCancelParams) -> Result<DeriveEmptyResult> {
        self.send_private_once("private/cancel", params).await
    }

    /// Cancels every open order on the subaccount, optionally scoped to an
    /// instrument.
    ///
    /// # Errors
    ///
    /// Returns [`DeriveHttpError::MissingCredentials`] when no credentials
    /// were installed; otherwise propagates transport and venue errors.
    pub async fn cancel_all(&self, params: &DeriveCancelAllParams) -> Result<DeriveEmptyResult> {
        self.send_private_once("private/cancel_all", params).await
    }

    /// Cancels every open order for the given user label on the subaccount.
    ///
    /// # Errors
    ///
    /// Returns [`DeriveHttpError::MissingCredentials`] when no credentials
    /// were installed; otherwise propagates transport and venue errors.
    pub async fn cancel_by_label(
        &self,
        params: &DeriveCancelByLabelParams,
    ) -> Result<DeriveEmptyResult> {
        self.send_private_once("private/cancel_by_label", params)
            .await
    }

    /// Submits a signed `private/replace` request that atomically cancels one
    /// order and creates a new one.
    ///
    /// `params` must be the fully-built typed request body.
    ///
    /// # Errors
    ///
    /// Returns [`DeriveHttpError::MissingCredentials`] when no credentials
    /// were installed; otherwise propagates transport and venue errors.
    pub async fn replace_order(&self, params: &DeriveReplaceParams) -> Result<DeriveOrder> {
        let result: DeriveReplaceResult = self.send_private_once("private/replace", params).await?;
        Ok(result.order)
    }

    /// Returns the subaccount snapshot including margin, balances, and
    /// open orders.
    ///
    /// # Errors
    ///
    /// Returns [`DeriveHttpError::MissingCredentials`] when no credentials
    /// were installed; otherwise propagates transport and venue errors.
    pub async fn get_subaccount(
        &self,
        params: &DeriveGetSubaccountParams,
    ) -> Result<DeriveSubaccount> {
        self.send_private("private/get_subaccount", params).await
    }

    /// Returns currently open orders for the subaccount.
    ///
    /// # Errors
    ///
    /// Returns [`DeriveHttpError::MissingCredentials`] when no credentials
    /// were installed; otherwise propagates transport and venue errors.
    pub async fn get_open_orders(
        &self,
        params: &DeriveGetOpenOrdersParams,
    ) -> Result<DeriveOpenOrdersResult> {
        self.send_private("private/get_open_orders", params).await
    }

    /// Returns currently untriggered trigger orders for the subaccount.
    ///
    /// # Errors
    ///
    /// Returns [`DeriveHttpError::MissingCredentials`] when no credentials
    /// were installed; otherwise propagates transport and venue errors.
    pub async fn get_trigger_orders(
        &self,
        params: &DeriveGetTriggerOrdersParams,
    ) -> Result<DeriveOpenOrdersResult> {
        self.send_private("private/get_trigger_orders", params)
            .await
    }

    /// Returns a single order by venue order id.
    ///
    /// # Errors
    ///
    /// Returns [`DeriveHttpError::MissingCredentials`] when no credentials
    /// were installed; otherwise propagates transport and venue errors.
    pub async fn get_order(&self, params: &DeriveGetOrderParams) -> Result<DeriveOrder> {
        self.send_private("private/get_order", params).await
    }

    /// Returns one page of order history for the subaccount, optionally
    /// scoped to an instrument and time window.
    ///
    /// `from_timestamp` / `to_timestamp` are UNIX milliseconds. `page` is
    /// 1-indexed and `page_size` is capped by the venue at 1000.
    ///
    /// # Errors
    ///
    /// Returns [`DeriveHttpError::MissingCredentials`] when no credentials
    /// were installed; otherwise propagates transport and venue errors.
    pub async fn get_order_history(
        &self,
        params: &DeriveGetOrderHistoryParams,
    ) -> Result<DeriveOrdersResult> {
        self.send_private("private/get_order_history", params).await
    }

    /// Returns one page of subaccount trade history.
    ///
    /// # Errors
    ///
    /// Returns [`DeriveHttpError::MissingCredentials`] when no credentials
    /// were installed; otherwise propagates transport and venue errors.
    pub async fn get_private_trade_history(
        &self,
        params: &DeriveGetTradeHistoryParams,
    ) -> Result<DeriveTradesResult> {
        self.send_private("private/get_trade_history", params).await
    }

    /// Returns the positions held by the subaccount.
    ///
    /// # Errors
    ///
    /// Returns [`DeriveHttpError::MissingCredentials`] when no credentials
    /// were installed; otherwise propagates transport and venue errors.
    pub async fn get_positions(
        &self,
        params: &DeriveGetPositionsParams,
    ) -> Result<DerivePositionsResult> {
        self.send_private("private/get_positions", params).await
    }

    async fn dispatch<P, R>(
        &self,
        method: &str,
        params: &P,
        id: u64,
        authenticate: bool,
        retry: bool,
    ) -> Result<R>
    where
        P: Serialize + ?Sized,
        R: DeserializeOwned,
    {
        let url = format!("{}/{}", self.base_url, method.trim_start_matches('/'));
        let body_value = serde_json::to_value(params).map_err(DeriveHttpError::from)?;
        let body = serde_json::to_vec(&body_value).map_err(DeriveHttpError::from)?;

        // Every REST call is a non-matching read; gate it on the shared
        // non-matching quota. A non-empty key is required: the limiter skips
        // requests sent with no keys even when a default quota is configured.
        let rate_keys = vec![DERIVE_NON_MATCHING_RATE_KEY.to_string()];

        // Sign per-attempt so the venue never sees a stale `X-LYRATIMESTAMP`
        // after a long backoff window; single-shot writes still run the
        // closure once and use freshly built headers.
        let attempt = || async {
            let mut headers: AHashMap<String, String> = AHashMap::with_capacity(4);
            headers.insert("Content-Type".to_string(), "application/json".to_string());

            if authenticate {
                let auth = self.build_auth_headers(method)?;
                headers.insert(HEADER_LYRA_WALLET.to_string(), auth.wallet);
                headers.insert(HEADER_LYRA_TIMESTAMP.to_string(), auth.timestamp);
                headers.insert(HEADER_LYRA_SIGNATURE.to_string(), auth.signature);
            }

            let response = self
                .client
                .post(
                    url.clone(),
                    None,
                    Some(headers.into_iter().collect()),
                    Some(body.clone()),
                    Some(self.timeout_secs),
                    Some(rate_keys.clone()),
                )
                .await
                .map_err(DeriveHttpError::from)?;

            decode_envelope(method, id, response)
        };

        if retry {
            self.retry_manager
                .execute_with_retry(method, attempt, should_retry_http_error, |msg| {
                    DeriveHttpError::transport(msg)
                })
                .await
        } else {
            attempt().await
        }
    }

    fn build_auth_headers(&self, method: &str) -> Result<AuthHeaders> {
        let credentials =
            self.credentials
                .as_ref()
                .ok_or_else(|| DeriveHttpError::MissingCredentials {
                    method: method.to_owned(),
                })?;
        let auth = build_rest_auth_headers(&credentials.wallet_address, &credentials.signer)?;
        Ok(auth)
    }
}

#[derive(Debug, Clone, Copy)]
struct TickerRequest<'a> {
    instrument_type: DeriveInstrumentType,
    currency: &'a str,
    expiry_date: Option<&'a str>,
}

fn ticker_request(instrument_name: &str) -> Result<TickerRequest<'_>> {
    let Some((currency, suffix)) = instrument_name.split_once('-') else {
        return Err(DeriveHttpError::decode(format!(
            "invalid Derive instrument name `{instrument_name}`"
        )));
    };

    if suffix == "PERP" {
        return Ok(TickerRequest {
            instrument_type: DeriveInstrumentType::Perp,
            currency,
            expiry_date: None,
        });
    }

    let mut parts = suffix.split('-');
    let Some(expiry_date) = parts.next() else {
        return Ok(TickerRequest {
            instrument_type: DeriveInstrumentType::Erc20,
            currency,
            expiry_date: None,
        });
    };
    let has_option_tail = parts.clone().count() == 2;
    if expiry_date.len() == 8 && expiry_date.chars().all(|c| c.is_ascii_digit()) && has_option_tail
    {
        return Ok(TickerRequest {
            instrument_type: DeriveInstrumentType::Option,
            currency,
            expiry_date: Some(expiry_date),
        });
    }

    Ok(TickerRequest {
        instrument_type: DeriveInstrumentType::Erc20,
        currency,
        expiry_date: None,
    })
}

fn build_client(
    timeout_secs: u64,
    proxy_url: Option<String>,
) -> std::result::Result<HttpClient, HttpClientError> {
    // Every REST endpoint this client calls is a non-matching read, so a single
    // default quota (keyed by `DERIVE_NON_MATCHING_RATE_KEY` at the call site)
    // covers them; no per-endpoint keyed quotas are needed.
    HttpClient::new(
        HashMap::new(),
        Vec::new(),
        Vec::new(),
        Some(rate_limit::non_matching_quota()),
        Some(timeout_secs),
        proxy_url,
    )
}

fn trim_trailing_slash(url: String) -> String {
    if url.ends_with('/') {
        url.trim_end_matches('/').to_string()
    } else {
        url
    }
}

fn decode_envelope<R: DeserializeOwned>(
    method: &str,
    request_id: u64,
    response: HttpResponse,
) -> Result<R> {
    let status = response.status.as_u16();
    let is_success_status = (200..300).contains(&status);
    let body = response.body;

    let envelope: JsonRpcResponse<R> = match serde_json::from_slice(&body) {
        Ok(env) => env,
        Err(e) => {
            if !is_success_status {
                let text = String::from_utf8_lossy(&body).into_owned();
                return Err(DeriveHttpError::http(status, truncate(text, 512)));
            }
            return Err(DeriveHttpError::decode(format!(
                "failed to decode `{method}` response: {e}",
            )));
        }
    };

    if let Some(err) = envelope.error {
        return Err(DeriveHttpError::JsonRpc {
            code: err.code,
            message: err.message,
            data: err.data,
        });
    }

    // Gateways (Cloudflare, the wallet auth proxy) return non-2xx with a JSON body
    // like {"message": "Unauthorized"} that parses into an empty envelope. Surface
    // those as Http errors so retry/reconcile logic sees the real status code
    // instead of MissingResult.
    if !is_success_status {
        let text = String::from_utf8_lossy(&body).into_owned();
        return Err(DeriveHttpError::http(status, truncate(text, 512)));
    }

    if let Some(echoed) = envelope.id
        && echoed != request_id
    {
        log::debug!(
            "derive: id mismatch for `{method}` (sent={request_id}, recv={echoed}); accepting result",
        );
    }

    envelope
        .result
        .ok_or_else(|| DeriveHttpError::MissingResult {
            method: method.to_owned(),
        })
}

fn truncate(s: String, max: usize) -> String {
    if s.len() <= max {
        return s;
    }
    let mut cutoff = max;
    while cutoff > 0 && !s.is_char_boundary(cutoff) {
        cutoff -= 1;
    }
    let mut out = String::with_capacity(cutoff + 3);
    out.push_str(&s[..cutoff]);
    out.push_str("...");
    out
}

#[cfg(test)]
mod tests {
    use nautilus_network::http::{HttpStatus, StatusCode};
    use rstest::rstest;

    use super::*;

    const SESSION_KEY_HEX: &str =
        "0x2ae8be44db8a590d20bffbe3b6872df9b569147d3bf6801a35a28281a4816bbd";
    const TEST_WALLET: &str = "0x000000000000000000000000000000000000aaaa";

    fn test_client() -> DeriveHttpClient {
        DeriveHttpClient::new("https://api.example/", None, None, None).expect("client builds")
    }

    fn test_response(status: u16, body: &serde_json::Value) -> HttpResponse {
        let status_code = StatusCode::from_u16(status).unwrap();
        HttpResponse {
            status: HttpStatus::new(status_code),
            headers: HashMap::new(),
            body: serde_json::to_vec(body).unwrap().into(),
        }
    }

    #[rstest]
    fn test_credentials_debug_redacts_signer() {
        let creds = DeriveCredentials::new(TEST_WALLET, SESSION_KEY_HEX).unwrap();
        let dbg = format!("{creds:?}");
        assert!(dbg.contains("***redacted***"));
        assert!(dbg.contains(TEST_WALLET));
        assert!(!dbg.contains(SESSION_KEY_HEX));
    }

    #[rstest]
    fn test_credentials_rejects_invalid_session_key() {
        let err = DeriveCredentials::new(TEST_WALLET, "not-hex").expect_err("must reject");
        match err {
            DeriveHttpError::Decode(msg) => assert!(msg.contains("invalid session key")),
            other => panic!("expected Decode, was {other:?}"),
        }
    }

    #[rstest]
    fn test_base_url_trims_trailing_slash() {
        let client = test_client();
        assert_eq!(client.base_url(), "https://api.example");
    }

    #[rstest]
    fn test_new_has_no_credentials() {
        assert!(!test_client().has_credentials());
    }

    #[rstest]
    fn test_with_credentials_sets_creds() {
        let creds = DeriveCredentials::new(TEST_WALLET, SESSION_KEY_HEX).unwrap();
        let client =
            DeriveHttpClient::with_credentials("https://api.example", creds, None, None, None)
                .unwrap();
        assert!(client.has_credentials());
    }

    #[rstest]
    fn test_next_id_increments_monotonically() {
        let client = test_client();
        let a = client.next_id();
        let b = client.next_id();
        let c = client.next_id();
        assert_eq!(b, a + 1);
        assert_eq!(c, b + 1);
    }

    #[rstest]
    fn test_decode_envelope_returns_result() {
        let resp = test_response(200, &serde_json::json!({"id": 1, "result": {"ok": true}}));
        let value: Value = decode_envelope("public/get_instruments", 1, resp).unwrap();
        assert_eq!(value["ok"], true);
    }

    #[rstest]
    fn test_decode_envelope_propagates_jsonrpc_error() {
        let resp = test_response(
            200,
            &serde_json::json!({
                "id": 1,
                "error": {"code": -32601, "message": "Method not found"}
            }),
        );
        let err: DeriveHttpError = decode_envelope::<Value>("public/missing", 1, resp).unwrap_err();
        match err {
            DeriveHttpError::JsonRpc { code, message, .. } => {
                assert_eq!(code, -32601);
                assert_eq!(message, "Method not found");
            }
            other => panic!("expected JsonRpc, was {other:?}"),
        }
    }

    #[rstest]
    fn test_decode_envelope_flags_missing_result() {
        let resp = test_response(200, &serde_json::json!({"id": 1}));
        let err = decode_envelope::<Value>("public/get_instruments", 1, resp).unwrap_err();
        assert!(matches!(err, DeriveHttpError::MissingResult { .. }));
    }

    #[rstest]
    fn test_decode_envelope_flags_non_2xx_with_unparsable_body() {
        let status_code = StatusCode::from_u16(503).unwrap();
        let response = HttpResponse {
            status: HttpStatus::new(status_code),
            headers: HashMap::new(),
            body: bytes::Bytes::from_static(b"<html>upstream down</html>"),
        };
        let err = decode_envelope::<Value>("public/get_instruments", 1, response).unwrap_err();
        match err {
            DeriveHttpError::Http { status, message } => {
                assert_eq!(status, 503);
                assert!(message.contains("upstream down"));
            }
            other => panic!("expected Http, was {other:?}"),
        }
    }

    #[rstest]
    fn test_decode_envelope_flags_non_2xx_with_non_envelope_json() {
        // Gateways return non-2xx with JSON bodies like {"message": "Unauthorized"}.
        // These parse as an empty JsonRpcResponse; the status must still surface.
        let resp = test_response(401, &serde_json::json!({"message": "Unauthorized"}));
        let err = decode_envelope::<Value>("private/order", 1, resp).unwrap_err();
        match err {
            DeriveHttpError::Http { status, message } => {
                assert_eq!(status, 401);
                assert!(message.contains("Unauthorized"));
            }
            other => panic!("expected Http, was {other:?}"),
        }
    }

    #[rstest]
    fn test_decode_envelope_prefers_jsonrpc_error_over_http_status() {
        // When the venue returns a proper JSON-RPC error envelope with a non-2xx
        // status, the envelope wins because it carries richer venue context.
        let status_code = StatusCode::from_u16(400).unwrap();
        let body = serde_json::json!({
            "id": 1,
            "error": {"code": -32602, "message": "Invalid params"},
        });
        let response = HttpResponse {
            status: HttpStatus::new(status_code),
            headers: HashMap::new(),
            body: serde_json::to_vec(&body).unwrap().into(),
        };
        let err = decode_envelope::<Value>("private/order", 1, response).unwrap_err();
        assert!(matches!(err, DeriveHttpError::JsonRpc { code: -32602, .. }));
    }

    #[rstest]
    fn test_truncate_handles_multi_byte_char_at_boundary() {
        // "Ω" is two bytes (0xCE 0xA9). Truncating to a length that lands mid-glyph
        // must not panic; we step back to the prior char boundary.
        let s = "ΩΩΩΩΩΩΩΩΩΩ".to_string();
        assert_eq!(s.len(), 20);
        let out = truncate(s, 5);
        assert!(out.ends_with("..."));
        let prefix = out.trim_end_matches("...");
        assert!(prefix.is_char_boundary(prefix.len()));
        assert!(prefix.chars().all(|c| c == 'Ω'));
    }

    #[rstest]
    fn test_truncate_returns_input_when_under_limit() {
        let s = "short".to_string();
        assert_eq!(truncate(s, 16), "short");
    }

    #[rstest]
    fn test_decode_envelope_non_2xx_body_with_non_ascii_does_not_panic() {
        // Regression: a Cloudflare-style 503 page containing non-ASCII bytes near
        // the truncation cutoff must not panic.
        let glyph = "Ω";
        let body = glyph.repeat(600);
        let status_code = StatusCode::from_u16(503).unwrap();
        let response = HttpResponse {
            status: HttpStatus::new(status_code),
            headers: HashMap::new(),
            body: body.into_bytes().into(),
        };
        let err = decode_envelope::<Value>("public/get_instruments", 1, response).unwrap_err();
        assert!(matches!(err, DeriveHttpError::Http { status: 503, .. }));
    }

    #[rstest]
    fn test_decode_envelope_accepts_id_mismatch() {
        let resp = test_response(200, &serde_json::json!({"id": 99, "result": "ok"}));
        let value: Value = decode_envelope("public/get_instruments", 1, resp).unwrap();
        assert_eq!(value, serde_json::json!("ok"));
    }

    #[tokio::test]
    async fn test_send_private_without_credentials_errors() {
        let client = test_client();
        let err = client
            .send_private::<_, Value>("private/order", &serde_json::json!({}))
            .await
            .expect_err("must require credentials");

        match err {
            DeriveHttpError::MissingCredentials { method } => {
                assert_eq!(method, "private/order");
            }
            other => panic!("expected MissingCredentials, was {other:?}"),
        }
    }
}