#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use {
crate::{
check_program_account,
extension::interest_bearing_mint::BasisPoints,
instruction::{encode_instruction, TokenInstruction},
},
bytemuck::{Pod, Zeroable},
num_enum::{IntoPrimitive, TryFromPrimitive},
trezoa_instruction::{AccountMeta, Instruction},
trezoa_program_error::ProgramError,
trezoa_pubkey::Pubkey,
tpl_pod::optional_keys::OptionalNonZeroPubkey,
std::convert::TryInto,
};
#[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 {
pub rate_authority: OptionalNonZeroPubkey,
pub rate: BasisPoints,
}
pub fn initialize(
token_program_id: &Pubkey,
mint: &Pubkey,
rate_authority: Option<Pubkey>,
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()?,
rate: rate.into(),
},
))
}
pub fn update_rate(
token_program_id: &Pubkey,
mint: &Pubkey,
rate_authority: &Pubkey,
signers: &[&Pubkey],
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),
))
}