Skip to main content

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