-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathorders.rs
More file actions
1551 lines (1457 loc) · 55.7 KB
/
Copy pathorders.rs
File metadata and controls
1551 lines (1457 loc) · 55.7 KB
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
use std::str::FromStr;
use mostro_core::prelude::*;
use nostr_sdk::prelude::*;
use ratatui::style::{Color, Style};
use crate::ui::constants::{
BUY_ORDER_FLOW_STEPS_MAKER, BUY_ORDER_FLOW_STEPS_TAKER, GENERIC_ORDER_FLOW_STEPS_TAKER,
SELL_ORDER_FLOW_STEPS_MAKER, SELL_ORDER_FLOW_STEPS_TAKER,
VIEW_MESSAGE_BUYER_TOOK_ORDER_PREVIEW, VIEW_MESSAGE_HOLD_INVOICE_PREVIEW,
};
pub use crate::ui::constants::StepLabel;
/// Stable My Trades header fields for one trade (maker publish or taker take). Not updated by later DMs.
#[derive(Clone, Debug)]
pub struct OrderChatStaticHeader {
pub order_id: uuid::Uuid,
pub kind: Option<mostro_core::order::Kind>,
pub created_at: Option<i64>,
pub trade_index: i64,
/// Local party's trade Nostr pubkey string (for display; matches DM path convention).
pub initiator_trade_pubkey: String,
/// `true` = we are maker, `false` = taker.
pub is_mine: bool,
}
#[derive(Clone, Debug, Default)]
pub struct OrderSuccess {
pub order_id: Option<uuid::Uuid>,
pub kind: Option<mostro_core::order::Kind>,
pub amount: i64,
pub fiat_code: String,
pub fiat_amount: i64,
pub min_amount: Option<i64>,
pub max_amount: Option<i64>,
pub payment_method: String,
pub premium: i64,
pub status: Option<Status>,
pub trade_index: Option<i64>, // Trade index used for this order
/// Filled on successful create/take for My Trades static header.
pub static_header: Option<OrderChatStaticHeader>,
}
/// `action` for a post-success placeholder [`OrderMessage`] so My Trades has a row before DMs land.
/// Never returns synthetic book-side `take-buy` / `take-sell` (those break Messages-tab Enter).
fn placeholder_action_for_order_success(os: &OrderSuccess) -> Option<Action> {
let header = os.static_header.as_ref()?;
if header.is_mine {
return match os.status {
Some(Status::WaitingMakerBond) => Some(Action::PayBondInvoice),
_ => Some(Action::NewOrder),
};
}
match os.status {
Some(Status::WaitingTakerBond) => Some(Action::PayBondInvoice),
Some(Status::WaitingPayment) => Some(Action::WaitingSellerToPay),
Some(Status::WaitingBuyerInvoice) | Some(Status::SettledHoldInvoice) => {
Some(Action::WaitingBuyerInvoice)
}
Some(Status::FiatSent) => Some(Action::FiatSent),
_ => Some(Action::BuyerTookOrder),
}
}
fn small_order_from_order_success(os: &OrderSuccess) -> SmallOrder {
SmallOrder {
id: os.order_id,
kind: os.kind,
status: os.status,
amount: os.amount,
fiat_code: os.fiat_code.clone(),
min_amount: os.min_amount,
max_amount: os.max_amount,
fiat_amount: os.fiat_amount,
payment_method: os.payment_method.clone(),
premium: os.premium,
buyer_invoice: None,
created_at: os.static_header.as_ref().and_then(|h| h.created_at),
expires_at: None,
..Default::default()
}
}
/// One synthetic [`OrderMessage`] when `Success` arrives before any DM row exists (My Trades sidebar).
pub(crate) fn try_placeholder_order_message_from_success(
os: &OrderSuccess,
) -> Option<OrderMessage> {
let header = os.static_header.as_ref()?;
let order_id = os.order_id?;
let trade_index = os.trade_index.unwrap_or(header.trade_index);
let action = placeholder_action_for_order_success(os)?;
let sender = PublicKey::from_str(header.initiator_trade_pubkey.as_str()).ok()?;
let small = small_order_from_order_success(os);
let message = Message::new_order(
Some(order_id),
None,
Some(trade_index),
action,
Some(Payload::Order(small)),
);
Some(OrderMessage {
message,
timestamp: chrono::Utc::now().timestamp(),
sender,
order_id: Some(order_id),
trade_index,
sat_amount: None,
buyer_invoice: None,
order_kind: os.kind,
is_mine: Some(header.is_mine),
order_status: os.status,
read: true,
auto_popup_shown: true,
})
}
/// Per-order buyer invoice preference when we act as taker on a SELL listing.
/// Stored in-memory only (not persisted to DB); later flows can use it to
/// decide how to pre-fill or submit buyer invoices for that specific order.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum BuyerInvoicePreference {
/// Prefer using the saved Settings `ln_address` (Lightning address) as
/// the buyer invoice source when appropriate.
UseSavedLnAddress,
/// Always prompt for a manual BOLT11 or Lightning address for this order.
ManualInvoice,
}
#[derive(Clone, Debug)]
pub enum OperationResult {
Success(OrderSuccess),
/// Payment request required - shows invoice popup for buy orders.
/// `action` discriminates between the trade hold invoice (`Action::PayInvoice`)
/// and the anti-abuse bond invoice (`Action::PayBondInvoice`); both arrive
/// with the same `Payload::PaymentRequest` shape and only differ by action.
PaymentRequestRequired {
order: mostro_core::prelude::SmallOrder,
invoice: String,
sat_amount: Option<i64>,
trade_index: i64,
static_header: OrderChatStaticHeader,
action: mostro_core::prelude::Action,
},
/// Generic informational popup (e.g. AddInvoice confirmation)
Info(String),
/// AddInvoice DM succeeded; optionally persist [`BuyerInvoicePreference::UseSavedLnAddress`]
/// for this order after send (main loop normalizes to [`Self::Info`] for display).
InvoiceSubmitted {
message: String,
remember_buyer_saved_ln_address_for_order: Option<uuid::Uuid>,
},
Error(String),
/// Observer chat loaded successfully from relays.
ObserverChatLoaded(Vec<crate::ui::chat::DisputeChatMessage>),
/// Observer chat fetch failed.
ObserverChatError(String),
/// Trade ended (e.g. cooperative cancel confirmed); remove order from Messages and show `message`.
TradeClosed {
order_id: uuid::Uuid,
message: String,
},
/// Local-only history cleanup result; remove these order rows from in-memory My Trades cache.
OrderHistoryDeleted {
deleted_order_ids: Vec<uuid::Uuid>,
message: String,
},
/// Rebuild [`crate::ui::AppState::my_trades_maker_book`] from SQLite (no UI popup).
MyTradesMakerBookChanged,
/// Open invoice / waiting popup from a synchronous execute reply (e.g. bond payout DM).
OpenInvoicePopup {
notification: MessageNotification,
order_message: Box<OrderMessage>,
},
/// User order chat attachment sent successfully (append local row + show info).
OrderChatAttachmentSent {
order_id: String,
chat_message: crate::ui::UserOrderChatMessage,
info_message: String,
},
// User order chat attachment error (show error popup).
OrderChatAttachmentError {
order_id: String,
error: String,
},
/// Blossom upload succeeded but order-chat DM failed; prepared payload kept for retry.
OrderChatAttachmentSendFailed {
prepared: crate::ui::helpers::PreparedOrderChatAttachment,
error: String,
},
}
/// Result of async Lightning address LNURL verification and save (settings flow; not order/dispute).
#[derive(Clone, Debug)]
pub enum LnAddressVerifyResult {
Verified { message: String },
Err(String),
}
/// Result of an async Mostro instance info fetch (sent from key handlers to main loop).
#[derive(Clone, Debug)]
pub enum MostroInfoFetchResult {
Ok {
info: Box<Option<crate::util::MostroInstanceInfo>>,
message: String,
},
/// Startup / background refresh: update `mostro_info` only; do not change mode or show toasts.
Applied {
info: Box<Option<crate::util::MostroInstanceInfo>>,
},
Err(String),
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Default)]
pub enum FormField {
#[default]
OrderType,
Currency,
AmountSats,
FiatAmount,
FiatAmountMax,
PaymentMethod,
Premium,
Invoice,
ExpirationDays,
}
impl FormField {
pub fn next(self, use_range: bool) -> Self {
use FormField::*;
match self {
OrderType => Currency,
Currency => AmountSats,
AmountSats => FiatAmount,
FiatAmount => {
if use_range {
FiatAmountMax
} else {
PaymentMethod
}
}
FiatAmountMax => PaymentMethod,
PaymentMethod => Premium,
Premium => Invoice,
Invoice => ExpirationDays,
ExpirationDays => OrderType,
}
}
pub fn prev(self, use_range: bool) -> Self {
use FormField::*;
match self {
OrderType => ExpirationDays,
Currency => OrderType,
AmountSats => Currency,
FiatAmount => AmountSats,
FiatAmountMax => FiatAmount,
PaymentMethod => {
if use_range {
FiatAmountMax
} else {
FiatAmount
}
}
Premium => PaymentMethod,
Invoice => Premium,
ExpirationDays => Invoice,
}
}
}
#[derive(Clone, Debug, Default)]
pub struct FormState {
pub kind: String, // buy | sell
pub fiat_code: String, // e.g. USD, EUR, ARS
pub fiat_amount: String, // numeric (single amount or min for range)
pub fiat_amount_max: String, // max amount for range (optional)
pub amount: String, // amount in sats (0 for market)
pub payment_method: String, // comma separated
pub premium: String, // premium percentage
pub invoice: String, // optional invoice
pub expiration_days: String, // expiration days (0 for no expiration)
pub focused: FormField, // which field is focused
pub use_range: bool, // whether to use fiat range
}
impl FormState {
/// Create a default order form used when entering the Create New Order tab.
pub fn new_default_form() -> Self {
Self {
kind: "buy".to_string(),
fiat_code: "USD".to_string(),
amount: "0".to_string(),
premium: "0".to_string(),
expiration_days: "1".to_string(),
focused: FormField::Currency,
..Default::default()
}
}
}
#[derive(Clone, Debug)]
pub struct TakeOrderState {
pub order: SmallOrder,
pub amount_input: String, // For range orders: the amount user wants to take
pub is_range_order: bool, // Whether this is a range order (has min/max)
pub validation_error: Option<String>, // Error message if amount is invalid
pub selected_button: bool, // true for YES, false for NO
}
/// Represents a message related to an order
#[derive(Clone, Debug)]
pub struct OrderMessage {
pub message: Message,
pub timestamp: i64,
pub sender: PublicKey,
pub order_id: Option<uuid::Uuid>,
pub trade_index: i64,
pub sat_amount: Option<i64>,
pub buyer_invoice: Option<String>,
/// Book side (`buy` / `sell`) for this trade, carried across DMs that omit `Payload::Order`.
pub order_kind: Option<mostro_core::order::Kind>,
/// `true` = maker (created the order), `false` = taker.
///
/// `None` = role not hydrated yet (e.g. first take-order DM before `save_order(false)`).
/// Populated from SQLite in the trade-DM listener after upsert (see `util::dm_utils`).
pub is_mine: Option<bool>,
/// Last known Mostro order status for this trade (payload or DB).
pub order_status: Option<mostro_core::order::Status>,
pub read: bool, // Whether the message has been read
/// Whether we've already shown the automatic popup for this message
pub auto_popup_shown: bool,
}
/// Drop `new-order` rows and fix selection. Those DMs are not shown in the Messages tab
/// (order creation ack is handled via `send_new_order` / waiting UI).
pub fn strip_new_order_messages_and_clamp_selected(
messages: &mut Vec<OrderMessage>,
selected_message_idx: &mut usize,
) {
messages.retain(|m| !matches!(m.message.get_inner_message_kind().action, Action::NewOrder));
if *selected_message_idx >= messages.len() {
*selected_message_idx = messages.len().saturating_sub(1);
}
}
/// Notification for a new message
#[derive(Clone, Debug)]
pub struct MessageNotification {
pub order_id: Option<uuid::Uuid>,
pub message_preview: String,
pub timestamp: i64,
pub action: Action,
pub sat_amount: Option<i64>,
pub invoice: Option<String>,
/// Long explanatory text for waiting-phase popups.
pub body: Option<String>,
/// Maker bond (Phase 5): pay before the order is published to the book.
pub maker_bond_publish: bool,
}
/// Whether an invoice modal is appropriate for the current trade phase.
/// `PayInvoice` is only for `waiting-payment`; after the seller pays, status moves to
/// `waiting-buyer-invoice` and a stale replayed `pay-invoice` DM must not reopen the payment popup.
#[must_use]
pub fn invoice_popup_allowed_for_order_status(
action: &Action,
order_status: Option<mostro_core::order::Status>,
) -> bool {
match action {
// Strict: only `waiting-payment` still requires paying the hold invoice.
Action::PayInvoice => matches!(
order_status,
Some(mostro_core::order::Status::WaitingPayment)
),
// Anti-abuse bond: taker bond (`WaitingTakerBond`) or maker bond (`WaitingMakerBond`).
// `None` covers fresh DMs that arrive before the local row hydrates.
Action::PayBondInvoice => matches!(
order_status,
Some(
mostro_core::order::Status::WaitingTakerBond
| mostro_core::order::Status::WaitingMakerBond
) | None
),
Action::AddInvoice => matches!(
order_status,
Some(
mostro_core::order::Status::WaitingBuyerInvoice
| mostro_core::order::Status::SettledHoldInvoice
) | None
),
Action::AddBondInvoice => !matches!(
order_status,
Some(
mostro_core::order::Status::Canceled | mostro_core::order::Status::CanceledByAdmin
)
),
_ => false,
}
}
/// Whether the local user must act on an invoice/payment popup (`AddInvoice`, `PayInvoice`, `PayBondInvoice`).
///
/// Buy/sell listing kind swaps which side is maker vs taker for each action. When [`OrderMessage::is_mine`]
/// is still `None`, we assume **taker** (same as the Messages timeline): safe for the common pre-`save_order`
/// take-order race; once the DM listener hydrates role from SQLite, `Some(true/false)` drives the decision.
#[must_use]
pub fn local_user_must_act_on_invoice_popup(msg: &OrderMessage, popup_action: &Action) -> bool {
let is_maker = msg.is_mine.unwrap_or(false);
match (msg.order_kind, popup_action) {
(Some(mostro_core::order::Kind::Buy), Action::AddInvoice) => is_maker,
(Some(mostro_core::order::Kind::Buy), Action::PayInvoice) => !is_maker,
(Some(mostro_core::order::Kind::Sell), Action::AddInvoice) => !is_maker,
(Some(mostro_core::order::Kind::Sell), Action::PayInvoice) => is_maker,
(_, Action::PayBondInvoice) => match msg.order_status {
Some(mostro_core::order::Status::WaitingMakerBond) => is_maker,
Some(mostro_core::order::Status::WaitingTakerBond) => !is_maker,
// Create-order sync path: maker is always the actor before status hydrates.
None => is_maker,
_ => false,
},
(_, Action::AddBondInvoice) => true,
(None, Action::PayInvoice) => msg
.buyer_invoice
.as_ref()
.map(|s| !s.is_empty())
.unwrap_or(false),
(None, Action::AddInvoice) => false,
_ => false,
}
}
/// Explanatory text when the local user is waiting on the counterparty (no invoice action required).
#[must_use]
pub fn waiting_phase_description(msg: &OrderMessage) -> &'static str {
let is_maker = msg.is_mine.unwrap_or(false);
let action = msg.message.get_inner_message_kind().action.clone();
match (msg.order_kind, is_maker, action) {
(
Some(mostro_core::order::Kind::Buy),
true,
Action::WaitingSellerToPay | Action::PayInvoice,
) => "Your order was taken. Waiting for the seller to pay the hold invoice. You will be prompted to add your Lightning invoice when it is your turn.",
(
Some(mostro_core::order::Kind::Sell),
false,
Action::WaitingSellerToPay | Action::PayInvoice,
) => "Waiting for the seller to pay the hold invoice.",
(
Some(mostro_core::order::Kind::Buy),
false,
Action::WaitingBuyerInvoice | Action::AddInvoice,
) => "Waiting for the buyer to add their Lightning invoice.",
(
Some(mostro_core::order::Kind::Sell),
true,
Action::WaitingBuyerInvoice | Action::AddInvoice,
) => "Waiting for the buyer to add their Lightning invoice.",
(_, true, Action::PayBondInvoice) if msg.order_status == Some(Status::WaitingTakerBond) => {
"Waiting for the taker to pay the anti-abuse bond."
}
(_, true, Action::PayBondInvoice) => {
"Pay the anti-abuse bond to publish your order to the book."
}
_ => "Waiting for the counterparty. No action is required from you right now.",
}
}
/// Short phase title for waiting-phase popups.
#[must_use]
pub fn waiting_phase_short_label(msg: &OrderMessage) -> &'static str {
let is_maker = msg.is_mine.unwrap_or(false);
let action = msg.message.get_inner_message_kind().action.clone();
match (msg.order_kind, is_maker, action) {
(_, true, Action::PayBondInvoice) if msg.order_status == Some(Status::WaitingTakerBond) => {
"Waiting for Taker Bond"
}
(_, true, Action::PayBondInvoice) => "Maker Bond Required",
(
Some(mostro_core::order::Kind::Buy),
true,
Action::WaitingSellerToPay | Action::PayInvoice,
)
| (
Some(mostro_core::order::Kind::Sell),
false,
Action::WaitingSellerToPay | Action::PayInvoice,
) => "Waiting for Seller to Pay",
(
Some(mostro_core::order::Kind::Buy),
false,
Action::WaitingBuyerInvoice | Action::AddInvoice,
)
| (
Some(mostro_core::order::Kind::Sell),
true,
Action::WaitingBuyerInvoice | Action::AddInvoice,
) => "Waiting for Buyer to Add Invoice",
_ => "Trade status",
}
}
/// UI action for a waiting-phase popup (preserves phase semantics for rendering).
#[must_use]
pub fn waiting_popup_action_for_message(msg: &OrderMessage) -> Action {
match msg.message.get_inner_message_kind().action {
Action::WaitingBuyerInvoice | Action::AddInvoice => Action::WaitingBuyerInvoice,
_ => Action::WaitingSellerToPay,
}
}
/// Build a waiting-phase notification from an order message row.
#[must_use]
pub fn order_message_to_waiting_notification(msg: &OrderMessage) -> MessageNotification {
let mut notification = order_message_to_notification(msg);
notification.message_preview = waiting_phase_short_label(msg).to_string();
notification.body = Some(waiting_phase_description(msg).to_string());
notification.action = waiting_popup_action_for_message(msg);
notification
}
/// State for handling invoice input in AddInvoice notifications
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum InvoiceNotificationActionSelection {
Primary,
Cancel,
}
/// State for handling invoice input in AddInvoice notifications
#[derive(Clone, Debug)]
pub struct InvoiceInputState {
pub invoice_input: String,
pub focused: bool,
pub just_pasted: bool, // Flag to ignore Enter immediately after paste
pub copied_to_clipboard: bool, // Flag to show "Copied!" message
/// Vertical scroll offset for long invoice display (PayInvoice popup).
pub scroll_y: u16,
/// Selected action in AddInvoice/PayInvoice popup.
pub action_selection: InvoiceNotificationActionSelection,
}
/// State for handling key input (pubkey or privkey) in admin settings
#[derive(Clone, Debug)]
pub struct KeyInputState {
pub key_input: String,
pub focused: bool,
pub just_pasted: bool, // Flag to ignore Enter immediately after paste
}
/// YES/NO selection for `ViewingMessage` popups (FiatSentOk, My Trades actions, etc.).
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ThreeState {
Yes,
No,
Cancel,
}
impl ThreeState {
pub const fn next(self) -> Self {
match self {
Self::Yes => Self::No,
Self::No => Self::Cancel,
Self::Cancel => Self::Yes,
}
}
pub const fn prev(self) -> Self {
match self {
Self::Yes => Self::Cancel,
Self::No => Self::Yes,
Self::Cancel => Self::No,
}
}
pub const fn index(self) -> u8 {
match self {
Self::Yes => 0,
Self::No => 1,
Self::Cancel => 2,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ViewingMessageButtonSelection {
/// `true` = YES highlighted, `false` = NO.
Two { yes_selected: bool },
/// Hold-invoice confirmation only: YES / NO / CANCEL.
Three(ThreeState),
}
impl ViewingMessageButtonSelection {
pub fn cycle_three_prev(&mut self) {
if let Self::Three(selected) = self {
*selected = selected.prev();
}
}
pub fn cycle_three_next(&mut self) {
if let Self::Three(selected) = self {
*selected = selected.next();
}
}
}
/// State for viewing a simple message popup
#[derive(Clone, Debug)]
pub struct MessageViewState {
pub message_content: String, // The message content to display
pub order_id: Option<uuid::Uuid>,
pub action: Action,
pub button_selection: ViewingMessageButtonSelection,
}
/// Rate counterparty after Mostro prompts with `action: rate` (daemon resolves peer by order id).
#[derive(Clone, Debug)]
pub struct RatingOrderState {
pub order_id: uuid::Uuid,
/// 1..=5 (`mostro_core::MIN_RATING`..=`MAX_RATING`).
pub selected_rating: u8,
}
/// Build a `MessageNotification` from an `OrderMessage` for use in popups.
pub fn order_message_to_notification(msg: &OrderMessage) -> MessageNotification {
let inner_message_kind = msg.message.get_inner_message_kind();
let action = inner_message_kind.action.clone();
let action_str = match action {
Action::NewOrder => "New Order created",
Action::AddInvoice => "Invoice Request",
Action::AddBondInvoice => "Bond Payout Invoice",
Action::PayInvoice => "Payment Request",
Action::PayBondInvoice => "Bond Invoice",
Action::TakeSell => "Take Sell",
Action::TakeBuy => "Take Buy",
Action::FiatSent => "Fiat Sent",
Action::FiatSentOk => "Fiat payment completed",
Action::WaitingBuyerInvoice => "Waiting for Buyer to Add Invoice",
Action::WaitingSellerToPay => "Waiting for Seller to Pay",
Action::HoldInvoicePaymentAccepted => VIEW_MESSAGE_HOLD_INVOICE_PREVIEW,
Action::BuyerTookOrder => VIEW_MESSAGE_BUYER_TOOK_ORDER_PREVIEW,
Action::Cancel => "Cancel",
Action::CooperativeCancelInitiatedByPeer => "Peer requested cooperative cancel",
Action::Canceled => "Order canceled",
Action::AdminCanceled => "Order canceled by admin",
Action::Dispute | Action::DisputeInitiatedByYou => "Dispute",
Action::Rate => "Rate Counterparty",
Action::RateReceived | Action::PurchaseCompleted => "Rate Counterparty completed",
Action::Release | Action::Released => "Release",
_ => "Message",
};
let body = if matches!(action, Action::AddBondInvoice) {
match inner_message_kind.payload.as_ref() {
Some(Payload::BondPayoutRequest(req)) => {
Some(bond_payout_notification_body(req.slashed_at))
}
_ => None,
}
} else {
None
};
MessageNotification {
order_id: msg.order_id,
message_preview: action_str.to_string(),
timestamp: msg.timestamp,
action,
sat_amount: msg.sat_amount,
invoice: msg.buyer_invoice.clone(),
body,
maker_bond_publish: msg.order_status == Some(Status::WaitingMakerBond),
}
}
fn bond_payout_notification_body(slashed_at: i64) -> String {
let anchor = chrono::DateTime::from_timestamp(slashed_at, 0)
.map(|d| d.format("%Y-%m-%d %H:%M UTC").to_string())
.unwrap_or_else(|| "unknown".to_string());
format!("Slash recorded: {anchor}. Claim deadline = anchor + instance payout window.")
}
/// Short, UI-friendly action label for the messages sidebar.
pub fn message_action_compact_label(action: &Action) -> &'static str {
match action {
Action::AddInvoice => "Invoice Request",
Action::AddBondInvoice => "Bond Payout Invoice",
Action::PayInvoice => "Payment Request",
Action::PayBondInvoice => "Bond Invoice",
Action::WaitingBuyerInvoice => "Waiting Buyer Invoice",
Action::WaitingSellerToPay => "Waiting Seller Payment",
Action::HoldInvoicePaymentAccepted => "Hold Invoice Accepted",
Action::BuyerTookOrder => "Buyer Took Order",
Action::FiatSent => "Fiat Sent",
Action::FiatSentOk => "Fiat Confirmed",
Action::Release | Action::Released => "Release sats",
Action::Dispute | Action::DisputeInitiatedByYou => "Dispute",
Action::Canceled => "Canceled",
Action::AdminCanceled => "Admin Canceled",
Action::Rate => "Rate Counterparty",
Action::RateReceived => "Rating Received",
Action::CooperativeCancelInitiatedByPeer => "Cooperative Cancel Initiated by Peer",
Action::CooperativeCancelInitiatedByYou => "Cooperative Cancel Initiated by You",
Action::NewOrder => "New Order Created",
_ => "Unknown Message",
}
}
/// Status-aware compact label for Messages sidebar/detail.
/// Keeps terminal statuses from showing stale action text after reboot replay.
pub fn message_action_compact_label_for_message(msg: &OrderMessage) -> &'static str {
match msg.order_status {
Some(Status::Pending) => "Pending order",
Some(Status::Success) => "Trade Completed",
Some(Status::SettledByAdmin) => "Settled by admin",
Some(Status::CompletedByAdmin) => "Completed by admin",
Some(Status::Canceled) => "Canceled",
Some(Status::CanceledByAdmin) => "Admin Canceled",
Some(Status::CooperativelyCanceled) => "Cooperatively Canceled",
Some(Status::WaitingBuyerInvoice) => "Waiting Buyer Invoice",
Some(Status::WaitingPayment) => "Waiting Seller Payment",
Some(Status::Expired) => "Expired",
Some(_) | None => {
message_action_compact_label(&msg.message.get_inner_message_kind().action)
}
}
}
/// Book order kind for sidebar: prefer persisted [`OrderMessage::order_kind`], then payload,
/// then take-action hints when the listing side is implied (`take-sell` → sell listing, etc.).
pub fn message_order_kind_label(msg: &OrderMessage) -> &'static str {
if let Some(k) = msg.order_kind {
return match k {
mostro_core::order::Kind::Buy => "BUY",
mostro_core::order::Kind::Sell => "SELL",
};
}
let inner = msg.message.get_inner_message_kind();
if let Some(Payload::Order(order)) = &inner.payload {
if let Some(kind) = order.kind {
return match kind {
mostro_core::order::Kind::Buy => "BUY",
mostro_core::order::Kind::Sell => "SELL",
};
}
}
match inner.action {
Action::TakeSell => "SELL",
Action::TakeBuy => "BUY",
_ => "N/A",
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
#[repr(u8)]
#[allow(clippy::enum_variant_names)] // Step* names match UI column semantics
pub enum StepLabelsBuy {
StepPendingOrder = 0,
StepSellerPayment = 1,
StepBuyerInvoice = 2,
StepChatActiveOrder = 3,
StepSendFiat = 4,
StepReleaseSats = 5,
StepRate = 6,
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
#[repr(u8)]
#[allow(clippy::enum_variant_names)] // Step* names match UI column semantics
pub enum StepLabelsSell {
StepPendingOrder = 0,
StepBuyerInvoice = 1,
StepSellerPayment = 2,
StepChatActiveOrder = 3,
StepSendFiat = 4,
StepReleaseSats = 5,
StepRate = 6,
}
/// Highlighted column (`1`..=`6`) for the Messages tab stepper; buy vs sell use different label order.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
#[repr(u8)]
#[allow(clippy::enum_variant_names)] // Step* names match UI "Step N" labels
pub enum FlowStep {
BuyFlowStep(StepLabelsBuy),
SellFlowStep(StepLabelsSell),
}
impl FlowStep {
/// Step index for UI (`1`..=`6`), aligned with stepper labels.
#[must_use]
pub const fn step_number(self) -> usize {
match self {
FlowStep::BuyFlowStep(step) => step as usize,
FlowStep::SellFlowStep(step) => step as usize,
}
}
}
/// Resolves the highlighted timeline step for the Messages tab (buy and sell listings).
#[must_use]
pub fn message_trade_timeline_step(msg: &OrderMessage) -> FlowStep {
let action = msg.message.get_inner_message_kind().action.clone();
match msg.order_kind {
Some(mostro_core::order::Kind::Buy) => buy_listing_flow_step(msg),
Some(mostro_core::order::Kind::Sell) => sell_listing_flow_step(msg),
None => message_buy_flow_step_fallback(&action),
}
}
/// Buy-listing timeline step: prefers [`OrderMessage::order_status`] + maker/taker for `Kind::Buy`.
pub fn buy_listing_flow_step(msg: &OrderMessage) -> FlowStep {
let action = msg.message.get_inner_message_kind().action.clone();
if msg.order_kind != Some(mostro_core::order::Kind::Buy) {
return message_buy_flow_step_fallback(&action);
}
// Takers often see `is_mine: None` until the local row hydrates; default to taker so the
// highlighted column matches [`listing_timeline_labels`] (taker wording per kind).
let is_maker = msg.is_mine.unwrap_or(false);
// Post-`success` phases are action-specific (`rate` vs release); handle before status.
if matches!(&action, Action::FiatSentOk) {
return FlowStep::BuyFlowStep(StepLabelsBuy::StepReleaseSats);
}
if let Some(status) = msg.order_status {
if let Some(step) = listing_step_from_status(mostro_core::order::Kind::Buy, status) {
return step;
}
}
buy_listing_flow_step_from_action(&action, is_maker)
}
/// Sell-listing timeline step: same pipeline as buy; action fallback matches [`SELL_ORDER_FLOW_STEPS_*`].
pub fn sell_listing_flow_step(msg: &OrderMessage) -> FlowStep {
let action = msg.message.get_inner_message_kind().action.clone();
if msg.order_kind != Some(mostro_core::order::Kind::Sell) {
return message_buy_flow_step_fallback(&action);
}
let is_maker = msg.is_mine.unwrap_or(false);
if matches!(&action, Action::FiatSentOk) {
return FlowStep::SellFlowStep(StepLabelsSell::StepReleaseSats);
}
if let Some(status) = msg.order_status {
if let Some(step) = listing_step_from_status(mostro_core::order::Kind::Sell, status) {
return step;
}
}
sell_listing_flow_step_from_action(&action, is_maker)
}
/// Shared Mostro `Status` machine; column indices differ for buy vs sell (see [`StepLabelsBuy`] vs [`StepLabelsSell`]).
fn listing_step_from_status(kind: mostro_core::order::Kind, status: Status) -> Option<FlowStep> {
match kind {
mostro_core::order::Kind::Buy => match status {
// Initial stat of order - no green steps visulized, orders is still pending.
Status::Pending | Status::WaitingTakerBond | Status::WaitingMakerBond => {
Some(FlowStep::BuyFlowStep(StepLabelsBuy::StepPendingOrder))
}
// `WaitingTakerBond` / `WaitingMakerBond`: pre-active bond phases; treat like `Pending`.
Status::WaitingPayment => Some(FlowStep::BuyFlowStep(StepLabelsBuy::StepSellerPayment)),
Status::WaitingBuyerInvoice | Status::SettledHoldInvoice => {
Some(FlowStep::BuyFlowStep(StepLabelsBuy::StepBuyerInvoice))
}
Status::InProgress | Status::Active => {
Some(FlowStep::BuyFlowStep(StepLabelsBuy::StepChatActiveOrder))
}
Status::FiatSent => Some(FlowStep::BuyFlowStep(StepLabelsBuy::StepSendFiat)),
// On completed trades, keep the timeline in the final column even if the latest
// replayed action is an older pre-success DM (relay ordering / reboot hydration).
Status::Success => Some(FlowStep::BuyFlowStep(StepLabelsBuy::StepRate)),
Status::Canceled
| Status::CanceledByAdmin
| Status::CooperativelyCanceled
| Status::Expired
| Status::Dispute
| Status::SettledByAdmin
| Status::CompletedByAdmin => {
Some(FlowStep::BuyFlowStep(StepLabelsBuy::StepPendingOrder))
}
},
mostro_core::order::Kind::Sell => match status {
// Initial stat of order - no green steps visulized, orders is still pending.
Status::Pending | Status::WaitingTakerBond | Status::WaitingMakerBond => {
Some(FlowStep::SellFlowStep(StepLabelsSell::StepPendingOrder))
}
// `WaitingTakerBond` / `WaitingMakerBond`: pre-active bond phases; treat like `Pending`.
Status::WaitingPayment => {
Some(FlowStep::SellFlowStep(StepLabelsSell::StepSellerPayment))
}
Status::WaitingBuyerInvoice | Status::SettledHoldInvoice => {
Some(FlowStep::SellFlowStep(StepLabelsSell::StepBuyerInvoice))
}
Status::InProgress | Status::Active => {
Some(FlowStep::SellFlowStep(StepLabelsSell::StepChatActiveOrder))
}
Status::FiatSent => Some(FlowStep::SellFlowStep(StepLabelsSell::StepSendFiat)),
// On completed trades, keep the timeline in the final column even if the latest
// replayed action is an older pre-success DM (relay ordering / reboot hydration).
Status::Success => Some(FlowStep::SellFlowStep(StepLabelsSell::StepRate)),
Status::Canceled
| Status::CanceledByAdmin
| Status::CooperativelyCanceled
| Status::Expired
| Status::Dispute
| Status::SettledByAdmin
| Status::CompletedByAdmin => {
Some(FlowStep::SellFlowStep(StepLabelsSell::StepPendingOrder))
}
},
}
}
/// Sell listing: maker = seller (created the sell order), taker = buyer.
fn sell_listing_flow_step_from_action(action: &Action, is_maker: bool) -> FlowStep {
if is_maker {
match action {
Action::WaitingSellerToPay | Action::PayInvoice => {
FlowStep::SellFlowStep(StepLabelsSell::StepSellerPayment)
}
Action::AddInvoice | Action::WaitingBuyerInvoice => {
FlowStep::SellFlowStep(StepLabelsSell::StepBuyerInvoice)
}
Action::HoldInvoicePaymentAccepted => {
FlowStep::SellFlowStep(StepLabelsSell::StepChatActiveOrder)
}
Action::FiatSent => FlowStep::SellFlowStep(StepLabelsSell::StepSendFiat),
Action::FiatSentOk | Action::Release | Action::Released => {
FlowStep::SellFlowStep(StepLabelsSell::StepReleaseSats)
}
Action::Rate | Action::RateReceived => FlowStep::SellFlowStep(StepLabelsSell::StepRate),
Action::TakeBuy | Action::TakeSell => {
FlowStep::SellFlowStep(StepLabelsSell::StepChatActiveOrder)
}
_ => FlowStep::SellFlowStep(StepLabelsSell::StepChatActiveOrder),
}
} else {
match action {
Action::AddInvoice | Action::WaitingBuyerInvoice => {
FlowStep::SellFlowStep(StepLabelsSell::StepBuyerInvoice)
}
Action::PayInvoice | Action::WaitingSellerToPay => {
FlowStep::SellFlowStep(StepLabelsSell::StepSellerPayment)
}
Action::HoldInvoicePaymentAccepted => {
FlowStep::SellFlowStep(StepLabelsSell::StepChatActiveOrder)
}
Action::FiatSent => FlowStep::SellFlowStep(StepLabelsSell::StepSendFiat),
Action::FiatSentOk | Action::Release | Action::Released => {
FlowStep::SellFlowStep(StepLabelsSell::StepReleaseSats)
}
Action::Rate | Action::RateReceived => FlowStep::SellFlowStep(StepLabelsSell::StepRate),
Action::TakeBuy | Action::TakeSell => {
FlowStep::SellFlowStep(StepLabelsSell::StepChatActiveOrder)
}
_ => FlowStep::SellFlowStep(StepLabelsSell::StepChatActiveOrder),
}
}
}
fn buy_listing_flow_step_from_action(action: &Action, is_maker: bool) -> FlowStep {
if is_maker {
match action {
Action::WaitingSellerToPay => FlowStep::BuyFlowStep(StepLabelsBuy::StepSellerPayment),
Action::AddInvoice | Action::WaitingBuyerInvoice => {
FlowStep::BuyFlowStep(StepLabelsBuy::StepBuyerInvoice)
}
Action::PayInvoice => FlowStep::BuyFlowStep(StepLabelsBuy::StepSellerPayment),
Action::HoldInvoicePaymentAccepted => {
FlowStep::BuyFlowStep(StepLabelsBuy::StepChatActiveOrder)
}
Action::FiatSent => FlowStep::BuyFlowStep(StepLabelsBuy::StepSendFiat),
Action::FiatSentOk | Action::Release | Action::Released => {
FlowStep::BuyFlowStep(StepLabelsBuy::StepReleaseSats)
}
Action::Rate | Action::RateReceived => FlowStep::BuyFlowStep(StepLabelsBuy::StepRate),
Action::TakeBuy | Action::TakeSell => {
FlowStep::BuyFlowStep(StepLabelsBuy::StepChatActiveOrder)
}
_ => FlowStep::BuyFlowStep(StepLabelsBuy::StepChatActiveOrder),
}
} else {
match action {
Action::PayInvoice | Action::WaitingSellerToPay => {
FlowStep::BuyFlowStep(StepLabelsBuy::StepSellerPayment)
}
Action::HoldInvoicePaymentAccepted => {
FlowStep::BuyFlowStep(StepLabelsBuy::StepChatActiveOrder)
}
Action::WaitingBuyerInvoice | Action::AddInvoice => {
FlowStep::BuyFlowStep(StepLabelsBuy::StepBuyerInvoice)
}
Action::FiatSent => FlowStep::BuyFlowStep(StepLabelsBuy::StepSendFiat),
Action::FiatSentOk | Action::Release | Action::Released => {
FlowStep::BuyFlowStep(StepLabelsBuy::StepReleaseSats)
}
Action::Rate | Action::RateReceived => FlowStep::BuyFlowStep(StepLabelsBuy::StepRate),
Action::TakeBuy | Action::TakeSell => {
FlowStep::BuyFlowStep(StepLabelsBuy::StepChatActiveOrder)
}
_ => FlowStep::BuyFlowStep(StepLabelsBuy::StepChatActiveOrder),
}
}