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
use std::error::Error;
use crate::instructions_data::dvl_instruction_data::DvlInstructionData;
use crate::instructions_data::instructions::Instructions;
use crate::instructions_data::payoff::InstructionPayoff;

pub struct PayoffParams {
    pub worker_id: u32,
    pub pool_id: u32,
}

impl<'a> DvlInstructionData<'a> for InstructionPayoff {
    type DvlInstrParams = PayoffParams;

    fn new(params: Self::DvlInstrParams) -> Result<Box<InstructionPayoff>, Box<dyn Error>> {
        Ok(Box::new(InstructionPayoff {
            cmd: Instructions::Payoff as u8,
            version: INSTRUCTION_VERSION,
            reserved: [0; 2],
            worker_id: params.worker_id,
            pool_id: params.pool_id,
        }))
    }
}

const INSTRUCTION_VERSION: u8 = 1;

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

    #[test]
    fn test_instruction_lp_trade_params() {
        const TEST_WORKER_ID: u32 = 1;
        const TEST_POOL_ID: u32 = 1;

        let payoff_params = PayoffParams {
            worker_id: TEST_WORKER_ID,
            pool_id: TEST_POOL_ID,
        };
        let data = DvlInstruction::new::<InstructionPayoff>(payoff_params).unwrap();
        assert_eq!(data.cmd, Instructions::Payoff as u8);
        assert_eq!(data.version, INSTRUCTION_VERSION);
        assert_eq!(data.worker_id, TEST_WORKER_ID);
        assert_eq!(data.pool_id, TEST_POOL_ID);
    }
}