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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//! Orca protocol farming instructions
//! See https://github.com/orca-so/aquafarm-sdk/blob/9ed9db0f04cf7406f1f6e9a3e316639f3d24e68c/src/instructions.ts
//! for more details and accounts references

use {
    crate::pack::check_data_len,
    arrayref::{array_mut_ref, mut_array_refs},
    solana_program::program_error::ProgramError,
};

#[repr(u8)]
#[derive(Clone, Copy, Debug)]
pub enum OrcaInstructionType {
    InitGlobalFarm,
    InitUserFarm,
    ConvertTokens,
    RevertTokens,
    Harvest,
    RemoveRewards,
    SetEmissionsPerSecond,
}

#[derive(Clone, Copy, Debug)]
pub struct OrcaUserInit {}

#[derive(Clone, Copy, Debug)]
pub struct OrcaStake {
    pub amount: u64,
}

#[derive(Clone, Copy, Debug)]
pub struct OrcaUnstake {
    pub amount: u64,
}

#[derive(Clone, Copy, Debug)]
pub struct OrcaHarvest {}

impl OrcaUserInit {
    pub const LEN: usize = 1;

    pub fn get_size(&self) -> usize {
        Self::LEN
    }

    pub fn pack(&self, output: &mut [u8]) -> Result<usize, ProgramError> {
        check_data_len(output, OrcaUserInit::LEN)?;

        let output = array_mut_ref![output, 0, OrcaUserInit::LEN];
        output[0] = OrcaInstructionType::InitUserFarm as u8;

        Ok(Self::LEN)
    }

    pub fn to_vec(&self) -> Result<Vec<u8>, ProgramError> {
        let mut output: [u8; OrcaUserInit::LEN] = [0; OrcaUserInit::LEN];
        if let Ok(len) = self.pack(&mut output[..]) {
            Ok(output[..len].to_vec())
        } else {
            Err(ProgramError::InvalidInstructionData)
        }
    }
}

impl OrcaStake {
    pub const LEN: usize = 9;

    pub fn get_size(&self) -> usize {
        Self::LEN
    }

    pub fn pack(&self, output: &mut [u8]) -> Result<usize, ProgramError> {
        check_data_len(output, OrcaStake::LEN)?;

        let output = array_mut_ref![output, 0, OrcaStake::LEN];

        let (instruction_out, amount_out) = mut_array_refs![output, 1, 8];

        instruction_out[0] = OrcaInstructionType::ConvertTokens as u8;
        *amount_out = self.amount.to_le_bytes();

        Ok(Self::LEN)
    }

    pub fn to_vec(&self) -> Result<Vec<u8>, ProgramError> {
        let mut output: [u8; OrcaStake::LEN] = [0; OrcaStake::LEN];
        if let Ok(len) = self.pack(&mut output[..]) {
            Ok(output[..len].to_vec())
        } else {
            Err(ProgramError::InvalidInstructionData)
        }
    }
}

impl OrcaUnstake {
    pub const LEN: usize = 9;

    pub fn get_size(&self) -> usize {
        Self::LEN
    }

    pub fn pack(&self, output: &mut [u8]) -> Result<usize, ProgramError> {
        check_data_len(output, OrcaUnstake::LEN)?;

        let output = array_mut_ref![output, 0, OrcaUnstake::LEN];

        let (instruction_out, amount_out) = mut_array_refs![output, 1, 8];

        instruction_out[0] = OrcaInstructionType::RevertTokens as u8;
        *amount_out = self.amount.to_le_bytes();

        Ok(Self::LEN)
    }

    pub fn to_vec(&self) -> Result<Vec<u8>, ProgramError> {
        let mut output: [u8; OrcaUnstake::LEN] = [0; OrcaUnstake::LEN];
        if let Ok(len) = self.pack(&mut output[..]) {
            Ok(output[..len].to_vec())
        } else {
            Err(ProgramError::InvalidInstructionData)
        }
    }
}

impl OrcaHarvest {
    pub const LEN: usize = 1;

    pub fn get_size(&self) -> usize {
        Self::LEN
    }

    pub fn pack(&self, output: &mut [u8]) -> Result<usize, ProgramError> {
        check_data_len(output, OrcaHarvest::LEN)?;

        let output = array_mut_ref![output, 0, OrcaHarvest::LEN];
        output[0] = OrcaInstructionType::Harvest as u8;

        Ok(Self::LEN)
    }

    pub fn to_vec(&self) -> Result<Vec<u8>, ProgramError> {
        let mut output: [u8; OrcaHarvest::LEN] = [0; OrcaHarvest::LEN];
        if let Ok(len) = self.pack(&mut output[..]) {
            Ok(output[..len].to_vec())
        } else {
            Err(ProgramError::InvalidInstructionData)
        }
    }
}