use {
crate::{
check_program_account,
instruction::{encode_instruction, TokenInstruction},
},
alloc::vec,
bytemuck::{Pod, Zeroable},
num_enum::{IntoPrimitive, TryFromPrimitive},
solana_address::Address,
solana_instruction::{AccountMeta, Instruction},
solana_nullable::MaybeNull,
solana_program_error::ProgramError,
};
#[cfg(feature = "serde")]
use {
serde::{Deserialize, Serialize},
serde_with::{As, DisplayFromStr},
};
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[derive(Clone, Copy, Debug, PartialEq, IntoPrimitive, TryFromPrimitive)]
#[repr(u8)]
pub enum TransferHookInstruction {
Initialize,
Update,
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[derive(Clone, Copy, Pod, Zeroable)]
#[repr(C)]
pub struct InitializeInstructionData {
#[cfg_attr(feature = "serde", serde(with = "As::<Option<DisplayFromStr>>"))]
pub authority: MaybeNull<Address>,
#[cfg_attr(feature = "serde", serde(with = "As::<Option<DisplayFromStr>>"))]
pub program_id: MaybeNull<Address>,
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[derive(Clone, Copy, Pod, Zeroable)]
#[repr(C)]
pub struct UpdateInstructionData {
#[cfg_attr(feature = "serde", serde(with = "As::<Option<DisplayFromStr>>"))]
pub program_id: MaybeNull<Address>,
}
pub fn initialize(
token_program_id: &Address,
mint: &Address,
authority: Option<Address>,
transfer_hook_program_id: Option<Address>,
) -> Result<Instruction, ProgramError> {
check_program_account(token_program_id)?;
let accounts = vec![AccountMeta::new(*mint, false)];
Ok(encode_instruction(
token_program_id,
accounts,
TokenInstruction::TransferHookExtension,
TransferHookInstruction::Initialize,
&InitializeInstructionData {
authority: authority
.try_into()
.map_err(|_| ProgramError::InvalidArgument)?,
program_id: transfer_hook_program_id
.try_into()
.map_err(|_| ProgramError::InvalidArgument)?,
},
))
}
pub fn update(
token_program_id: &Address,
mint: &Address,
authority: &Address,
signers: &[&Address],
transfer_hook_program_id: Option<Address>,
) -> 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::TransferHookExtension,
TransferHookInstruction::Update,
&UpdateInstructionData {
program_id: transfer_hook_program_id
.try_into()
.map_err(|_| ProgramError::InvalidArgument)?,
},
))
}