nifty_asset_interface/generated/instructions/
write.rs1use borsh::BorshDeserialize;
9use borsh::BorshSerialize;
10
11pub struct Write {
13 pub asset: solana_program::pubkey::Pubkey,
15 pub payer: solana_program::pubkey::Pubkey,
17 pub system_program: solana_program::pubkey::Pubkey,
19}
20
21impl Write {
22 pub fn instruction(
23 &self,
24 args: WriteInstructionArgs,
25 ) -> solana_program::instruction::Instruction {
26 self.instruction_with_remaining_accounts(args, &[])
27 }
28 #[allow(clippy::vec_init_then_push)]
29 pub fn instruction_with_remaining_accounts(
30 &self,
31 args: WriteInstructionArgs,
32 remaining_accounts: &[solana_program::instruction::AccountMeta],
33 ) -> solana_program::instruction::Instruction {
34 let mut accounts = Vec::with_capacity(3 + remaining_accounts.len());
35 accounts.push(solana_program::instruction::AccountMeta::new(
36 self.asset, true,
37 ));
38 accounts.push(solana_program::instruction::AccountMeta::new(
39 self.payer, true,
40 ));
41 accounts.push(solana_program::instruction::AccountMeta::new_readonly(
42 self.system_program,
43 false,
44 ));
45 accounts.extend_from_slice(remaining_accounts);
46 let mut data = WriteInstructionData::new().try_to_vec().unwrap();
47 let mut args = args.try_to_vec().unwrap();
48 data.append(&mut args);
49
50 solana_program::instruction::Instruction {
51 program_id: crate::INTERFACE_ID,
52 accounts,
53 data,
54 }
55 }
56}
57
58#[derive(BorshDeserialize, BorshSerialize)]
59struct WriteInstructionData {
60 discriminator: u8,
61}
62
63impl WriteInstructionData {
64 fn new() -> Self {
65 Self { discriminator: 12 }
66 }
67}
68
69#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
70#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
71pub struct WriteInstructionArgs {
72 pub overwrite: bool,
73 pub bytes: Vec<u8>,
74}
75
76#[derive(Default)]
84pub struct WriteBuilder {
85 asset: Option<solana_program::pubkey::Pubkey>,
86 payer: Option<solana_program::pubkey::Pubkey>,
87 system_program: Option<solana_program::pubkey::Pubkey>,
88 overwrite: Option<bool>,
89 bytes: Option<Vec<u8>>,
90 __remaining_accounts: Vec<solana_program::instruction::AccountMeta>,
91}
92
93impl WriteBuilder {
94 pub fn new() -> Self {
95 Self::default()
96 }
97 #[inline(always)]
99 pub fn asset(&mut self, asset: solana_program::pubkey::Pubkey) -> &mut Self {
100 self.asset = Some(asset);
101 self
102 }
103 #[inline(always)]
105 pub fn payer(&mut self, payer: solana_program::pubkey::Pubkey) -> &mut Self {
106 self.payer = Some(payer);
107 self
108 }
109 #[inline(always)]
112 pub fn system_program(&mut self, system_program: solana_program::pubkey::Pubkey) -> &mut Self {
113 self.system_program = Some(system_program);
114 self
115 }
116 #[inline(always)]
117 pub fn overwrite(&mut self, overwrite: bool) -> &mut Self {
118 self.overwrite = Some(overwrite);
119 self
120 }
121 #[inline(always)]
122 pub fn bytes(&mut self, bytes: Vec<u8>) -> &mut Self {
123 self.bytes = Some(bytes);
124 self
125 }
126 #[inline(always)]
128 pub fn add_remaining_account(
129 &mut self,
130 account: solana_program::instruction::AccountMeta,
131 ) -> &mut Self {
132 self.__remaining_accounts.push(account);
133 self
134 }
135 #[inline(always)]
137 pub fn add_remaining_accounts(
138 &mut self,
139 accounts: &[solana_program::instruction::AccountMeta],
140 ) -> &mut Self {
141 self.__remaining_accounts.extend_from_slice(accounts);
142 self
143 }
144 #[allow(clippy::clone_on_copy)]
145 pub fn instruction(&self) -> solana_program::instruction::Instruction {
146 let accounts = Write {
147 asset: self.asset.expect("asset is not set"),
148 payer: self.payer.expect("payer is not set"),
149 system_program: self
150 .system_program
151 .unwrap_or(solana_program::pubkey!("11111111111111111111111111111111")),
152 };
153 let args = WriteInstructionArgs {
154 overwrite: self.overwrite.clone().expect("overwrite is not set"),
155 bytes: self.bytes.clone().expect("bytes is not set"),
156 };
157
158 accounts.instruction_with_remaining_accounts(args, &self.__remaining_accounts)
159 }
160}
161
162pub struct WriteCpiAccounts<'a, 'b> {
164 pub asset: &'b solana_program::account_info::AccountInfo<'a>,
166 pub payer: &'b solana_program::account_info::AccountInfo<'a>,
168 pub system_program: &'b solana_program::account_info::AccountInfo<'a>,
170}
171
172pub struct WriteCpi<'a, 'b> {
174 pub __program: &'b solana_program::account_info::AccountInfo<'a>,
176 pub asset: &'b solana_program::account_info::AccountInfo<'a>,
178 pub payer: &'b solana_program::account_info::AccountInfo<'a>,
180 pub system_program: &'b solana_program::account_info::AccountInfo<'a>,
182 pub __args: WriteInstructionArgs,
184}
185
186impl<'a, 'b> WriteCpi<'a, 'b> {
187 pub fn new(
188 program: &'b solana_program::account_info::AccountInfo<'a>,
189 accounts: WriteCpiAccounts<'a, 'b>,
190 args: WriteInstructionArgs,
191 ) -> Self {
192 Self {
193 __program: program,
194 asset: accounts.asset,
195 payer: accounts.payer,
196 system_program: accounts.system_program,
197 __args: args,
198 }
199 }
200 #[inline(always)]
201 pub fn invoke(&self) -> solana_program::entrypoint::ProgramResult {
202 self.invoke_signed_with_remaining_accounts(&[], &[])
203 }
204 #[inline(always)]
205 pub fn invoke_with_remaining_accounts(
206 &self,
207 remaining_accounts: &[(
208 &'b solana_program::account_info::AccountInfo<'a>,
209 bool,
210 bool,
211 )],
212 ) -> solana_program::entrypoint::ProgramResult {
213 self.invoke_signed_with_remaining_accounts(&[], remaining_accounts)
214 }
215 #[inline(always)]
216 pub fn invoke_signed(
217 &self,
218 signers_seeds: &[&[&[u8]]],
219 ) -> solana_program::entrypoint::ProgramResult {
220 self.invoke_signed_with_remaining_accounts(signers_seeds, &[])
221 }
222 #[allow(clippy::clone_on_copy)]
223 #[allow(clippy::vec_init_then_push)]
224 pub fn invoke_signed_with_remaining_accounts(
225 &self,
226 signers_seeds: &[&[&[u8]]],
227 remaining_accounts: &[(
228 &'b solana_program::account_info::AccountInfo<'a>,
229 bool,
230 bool,
231 )],
232 ) -> solana_program::entrypoint::ProgramResult {
233 let mut accounts = Vec::with_capacity(3 + remaining_accounts.len());
234 accounts.push(solana_program::instruction::AccountMeta::new(
235 *self.asset.key,
236 true,
237 ));
238 accounts.push(solana_program::instruction::AccountMeta::new(
239 *self.payer.key,
240 true,
241 ));
242 accounts.push(solana_program::instruction::AccountMeta::new_readonly(
243 *self.system_program.key,
244 false,
245 ));
246 remaining_accounts.iter().for_each(|remaining_account| {
247 accounts.push(solana_program::instruction::AccountMeta {
248 pubkey: *remaining_account.0.key,
249 is_signer: remaining_account.1,
250 is_writable: remaining_account.2,
251 })
252 });
253 let mut data = WriteInstructionData::new().try_to_vec().unwrap();
254 let mut args = self.__args.try_to_vec().unwrap();
255 data.append(&mut args);
256
257 let instruction = solana_program::instruction::Instruction {
258 program_id: crate::INTERFACE_ID,
259 accounts,
260 data,
261 };
262 let mut account_infos = Vec::with_capacity(3 + 1 + remaining_accounts.len());
263 account_infos.push(self.__program.clone());
264 account_infos.push(self.asset.clone());
265 account_infos.push(self.payer.clone());
266 account_infos.push(self.system_program.clone());
267 remaining_accounts
268 .iter()
269 .for_each(|remaining_account| account_infos.push(remaining_account.0.clone()));
270
271 if signers_seeds.is_empty() {
272 solana_program::program::invoke(&instruction, &account_infos)
273 } else {
274 solana_program::program::invoke_signed(&instruction, &account_infos, signers_seeds)
275 }
276 }
277}
278
279pub struct WriteCpiBuilder<'a, 'b> {
287 instruction: Box<WriteCpiBuilderInstruction<'a, 'b>>,
288}
289
290impl<'a, 'b> WriteCpiBuilder<'a, 'b> {
291 pub fn new(program: &'b solana_program::account_info::AccountInfo<'a>) -> Self {
292 let instruction = Box::new(WriteCpiBuilderInstruction {
293 __program: program,
294 asset: None,
295 payer: None,
296 system_program: None,
297 overwrite: None,
298 bytes: None,
299 __remaining_accounts: Vec::new(),
300 });
301 Self { instruction }
302 }
303 #[inline(always)]
305 pub fn asset(&mut self, asset: &'b solana_program::account_info::AccountInfo<'a>) -> &mut Self {
306 self.instruction.asset = Some(asset);
307 self
308 }
309 #[inline(always)]
311 pub fn payer(&mut self, payer: &'b solana_program::account_info::AccountInfo<'a>) -> &mut Self {
312 self.instruction.payer = Some(payer);
313 self
314 }
315 #[inline(always)]
317 pub fn system_program(
318 &mut self,
319 system_program: &'b solana_program::account_info::AccountInfo<'a>,
320 ) -> &mut Self {
321 self.instruction.system_program = Some(system_program);
322 self
323 }
324 #[inline(always)]
325 pub fn overwrite(&mut self, overwrite: bool) -> &mut Self {
326 self.instruction.overwrite = Some(overwrite);
327 self
328 }
329 #[inline(always)]
330 pub fn bytes(&mut self, bytes: Vec<u8>) -> &mut Self {
331 self.instruction.bytes = Some(bytes);
332 self
333 }
334 #[inline(always)]
336 pub fn add_remaining_account(
337 &mut self,
338 account: &'b solana_program::account_info::AccountInfo<'a>,
339 is_writable: bool,
340 is_signer: bool,
341 ) -> &mut Self {
342 self.instruction
343 .__remaining_accounts
344 .push((account, is_writable, is_signer));
345 self
346 }
347 #[inline(always)]
352 pub fn add_remaining_accounts(
353 &mut self,
354 accounts: &[(
355 &'b solana_program::account_info::AccountInfo<'a>,
356 bool,
357 bool,
358 )],
359 ) -> &mut Self {
360 self.instruction
361 .__remaining_accounts
362 .extend_from_slice(accounts);
363 self
364 }
365 #[inline(always)]
366 pub fn invoke(&self) -> solana_program::entrypoint::ProgramResult {
367 self.invoke_signed(&[])
368 }
369 #[allow(clippy::clone_on_copy)]
370 #[allow(clippy::vec_init_then_push)]
371 pub fn invoke_signed(
372 &self,
373 signers_seeds: &[&[&[u8]]],
374 ) -> solana_program::entrypoint::ProgramResult {
375 let args = WriteInstructionArgs {
376 overwrite: self
377 .instruction
378 .overwrite
379 .clone()
380 .expect("overwrite is not set"),
381 bytes: self.instruction.bytes.clone().expect("bytes is not set"),
382 };
383 let instruction = WriteCpi {
384 __program: self.instruction.__program,
385
386 asset: self.instruction.asset.expect("asset is not set"),
387
388 payer: self.instruction.payer.expect("payer is not set"),
389
390 system_program: self
391 .instruction
392 .system_program
393 .expect("system_program is not set"),
394 __args: args,
395 };
396 instruction.invoke_signed_with_remaining_accounts(
397 signers_seeds,
398 &self.instruction.__remaining_accounts,
399 )
400 }
401}
402
403struct WriteCpiBuilderInstruction<'a, 'b> {
404 __program: &'b solana_program::account_info::AccountInfo<'a>,
405 asset: Option<&'b solana_program::account_info::AccountInfo<'a>>,
406 payer: Option<&'b solana_program::account_info::AccountInfo<'a>>,
407 system_program: Option<&'b solana_program::account_info::AccountInfo<'a>>,
408 overwrite: Option<bool>,
409 bytes: Option<Vec<u8>>,
410 __remaining_accounts: Vec<(
412 &'b solana_program::account_info::AccountInfo<'a>,
413 bool,
414 bool,
415 )>,
416}