spl_token_2022/extension/interest_bearing_mint/
instruction.rs

1#[cfg(feature = "serde-traits")]
2use serde::{Deserialize, Serialize};
3use {
4    crate::{
5        check_program_account,
6        extension::interest_bearing_mint::BasisPoints,
7        instruction::{encode_instruction, TokenInstruction},
8    },
9    bytemuck::{Pod, Zeroable},
10    num_enum::{IntoPrimitive, TryFromPrimitive},
11    solana_instruction::{AccountMeta, Instruction},
12    solana_program_error::ProgramError,
13    solana_pubkey::Pubkey,
14    spl_pod::optional_keys::OptionalNonZeroPubkey,
15    std::convert::TryInto,
16};
17
18/// Interesting-bearing mint extension instructions
19#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
20#[cfg_attr(feature = "serde-traits", serde(rename_all = "camelCase"))]
21#[derive(Clone, Copy, Debug, PartialEq, IntoPrimitive, TryFromPrimitive)]
22#[repr(u8)]
23pub enum InterestBearingMintInstruction {
24    /// Initialize a new mint with interest accrual.
25    ///
26    /// Fails if the mint has already been initialized, so must be called before
27    /// `InitializeMint`.
28    ///
29    /// The mint must have exactly enough space allocated for the base mint (82
30    /// bytes), plus 83 bytes of padding, 1 byte reserved for the account type,
31    /// then space required for this extension, plus any others.
32    ///
33    /// Accounts expected by this instruction:
34    ///
35    ///   0. `[writable]` The mint to initialize.
36    ///
37    /// Data expected by this instruction:
38    ///   `crate::extension::interest_bearing::instruction::InitializeInstructionData`
39    Initialize,
40    /// Update the interest rate. Only supported for mints that include the
41    /// `InterestBearingConfig` extension.
42    ///
43    /// Accounts expected by this instruction:
44    ///
45    ///   * Single authority
46    ///   0. `[writable]` The mint.
47    ///   1. `[signer]` The mint rate authority.
48    ///
49    ///   * Multisignature authority
50    ///   0. `[writable]` The mint.
51    ///   1. `[]` The mint's multisignature rate authority.
52    ///   2. `..2+M` `[signer]` M signer accounts.
53    ///
54    /// Data expected by this instruction:
55    ///   `crate::extension::interest_bearing::BasisPoints`
56    UpdateRate,
57}
58
59/// Data expected by `InterestBearing::Initialize`
60#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
61#[cfg_attr(feature = "serde-traits", serde(rename_all = "camelCase"))]
62#[derive(Clone, Copy, Pod, Zeroable)]
63#[repr(C)]
64pub struct InitializeInstructionData {
65    /// The public key for the account that can update the rate
66    pub rate_authority: OptionalNonZeroPubkey,
67    /// The initial interest rate
68    pub rate: BasisPoints,
69}
70
71/// Create an `Initialize` instruction
72pub fn initialize(
73    token_program_id: &Pubkey,
74    mint: &Pubkey,
75    rate_authority: Option<Pubkey>,
76    rate: i16,
77) -> Result<Instruction, ProgramError> {
78    check_program_account(token_program_id)?;
79    let accounts = vec![AccountMeta::new(*mint, false)];
80    Ok(encode_instruction(
81        token_program_id,
82        accounts,
83        TokenInstruction::InterestBearingMintExtension,
84        InterestBearingMintInstruction::Initialize,
85        &InitializeInstructionData {
86            rate_authority: rate_authority.try_into()?,
87            rate: rate.into(),
88        },
89    ))
90}
91
92/// Create an `UpdateRate` instruction
93pub fn update_rate(
94    token_program_id: &Pubkey,
95    mint: &Pubkey,
96    rate_authority: &Pubkey,
97    signers: &[&Pubkey],
98    rate: i16,
99) -> Result<Instruction, ProgramError> {
100    check_program_account(token_program_id)?;
101    let mut accounts = vec![
102        AccountMeta::new(*mint, false),
103        AccountMeta::new_readonly(*rate_authority, signers.is_empty()),
104    ];
105    for signer_pubkey in signers.iter() {
106        accounts.push(AccountMeta::new_readonly(**signer_pubkey, true));
107    }
108    Ok(encode_instruction(
109        token_program_id,
110        accounts,
111        TokenInstruction::InterestBearingMintExtension,
112        InterestBearingMintInstruction::UpdateRate,
113        &BasisPoints::from(rate),
114    ))
115}