solana_address_lookup_table_interface/
instruction.rs1#[cfg(feature = "serde")]
2use serde_derive::{Deserialize, Serialize};
3use {solana_clock::Slot, solana_pubkey::Pubkey, solana_sdk_ids::address_lookup_table::id};
4#[cfg(all(not(feature = "wincode"), feature = "bincode"))]
5use {
6 solana_instruction::{AccountMeta, Instruction},
7 solana_sdk_ids::system_program,
8};
9#[cfg(feature = "wincode")]
10use {
11 solana_instruction::{AccountMeta, Instruction},
12 solana_sdk_ids::system_program,
13 wincode::{SchemaRead, SchemaWrite},
14};
15
16#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
17#[cfg_attr(feature = "wincode", derive(SchemaRead, SchemaWrite))]
18#[derive(Debug, PartialEq, Eq, Clone)]
19pub enum ProgramInstruction {
20 CreateLookupTable {
28 recent_slot: Slot,
34 bump_seed: u8,
38 },
39
40 FreezeLookupTable,
46
47 ExtendLookupTable { new_addresses: Vec<Pubkey> },
58
59 DeactivateLookupTable,
66
67 CloseLookupTable,
74}
75
76pub fn derive_lookup_table_address(
78 authority_address: &Pubkey,
79 recent_block_slot: Slot,
80) -> (Pubkey, u8) {
81 Pubkey::find_program_address(
82 &[authority_address.as_ref(), &recent_block_slot.to_le_bytes()],
83 &id(),
84 )
85}
86
87#[cfg(any(feature = "wincode", feature = "bincode"))]
90fn create_lookup_table_common(
91 authority_address: Pubkey,
92 payer_address: Pubkey,
93 recent_slot: Slot,
94 authority_is_signer: bool,
95) -> (Instruction, Pubkey) {
96 let (lookup_table_address, bump_seed) =
97 derive_lookup_table_address(&authority_address, recent_slot);
98 #[cfg(all(not(feature = "wincode"), feature = "bincode"))]
99 let instruction = Instruction::new_with_bincode(
100 id(),
101 &ProgramInstruction::CreateLookupTable {
102 recent_slot,
103 bump_seed,
104 },
105 vec![
106 AccountMeta::new(lookup_table_address, false),
107 AccountMeta::new_readonly(authority_address, authority_is_signer),
108 AccountMeta::new(payer_address, true),
109 AccountMeta::new_readonly(system_program::id(), false),
110 ],
111 );
112 #[cfg(feature = "wincode")]
113 let instruction = Instruction::new_with_wincode(
114 id(),
115 &ProgramInstruction::CreateLookupTable {
116 recent_slot,
117 bump_seed,
118 },
119 vec![
120 AccountMeta::new(lookup_table_address, false),
121 AccountMeta::new_readonly(authority_address, authority_is_signer),
122 AccountMeta::new(payer_address, true),
123 AccountMeta::new_readonly(system_program::id(), false),
124 ],
125 );
126
127 (instruction, lookup_table_address)
128}
129
130#[cfg(any(feature = "wincode", feature = "bincode"))]
139pub fn create_lookup_table(
140 authority_address: Pubkey,
141 payer_address: Pubkey,
142 recent_slot: Slot,
143) -> (Instruction, Pubkey) {
144 create_lookup_table_common(authority_address, payer_address, recent_slot, false)
145}
146
147#[cfg(any(feature = "wincode", feature = "bincode"))]
151pub fn freeze_lookup_table(lookup_table_address: Pubkey, authority_address: Pubkey) -> Instruction {
152 #[cfg(all(not(feature = "wincode"), feature = "bincode"))]
153 let instruction = Instruction::new_with_bincode(
154 id(),
155 &ProgramInstruction::FreezeLookupTable,
156 vec![
157 AccountMeta::new(lookup_table_address, false),
158 AccountMeta::new_readonly(authority_address, true),
159 ],
160 );
161
162 #[cfg(feature = "wincode")]
163 let instruction = Instruction::new_with_wincode(
164 id(),
165 &ProgramInstruction::FreezeLookupTable,
166 vec![
167 AccountMeta::new(lookup_table_address, false),
168 AccountMeta::new_readonly(authority_address, true),
169 ],
170 );
171 instruction
172}
173
174#[cfg(any(feature = "wincode", feature = "bincode"))]
177pub fn extend_lookup_table(
178 lookup_table_address: Pubkey,
179 authority_address: Pubkey,
180 payer_address: Option<Pubkey>,
181 new_addresses: Vec<Pubkey>,
182) -> Instruction {
183 let mut accounts = vec![
184 AccountMeta::new(lookup_table_address, false),
185 AccountMeta::new_readonly(authority_address, true),
186 ];
187
188 if let Some(payer_address) = payer_address {
189 accounts.extend([
190 AccountMeta::new(payer_address, true),
191 AccountMeta::new_readonly(system_program::id(), false),
192 ]);
193 }
194
195 #[cfg(all(not(feature = "wincode"), feature = "bincode"))]
196 let instruction = Instruction::new_with_bincode(
197 id(),
198 &ProgramInstruction::ExtendLookupTable { new_addresses },
199 accounts,
200 );
201 #[cfg(feature = "wincode")]
202 let instruction = Instruction::new_with_wincode(
203 id(),
204 &ProgramInstruction::ExtendLookupTable { new_addresses },
205 accounts,
206 );
207 instruction
208}
209
210#[cfg(any(feature = "wincode", feature = "bincode"))]
214pub fn deactivate_lookup_table(
215 lookup_table_address: Pubkey,
216 authority_address: Pubkey,
217) -> Instruction {
218 #[cfg(all(not(feature = "wincode"), feature = "bincode"))]
219 let instruction = Instruction::new_with_bincode(
220 id(),
221 &ProgramInstruction::DeactivateLookupTable,
222 vec![
223 AccountMeta::new(lookup_table_address, false),
224 AccountMeta::new_readonly(authority_address, true),
225 ],
226 );
227 #[cfg(feature = "wincode")]
228 let instruction = Instruction::new_with_wincode(
229 id(),
230 &ProgramInstruction::DeactivateLookupTable,
231 vec![
232 AccountMeta::new(lookup_table_address, false),
233 AccountMeta::new_readonly(authority_address, true),
234 ],
235 );
236 instruction
237}
238
239#[cfg(any(feature = "wincode", feature = "bincode"))]
243pub fn close_lookup_table(
244 lookup_table_address: Pubkey,
245 authority_address: Pubkey,
246 recipient_address: Pubkey,
247) -> Instruction {
248 #[cfg(all(not(feature = "wincode"), feature = "bincode"))]
249 let instruction = Instruction::new_with_bincode(
250 id(),
251 &ProgramInstruction::CloseLookupTable,
252 vec![
253 AccountMeta::new(lookup_table_address, false),
254 AccountMeta::new_readonly(authority_address, true),
255 AccountMeta::new(recipient_address, false),
256 ],
257 );
258 #[cfg(feature = "wincode")]
259 let instruction = Instruction::new_with_wincode(
260 id(),
261 &ProgramInstruction::CloseLookupTable,
262 vec![
263 AccountMeta::new(lookup_table_address, false),
264 AccountMeta::new_readonly(authority_address, true),
265 AccountMeta::new(recipient_address, false),
266 ],
267 );
268 instruction
269}