cripsy_binary_api/domain/
payout.rs1use serde::{Deserialize, Serialize};
4
5use super::operation::OptionType;
6
7#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
8pub struct Payout {
9 pub symbol: String,
10 pub expiration_seconds: u64,
11 pub profit_percent: f64,
13 pub is_spot: bool,
15 pub strike: Option<f64>,
16 pub closed: bool,
18}
19
20#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
26pub struct PayoutQuote {
27 pub symbol: String,
28 pub option_type: OptionType,
29 pub expiration_seconds: u64,
30 pub profit_percent: Option<f64>,
32 pub min_amount: Option<f64>,
33 pub max_amount: Option<f64>,
34 pub available: bool,
36 pub captured_at: i64,
38}
39
40impl PayoutQuote {
41 pub fn unavailable(
43 symbol: impl Into<String>,
44 option_type: OptionType,
45 expiration_seconds: u64,
46 ) -> Self {
47 Self {
48 symbol: symbol.into(),
49 option_type,
50 expiration_seconds,
51 profit_percent: None,
52 min_amount: None,
53 max_amount: None,
54 available: false,
55 captured_at: 0,
56 }
57 }
58}
59
60#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
61pub struct StrikeQuote {
62 pub strike: f64,
63 pub call_instrument_id: Option<String>,
65 pub put_instrument_id: Option<String>,
66 pub call_profit_percent: Option<f64>,
67 pub put_profit_percent: Option<f64>,
68}
69
70#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
71pub struct StrikeList {
72 pub symbol: String,
73 pub duration_minutes: u64,
74 pub quotes: Vec<StrikeQuote>,
75 pub captured_at: i64,
77}
78
79impl StrikeList {
80 pub fn nearest(&self, price: f64) -> Option<&StrikeQuote> {
82 self.quotes.iter().min_by(|a, b| {
83 (a.strike - price)
84 .abs()
85 .partial_cmp(&(b.strike - price).abs())
86 .unwrap_or(std::cmp::Ordering::Equal)
87 })
88 }
89}
90
91#[cfg(test)]
92mod tests {
93 use super::*;
94
95 fn quote(strike: f64) -> StrikeQuote {
96 StrikeQuote {
97 strike,
98 call_instrument_id: None,
99 put_instrument_id: None,
100 call_profit_percent: None,
101 put_profit_percent: None,
102 }
103 }
104
105 #[test]
106 fn nearest_encuentra_el_mas_cercano() {
107 let list = StrikeList {
108 symbol: "EURUSD".into(),
109 duration_minutes: 1,
110 quotes: vec![quote(1.10), quote(1.25), quote(1.30)],
111 captured_at: 0,
112 };
113 let q = list.nearest(1.24).unwrap();
114 assert_eq!(q.strike, 1.25);
115 }
116
117 #[test]
118 fn nearest_vacio_es_none() {
119 let list = StrikeList {
120 symbol: "EURUSD".into(),
121 duration_minutes: 1,
122 quotes: vec![],
123 captured_at: 0,
124 };
125 assert!(list.nearest(1.0).is_none());
126 }
127
128 #[test]
129 fn payout_no_disponible_no_fabrica_porcentaje() {
130 let quote = PayoutQuote::unavailable("EURUSD", OptionType::Binary, 60);
131 assert!(!quote.available);
132 assert_eq!(quote.profit_percent, None);
133 assert_eq!(quote.min_amount, None);
134 assert_eq!(quote.max_amount, None);
135 assert_eq!(quote.captured_at, 0);
136
137 let json = serde_json::to_string("e).unwrap();
138 assert!(json.contains("\"available\":false"));
139 assert!(json.contains("\"profit_percent\":null"));
140 }
141}