solana_program/address_lookup_table/
instruction.rs

1use {
2    crate::{
3        address_lookup_table::program::id,
4        instruction::{AccountMeta, Instruction},
5        pubkey::Pubkey,
6        system_program,
7    },
8    serde_derive::{Deserialize, Serialize},
9    solana_clock::Slot,
10};
11
12#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
13pub enum ProgramInstruction {
14    /// Create an address lookup table
15    ///
16    /// # Account references
17    ///   0. `[WRITE]` Uninitialized address lookup table account
18    ///   1. `[SIGNER]` Account used to derive and control the new address lookup table.
19    ///   2. `[SIGNER, WRITE]` Account that will fund the new address lookup table.
20    ///   3. `[]` System program for CPI.
21    CreateLookupTable {
22        /// A recent slot must be used in the derivation path
23        /// for each initialized table. When closing table accounts,
24        /// the initialization slot must no longer be "recent" to prevent
25        /// address tables from being recreated with reordered or
26        /// otherwise malicious addresses.
27        recent_slot: Slot,
28        /// Address tables are always initialized at program-derived
29        /// addresses using the funding address, recent blockhash, and
30        /// the user-passed `bump_seed`.
31        bump_seed: u8,
32    },
33
34    /// Permanently freeze an address lookup table, making it immutable.
35    ///
36    /// # Account references
37    ///   0. `[WRITE]` Address lookup table account to freeze
38    ///   1. `[SIGNER]` Current authority
39    FreezeLookupTable,
40
41    /// Extend an address lookup table with new addresses. Funding account and
42    /// system program account references are only required if the lookup table
43    /// account requires additional lamports to cover the rent-exempt balance
44    /// after being extended.
45    ///
46    /// # Account references
47    ///   0. `[WRITE]` Address lookup table account to extend
48    ///   1. `[SIGNER]` Current authority
49    ///   2. `[SIGNER, WRITE, OPTIONAL]` Account that will fund the table reallocation
50    ///   3. `[OPTIONAL]` System program for CPI.
51    ExtendLookupTable { new_addresses: Vec<Pubkey> },
52
53    /// Deactivate an address lookup table, making it unusable and
54    /// eligible for closure after a short period of time.
55    ///
56    /// # Account references
57    ///   0. `[WRITE]` Address lookup table account to deactivate
58    ///   1. `[SIGNER]` Current authority
59    DeactivateLookupTable,
60
61    /// Close an address lookup table account
62    ///
63    /// # Account references
64    ///   0. `[WRITE]` Address lookup table account to close
65    ///   1. `[SIGNER]` Current authority
66    ///   2. `[WRITE]` Recipient of closed account lamports
67    CloseLookupTable,
68}
69
70/// Derives the address of an address table account from a wallet address and a recent block's slot.
71pub fn derive_lookup_table_address(
72    authority_address: &Pubkey,
73    recent_block_slot: Slot,
74) -> (Pubkey, u8) {
75    Pubkey::find_program_address(
76        &[authority_address.as_ref(), &recent_block_slot.to_le_bytes()],
77        &id(),
78    )
79}
80
81/// Constructs an instruction to create a table account and returns
82/// the instruction and the table account's derived address.
83fn create_lookup_table_common(
84    authority_address: Pubkey,
85    payer_address: Pubkey,
86    recent_slot: Slot,
87    authority_is_signer: bool,
88) -> (Instruction, Pubkey) {
89    let (lookup_table_address, bump_seed) =
90        derive_lookup_table_address(&authority_address, recent_slot);
91    let instruction = Instruction::new_with_bincode(
92        id(),
93        &ProgramInstruction::CreateLookupTable {
94            recent_slot,
95            bump_seed,
96        },
97        vec![
98            AccountMeta::new(lookup_table_address, false),
99            AccountMeta::new_readonly(authority_address, authority_is_signer),
100            AccountMeta::new(payer_address, true),
101            AccountMeta::new_readonly(system_program::id(), false),
102        ],
103    );
104
105    (instruction, lookup_table_address)
106}
107
108/// Constructs an instruction to create a table account and returns
109/// the instruction and the table account's derived address.
110///
111/// # Note
112///
113/// This instruction requires the authority to be a signer but
114/// in v1.12 the address lookup table program will no longer require
115/// the authority to sign the transaction.
116pub fn create_lookup_table_signed(
117    authority_address: Pubkey,
118    payer_address: Pubkey,
119    recent_slot: Slot,
120) -> (Instruction, Pubkey) {
121    create_lookup_table_common(authority_address, payer_address, recent_slot, true)
122}
123
124/// Constructs an instruction to create a table account and returns
125/// the instruction and the table account's derived address.
126///
127/// # Note
128///
129/// This instruction doesn't require the authority to be a signer but
130/// until v1.12 the address lookup table program still requires the
131/// authority to sign the transaction.
132pub fn create_lookup_table(
133    authority_address: Pubkey,
134    payer_address: Pubkey,
135    recent_slot: Slot,
136) -> (Instruction, Pubkey) {
137    create_lookup_table_common(authority_address, payer_address, recent_slot, false)
138}
139
140/// Constructs an instruction that freezes an address lookup
141/// table so that it can never be closed or extended again. Empty
142/// lookup tables cannot be frozen.
143pub fn freeze_lookup_table(lookup_table_address: Pubkey, authority_address: Pubkey) -> Instruction {
144    Instruction::new_with_bincode(
145        id(),
146        &ProgramInstruction::FreezeLookupTable,
147        vec![
148            AccountMeta::new(lookup_table_address, false),
149            AccountMeta::new_readonly(authority_address, true),
150        ],
151    )
152}
153
154/// Constructs an instruction which extends an address lookup
155/// table account with new addresses.
156pub fn extend_lookup_table(
157    lookup_table_address: Pubkey,
158    authority_address: Pubkey,
159    payer_address: Option<Pubkey>,
160    new_addresses: Vec<Pubkey>,
161) -> Instruction {
162    let mut accounts = vec![
163        AccountMeta::new(lookup_table_address, false),
164        AccountMeta::new_readonly(authority_address, true),
165    ];
166
167    if let Some(payer_address) = payer_address {
168        accounts.extend([
169            AccountMeta::new(payer_address, true),
170            AccountMeta::new_readonly(system_program::id(), false),
171        ]);
172    }
173
174    Instruction::new_with_bincode(
175        id(),
176        &ProgramInstruction::ExtendLookupTable { new_addresses },
177        accounts,
178    )
179}
180
181/// Constructs an instruction that deactivates an address lookup
182/// table so that it cannot be extended again and will be unusable
183/// and eligible for closure after a short amount of time.
184pub fn deactivate_lookup_table(
185    lookup_table_address: Pubkey,
186    authority_address: Pubkey,
187) -> Instruction {
188    Instruction::new_with_bincode(
189        id(),
190        &ProgramInstruction::DeactivateLookupTable,
191        vec![
192            AccountMeta::new(lookup_table_address, false),
193            AccountMeta::new_readonly(authority_address, true),
194        ],
195    )
196}
197
198/// Returns an instruction that closes an address lookup table
199/// account. The account will be deallocated and the lamports
200/// will be drained to the recipient address.
201pub fn close_lookup_table(
202    lookup_table_address: Pubkey,
203    authority_address: Pubkey,
204    recipient_address: Pubkey,
205) -> Instruction {
206    Instruction::new_with_bincode(
207        id(),
208        &ProgramInstruction::CloseLookupTable,
209        vec![
210            AccountMeta::new(lookup_table_address, false),
211            AccountMeta::new_readonly(authority_address, true),
212            AccountMeta::new(recipient_address, false),
213        ],
214    )
215}