spl_token_2022/extension/memo_transfer/
instruction.rs

1#[cfg(feature = "serde-traits")]
2use serde::{Deserialize, Serialize};
3use {
4    crate::{
5        check_program_account,
6        instruction::{encode_instruction, TokenInstruction},
7    },
8    num_enum::{IntoPrimitive, TryFromPrimitive},
9    solana_program::{
10        instruction::{AccountMeta, Instruction},
11        program_error::ProgramError,
12        pubkey::Pubkey,
13    },
14};
15
16/// Required Memo Transfers extension instructions
17#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
18#[cfg_attr(feature = "serde-traits", serde(rename_all = "camelCase"))]
19#[derive(Clone, Copy, Debug, PartialEq, IntoPrimitive, TryFromPrimitive)]
20#[repr(u8)]
21pub enum RequiredMemoTransfersInstruction {
22    /// Require memos for transfers into this Account. Adds the `MemoTransfer`
23    /// extension to the Account, if it doesn't already exist.
24    ///
25    /// Accounts expected by this instruction:
26    ///
27    ///   0. `[writable]` The account to update.
28    ///   1. `[signer]` The account's owner.
29    ///
30    ///   * Multisignature authority
31    ///   0. `[writable]` The account to update.
32    ///   1. `[]` The account's multisignature owner.
33    ///   2. `..2+M` `[signer]` M signer accounts.
34    Enable,
35    /// Stop requiring memos for transfers into this Account.
36    ///
37    /// Implicitly initializes the extension in the case where it is not
38    /// present.
39    ///
40    /// Accounts expected by this instruction:
41    ///
42    ///   0. `[writable]` The account to update.
43    ///   1. `[signer]` The account's owner.
44    ///
45    ///   * Multisignature authority
46    ///   0. `[writable]` The account to update.
47    ///   1. `[]`  The account's multisignature owner.
48    ///   2. `..2+M` `[signer]` M signer accounts.
49    Disable,
50}
51
52/// Create an `Enable` instruction
53pub fn enable_required_transfer_memos(
54    token_program_id: &Pubkey,
55    account: &Pubkey,
56    owner: &Pubkey,
57    signers: &[&Pubkey],
58) -> Result<Instruction, ProgramError> {
59    check_program_account(token_program_id)?;
60    let mut accounts = vec![
61        AccountMeta::new(*account, false),
62        AccountMeta::new_readonly(*owner, signers.is_empty()),
63    ];
64    for signer_pubkey in signers.iter() {
65        accounts.push(AccountMeta::new_readonly(**signer_pubkey, true));
66    }
67    Ok(encode_instruction(
68        token_program_id,
69        accounts,
70        TokenInstruction::MemoTransferExtension,
71        RequiredMemoTransfersInstruction::Enable,
72        &(),
73    ))
74}
75
76/// Create a `Disable` instruction
77pub fn disable_required_transfer_memos(
78    token_program_id: &Pubkey,
79    account: &Pubkey,
80    owner: &Pubkey,
81    signers: &[&Pubkey],
82) -> Result<Instruction, ProgramError> {
83    check_program_account(token_program_id)?;
84    let mut accounts = vec![
85        AccountMeta::new(*account, false),
86        AccountMeta::new_readonly(*owner, signers.is_empty()),
87    ];
88    for signer_pubkey in signers.iter() {
89        accounts.push(AccountMeta::new_readonly(**signer_pubkey, true));
90    }
91    Ok(encode_instruction(
92        token_program_id,
93        accounts,
94        TokenInstruction::MemoTransferExtension,
95        RequiredMemoTransfersInstruction::Disable,
96        &(),
97    ))
98}