longtrader-worker 0.2.1

Local strategy host running portable strategies against pluggable backends (exchange daemon / terminal API / mock)
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
//! Strategy-facing ports whose DTOs are generated protobuf types.
//!
//! Strategies depend on these traits only; transport and backend selection
//! live in [`crate::adapters`]. This is the hexagonal boundary of the worker.

use async_trait::async_trait;
use longtrader_contract::ext::DecimalConvertError;
use rust_decimal::Decimal;
use tokio::sync::mpsc;

use crate::proto::{common, market, trading, worker};

// ---------------------------------------------------------------------------
// Decimal encode/decode
//
// The wire representation of a decimal is a single base-10 string, and
// `longtrader_contract::ext` owns the only encode/decode pair for it
// (`decimal_to_common` / `common_to_decimal`). The duplicate helpers that used
// to live here were removed so that pair has one owner. Strategy code should
// call `longtrader_contract::ext::decimal_to_common` directly.
// ---------------------------------------------------------------------------

// ---------------------------------------------------------------------------
// Sequence gap detection (design doc §6.1: per-stream gap-free `EventHeader.sequence`)
// ---------------------------------------------------------------------------

/// Build a [`common::ExchangeId`] from config strings.
///
/// Lives on the ports side, not in `adapters`: constructing the venue identity
/// is port-level vocabulary that strategies, the session control plane and the
/// adapters all need, and an adapter-owned constructor forced every one of
/// them to depend on the transport layer to name a venue.
#[inline]
#[must_use]
pub fn exchange_id(id: &str, label: &str) -> common::ExchangeId {
    common::ExchangeId { id: id.to_string(), label: label.to_string(), ..Default::default() }
}

/// Returns `true` when `next` does NOT follow `prev` by exactly one.
/// Consumers MUST treat a gap as a trigger for resync: orderbook streams
/// re-fetch a fresh snapshot, private streams re-run `ReconcileState`.
///
/// `prev` is a watermark: `0` means "before the first sequence" (e.g. a client
/// resuming from scratch against [`SessionHandle::replay_after`]), so it is a
/// *valid* predecessor — `0 -> 1` is contiguous, `0 -> 2` missed event `1`.
/// `u64::MAX -> 0` is contiguous via `wrapping_add`.
#[inline]
pub fn is_sequence_gap(prev: u64, next: u64) -> bool {
    next != prev.wrapping_add(1)
}

/// Sequence gap helper on top of raw headers; `None` means no previous
/// watermark (e.g. first event), so no gap. A header carrying sequence `0`
/// is an unset proto default (never emitted by `publish` / `poll_snapshot`,
/// which start at `1`), so it is ignored rather than reported as a gap —
/// use this helper (not the raw [`is_sequence_gap`]) for stream headers.
#[inline]
pub fn has_sequence_gap(prev: Option<u64>, header: &common::EventHeader) -> bool {
    if header.sequence == 0 {
        return false;
    }
    prev.is_some_and(|p| is_sequence_gap(p, header.sequence))
}

/// Backpressure policy applied to streamed events (see design doc §6.5).
///
/// Each session owns an independent `mpsc` channel per logical stream;
/// overflowing one session never blocks another. The policy is chosen per
/// stream kind (doc constants below).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OverflowPolicy {
    /// Drop oldest, keep newest — tickers, L1 quotes, OHLCV.
    /// Use for idempotent, high-frequency market data where newest value
    /// subsumes older ticks. Maps to `ticker` channels.
    DropOldest,
    /// Fold updates into latest state per key — book deltas collapse to the
    /// newest view per symbol/channel. Maps to `book` (orderbook) channels;
    /// stale intermediates are merged, unrecoverable lag surfaces as a
    /// sequence gap → automatic resync (§6.1).
    Coalesce,
    /// Bounded block — order/balance/position events are NEVER dropped;
    /// sustained blockage backpressures the upstream pump and ultimately
    /// trips the lease → kill-switch (final backstop). Maps to `orders`
    /// channels.
    Block,
}

/// Per-channel default overflow policies (design doc §6.5 table).
/// Ticker/trades/ohlcv → `DropOldest`; orderbook → `Coalesce`;
/// orders/balances/positions/my-trades → `Block`.
pub const OVERFLOW_TICKER: OverflowPolicy = OverflowPolicy::DropOldest;
pub const OVERFLOW_ORDERBOOK: OverflowPolicy = OverflowPolicy::Coalesce;
pub const OVERFLOW_ORDERS: OverflowPolicy = OverflowPolicy::Block;
/// Generic alias kept for callers that already know the mapping.
pub const OVERFLOW_TRADES: OverflowPolicy = OverflowPolicy::DropOldest;
pub const OVERFLOW_OHLCV: OverflowPolicy = OverflowPolicy::DropOldest;
pub const OVERFLOW_BALANCES: OverflowPolicy = OverflowPolicy::Block;
pub const OVERFLOW_POSITIONS: OverflowPolicy = OverflowPolicy::Block;

/// Default page size when a request leaves `Pagination.limit` unset (0).
pub const DEFAULT_PAGE_LIMIT: u32 = 100;
/// Hard cap for any paged list; mirrors `session::proxy::pagination_limit`.
pub const MAX_PAGE_LIMIT: u32 = 1000;

/// Page size from a bare `limit`: `0` means "unset" and resolves to
/// [`DEFAULT_PAGE_LIMIT`]; larger values are capped at [`MAX_PAGE_LIMIT`].
/// Single owner so `RemoteAdapter`, the proxy, and the CLI cannot drift.
#[inline]
#[must_use]
pub fn page_limit_or_default(limit: u32) -> u32 {
    if limit == 0 { DEFAULT_PAGE_LIMIT } else { limit.min(MAX_PAGE_LIMIT) }
}

/// Resolve the OverflowPolicy for a market `StreamChannel`.
///
/// Fail-closed: unknown / future channels (including private streams such as
/// orders/balances/positions/my-trades) resolve to `Block`, never to a
/// dropping policy. A new channel must be mapped explicitly if dropping is
/// ever intended.
#[inline]
pub fn overflow_policy_for_channel(channel: market::StreamChannel) -> OverflowPolicy {
    match channel {
        market::StreamChannel::Orderbook => OVERFLOW_ORDERBOOK,
        market::StreamChannel::Ticker |
        market::StreamChannel::Trades |
        market::StreamChannel::Ohlcv => OVERFLOW_TICKER,
        _ => OVERFLOW_ORDERS,
    }
}

/// Errors surfaced by port implementations.
#[derive(Debug, thiserror::Error)]
pub enum PortError {
    #[error("transport error: {0}")]
    Transport(String),
    #[error("rpc error ({code}): {message}")]
    Rpc { code: u32, message: String },
    #[error("operation unsupported by this backend: {0}")]
    Unsupported(String),
    #[error("missing required field: {0}")]
    MissingField(String),
    #[error("invalid argument: {0}")]
    InvalidArgument(String),
    /// The backend answered, but has no such object. Distinct from
    /// `Unsupported` (the backend can never have it) and from an empty
    /// successful result (which a caller would read as "none exist").
    #[error("not found: {0}")]
    NotFound(String),
    #[error(transparent)]
    Decimal(#[from] DecimalConvertError),
}

// ponytail: tradingcharts_proto removed for open source; former From impl deleted.
// RemoteAdapter now uses hpx+contract directly, so no TerminalClientError mapping needed.

/// Streamed market data events delivered under an [`OverflowPolicy`].
pub type MarketEventStream = mpsc::Receiver<market::MarketDataEvent>;

/// Order execution port.
#[async_trait]
pub trait TradingGateway: Send + Sync {
    async fn create_order(
        &self,
        req: trading::CreateOrderRequest,
    ) -> Result<trading::Order, PortError>;

    /// Batch placement (grids / market making); contract-level batch RPC.
    async fn batch_create_orders(
        &self,
        req: trading::CreateOrdersRequest,
    ) -> Result<Vec<trading::Order>, PortError>;

    async fn cancel_order(
        &self,
        req: trading::CancelOrderRequest,
    ) -> Result<trading::Order, PortError>;

    /// Cancel every open order for the exchange (optionally one symbol);
    /// kill-switch execution path.
    async fn cancel_all_orders(
        &self,
        req: trading::CancelAllOrdersRequest,
    ) -> Result<Vec<trading::Order>, PortError>;

    async fn fetch_open_orders(
        &self,
        req: trading::FetchOpenOrdersRequest,
    ) -> Result<Vec<trading::Order>, PortError>;

    async fn get_account(
        &self,
        req: trading::GetAccountRequest,
    ) -> Result<trading::GetAccountResponse, PortError>;

    async fn get_positions(
        &self,
        req: trading::GetPositionsRequest,
    ) -> Result<trading::GetPositionsResponse, PortError>;

    async fn get_order_history(
        &self,
        req: trading::GetOrderHistoryRequest,
    ) -> Result<trading::GetOrderHistoryResponse, PortError>;

    async fn get_closed_positions(
        &self,
        req: trading::GetClosedPositionsRequest,
    ) -> Result<trading::GetClosedPositionsResponse, PortError>;

    async fn close_position(
        &self,
        req: trading::ClosePositionRequest,
    ) -> Result<trading::ClosePositionResponse, PortError>;

    /// Close every open position; kill-switch execution path.
    async fn close_all_positions(
        &self,
        req: trading::CloseAllPositionsRequest,
    ) -> Result<trading::CloseAllPositionsResponse, PortError>;

    /// Modify the take-profit / stop-loss of an open position.
    async fn modify_position(
        &self,
        req: trading::ModifyPositionRequest,
    ) -> Result<trading::ModifyPositionResponse, PortError>;

    /// Best-effort snapshot for deterministic recovery. Adapters aggregate
    /// their backend's balance/position/open-order reads; the three reads are
    /// NOT atomic on the open backend (see `RemoteAdapter::sync_state`).
    /// Callers must replay deltas after `snapshot_sequence` to converge.
    async fn sync_state(
        &self,
        exchange_id: &common::ExchangeId,
    ) -> Result<worker::ReconcileStateResponse, PortError>;
}

/// Market data port.
#[async_trait]
pub trait MarketDataSource: Send + Sync {
    async fn fetch_ticker(
        &self,
        req: market::FetchTickerRequest,
    ) -> Result<market::Ticker, PortError>;

    async fn fetch_order_book(
        &self,
        req: market::FetchOrderBookRequest,
    ) -> Result<market::OrderBook, PortError>;

    async fn get_candles(
        &self,
        req: market::GetCandlesRequest,
    ) -> Result<market::GetCandlesResponse, PortError>;

    async fn list_symbols(
        &self,
        req: market::ListSymbolsRequest,
    ) -> Result<market::ListSymbolsResponse, PortError>;

    /// Case-insensitive substring search over symbol names.
    async fn search_symbols(
        &self,
        req: market::SearchSymbolsRequest,
    ) -> Result<market::SearchSymbolsResponse, PortError>;

    /// Batch ticker snapshots (empty `symbols` = every symbol).
    async fn list_tickers(
        &self,
        req: market::ListTickersRequest,
    ) -> Result<market::ListTickersResponse, PortError>;

    /// Subscribe with backpressure protection; the returned receiver yields
    /// events already filtered through `policy`.
    async fn subscribe_market_data(
        &self,
        req: market::StreamMarketDataRequest,
        policy: OverflowPolicy,
    ) -> Result<MarketEventStream, PortError>;
}

// ---------------------------------------------------------------------------
// Extended capability ports (P2): funding rates, trigger orders, venue ops,
// wallet operations. Backends that do not implement a capability return
// [`PortError::Unsupported`].
// ---------------------------------------------------------------------------

/// One funding-rate snapshot.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FundingRateSnapshot {
    pub symbol: String,
    pub rate: Decimal,
    pub next_funding_time_ms: i64,
    pub mark_price: Option<Decimal>,
}

/// One historical funding-rate point.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FundingRatePoint {
    pub rate: Decimal,
    pub time_ms: i64,
}

/// Perpetual funding-rate data port.
#[async_trait]
pub trait FundingRateSource: Send + Sync {
    async fn fetch_funding_rate(
        &self,
        exchange_id: &common::ExchangeId,
        symbol: &str,
    ) -> Result<FundingRateSnapshot, PortError>;

    async fn fetch_funding_rate_history(
        &self,
        exchange_id: &common::ExchangeId,
        symbol: &str,
        limit: u32,
    ) -> Result<Vec<FundingRatePoint>, PortError>;

    /// Batch funding snapshots (empty `symbols` = every symbol the venue
    /// reports funding for). One call, one venue: a batch spanning venues would
    /// make a cross-venue basis comparison unattributable.
    async fn list_funding_rates(
        &self,
        exchange_id: &common::ExchangeId,
        symbols: &[String],
    ) -> Result<Vec<FundingRateSnapshot>, PortError>;
}

/// A venue-side conditional (trigger) order request.
#[derive(Debug, Clone)]
pub struct TriggerOrderRequest {
    pub client_order_id: String,
    pub symbol: String,
    pub is_buy: bool,
    pub trigger_price: Decimal,
    pub qty: Decimal,
    pub reduce_only: bool,
}

/// Venue-side protective order port (crash-safe stop-loss backstop).
#[async_trait]
pub trait TriggerOrderGateway: Send + Sync {
    /// Places the order and returns it as the venue now sees it.
    ///
    /// Returning the record (not just an id) means the caller can confirm the
    /// venue accepted the intended trigger price and status, instead of
    /// discovering a mismatch when it later tries to cancel.
    async fn create_trigger_order(
        &self,
        exchange_id: &common::ExchangeId,
        req: TriggerOrderRequest,
    ) -> Result<TriggerOrder, PortError>;

    /// Cancels a resting conditional order, returning it as the venue now
    /// sees it.
    ///
    /// The record matters: a caller confirming the cancellation must be able to
    /// see which order was cancelled (price, quantity, side), because a
    /// response synthesised from the request cannot distinguish "I cancelled
    /// the stop I meant to" from "I cancelled something else with this id".
    async fn cancel_trigger_order(
        &self,
        exchange_id: &common::ExchangeId,
        order_id: &str,
        symbol: &str,
    ) -> Result<TriggerOrder, PortError>;

    /// Resting conditional orders, filtered by `symbols` (empty = every
    /// symbol).
    async fn list_trigger_orders(
        &self,
        exchange_id: &common::ExchangeId,
        symbols: &[String],
    ) -> Result<Vec<TriggerOrder>, PortError>;
}

/// Lifecycle of a venue-side conditional order.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum TriggerOrderStatus {
    /// Resting at the venue, waiting for the trigger price.
    #[default]
    Open,
    /// Trigger fired; `order_id` names the resulting live order.
    Triggered,
    Canceled,
    Rejected,
}

impl std::fmt::Display for TriggerOrderStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

impl TriggerOrderStatus {
    /// The wire representation.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Open => "open",
            Self::Triggered => "triggered",
            Self::Canceled => "canceled",
            Self::Rejected => "rejected",
        }
    }
}

/// A resting or fired venue-side conditional order.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TriggerOrder {
    pub id: String,
    pub client_order_id: String,
    pub symbol: String,
    pub is_buy: bool,
    pub trigger_price: Decimal,
    pub qty: Decimal,
    pub reduce_only: bool,
    pub status: TriggerOrderStatus,
    pub created_at_ms: i64,
    /// Set once the trigger fired; names the resulting live order.
    pub order_id: Option<String>,
    pub triggered_at_ms: Option<i64>,
}

/// Type of a venue-op parameter, mirroring `ops.v1.ParamType`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum VenueOpParamType {
    #[default]
    Unspecified,
    String,
    Int64,
    Decimal,
    Bool,
    Enum,
    List,
    Map,
}

impl VenueOpParamType {
    /// The `ops.v1.ParamType` enum name.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Unspecified => "PARAM_TYPE_UNSPECIFIED",
            Self::String => "PARAM_TYPE_STRING",
            Self::Int64 => "PARAM_TYPE_INT64",
            Self::Decimal => "PARAM_TYPE_DECIMAL",
            Self::Bool => "PARAM_TYPE_BOOL",
            Self::Enum => "PARAM_TYPE_ENUM",
            Self::List => "PARAM_TYPE_LIST",
            Self::Map => "PARAM_TYPE_MAP",
        }
    }
}

/// One parameter of a venue operation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VenueOpParam {
    /// Parameter name exactly as the venue API expects it.
    pub name: String,
    pub r#type: VenueOpParamType,
    pub required: bool,
    /// Human-readable documentation, empty when the venue supplies none.
    pub doc: String,
    /// Populated when `type` is `Enum`.
    pub enum_values: Vec<String>,
}

/// Self-describing metadata for one venue operation.
///
/// Carrying the full descriptor rather than just a name matters: `mutating` is
/// what a client uses to decide whether a call needs a confirmation prompt, and
/// reporting `false` for `account.transfer` would tell it a fund-moving
/// operation is read-only.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct VenueOpDescriptor {
    pub name: String,
    pub category: String,
    pub summary: String,
    /// True when the operation moves funds or changes account state.
    pub mutating: bool,
    pub params: Vec<VenueOpParam>,
}

/// Generic venue-operation invocation port backed by the self-describing
/// venue-ops registry (`account.transfer`, `margin.borrow`, ...).
#[async_trait]
pub trait VenueOpInvoker: Send + Sync {
    /// Invokes `op` with JSON params; returns the venue's JSON response.
    async fn invoke_venue_op(
        &self,
        exchange_id: &common::ExchangeId,
        op: &str,
        params: serde_json::Map<String, serde_json::Value>,
    ) -> Result<serde_json::Value, PortError>;

    /// Lists the operations this backend exposes, with their full descriptors.
    async fn list_venue_ops(
        &self,
        exchange_id: &common::ExchangeId,
    ) -> Result<Vec<VenueOpDescriptor>, PortError>;

    /// Full schema of one operation.
    ///
    /// # Errors
    ///
    /// [`PortError::NotFound`] when the venue does not expose `op`. A caller
    /// must be able to tell "no such operation" from "exists but undocumented".
    async fn describe_venue_op(
        &self,
        exchange_id: &common::ExchangeId,
        op: &str,
    ) -> Result<VenueOpDescriptor, PortError> {
        let ops = self.list_venue_ops(exchange_id).await?;
        ops.into_iter()
            .find(|o| o.name == op)
            .ok_or_else(|| PortError::NotFound(format!("venue operation {op}")))
    }
}

/// One wallet ledger entry (deposit / withdrawal / transfer).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LedgerEntry {
    pub id: String,
    pub currency: String,
    /// **Signed**: a withdrawal is negative, a deposit positive. Keeping the
    /// sign here (rather than trusting a venue's unsigned `direction` field)
    /// means a caller cannot read a withdrawal as a deposit.
    pub amount: Decimal,
    pub entry_type: String,
    pub completed: bool,
    pub time_ms: i64,
}

/// A venue's answer to a [`WalletGateway::transfer`] call.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct TransferReceipt {
    /// Venue-assigned transfer id; empty when the venue reports none.
    pub transfer_id: String,
    /// The resulting ledger row, when the venue reports one.
    pub entry: Option<LedgerEntry>,
}

/// Wallet operations port: ledger scanning and internal transfers.
#[async_trait]
pub trait WalletGateway: Send + Sync {
    /// Ledger rows newest first, filtered by `currency` and `entry_type`
    /// (either empty meaning "no filter").
    async fn list_ledger_entries(
        &self,
        exchange_id: &common::ExchangeId,
        currency: &str,
        entry_type: &str,
        limit: u32,
    ) -> Result<Vec<LedgerEntry>, PortError>;

    /// Completed deposits, newest first.
    ///
    /// A default over [`Self::list_ledger_entries`] so a backend that can
    /// serve a filtered ledger does not have to implement this separately.
    async fn fetch_deposits(
        &self,
        exchange_id: &common::ExchangeId,
        limit: u32,
    ) -> Result<Vec<LedgerEntry>, PortError> {
        self.list_ledger_entries(exchange_id, "", "deposit", limit).await
    }

    /// Transfers `amount` of `asset` to the destination account label.
    ///
    /// `client_transfer_id` is an optional idempotency key. Transfers move
    /// funds, so a retry without one risks a double spend; supply it whenever
    /// the caller can retry.
    #[allow(clippy::too_many_arguments)]
    async fn transfer(
        &self,
        exchange_id: &common::ExchangeId,
        asset: &str,
        amount: Decimal,
        dest_label: &str,
        client_transfer_id: &str,
    ) -> Result<TransferReceipt, PortError>;
}

#[cfg(test)]
mod tests {
    use async_trait::async_trait;
    use longtrader_contract::ext::common_to_decimal;
    use proptest::prelude::*;
    use rust_decimal_macros::dec;

    use super::*;

    /// The decode failure for a payload no `Decimal` can hold.
    ///
    /// `Decimal` carries one base-10 string, so "cannot represent" is a property
    /// of that payload rather than of a numeric pair. Built by running the real
    /// decoder so the fixture cannot drift from what the parser does.
    fn out_of_range(payload: &str) -> DecimalConvertError {
        let wire = common::Decimal { value: payload.to_string(), ..Default::default() };
        match common_to_decimal(&wire) {
            Err(err) => err,
            Ok(_) => unreachable!("{payload} must not decode"),
        }
    }

    // -----------------------------------------------------------------------
    // Sequence-gap detection
    // -----------------------------------------------------------------------

    #[test]
    fn consecutive_sequences_are_not_a_gap() {
        assert!(!is_sequence_gap(0, 1));
        assert!(!is_sequence_gap(1, 2));
        assert!(!is_sequence_gap(u64::MAX - 1, u64::MAX));
    }

    /// `wrapping_add` is what makes `u64::MAX -> 0` contiguous rather than a
    /// gap; a saturating add would have reported a false resync here.
    #[test]
    fn the_sequence_wraps_through_zero_without_a_gap() {
        assert!(!is_sequence_gap(u64::MAX, 0), "u64::MAX -> 0 is contiguous");
    }

    #[test]
    fn skipped_duplicate_and_regressed_sequences_are_gaps() {
        // `0` is the "before the first sequence" watermark: `0 -> 0` is a
        // duplicate, `0 -> 2` skipped event `1`, and `1 -> 0` regressed.
        assert!(is_sequence_gap(0, 0), "a duplicate is not an increment");
        assert!(is_sequence_gap(0, 2), "a skip is a gap");
        assert!(is_sequence_gap(5, 1), "a regression is a gap");
        assert!(is_sequence_gap(1, 0), "one backwards is a gap");
    }

    #[test]
    fn a_missing_watermark_is_never_a_gap() {
        let header = common::EventHeader { sequence: 4_242, ..Default::default() };
        assert!(!has_sequence_gap(None, &header), "the first event cannot gap");
    }

    #[test]
    fn an_unset_header_sequence_is_never_a_gap() {
        // Sequence `0` is the proto default, never emitted by `publish` /
        // `poll_snapshot` (which start at `1`): it means "no sequence", not
        // "the zeroth event", so even a stale watermark must not resync on it.
        let header = common::EventHeader::default();
        assert!(!has_sequence_gap(None, &header));
        assert!(!has_sequence_gap(Some(0), &header));
        assert!(!has_sequence_gap(Some(5), &header));
        assert!(!has_sequence_gap(Some(u64::MAX), &header));
    }

    #[test]
    fn has_sequence_gap_forwards_the_watermark() {
        let header = common::EventHeader { sequence: 10, ..Default::default() };
        assert!(!has_sequence_gap(Some(9), &header));
        assert!(has_sequence_gap(Some(10), &header));
        assert!(has_sequence_gap(Some(3), &header));
    }

    // -----------------------------------------------------------------------
    // Overflow policy mapping
    // -----------------------------------------------------------------------

    #[test]
    fn orderbook_coalesces_ticker_drops_and_unknown_blocks() {
        assert_eq!(
            overflow_policy_for_channel(market::StreamChannel::Orderbook),
            OverflowPolicy::Coalesce
        );
        for channel in [
            market::StreamChannel::Ticker,
            market::StreamChannel::Trades,
            market::StreamChannel::Ohlcv,
        ] {
            assert_eq!(
                overflow_policy_for_channel(channel),
                OverflowPolicy::DropOldest,
                "{channel:?} must drop the oldest"
            );
        }
        // Fail-closed: unknown / future channels (including private streams)
        // never resolve to a dropping policy.
        assert_eq!(
            overflow_policy_for_channel(market::StreamChannel::Unspecified),
            OverflowPolicy::Block,
            "Unspecified must fail closed to Block"
        );
    }

    /// The policy constants are the documented §6.5 table; pin every one so a
    /// refactor cannot quietly repoint a channel at a lossy policy.
    #[test]
    fn the_documented_policy_constants_are_stable() {
        assert_eq!(OVERFLOW_TICKER, OverflowPolicy::DropOldest);
        assert_eq!(OVERFLOW_ORDERBOOK, OverflowPolicy::Coalesce);
        assert_eq!(OVERFLOW_ORDERS, OverflowPolicy::Block);
        assert_eq!(OVERFLOW_TRADES, OverflowPolicy::DropOldest);
        assert_eq!(OVERFLOW_OHLCV, OverflowPolicy::DropOldest);
        assert_eq!(OVERFLOW_BALANCES, OverflowPolicy::Block);
        assert_eq!(OVERFLOW_POSITIONS, OverflowPolicy::Block);
    }

    // -----------------------------------------------------------------------
    // PortError
    // -----------------------------------------------------------------------

    #[test]
    fn every_port_error_renders_a_distinct_message() {
        let errors = [
            PortError::Transport("socket closed".into()),
            PortError::Rpc { code: 5, message: "not found".into() },
            PortError::Unsupported("trigger orders".into()),
            PortError::MissingField("last".into()),
            PortError::InvalidArgument("qty must be positive".into()),
            PortError::NotFound("venue operation nope".into()),
        ];
        let rendered: Vec<String> = errors.iter().map(ToString::to_string).collect();
        for pair in rendered.iter().zip(rendered.iter().skip(1)) {
            assert_ne!(pair.0, pair.1, "two errors share the message {}", pair.0);
        }
        for message in &rendered {
            assert!(!message.is_empty(), "an error rendered as an empty string");
        }
        assert!(rendered.iter().any(|m| m.contains('5') && m.contains("not found")));
    }

    /// `NotFound` must read as "the backend answered, no such object" rather
    /// than as the "this backend can never have it" `Unsupported`.
    #[test]
    fn not_found_and_unsupported_are_distinguishable_in_the_message() {
        let not_found = PortError::NotFound("thing".into()).to_string();
        let unsupported = PortError::Unsupported("thing".into()).to_string();
        assert_ne!(not_found, unsupported);
        assert!(not_found.contains("not found"));
        assert!(unsupported.contains("unsupported"));
    }

    #[test]
    fn a_decimal_convert_error_converts_into_a_port_error() {
        // 29 nines is inside the contract grammar but wider than a `Decimal`.
        let payload = "9".repeat(29);
        let err: PortError = out_of_range(&payload).into();
        assert!(matches!(err, PortError::Decimal(_)));
        assert!(err.to_string().contains(&payload), "the payload must be echoed: {err}");
    }

    // -----------------------------------------------------------------------
    // Wire-name mappings
    // -----------------------------------------------------------------------

    #[test]
    fn trigger_order_status_wire_names() {
        assert_eq!(TriggerOrderStatus::Open.as_str(), "open");
        assert_eq!(TriggerOrderStatus::Triggered.as_str(), "triggered");
        assert_eq!(TriggerOrderStatus::Canceled.as_str(), "canceled");
        assert_eq!(TriggerOrderStatus::Rejected.as_str(), "rejected");
    }

    #[test]
    fn trigger_order_status_display_matches_as_str() {
        for status in [
            TriggerOrderStatus::Open,
            TriggerOrderStatus::Triggered,
            TriggerOrderStatus::Canceled,
            TriggerOrderStatus::Rejected,
        ] {
            assert_eq!(status.to_string(), status.as_str(), "{status:?}");
        }
    }

    #[test]
    fn trigger_order_status_defaults_to_open() {
        assert_eq!(TriggerOrderStatus::default(), TriggerOrderStatus::Open);
    }

    /// `VenueOpParamType::as_str` mirrors `ops.v1.ParamType`; the proxy maps
    /// these straight onto the wire, so a drift would publish an unknown enum
    /// name to clients.
    #[test]
    fn venue_op_param_type_wire_names() {
        assert_eq!(VenueOpParamType::Unspecified.as_str(), "PARAM_TYPE_UNSPECIFIED");
        assert_eq!(VenueOpParamType::String.as_str(), "PARAM_TYPE_STRING");
        assert_eq!(VenueOpParamType::Int64.as_str(), "PARAM_TYPE_INT64");
        assert_eq!(VenueOpParamType::Decimal.as_str(), "PARAM_TYPE_DECIMAL");
        assert_eq!(VenueOpParamType::Bool.as_str(), "PARAM_TYPE_BOOL");
        assert_eq!(VenueOpParamType::Enum.as_str(), "PARAM_TYPE_ENUM");
        assert_eq!(VenueOpParamType::List.as_str(), "PARAM_TYPE_LIST");
        assert_eq!(VenueOpParamType::Map.as_str(), "PARAM_TYPE_MAP");
    }

    #[test]
    fn venue_op_param_type_defaults_to_unspecified() {
        assert_eq!(VenueOpParamType::default(), VenueOpParamType::Unspecified);
    }

    /// The wire names are part of the published contract, so they must be
    /// unique across every variant.
    #[test]
    fn every_param_type_name_is_unique() {
        let all = [
            VenueOpParamType::Unspecified,
            VenueOpParamType::String,
            VenueOpParamType::Int64,
            VenueOpParamType::Decimal,
            VenueOpParamType::Bool,
            VenueOpParamType::Enum,
            VenueOpParamType::List,
            VenueOpParamType::Map,
        ];
        let mut names: Vec<&str> = all.iter().map(|t| t.as_str()).collect();
        names.sort_unstable();
        let count = names.len();
        names.dedup();
        assert_eq!(names.len(), count, "two param types share a wire name");
    }

    // -----------------------------------------------------------------------
    // Defaulted trait methods
    // -----------------------------------------------------------------------

    /// A backend that only implements `list_venue_ops` must still get the
    /// documented `describe_venue_op` behaviour for free.
    struct StubOps {
        ops: Vec<VenueOpDescriptor>,
    }

    #[async_trait]
    impl VenueOpInvoker for StubOps {
        async fn invoke_venue_op(
            &self,
            _exchange_id: &common::ExchangeId,
            _op: &str,
            _params: serde_json::Map<String, serde_json::Value>,
        ) -> Result<serde_json::Value, PortError> {
            Err(PortError::Unsupported("invoke".into()))
        }

        async fn list_venue_ops(
            &self,
            _exchange_id: &common::ExchangeId,
        ) -> Result<Vec<VenueOpDescriptor>, PortError> {
            Ok(self.ops.clone())
        }
    }

    fn op(name: &str) -> VenueOpDescriptor {
        VenueOpDescriptor {
            name: name.into(),
            category: "account".into(),
            summary: String::new(),
            mutating: false,
            params: Vec::new(),
        }
    }

    #[tokio::test]
    async fn the_default_describe_venue_op_finds_a_known_operation() {
        let stub = StubOps { ops: vec![op("account.balance"), op("account.transfer")] };
        let exchange = common::ExchangeId { id: "mock".into(), ..Default::default() };
        let found = stub.describe_venue_op(&exchange, "account.transfer").await.expect("found");
        assert_eq!(found.name, "account.transfer");
    }

    #[tokio::test]
    async fn the_default_describe_venue_op_reports_not_found() {
        let stub = StubOps { ops: vec![op("account.balance")] };
        let exchange = common::ExchangeId { id: "mock".into(), ..Default::default() };
        let err = stub.describe_venue_op(&exchange, "nope").await.expect_err("must not resolve");
        assert!(matches!(err, PortError::NotFound(_)), "got {err:?}");
        assert!(err.to_string().contains("nope"), "the message must name the op: {err}");
    }

    #[tokio::test]
    async fn the_default_describe_venue_op_on_an_empty_registry_is_not_found() {
        let stub = StubOps { ops: Vec::new() };
        let exchange = common::ExchangeId { id: "mock".into(), ..Default::default() };
        let err = stub.describe_venue_op(&exchange, "account.balance").await.expect_err("empty");
        assert!(matches!(err, PortError::NotFound(_)), "got {err:?}");
    }

    /// The default `fetch_deposits` forwards an empty currency filter and the
    /// `deposit` entry type; a backend that ignores the filter would report
    /// withdrawals as deposits.
    #[tokio::test]
    async fn the_default_fetch_deposits_forwards_the_deposit_filter() {
        struct RecordingWallet {
            seen: std::sync::Mutex<Option<(String, String, u32)>>,
        }

        #[async_trait]
        impl WalletGateway for RecordingWallet {
            async fn list_ledger_entries(
                &self,
                _exchange_id: &common::ExchangeId,
                currency: &str,
                entry_type: &str,
                limit: u32,
            ) -> Result<Vec<LedgerEntry>, PortError> {
                *self.seen.lock().expect("wallet mutex") =
                    Some((currency.to_string(), entry_type.to_string(), limit));
                Ok(Vec::new())
            }

            async fn transfer(
                &self,
                _exchange_id: &common::ExchangeId,
                _asset: &str,
                _amount: Decimal,
                _dest_label: &str,
                _client_transfer_id: &str,
            ) -> Result<TransferReceipt, PortError> {
                Err(PortError::Unsupported("transfer".into()))
            }
        }

        let wallet = RecordingWallet { seen: std::sync::Mutex::new(None) };
        let exchange = common::ExchangeId { id: "mock".into(), ..Default::default() };
        let rows = wallet.fetch_deposits(&exchange, 42).await.expect("deposits");
        assert!(rows.is_empty());
        let seen = wallet.seen.lock().expect("wallet mutex").clone();
        assert_eq!(
            seen,
            Some((String::new(), "deposit".to_string(), 42)),
            "the default must forward an empty currency filter and the deposit type"
        );
    }

    // -----------------------------------------------------------------------
    // Ledger signing
    // -----------------------------------------------------------------------

    /// `LedgerEntry::amount` is signed on purpose: a caller must not be able to
    /// read a withdrawal as a deposit.
    #[test]
    fn ledger_entries_keep_the_direction_in_the_sign() {
        let deposit = LedgerEntry {
            id: "d1".into(),
            currency: "USDT".into(),
            amount: dec!(100),
            entry_type: "deposit".into(),
            completed: true,
            time_ms: 0,
        };
        let withdrawal = LedgerEntry { amount: dec!(-100), ..deposit.clone() };
        assert!(deposit.amount.is_sign_positive());
        assert!(withdrawal.amount.is_sign_negative());
        assert_ne!(deposit, withdrawal);
    }

    // -----------------------------------------------------------------------
    // Properties
    // -----------------------------------------------------------------------

    /// Unknown / future channels fail closed to `Block` (private-stream
    /// semantics), never to a dropping policy.
    #[test]
    fn unknown_channels_fail_closed_to_block() {
        assert_eq!(
            overflow_policy_for_channel(market::StreamChannel::Unspecified),
            OverflowPolicy::Block
        );
    }

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(256))]

        /// `is_sequence_gap(prev, next)` is gap-free exactly when `next` is
        /// `prev + 1` in wrapping arithmetic.
        #[test]
        fn a_sequence_is_gap_free_exactly_when_it_wraps_to_prev_plus_one(
            prev in any::<u64>(),
            delta in 0u64..8,
        ) {
            let next = prev.wrapping_add(delta);
            prop_assert_eq!(is_sequence_gap(prev, next), delta != 1, "prev={} delta={}", prev, delta );
        }

        /// Adding a sequence and then checking the next one is always
        /// gap-free: the detector must agree with its own producer.
        #[test]
        fn incrementing_then_checking_is_never_a_gap(prev in any::<u64>()) {
            let next = prev.wrapping_add(1);
            let header = common::EventHeader { sequence: next, ..Default::default() };
            prop_assert!(!is_sequence_gap(prev, next));
            prop_assert!(!has_sequence_gap(Some(prev), &header));
        }

        /// Presenting the same sequence twice always reports a gap at the raw
        /// level; the `has_sequence_gap` half additionally ignores an unset
        /// (`0`) header, so sequence `0` is excluded here (covered by
        /// `an_unset_header_sequence_is_never_a_gap`).
        #[test]
        fn repeating_a_sequence_is_always_a_gap(sequence in any::<u64>()) {
            prop_assume!(sequence != 0);
            let header = common::EventHeader { sequence, ..Default::default() };
            prop_assert!(is_sequence_gap(sequence, sequence));
            prop_assert!(has_sequence_gap(Some(sequence), &header));
        }

        /// Orderbook is the only channel that coalesces — the §6.5 table. Any
        /// other channel, present or future, must never coalesce.
        #[test]
        fn only_the_orderbook_channel_coalesces(
            channel in prop::sample::select(&[
                market::StreamChannel::Unspecified,
                market::StreamChannel::Ticker,
                market::StreamChannel::Orderbook,
                market::StreamChannel::Trades,
                market::StreamChannel::Ohlcv,
            ]),
        ) {
            let policy = overflow_policy_for_channel(channel);
            prop_assert_eq!(
                policy == OverflowPolicy::Coalesce,
                channel == market::StreamChannel::Orderbook, "{:?} mapped to {:?}", channel, policy
            );
        }

        /// Every status renders as a non-empty, unique, lowercase wire name.
        #[test]
        fn trigger_order_status_names_are_stable(idx in 0u8..4) {
            let status = match idx {
                0 => TriggerOrderStatus::Open,
                1 => TriggerOrderStatus::Triggered,
                2 => TriggerOrderStatus::Canceled,
                _ => TriggerOrderStatus::Rejected,
            };
            let name = status.as_str();
            prop_assert!(!name.is_empty());
            prop_assert_eq!(name.to_lowercase(), name);
            prop_assert_eq!(status.to_string(), name);
        }

        /// `Decimal` converts into `PortError` and renders the offending payload.
        ///
        /// Nine digits past what the 96-bit mantissa holds: the payload is
        /// grammar-clean, so only the width can reject it.
        #[test]
        fn an_out_of_range_decimal_always_converts(digits in 29usize..40) {
            let payload = "9".repeat(digits);
            let err: PortError = out_of_range(&payload).into();
            prop_assert!(matches!(err, PortError::Decimal(_)));
            prop_assert!(err.to_string().contains(&payload), "{err}");
        }
    }
}