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
use std::error::Error;
use crate::instructions_data::dvl_instruction_data::DvlInstructionData;
use crate::instructions_data::instructions::Instructions;
use crate::instructions_data::withdraw_token::{INSTRUCTION_WITHDRAW_TOKEN_VERSION, InstructionWithdrawToken};

pub struct WithdrawTokenParams {
    pub mint_id: u32,
    pub amount: u64,
}

impl<'a> DvlInstructionData<'a> for InstructionWithdrawToken {

    type DvlInstrParams = WithdrawTokenParams;

    fn new(params: Self::DvlInstrParams) -> Result<Box<InstructionWithdrawToken>, Box<dyn Error>> {
        Ok(Box::new(InstructionWithdrawToken {
            cmd: Instructions::WithdrawToken as u8,
            version: INSTRUCTION_WITHDRAW_TOKEN_VERSION,
            reserved: [0; 2],
            mint_id: params.mint_id,
            amount: params.amount,
        }))
    }
}

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

    #[test]
    fn test_instruction_withdraw_token_params() {
        const TEST_MINT_ID: u32 = 1;
        const TEST_AMOUNT: u64 = 2;

        let withdraw_token_params = WithdrawTokenParams {
            mint_id: TEST_MINT_ID,
            amount: TEST_AMOUNT,
        };
        let data = DvlInstruction::new::<InstructionWithdrawToken>(withdraw_token_params).unwrap();
        assert_eq!(data.cmd, Instructions::WithdrawToken as u8);
        assert_eq!(data.version, INSTRUCTION_WITHDRAW_TOKEN_VERSION);
        assert_eq!(data.mint_id, TEST_MINT_ID);
        assert_eq!(data.amount, TEST_AMOUNT);
    }
}