use {
crate::{
check_program_account,
extension::interest_bearing_mint::BasisPoints,
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 InterestBearingMintInstruction {
Initialize,
UpdateRate,
}
#[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 rate_authority: MaybeNull<Address>,
pub rate: BasisPoints,
}
pub fn initialize(
token_program_id: &Address,
mint: &Address,
rate_authority: Option<Address>,
rate: i16,
) -> Result<Instruction, ProgramError> {
check_program_account(token_program_id)?;
let accounts = vec![AccountMeta::new(*mint, false)];
Ok(encode_instruction(
token_program_id,
accounts,
TokenInstruction::InterestBearingMintExtension,
InterestBearingMintInstruction::Initialize,
&InitializeInstructionData {
rate_authority: rate_authority
.try_into()
.map_err(|_| ProgramError::InvalidArgument)?,
rate: rate.into(),
},
))
}
pub fn update_rate(
token_program_id: &Address,
mint: &Address,
rate_authority: &Address,
signers: &[&Address],
rate: i16,
) -> Result<Instruction, ProgramError> {
check_program_account(token_program_id)?;
let mut accounts = vec![
AccountMeta::new(*mint, false),
AccountMeta::new_readonly(*rate_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::InterestBearingMintExtension,
InterestBearingMintInstruction::UpdateRate,
&BasisPoints::from(rate),
))
}