spl_token_2022/extension/metadata_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_instruction::{AccountMeta, Instruction},
11 solana_program_error::ProgramError,
12 solana_pubkey::Pubkey,
13 spl_pod::optional_keys::OptionalNonZeroPubkey,
14 std::convert::TryInto,
15};
16
17#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
19#[cfg_attr(feature = "serde-traits", serde(rename_all = "camelCase"))]
20#[derive(Clone, Copy, Debug, PartialEq, IntoPrimitive, TryFromPrimitive)]
21#[repr(u8)]
22pub enum MetadataPointerInstruction {
23 Initialize,
39 Update,
56}
57
58#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
60#[cfg_attr(feature = "serde-traits", serde(rename_all = "camelCase"))]
61#[derive(Clone, Copy, Pod, Zeroable)]
62#[repr(C)]
63pub struct InitializeInstructionData {
64 pub authority: OptionalNonZeroPubkey,
66 pub metadata_address: OptionalNonZeroPubkey,
68}
69
70#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
72#[cfg_attr(feature = "serde-traits", serde(rename_all = "camelCase"))]
73#[derive(Clone, Copy, Pod, Zeroable)]
74#[repr(C)]
75pub struct UpdateInstructionData {
76 pub metadata_address: OptionalNonZeroPubkey,
78}
79
80pub fn initialize(
82 token_program_id: &Pubkey,
83 mint: &Pubkey,
84 authority: Option<Pubkey>,
85 metadata_address: Option<Pubkey>,
86) -> Result<Instruction, ProgramError> {
87 check_program_account(token_program_id)?;
88 let accounts = vec![AccountMeta::new(*mint, false)];
89 Ok(encode_instruction(
90 token_program_id,
91 accounts,
92 TokenInstruction::MetadataPointerExtension,
93 MetadataPointerInstruction::Initialize,
94 &InitializeInstructionData {
95 authority: authority.try_into()?,
96 metadata_address: metadata_address.try_into()?,
97 },
98 ))
99}
100
101pub fn update(
103 token_program_id: &Pubkey,
104 mint: &Pubkey,
105 authority: &Pubkey,
106 signers: &[&Pubkey],
107 metadata_address: Option<Pubkey>,
108) -> Result<Instruction, ProgramError> {
109 check_program_account(token_program_id)?;
110 let mut accounts = vec![
111 AccountMeta::new(*mint, false),
112 AccountMeta::new_readonly(*authority, signers.is_empty()),
113 ];
114 for signer_pubkey in signers.iter() {
115 accounts.push(AccountMeta::new_readonly(**signer_pubkey, true));
116 }
117 Ok(encode_instruction(
118 token_program_id,
119 accounts,
120 TokenInstruction::MetadataPointerExtension,
121 MetadataPointerInstruction::Update,
122 &UpdateInstructionData {
123 metadata_address: metadata_address.try_into()?,
124 },
125 ))
126}