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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//! Instruction types

use {
    bytemuck::{Pod, Zeroable},
    solana_program::{
        instruction::{AccountMeta, Instruction},
        program_error::ProgramError,
        pubkey::Pubkey,
    },
    spl_discriminator::{ArrayDiscriminator, SplDiscriminate},
    spl_pod::{
        bytemuck::{pod_bytes_of, pod_from_bytes},
        optional_keys::OptionalNonZeroPubkey,
        primitives::PodU32,
    },
};

/// Instruction data for initializing a new `Group`
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable, SplDiscriminate)]
#[discriminator_hash_input("spl_token_group_interface:initialize_token_group")]
pub struct InitializeGroup {
    /// Update authority for the group
    pub update_authority: OptionalNonZeroPubkey,
    /// The maximum number of group members
    pub max_size: PodU32,
}

/// Instruction data for updating the max size of a `Group`
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable, SplDiscriminate)]
#[discriminator_hash_input("spl_token_group_interface:update_group_max_size")]
pub struct UpdateGroupMaxSize {
    /// New max size for the group
    pub max_size: PodU32,
}

/// Instruction data for updating the authority of a `Group`
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable, SplDiscriminate)]
#[discriminator_hash_input("spl_token_group_interface:update_authority")]
pub struct UpdateGroupAuthority {
    /// New authority for the group, or unset if `None`
    pub new_authority: OptionalNonZeroPubkey,
}

/// Instruction data for initializing a new `Member` of a `Group`
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable, SplDiscriminate)]
#[discriminator_hash_input("spl_token_group_interface:initialize_member")]
pub struct InitializeMember;

/// All instructions that must be implemented in the SPL Token Group Interface
#[derive(Clone, Debug, PartialEq)]
pub enum TokenGroupInstruction {
    /// Initialize a new `Group`
    ///
    /// Assumes one has already initialized a mint for the
    /// group.
    ///
    /// Accounts expected by this instruction:
    ///
    ///   0. `[w]`  Group
    ///   1. `[]`   Mint
    ///   2. `[s]`  Mint authority
    InitializeGroup(InitializeGroup),

    /// Update the max size of a `Group`
    ///
    /// Accounts expected by this instruction:
    ///
    ///   0. `[w]`  Group
    ///   1. `[s]`  Update authority
    UpdateGroupMaxSize(UpdateGroupMaxSize),

    /// Update the authority of a `Group`
    ///
    /// Accounts expected by this instruction:
    ///
    ///   0. `[w]`  Group
    ///   1. `[s]`  Current update authority
    UpdateGroupAuthority(UpdateGroupAuthority),

    /// Initialize a new `Member` of a `Group`
    ///
    /// Assumes the `Group` has already been initialized,
    /// as well as the mint for the member.
    ///
    /// Accounts expected by this instruction:
    ///
    ///   0. `[w]`  Member
    ///   1. `[]`   Member mint
    ///   1. `[s]`  Member mint authority
    ///   2. `[w]`  Group
    ///   3. `[s]`  Group update authority
    InitializeMember(InitializeMember),
}
impl TokenGroupInstruction {
    /// Unpacks a byte buffer into a `TokenGroupInstruction`
    pub fn unpack(input: &[u8]) -> Result<Self, ProgramError> {
        if input.len() < ArrayDiscriminator::LENGTH {
            return Err(ProgramError::InvalidInstructionData);
        }
        let (discriminator, rest) = input.split_at(ArrayDiscriminator::LENGTH);
        Ok(match discriminator {
            InitializeGroup::SPL_DISCRIMINATOR_SLICE => {
                let data = pod_from_bytes::<InitializeGroup>(rest)?;
                Self::InitializeGroup(*data)
            }
            UpdateGroupMaxSize::SPL_DISCRIMINATOR_SLICE => {
                let data = pod_from_bytes::<UpdateGroupMaxSize>(rest)?;
                Self::UpdateGroupMaxSize(*data)
            }
            UpdateGroupAuthority::SPL_DISCRIMINATOR_SLICE => {
                let data = pod_from_bytes::<UpdateGroupAuthority>(rest)?;
                Self::UpdateGroupAuthority(*data)
            }
            InitializeMember::SPL_DISCRIMINATOR_SLICE => {
                let data = pod_from_bytes::<InitializeMember>(rest)?;
                Self::InitializeMember(*data)
            }
            _ => return Err(ProgramError::InvalidInstructionData),
        })
    }

    /// Packs a `TokenGroupInstruction` into a byte buffer.
    pub fn pack(&self) -> Vec<u8> {
        let mut buf = vec![];
        match self {
            Self::InitializeGroup(data) => {
                buf.extend_from_slice(InitializeGroup::SPL_DISCRIMINATOR_SLICE);
                buf.extend_from_slice(pod_bytes_of(data));
            }
            Self::UpdateGroupMaxSize(data) => {
                buf.extend_from_slice(UpdateGroupMaxSize::SPL_DISCRIMINATOR_SLICE);
                buf.extend_from_slice(pod_bytes_of(data));
            }
            Self::UpdateGroupAuthority(data) => {
                buf.extend_from_slice(UpdateGroupAuthority::SPL_DISCRIMINATOR_SLICE);
                buf.extend_from_slice(pod_bytes_of(data));
            }
            Self::InitializeMember(data) => {
                buf.extend_from_slice(InitializeMember::SPL_DISCRIMINATOR_SLICE);
                buf.extend_from_slice(pod_bytes_of(data));
            }
        };
        buf
    }
}

/// Creates a `InitializeGroup` instruction
pub fn initialize_group(
    program_id: &Pubkey,
    group: &Pubkey,
    mint: &Pubkey,
    mint_authority: &Pubkey,
    update_authority: Option<Pubkey>,
    max_size: u32,
) -> Instruction {
    let update_authority = OptionalNonZeroPubkey::try_from(update_authority)
        .expect("Failed to deserialize `Option<Pubkey>`");
    let data = TokenGroupInstruction::InitializeGroup(InitializeGroup {
        update_authority,
        max_size: max_size.into(),
    })
    .pack();
    Instruction {
        program_id: *program_id,
        accounts: vec![
            AccountMeta::new(*group, false),
            AccountMeta::new_readonly(*mint, false),
            AccountMeta::new_readonly(*mint_authority, true),
        ],
        data,
    }
}

/// Creates a `UpdateGroupMaxSize` instruction
pub fn update_group_max_size(
    program_id: &Pubkey,
    group: &Pubkey,
    update_authority: &Pubkey,
    max_size: u32,
) -> Instruction {
    let data = TokenGroupInstruction::UpdateGroupMaxSize(UpdateGroupMaxSize {
        max_size: max_size.into(),
    })
    .pack();
    Instruction {
        program_id: *program_id,
        accounts: vec![
            AccountMeta::new(*group, false),
            AccountMeta::new_readonly(*update_authority, true),
        ],
        data,
    }
}

/// Creates a `UpdateGroupAuthority` instruction
pub fn update_group_authority(
    program_id: &Pubkey,
    group: &Pubkey,
    current_authority: &Pubkey,
    new_authority: Option<Pubkey>,
) -> Instruction {
    let new_authority = OptionalNonZeroPubkey::try_from(new_authority)
        .expect("Failed to deserialize `Option<Pubkey>`");
    let data =
        TokenGroupInstruction::UpdateGroupAuthority(UpdateGroupAuthority { new_authority }).pack();
    Instruction {
        program_id: *program_id,
        accounts: vec![
            AccountMeta::new(*group, false),
            AccountMeta::new_readonly(*current_authority, true),
        ],
        data,
    }
}

/// Creates a `InitializeMember` instruction
#[allow(clippy::too_many_arguments)]
pub fn initialize_member(
    program_id: &Pubkey,
    member: &Pubkey,
    member_mint: &Pubkey,
    member_mint_authority: &Pubkey,
    group: &Pubkey,
    group_update_authority: &Pubkey,
) -> Instruction {
    let data = TokenGroupInstruction::InitializeMember(InitializeMember {}).pack();
    Instruction {
        program_id: *program_id,
        accounts: vec![
            AccountMeta::new(*member, false),
            AccountMeta::new_readonly(*member_mint, false),
            AccountMeta::new_readonly(*member_mint_authority, true),
            AccountMeta::new(*group, false),
            AccountMeta::new_readonly(*group_update_authority, true),
        ],
        data,
    }
}

#[cfg(test)]
mod test {
    use {super::*, crate::NAMESPACE, solana_program::hash};

    #[repr(C)]
    #[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable, SplDiscriminate)]
    #[discriminator_hash_input("mock_group")]
    struct MockGroup;

    fn instruction_pack_unpack<I>(instruction: TokenGroupInstruction, discriminator: &[u8], data: I)
    where
        I: core::fmt::Debug + PartialEq + Pod + Zeroable + SplDiscriminate,
    {
        let mut expect = vec![];
        expect.extend_from_slice(discriminator.as_ref());
        expect.extend_from_slice(pod_bytes_of(&data));
        let packed = instruction.pack();
        assert_eq!(packed, expect);
        let unpacked = TokenGroupInstruction::unpack(&expect).unwrap();
        assert_eq!(unpacked, instruction);
    }

    #[test]
    fn initialize_group_pack() {
        let data = InitializeGroup {
            update_authority: OptionalNonZeroPubkey::default(),
            max_size: 100.into(),
        };
        let instruction = TokenGroupInstruction::InitializeGroup(data);
        let preimage = hash::hashv(&[format!("{NAMESPACE}:initialize_token_group").as_bytes()]);
        let discriminator = &preimage.as_ref()[..ArrayDiscriminator::LENGTH];
        instruction_pack_unpack::<InitializeGroup>(instruction, discriminator, data);
    }

    #[test]
    fn update_group_max_size_pack() {
        let data = UpdateGroupMaxSize {
            max_size: 200.into(),
        };
        let instruction = TokenGroupInstruction::UpdateGroupMaxSize(data);
        let preimage = hash::hashv(&[format!("{NAMESPACE}:update_group_max_size").as_bytes()]);
        let discriminator = &preimage.as_ref()[..ArrayDiscriminator::LENGTH];
        instruction_pack_unpack::<UpdateGroupMaxSize>(instruction, discriminator, data);
    }

    #[test]
    fn update_authority_pack() {
        let data = UpdateGroupAuthority {
            new_authority: OptionalNonZeroPubkey::default(),
        };
        let instruction = TokenGroupInstruction::UpdateGroupAuthority(data);
        let preimage = hash::hashv(&[format!("{NAMESPACE}:update_authority").as_bytes()]);
        let discriminator = &preimage.as_ref()[..ArrayDiscriminator::LENGTH];
        instruction_pack_unpack::<UpdateGroupAuthority>(instruction, discriminator, data);
    }

    #[test]
    fn initialize_member_pack() {
        let data = InitializeMember {};
        let instruction = TokenGroupInstruction::InitializeMember(data);
        let preimage = hash::hashv(&[format!("{NAMESPACE}:initialize_member").as_bytes()]);
        let discriminator = &preimage.as_ref()[..ArrayDiscriminator::LENGTH];
        instruction_pack_unpack::<InitializeMember>(instruction, discriminator, data);
    }
}