devol_accounts_kit/instructions_data/constructors/
option_trade_debug.rs1use std::error::Error;
2use crate::instructions_data::constructors::option_trade::OptionTradeParams;
3use crate::instructions_data::dvl_instruction_data::{DvlInstructionData, DvlInstruction};
4use crate::instructions_data::instructions::Instructions;
5use crate::instructions_data::option_trade::{InstructionOptionTrade};
6use crate::instructions_data::option_trade_debug::{InstructionOptionTradeDebug, OptionTradeDebugParams};
7
8impl<'a> DvlInstructionData<'a> for InstructionOptionTradeDebug {
9 type DvlInstrParams = OptionTradeDebugParams<'a>;
10
11 fn new(params: Self::DvlInstrParams) -> Result<Box<Self>, Box<dyn Error>> where Self: Sized {
12 let trade_params = OptionTradeParams {
13 basket: params.basket,
14 max_cost: params.max_cost,
15 trade_qty: params.trade_qty,
16 };
17 let mut option_trade = DvlInstruction::new::<InstructionOptionTrade>(trade_params)?;
18 option_trade.cmd = Instructions::OptionTradeDebug as u8;
19 Ok(Box::new(InstructionOptionTradeDebug {
20 option_trade: *option_trade,
21 underlying_price: params.underlying_price,
22 time_to_expiration: params.time_to_expiration,
23 }))
24 }
25}
26
27#[cfg(test)]
28impl Default for InstructionOptionTradeDebug {
29 fn default() -> Self {
30 InstructionOptionTradeDebug {
31 option_trade: InstructionOptionTrade::default(),
32 time_to_expiration: 0,
33 underlying_price: 0,
34 }
35 }
36}
37
38#[cfg(test)]
39mod tests {
40 use super::*;
41 use crate::constants::BUCKETS_COUNT;
42 use crate::instructions_data::option_trade_debug::INSTRUCTION_OPTION_TRADE_DEBUG_DATA_SIZE;
43
44 #[test]
45 fn test_default_instruction_option_trade_debug() {
46 let debug_trade = InstructionOptionTradeDebug::default();
47
48 assert_eq!(debug_trade.time_to_expiration, 0, "The default value for time_to_expiration should be zero.");
49 assert_eq!(debug_trade.underlying_price, 0, "The default value for underlying_price should be zero.");
50 }
51
52 #[test]
53 fn test_specific_instruction_option_trade_debug() {
54 let test_max_cost = 200;
55 let test_trade_qty = [100; BUCKETS_COUNT];
56 let test_time_to_expiration = 3600;
57 let test_underlying_price = 50000;
58
59 let params = OptionTradeDebugParams {
60 trade_qty: test_trade_qty,
61 max_cost: Some(test_max_cost),
62 basket: None,
63 time_to_expiration: test_time_to_expiration,
64 underlying_price: test_underlying_price,
65 };
66
67 let debug_trade_result = InstructionOptionTradeDebug::new(params);
68 assert!(debug_trade_result.is_ok());
69
70 let debug_trade = debug_trade_result.unwrap();
71 assert_eq!(debug_trade.time_to_expiration, test_time_to_expiration);
72 assert_eq!(debug_trade.underlying_price, test_underlying_price);
73 assert_eq!(debug_trade.option_trade.cmd, Instructions::OptionTradeDebug as u8);
74 assert_eq!(debug_trade.option_trade.max_cost, test_max_cost);
75 assert_eq!(debug_trade.option_trade.trade_qty, test_trade_qty);
76 }
77
78 #[test]
79 fn test_as_vec_le_instruction_option_trade_debug() {
80 let trade_params = OptionTradeDebugParams {
81 trade_qty: [0; BUCKETS_COUNT],
82 basket: None,
83 max_cost: None,
84 time_to_expiration: 0,
85 underlying_price: 0,
86 };
87 let data = DvlInstruction::new::<InstructionOptionTradeDebug>(trade_params).unwrap();
88 let buf = data.to_vec_le();
89 assert_eq!(buf.len(), INSTRUCTION_OPTION_TRADE_DEBUG_DATA_SIZE);
90 }
91
92}