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
use std::error::Error;
use crate::constants::FD;
use crate::instructions_data::dvl_instruction_data::DvlInstructionData;
use crate::instructions_data::fin_pool::{INSTRUCTION_FIN_POOL_VERSION, InstructionFinPool};
use crate::instructions_data::instructions::Instructions;

pub struct FinPoolParams {
    pub price: f64,
    pub terminate: bool,
}

impl<'a> DvlInstructionData<'a> for InstructionFinPool {
    type DvlInstrParams = FinPoolParams;

    fn new(params: Self::DvlInstrParams) -> Result<Box<InstructionFinPool>, Box<dyn Error>> {
        Ok(Box::new(InstructionFinPool {
            cmd: Instructions::FinPool as u8,
            version: INSTRUCTION_FIN_POOL_VERSION,
            reserved: [0; 5],
            price: (params.price * FD) as i64,
            terminate: params.terminate,
        }))
    }
}


#[cfg(test)]
mod tests {
    use super::*;
    use crate::instructions_data::constructors::fin_pool::FinPoolParams;
    use crate::instructions_data::dvl_instruction_data::DvlInstruction;

    #[test]
    fn test_instruction_fin_pool_params() {
        const TEST_PRICE: f64 = 10.;
        const TEST_TERMINATE: bool = true;

        let fin_pool_params = FinPoolParams {
            price: TEST_PRICE,
            terminate: TEST_TERMINATE,
        };
        let data = DvlInstruction::new::<InstructionFinPool>(fin_pool_params).unwrap();
        assert_eq!(data.cmd, Instructions::FinPool as u8);
        assert_eq!(data.version, INSTRUCTION_FIN_POOL_VERSION);
        assert_eq!(data.price as f64 / FD, TEST_PRICE);
        assert_eq!(data.terminate, TEST_TERMINATE);
    }
}