shank_render 0.4.8

Renders implementaions derived from shank macros
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
use proc_macro2::TokenStream;
use quote::quote;
use shank_macro_impl::{
    builder::BuilderVariant,
    instruction::InstructionVariantFields,
    syn::{parse_str, Expr, ExprPath, Ident},
};
use std::collections::HashMap;

const DEFAULT_PUBKEYS: [(&str, &str); 7] = [
    ("system_program", "solana_program::system_program::ID"),
    ("spl_token_program", "spl_token::ID"),
    ("spl_token_2022_program", "spl_token_2022::ID"),
    ("spl_ata_program", "spl_associated_token_account::ID"),
    (
        "sysvar_instructions",
        "solana_program::sysvar::instructions::ID",
    ),
    ("token_metadata_program", "mpl_token_metadata::ID"),
    ("authorization_rules_program", "mpl_token_auth_rules::ID"),
];

pub(crate) fn generate_builders(
    item: &Ident,
    variant: &BuilderVariant,
) -> TokenStream {
    let default_pubkeys = DEFAULT_PUBKEYS
        .iter()
        .map(|(name, pubkey)| {
            (name.to_string(), parse_str::<ExprPath>(pubkey).unwrap())
        })
        .collect::<HashMap<String, ExprPath>>();

    let field_names: Vec<Ident> = match &variant.field_tys {
        InstructionVariantFields::Named(field_tys) => field_tys
            .iter()
            .map(|(name, _)| parse_str::<Ident>(name).unwrap())
            .collect(),
        InstructionVariantFields::Unnamed(field_tys) => field_tys
            .iter()
            .enumerate()
            .map(|(idx, _)| {
                parse_str::<Ident>(&format!(
                    "args{}",
                    if idx == 0 {
                        String::new()
                    } else {
                        idx.to_string()
                    }
                ))
                .unwrap()
            })
            .collect(),
    };

    // instruction struct

    // accounts
    let struct_accounts = variant.accounts.iter().map(|account| {
        let account_name = parse_str::<Ident>(&account.name).unwrap();
        if account.optional {
            quote! {
                pub #account_name: Option<solana_program::pubkey::Pubkey>
            }
        } else {
            quote! {
                pub #account_name: solana_program::pubkey::Pubkey
            }
        }
    });

    // optional signers
    let struct_optional_signers = variant
        .accounts
        .iter()
        .filter(|account| account.optional_signer)
        .map(|account| {
            let optional_signer =
                parse_str::<Ident>(&format!("{}_signer", account.name))
                    .unwrap();
            quote! {
                pub #optional_signer: bool
            }
        });

    // args (builder)
    let struct_builder_args = variant.arguments.iter().map(|argument| {
        let ident_ty = parse_str::<Ident>(&argument.ty).unwrap();
        let arg_ty = if let Some(genetic_ty) = &argument.generic_ty {
            let arg_generic_ty = parse_str::<Ident>(genetic_ty).unwrap();
            quote! { #ident_ty<#arg_generic_ty> }
        } else {
            quote! { #ident_ty }
        };
        let arg_name = parse_str::<Ident>(&argument.name).unwrap();

        quote! {
            pub #arg_name: #arg_ty
        }
    });

    // builder struct

    // accounts
    let builder_accounts = variant.accounts.iter().map(|account| {
        let account_name = parse_str::<Ident>(&account.name).unwrap();
        quote! {
            pub #account_name: Option<solana_program::pubkey::Pubkey>
        }
    });

    // optional signers
    let builder_optional_signers = variant
        .accounts
        .iter()
        .filter(|account| account.optional_signer)
        .map(|account| {
            let optional_signer =
                parse_str::<Ident>(&format!("{}_signer", account.name))
                    .unwrap();
            quote! {
                pub #optional_signer: bool
            }
        });

    // accounts initialization
    let builder_initialize_accounts = variant.accounts.iter().map(|account| {
        let account_name = parse_str::<Ident>(&account.name).unwrap();
        quote! {
            #account_name: None
        }
    });

    // optional signers initialization
    let builder_initialize_optional_signers = variant
        .accounts
        .iter()
        .filter(|account| account.optional_signer)
        .map(|account| {
            let optional_signer =
                parse_str::<Ident>(&format!("{}_signer", account.name))
                    .unwrap();
            quote! {
                #optional_signer: false
            }
        });

    // args (builder)
    let builder_args = variant.arguments.iter().map(|argument| {
        let ident_ty = parse_str::<Ident>(&argument.ty).unwrap();
        let arg_ty = if let Some(genetic_ty) = &argument.generic_ty {
            let arg_generic_ty = parse_str::<Ident>(genetic_ty).unwrap();
            quote! { #ident_ty<#arg_generic_ty> }
        } else {
            quote! { #ident_ty }
        };
        let arg_name = parse_str::<Ident>(&argument.name).unwrap();

        quote! {
            pub #arg_name: Option<#arg_ty>
        }
    });

    // args initialization
    let builder_initialize_args = variant.arguments.iter().map(|argument| {
        let arg_name = parse_str::<Ident>(&argument.name).unwrap();
        quote! {
            #arg_name: None
        }
    });

    // account setter methods
    let builder_accounts_methods = variant.accounts.iter().map(|account| {
            let account_name = parse_str::<Ident>(&account.name).unwrap();

            if account.optional_signer {
                let optional_signer = parse_str::<Ident>(&format!("{}_signer", account.name)).unwrap();
                quote! {
                    pub fn #account_name(&mut self, #account_name: solana_program::pubkey::Pubkey, signer: bool) -> &mut Self {
                        self.#account_name = Some(#account_name);
                        self.#optional_signer = signer;
                        self
                    }
                }
            } else {
            quote! {
                pub fn #account_name(&mut self, #account_name: solana_program::pubkey::Pubkey) -> &mut Self {
                    self.#account_name = Some(#account_name);
                    self
                }
            }
        }
});

    // args (builder) setter methods
    let builder_args_methods = variant.arguments.iter().map(|argument| {
        let ident_ty = parse_str::<Ident>(&argument.ty).unwrap();
        let arg_ty = if let Some(genetic_ty) = &argument.generic_ty {
            let arg_generic_ty = parse_str::<Ident>(genetic_ty).unwrap();
            quote! { #ident_ty<#arg_generic_ty> }
        } else {
            quote! { #ident_ty }
        };
        let arg_name = parse_str::<Ident>(&argument.name).unwrap();

        quote! {
            pub fn #arg_name(&mut self, #arg_name: #arg_ty) -> &mut Self {
                self.#arg_name = Some(#arg_name);
                self
            }
        }
    });

    // required accounts
    let required_accounts = variant.accounts.iter().map(|account| {
            let account_name = parse_str::<Ident>(&account.name).unwrap();

            if account.optional {
                quote! {
                    #account_name: self.#account_name
                }
            } else {
                // are we dealing with a default pubkey?
                if default_pubkeys.contains_key(&account.name) {
                    let pubkey = default_pubkeys.get(&account.name).unwrap();
                    // we add the default key as the fallback value
                    quote! {
                        #account_name: self.#account_name.unwrap_or(#pubkey)
                    }
                }
                else {
                    // if not a default pubkey, we will need to have it set
                    quote! {
                        #account_name: self.#account_name.ok_or(concat!(stringify!(#account_name), " is not set"))?
                    }
                }
            }
        });

    // required optional signers
    let required_optional_signers = variant
        .accounts
        .iter()
        .filter(|account| account.optional_signer)
        .map(|account| {
            let optional_signer =
                parse_str::<Ident>(&format!("{}_signer", account.name))
                    .unwrap();
            quote! {
                #optional_signer: self.#optional_signer
            }
        });

    // required args (builder)
    let required_args = variant.arguments.iter().map(|argument| {
            let arg_name = parse_str::<Ident>(&argument.name).unwrap();
            quote! {
                #arg_name: self.#arg_name.clone().ok_or(concat!(stringify!(#arg_name), " is not set"))?
            }
        });

    // required args (builder) list
    let args: Vec<TokenStream> = match &variant.field_tys {
        InstructionVariantFields::Named(field_tys) => field_tys
            .iter()
            .enumerate()
            .map(|(idx, (_, ty))| {
                let name = field_names.get(idx).unwrap();
                let ty = &ty.ident;

                quote! { #name: #ty }
            })
            .collect(),
        InstructionVariantFields::Unnamed(field_tys) => field_tys
            .iter()
            .enumerate()
            .map(|(idx, ty)| {
                let name = field_names.get(idx).unwrap();
                let ty = &ty.ident;

                quote! { #name: #ty }
            })
            .collect(),
    };

    // instruction args
    let instruction_args: Vec<TokenStream> = match &variant.field_tys {
        InstructionVariantFields::Named(field_tys) => field_tys
            .iter()
            .enumerate()
            .map(|(idx, (_, ty))| {
                let name = field_names.get(idx).unwrap();
                let ty = &ty.ident;

                quote! { pub #name: #ty }
            })
            .collect(),
        InstructionVariantFields::Unnamed(field_tys) => field_tys
            .iter()
            .enumerate()
            .map(|(idx, ty)| {
                let name = field_names.get(idx).unwrap();
                let ty = &ty.ident;

                quote! { pub #name: #ty }
            })
            .collect(),
    };

    // required instruction args
    let required_instruction_args: Vec<TokenStream> = match &variant.field_tys {
        InstructionVariantFields::Named(field_tys) => field_tys
            .iter()
            .enumerate()
            .map(|(idx, _)| {
                let name = field_names.get(idx).unwrap();
                quote! { #name }
            })
            .collect(),
        InstructionVariantFields::Unnamed(field_tys) => field_tys
            .iter()
            .enumerate()
            .map(|(idx, _)| {
                let name = field_names.get(idx).unwrap();
                quote! { #name }
            })
            .collect(),
    };

    // account metas
    let account_metas: Vec<TokenStream> = variant.accounts.iter().map(|account| {
        let account_name = parse_str::<Ident>(&account.name).unwrap();
        let signer = if account.optional_signer {
            parse_str::<Expr>(&format!("self.{}_signer", account.name)).unwrap()
        } else {
            parse_str::<Expr>(&format!("{}", account.signer)).unwrap()
        };

        if account.optional {
            if account.writable {
                quote! {
                    if let Some(#account_name) = self.#account_name {
                        solana_program::instruction::AccountMeta::new(#account_name, #signer)
                    } else {
                        solana_program::instruction::AccountMeta::new_readonly(crate::ID, false)
                    }
                }
            } else if account.signer {
                quote! {
                    if let Some(#account_name) = self.#account_name {
                        solana_program::instruction::AccountMeta::new_readonly(#account_name, #signer)
                    } else {
                        solana_program::instruction::AccountMeta::new_readonly(crate::ID, false)
                    }
                }
            } else {
                quote!{
                    solana_program::instruction::AccountMeta::new_readonly(self.#account_name.unwrap_or(crate::ID), false)
                }
            }
        } else if account.writable {
            quote! {
                solana_program::instruction::AccountMeta::new(self.#account_name, #signer)
            }
        } else {
            quote!{
                solana_program::instruction::AccountMeta::new_readonly(self.#account_name, #signer)
            }
        }
    }).collect();

    // builder name
    let name = &variant.ident;
    let builder_name = parse_str::<Ident>(&format!("{}Builder", name)).unwrap();

    // instruction args list
    let struct_instruction_args: Vec<TokenStream> = match &variant.field_tys {
        InstructionVariantFields::Named(field_tys) => field_tys
            .iter()
            .enumerate()
            .map(|(idx, _)| {
                let name = field_names.get(idx).unwrap();
                quote! { self.#name.clone() }
            })
            .collect(),
        InstructionVariantFields::Unnamed(field_tys) => field_tys
            .iter()
            .enumerate()
            .map(|(idx, _)| {
                let name = field_names.get(idx).unwrap();
                quote! { self.#name.clone() }
            })
            .collect(),
    };

    let instruction_data = if struct_instruction_args.is_empty() {
        quote! {
            #item::#name.try_to_vec().unwrap()
        }
    } else {
        quote! {
            #item::#name(#(#struct_instruction_args,)*).try_to_vec().unwrap()
        }
    };

    // default instruction builder (only generated if the instruction builder does
    // not have custom arguments)
    let default_instruction_builder = if variant.arguments.is_empty() {
        quote! {
            impl InstructionBuilder for #name {
                fn instruction(&self) -> solana_program::instruction::Instruction {
                    solana_program::instruction::Instruction {
                        program_id: crate::ID,
                        accounts: vec![
                            #(#account_metas,)*
                        ],
                        data: #instruction_data,
                    }
                }
            }
        }
    } else {
        quote! {}
    };

    quote! {
        pub struct #name {
            #(#struct_accounts,)*
            #(#struct_optional_signers,)*
            #(#instruction_args,)*
            #(#struct_builder_args,)*
        }

        #default_instruction_builder

        pub struct #builder_name {
            #(#builder_accounts,)*
            #(#builder_optional_signers,)*
            #(#builder_args,)*
        }

        impl #builder_name {
            pub fn new() -> Box<#builder_name> {
                Box::new(#builder_name {
                    #(#builder_initialize_accounts,)*
                    #(#builder_initialize_optional_signers,)*
                    #(#builder_initialize_args,)*
                })
            }

            #(#builder_accounts_methods)*
            #(#builder_args_methods)*

            pub fn build(&mut self, #(#args,)*) -> Result<Box<#name>, Box<dyn std::error::Error>> {
                Ok(Box::new(#name {
                    #(#required_accounts,)*
                    #(#required_optional_signers,)*
                    #(#required_instruction_args,)*
                    #(#required_args,)*
                }))
            }
        }
    }
}