spl_token_2022/extension/metadata_pointer/
instruction.rs#[cfg(feature = "serde-traits")]
use serde::{Deserialize, Serialize};
use {
crate::{
check_program_account,
instruction::{encode_instruction, TokenInstruction},
},
bytemuck::{Pod, Zeroable},
num_enum::{IntoPrimitive, TryFromPrimitive},
solana_program::{
instruction::{AccountMeta, Instruction},
program_error::ProgramError,
pubkey::Pubkey,
},
spl_pod::optional_keys::OptionalNonZeroPubkey,
std::convert::TryInto,
};
#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde-traits", serde(rename_all = "camelCase"))]
#[derive(Clone, Copy, Debug, PartialEq, IntoPrimitive, TryFromPrimitive)]
#[repr(u8)]
pub enum MetadataPointerInstruction {
Initialize,
Update,
}
#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde-traits", serde(rename_all = "camelCase"))]
#[derive(Clone, Copy, Pod, Zeroable)]
#[repr(C)]
pub struct InitializeInstructionData {
pub authority: OptionalNonZeroPubkey,
pub metadata_address: OptionalNonZeroPubkey,
}
#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde-traits", serde(rename_all = "camelCase"))]
#[derive(Clone, Copy, Pod, Zeroable)]
#[repr(C)]
pub struct UpdateInstructionData {
pub metadata_address: OptionalNonZeroPubkey,
}
pub fn initialize(
token_program_id: &Pubkey,
mint: &Pubkey,
authority: Option<Pubkey>,
metadata_address: Option<Pubkey>,
) -> Result<Instruction, ProgramError> {
check_program_account(token_program_id)?;
let accounts = vec![AccountMeta::new(*mint, false)];
Ok(encode_instruction(
token_program_id,
accounts,
TokenInstruction::MetadataPointerExtension,
MetadataPointerInstruction::Initialize,
&InitializeInstructionData {
authority: authority.try_into()?,
metadata_address: metadata_address.try_into()?,
},
))
}
pub fn update(
token_program_id: &Pubkey,
mint: &Pubkey,
authority: &Pubkey,
signers: &[&Pubkey],
metadata_address: Option<Pubkey>,
) -> Result<Instruction, ProgramError> {
check_program_account(token_program_id)?;
let mut accounts = vec![
AccountMeta::new(*mint, false),
AccountMeta::new_readonly(*authority, signers.is_empty()),
];
for signer_pubkey in signers.iter() {
accounts.push(AccountMeta::new_readonly(**signer_pubkey, true));
}
Ok(encode_instruction(
token_program_id,
accounts,
TokenInstruction::MetadataPointerExtension,
MetadataPointerInstruction::Update,
&UpdateInstructionData {
metadata_address: metadata_address.try_into()?,
},
))
}