spl_token_2022/extension/scaled_ui_amount/
instruction.rs

1#[cfg(feature = "serde-traits")]
2use serde::{Deserialize, Serialize};
3use {
4    crate::{
5        check_program_account,
6        extension::scaled_ui_amount::{PodF64, UnixTimestamp},
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 ScaledUiAmountMintInstruction {
24    /// Initialize a new mint with scaled UI amounts.
25    ///
26    /// Fails if the mint has already been initialized, so must be called before
27    /// `InitializeMint`.
28    ///
29    /// Fails if the multiplier is less than or equal to 0 or if it's
30    /// [subnormal](https://en.wikipedia.org/wiki/Subnormal_number).
31    ///
32    /// The mint must have exactly enough space allocated for the base mint (82
33    /// bytes), plus 83 bytes of padding, 1 byte reserved for the account type,
34    /// then space required for this extension, plus any others.
35    ///
36    /// Accounts expected by this instruction:
37    ///
38    ///   0. `[writable]` The mint to initialize.
39    ///
40    /// Data expected by this instruction:
41    ///   `crate::extension::scaled_ui_amount::instruction::InitializeInstructionData`
42    Initialize,
43    /// Update the multiplier. Only supported for mints that include the
44    /// `ScaledUiAmount` extension.
45    ///
46    /// Fails if the multiplier is less than or equal to 0 or if it's
47    /// [subnormal](https://en.wikipedia.org/wiki/Subnormal_number).
48    ///
49    /// The authority provides a new multiplier and a UNIX timestamp on which
50    /// it should take effect. If the timestamp is before the current time,
51    /// immediately sets the multiplier.
52    ///
53    /// Accounts expected by this instruction:
54    ///
55    ///   * Single authority
56    ///   0. `[writable]` The mint.
57    ///   1. `[signer]` The multiplier authority.
58    ///
59    ///   * Multisignature authority
60    ///   0. `[writable]` The mint.
61    ///   1. `[]` The mint's multisignature multiplier authority.
62    ///   2. `..2+M` `[signer]` M signer accounts.
63    ///
64    /// Data expected by this instruction:
65    ///   `crate::extension::scaled_ui_amount::instruction::UpdateMultiplierInstructionData`
66    UpdateMultiplier,
67}
68
69/// Data expected by `ScaledUiAmountMint::Initialize`
70#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
71#[cfg_attr(feature = "serde-traits", serde(rename_all = "camelCase"))]
72#[derive(Clone, Copy, Pod, Zeroable)]
73#[repr(C)]
74pub struct InitializeInstructionData {
75    /// The public key for the account that can update the multiplier
76    pub authority: OptionalNonZeroPubkey,
77    /// The initial multiplier
78    pub multiplier: PodF64,
79}
80
81/// Data expected by `ScaledUiAmountMint::UpdateMultiplier`
82#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
83#[cfg_attr(feature = "serde-traits", serde(rename_all = "camelCase"))]
84#[derive(Clone, Copy, Pod, Zeroable)]
85#[repr(C)]
86pub struct UpdateMultiplierInstructionData {
87    /// The new multiplier
88    pub multiplier: PodF64,
89    /// Timestamp at which the new multiplier will take effect
90    pub effective_timestamp: UnixTimestamp,
91}
92
93/// Create an `Initialize` instruction
94pub fn initialize(
95    token_program_id: &Pubkey,
96    mint: &Pubkey,
97    authority: Option<Pubkey>,
98    multiplier: f64,
99) -> Result<Instruction, ProgramError> {
100    check_program_account(token_program_id)?;
101    let accounts = vec![AccountMeta::new(*mint, false)];
102    Ok(encode_instruction(
103        token_program_id,
104        accounts,
105        TokenInstruction::ScaledUiAmountExtension,
106        ScaledUiAmountMintInstruction::Initialize,
107        &InitializeInstructionData {
108            authority: authority.try_into()?,
109            multiplier: multiplier.into(),
110        },
111    ))
112}
113
114/// Create an `UpdateMultiplier` instruction
115pub fn update_multiplier(
116    token_program_id: &Pubkey,
117    mint: &Pubkey,
118    authority: &Pubkey,
119    signers: &[&Pubkey],
120    multiplier: f64,
121    effective_timestamp: i64,
122) -> Result<Instruction, ProgramError> {
123    check_program_account(token_program_id)?;
124    let mut accounts = vec![
125        AccountMeta::new(*mint, false),
126        AccountMeta::new_readonly(*authority, signers.is_empty()),
127    ];
128    for signer_pubkey in signers.iter() {
129        accounts.push(AccountMeta::new_readonly(**signer_pubkey, true));
130    }
131    Ok(encode_instruction(
132        token_program_id,
133        accounts,
134        TokenInstruction::ScaledUiAmountExtension,
135        ScaledUiAmountMintInstruction::UpdateMultiplier,
136        &UpdateMultiplierInstructionData {
137            effective_timestamp: effective_timestamp.into(),
138            multiplier: multiplier.into(),
139        },
140    ))
141}