|
1 | 1 | use { |
2 | | - crate::instructions::{extensions::ExtensionDiscriminator, MAX_MULTISIG_SIGNERS}, |
| 2 | + crate::{ |
| 3 | + instructions::{extensions::ExtensionDiscriminator, MAX_MULTISIG_SIGNERS}, |
| 4 | + state::AccountState, |
| 5 | + }, |
3 | 6 | core::{mem::MaybeUninit, slice}, |
4 | 7 | solana_account_view::AccountView, |
5 | 8 | solana_address::Address, |
@@ -31,101 +34,110 @@ pub struct Update<'a, 'b, 'c> { |
31 | 34 | pub freeze_authority: &'a AccountView, |
32 | 35 |
|
33 | 36 | /// The signer accounts if the authority is a multisig. |
34 | | - pub signers: &'c [&'a AccountView], |
| 37 | + pub multisig_signers: &'c [&'a AccountView], |
35 | 38 |
|
36 | 39 | /// The new account state in which new token accounts should be |
37 | 40 | /// initialized. |
38 | | - pub state: u8, |
| 41 | + pub state: AccountState, |
39 | 42 |
|
40 | 43 | /// The token program. |
41 | 44 | pub token_program: &'b Address, |
42 | 45 | } |
43 | 46 |
|
44 | | -impl Update<'_, '_, '_> { |
| 47 | +impl<'a, 'b, 'c> Update<'a, 'b, 'c> { |
45 | 48 | pub const DISCRIMINATOR: u8 = 1; |
46 | 49 |
|
| 50 | + /// Creates a new `Update` instruction with a single owner/delegate |
| 51 | + /// authority. |
| 52 | + #[inline(always)] |
| 53 | + pub fn new( |
| 54 | + token_program: &'b Address, |
| 55 | + mint: &'a AccountView, |
| 56 | + freeze_authority: &'a AccountView, |
| 57 | + state: AccountState, |
| 58 | + ) -> Self { |
| 59 | + Self::with_multisig_signers(token_program, mint, freeze_authority, state, &[]) |
| 60 | + } |
| 61 | + |
| 62 | + /// Creates a new `Update` instruction with a multisignature owner/delegate |
| 63 | + /// authority and signer accounts. |
| 64 | + #[inline(always)] |
| 65 | + pub fn with_multisig_signers( |
| 66 | + token_program: &'b Address, |
| 67 | + mint: &'a AccountView, |
| 68 | + freeze_authority: &'a AccountView, |
| 69 | + state: AccountState, |
| 70 | + multisig_signers: &'c [&'a AccountView], |
| 71 | + ) -> Self { |
| 72 | + Self { |
| 73 | + mint, |
| 74 | + freeze_authority, |
| 75 | + multisig_signers, |
| 76 | + state, |
| 77 | + token_program, |
| 78 | + } |
| 79 | + } |
| 80 | + |
47 | 81 | #[inline(always)] |
48 | 82 | pub fn invoke(&self) -> ProgramResult { |
49 | 83 | self.invoke_signed(&[]) |
50 | 84 | } |
51 | 85 |
|
52 | 86 | #[inline(always)] |
53 | 87 | pub fn invoke_signed(&self, signers: &[Signer]) -> ProgramResult { |
54 | | - if self.signers.len() > MAX_MULTISIG_SIGNERS { |
| 88 | + if self.multisig_signers.len() > MAX_MULTISIG_SIGNERS { |
55 | 89 | return Err(ProgramError::InvalidArgument); |
56 | 90 | } |
57 | 91 |
|
58 | | - let expected_accounts = 2 + self.signers.len(); |
| 92 | + let expected_accounts = 2 + self.multisig_signers.len(); |
59 | 93 |
|
60 | 94 | // Instruction accounts. |
61 | 95 |
|
62 | | - const UNINIT_INSTRUCTION_ACCOUNTS: MaybeUninit<InstructionAccount> = |
63 | | - MaybeUninit::<InstructionAccount>::uninit(); |
64 | | - let mut instruction_accounts = [UNINIT_INSTRUCTION_ACCOUNTS; 2 + MAX_MULTISIG_SIGNERS]; |
65 | | - |
66 | | - // SAFETY: The allocation is valid to the maximum number of accounts. |
67 | | - unsafe { |
68 | | - // mint |
69 | | - instruction_accounts |
70 | | - .get_unchecked_mut(0) |
71 | | - .write(InstructionAccount::writable(self.mint.address())); |
72 | | - |
73 | | - // freeze_authority |
74 | | - instruction_accounts |
75 | | - .get_unchecked_mut(1) |
76 | | - .write(InstructionAccount::new( |
77 | | - self.freeze_authority.address(), |
78 | | - false, |
79 | | - self.signers.is_empty(), |
80 | | - )); |
81 | | - |
82 | | - // signer accounts |
83 | | - for (account, signer) in instruction_accounts |
84 | | - .get_unchecked_mut(2..) |
85 | | - .iter_mut() |
86 | | - .zip(self.signers.iter()) |
87 | | - { |
88 | | - account.write(InstructionAccount::readonly_signer(signer.address())); |
89 | | - } |
90 | | - } |
| 96 | + let mut instruction_accounts = |
| 97 | + [const { MaybeUninit::<InstructionAccount>::uninit() }; 2 + MAX_MULTISIG_SIGNERS]; |
91 | 98 |
|
92 | | - let instruction = InstructionView { |
93 | | - program_id: self.token_program, |
94 | | - data: &[ |
95 | | - ExtensionDiscriminator::DefaultAccountState as u8, |
96 | | - Self::DISCRIMINATOR, |
97 | | - self.state, |
98 | | - ], |
99 | | - accounts: unsafe { |
100 | | - slice::from_raw_parts(instruction_accounts.as_ptr() as _, expected_accounts) |
101 | | - }, |
102 | | - }; |
| 99 | + instruction_accounts[0].write(InstructionAccount::writable(self.mint.address())); |
| 100 | + |
| 101 | + instruction_accounts[1].write(InstructionAccount::new( |
| 102 | + self.freeze_authority.address(), |
| 103 | + false, |
| 104 | + self.multisig_signers.is_empty(), |
| 105 | + )); |
| 106 | + |
| 107 | + for (account, signer) in instruction_accounts[2..] |
| 108 | + .iter_mut() |
| 109 | + .zip(self.multisig_signers.iter()) |
| 110 | + { |
| 111 | + account.write(InstructionAccount::readonly_signer(signer.address())); |
| 112 | + } |
103 | 113 |
|
104 | 114 | // Accounts. |
105 | 115 |
|
106 | | - const UNINIT_ACCOUNT_VIEWS: MaybeUninit<&AccountView> = MaybeUninit::uninit(); |
107 | | - let mut accounts = [UNINIT_ACCOUNT_VIEWS; 2 + MAX_MULTISIG_SIGNERS]; |
108 | | - |
109 | | - // SAFETY: The allocation is valid to the maximum number of accounts. |
110 | | - unsafe { |
111 | | - // mint |
112 | | - accounts.get_unchecked_mut(0).write(self.mint); |
113 | | - |
114 | | - // freeze_authority |
115 | | - accounts.get_unchecked_mut(1).write(self.freeze_authority); |
116 | | - |
117 | | - // signer accounts |
118 | | - for (account, signer) in accounts |
119 | | - .get_unchecked_mut(2..) |
120 | | - .iter_mut() |
121 | | - .zip(self.signers.iter()) |
122 | | - { |
123 | | - account.write(signer); |
124 | | - } |
| 116 | + let mut accounts = |
| 117 | + [const { MaybeUninit::<&AccountView>::uninit() }; 2 + MAX_MULTISIG_SIGNERS]; |
| 118 | + |
| 119 | + accounts[0].write(self.mint); |
| 120 | + |
| 121 | + accounts[1].write(self.freeze_authority); |
| 122 | + |
| 123 | + for (account, signer) in accounts[2..].iter_mut().zip(self.multisig_signers.iter()) { |
| 124 | + account.write(signer); |
125 | 125 | } |
126 | 126 |
|
127 | 127 | invoke_signed_with_bounds::<{ 2 + MAX_MULTISIG_SIGNERS }>( |
128 | | - &instruction, |
| 128 | + &InstructionView { |
| 129 | + program_id: self.token_program, |
| 130 | + // SAFETY: instruction accounts has `expected_accounts` initialized. |
| 131 | + accounts: unsafe { |
| 132 | + slice::from_raw_parts(instruction_accounts.as_ptr() as _, expected_accounts) |
| 133 | + }, |
| 134 | + data: &[ |
| 135 | + ExtensionDiscriminator::DefaultAccountState as u8, |
| 136 | + Self::DISCRIMINATOR, |
| 137 | + self.state as u8, |
| 138 | + ], |
| 139 | + }, |
| 140 | + // SAFETY: accounts has `expected_accounts` initialized. |
129 | 141 | unsafe { slice::from_raw_parts(accounts.as_ptr() as _, expected_accounts) }, |
130 | 142 | signers, |
131 | 143 | ) |
|
0 commit comments