spl_token_2022/extension/group_pointer/
instruction.rs1#[cfg(feature = "serde-traits")]
2use serde::{Deserialize, Serialize};
3use {
4 crate::{
5 check_program_account,
6 instruction::{encode_instruction, TokenInstruction},
7 },
8 bytemuck::{Pod, Zeroable},
9 num_enum::{IntoPrimitive, TryFromPrimitive},
10 solana_program::{
11 instruction::{AccountMeta, Instruction},
12 program_error::ProgramError,
13 pubkey::Pubkey,
14 },
15 spl_pod::optional_keys::OptionalNonZeroPubkey,
16 std::convert::TryInto,
17};
18
19#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
21#[cfg_attr(feature = "serde-traits", serde(rename_all = "camelCase"))]
22#[derive(Clone, Copy, Debug, PartialEq, IntoPrimitive, TryFromPrimitive)]
23#[repr(u8)]
24pub enum GroupPointerInstruction {
25 Initialize,
41 Update,
58}
59
60#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
62#[cfg_attr(feature = "serde-traits", serde(rename_all = "camelCase"))]
63#[derive(Clone, Copy, Pod, Zeroable)]
64#[repr(C)]
65pub struct InitializeInstructionData {
66 pub authority: OptionalNonZeroPubkey,
68 pub group_address: OptionalNonZeroPubkey,
70}
71
72#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
74#[cfg_attr(feature = "serde-traits", serde(rename_all = "camelCase"))]
75#[derive(Clone, Copy, Pod, Zeroable)]
76#[repr(C)]
77pub struct UpdateInstructionData {
78 pub group_address: OptionalNonZeroPubkey,
80}
81
82pub fn initialize(
84 token_program_id: &Pubkey,
85 mint: &Pubkey,
86 authority: Option<Pubkey>,
87 group_address: Option<Pubkey>,
88) -> Result<Instruction, ProgramError> {
89 check_program_account(token_program_id)?;
90 let accounts = vec![AccountMeta::new(*mint, false)];
91 Ok(encode_instruction(
92 token_program_id,
93 accounts,
94 TokenInstruction::GroupPointerExtension,
95 GroupPointerInstruction::Initialize,
96 &InitializeInstructionData {
97 authority: authority.try_into()?,
98 group_address: group_address.try_into()?,
99 },
100 ))
101}
102
103pub fn update(
105 token_program_id: &Pubkey,
106 mint: &Pubkey,
107 authority: &Pubkey,
108 signers: &[&Pubkey],
109 group_address: Option<Pubkey>,
110) -> Result<Instruction, ProgramError> {
111 check_program_account(token_program_id)?;
112 let mut accounts = vec![
113 AccountMeta::new(*mint, false),
114 AccountMeta::new_readonly(*authority, signers.is_empty()),
115 ];
116 for signer_pubkey in signers.iter() {
117 accounts.push(AccountMeta::new_readonly(**signer_pubkey, true));
118 }
119 Ok(encode_instruction(
120 token_program_id,
121 accounts,
122 TokenInstruction::GroupPointerExtension,
123 GroupPointerInstruction::Update,
124 &UpdateInstructionData {
125 group_address: group_address.try_into()?,
126 },
127 ))
128}