typhoon-context-macro 0.1.0-alpha.16

TODO
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
use {
    proc_macro2::TokenStream,
    quote::{format_ident, quote},
    syn::{parse_quote, Expr, Ident},
    typhoon_syn::{
        error,
        utils::{ContextExpr, SeedsExpr},
        InstructionAccount,
    },
};

pub enum AccountType {
    TokenAccount {
        is_ata: bool,
        mint: Option<Ident>,
        owner: Option<Expr>,
    },
    Mint {
        decimals: Option<Expr>,
        authority: Option<Expr>,
        freeze_authority: Box<Option<Expr>>,
    },
    Other {
        space: Option<Expr>,
        targets: Vec<(Ident, Option<Expr>)>,
    },
}

#[derive(Default)]
pub struct InitContext {
    pub is_init_if_needed: bool,
    pub payer: Option<Ident>,
}

#[derive(Default)]
pub struct PdaContext {
    pub keys: Option<SeedsExpr>,
    pub bump: Option<ContextExpr>,
    pub is_seeded: bool,
    pub program_id: Option<Expr>,
}

pub struct AccountGenerator<'a> {
    pub account: &'a InstructionAccount,
    pub account_ty: AccountType,
    pub init: Option<InitContext>,
    pub pda: Option<PdaContext>,
    pub init_state: bool,
}

impl<'a> AccountGenerator<'a> {
    pub fn new(account: &'a InstructionAccount, account_ty: AccountType) -> Self {
        Self {
            account,
            account_ty,
            init: None,
            pda: Default::default(),
            init_state: false,
        }
    }
}

impl AccountGenerator<'_> {
    pub fn needs_programs(&self) -> Vec<String> {
        let mut programs = Vec::with_capacity(3);
        if self.init.is_some() {
            programs.push("System".to_string());
            match self.account_ty {
                AccountType::TokenAccount { is_ata, .. } => {
                    programs.push("TokenProgram".to_string());

                    if is_ata {
                        programs.push("AtaTokenProgram".to_string());
                    }
                }
                AccountType::Mint { .. } => programs.push("TokenProgram".to_string()),
                _ => (),
            }
        }
        programs
    }

    pub fn is_init_signer(&self) -> bool {
        match self.account_ty {
            AccountType::TokenAccount { is_ata: true, .. } => true,
            _ => self.pda.is_some() || self.account.meta.is_signer,
        }
    }

    fn get_pda(
        &self,
        ctx: &PdaContext,
        find: bool,
        define_key: bool,
    ) -> Result<TokenStream, syn::Error> {
        let name = &self.account.name;
        let pda_key = format_ident!("{}_key", name);
        let pda_bump = format_ident!("{}_bump", name);
        let program_id = ctx
            .program_id
            .as_ref()
            .map(|p| quote!(#p))
            .unwrap_or(quote!(program_id));

        match &ctx.bump {
            Some(ref bump) if !find => {
                let seeds_token = if ctx.is_seeded {
                    let var_name = format_ident!("{}_state", self.account.name);
                    quote!(#var_name.seeds_with_bump(&[#pda_bump]))
                } else {
                    let Some(ref seed_keys) = ctx.keys else {
                        error!(
                            &self.account.name,
                            "No seeds specified for the current PDA."
                        );
                    };

                    match seed_keys {
                        SeedsExpr::Punctuated(punctuated) => quote!([#punctuated, &[#pda_bump]]),
                        SeedsExpr::Single(expr) => quote!(#expr(&[#pda_bump])),
                    }
                };

                Ok(quote! {
                    let #pda_bump = #bump;
                    let #pda_key = create_program_address(&#seeds_token, &#program_id)?;
                })
            }
            _ => {
                let Some(ref seed_keys) = ctx.keys else {
                    error!(
                        &self.account.name,
                        "No seeds specified for the current PDA."
                    );
                };

                let seeds_token = if ctx.is_seeded {
                    let inner_ty = &self.account.inner_ty;
                    quote!(#inner_ty::derive(#seed_keys))
                } else {
                    match seed_keys {
                        SeedsExpr::Punctuated(punctuated) => quote!([#punctuated]),
                        SeedsExpr::Single(expr) => quote!(#expr),
                    }
                };

                let key_token = if define_key {
                    quote! {
                        let (#pda_key, #pda_bump) = find_program_address(&#seeds_token, &#program_id);
                    }
                } else {
                    quote! {
                        let (_, #pda_bump) = find_program_address(&#seeds_token, &#program_id);
                    }
                };
                Ok(key_token)
            }
        }
    }

    fn get_signer_init(&self, ctx: &PdaContext) -> Result<TokenStream, syn::Error> {
        let pda_bump = format_ident!("{}_bump", self.account.name);
        let punctuated_keys = ctx.keys.as_ref().ok_or(syn::Error::new_spanned(
            &self.account.name,
            "The seeds cannot be empty.",
        ))?;

        let seeds = if ctx.is_seeded {
            let account_ty = &self.account.inner_ty;
            quote! {
                let seeds = #account_ty::derive_signer_seeds_with_bump(#punctuated_keys, &bump);
                let signer = instruction::CpiSigner::from(&seeds);
            }
        } else {
            match punctuated_keys {
                SeedsExpr::Punctuated(punctuated) => {
                    quote! {
                        let seeds = seeds!(#punctuated, &bump);
                        let signer = instruction::CpiSigner::from(&seeds);
                    }
                }
                SeedsExpr::Single(expr) => {
                    quote! {
                        let expr = #expr;
                        let expr_len = expr.len();
                        let mut buffer = [bytes::UNINIT_SEED; MAX_SEEDS];
                        for (uninit_byte, &src_byte) in buffer[..expr_len].iter_mut().zip(&expr) {
                            uninit_byte.write(instruction::Seed::from(src_byte));
                        }
                        buffer[expr_len].write(instruction::Seed::from(&bump));

                        let signer = instruction::CpiSigner::from(unsafe { core::slice::from_raw_parts(buffer.as_ptr() as *const instruction::Seed, expr_len + 1) });
                    }
                }
            }
        };

        Ok(quote! {
            // TODO: avoid reusing seeds here and in verifications
            let bump = [#pda_bump];
            #seeds
        })
    }

    fn get_init_token(
        &self,
        ctx: &InitContext,
        signers: TokenStream,
    ) -> Result<TokenStream, syn::Error> {
        let name = &self.account.name;
        if !self.account.meta.is_mutable || !self.is_init_signer() {
            error!(name, "The account needs to be mutable and signer");
        }
        let payer = ctx.payer.as_ref().ok_or(syn::Error::new_spanned(
            name,
            "A payer needs to be specified for `init` or init_if_needed` constraint.",
        ))?;

        let init_token = match &self.account_ty {
            AccountType::TokenAccount {
                is_ata,
                mint,
                owner,
            } => {
                let Some(owner) = owner else {
                    error!(name, "An `owner` need to be specified for the `init` or `init_if_needed` constraint.");
                };
                let Some(mint) = mint else {
                    error!(name, "A `mint` need to be specified for the `init` or `init_if_needed` constraint.");
                };

                if *is_ata {
                    quote!(SplCreateToken::create_associated_token_account(#name, &#payer, &#mint, &#owner, &system_program, &token_program)?)
                } else {
                    quote!(SplCreateToken::create_token_account(#name, &rent, &#payer, &#mint, &#owner, #signers)?)
                }
            }
            AccountType::Mint {
                decimals,
                authority,
                freeze_authority,
            } => {
                let default_decimals = parse_quote!(9);
                let decimals = decimals.as_ref().unwrap_or(&default_decimals);
                let Some(authority) = authority else {
                    error!(name, "An `authority` need to be specified for the `init` or `init_if_needed` constraint.");
                };
                let f_auth_token = if let Some(auth) = freeze_authority.as_ref() {
                    quote!(Some(#auth))
                } else {
                    quote!(None)
                };
                quote!(SplCreateMint::create_mint(#name, &rent, &#payer, &#authority, #decimals, #f_auth_token, #signers)?)
            }
            AccountType::Other { space, .. } => {
                let account_ty = &self.account.inner_ty;
                let default_space = parse_quote!(#account_ty::SPACE);
                let space = space.as_ref().unwrap_or(&default_space);
                quote!(CreateAccountCpi::create(#name, &rent, &#payer, &program_id, #space, #signers)?)
            }
        };

        Ok(init_token)
    }

    pub fn account_token(&self) -> Result<TokenStream, syn::Error> {
        let mut token = TokenStream::new();
        let name = &self.account.name;
        let name_str = name.to_string();
        let account_ty = self.account.get_ty();
        let var_name = format_ident!("{}_state", name);
        let pda_key = format_ident!("{}_key", name);

        token.extend(quote!(let #name = <#account_ty as FromAccountInfo>::try_from_info(#name).trace_account(#name_str)?;));

        if self.init_state {
            token.extend(quote!(let #var_name = #name.data_unchecked()?;));
        }

        if let Some(ref pda_ctx) = self.pda {
            let pda = self.get_pda(pda_ctx, false, true)?;
            token.extend(pda);
            token.extend(quote! {
                if #name.key() != &#pda_key {
                    return Err(Error::new(ProgramError::InvalidSeeds).with_account(#name_str));
                }
            });
        }

        match self.account_ty {
            AccountType::TokenAccount {
                ref mint,
                ref owner,
                ..
            } => {
                if let Some(mint) = mint {
                    token.extend(quote! {
                        if #var_name.mint() != #mint.key() {
                            return Err(ErrorCode::TokenConstraintViolated.into());
                        }
                    });
                }

                if let Some(owner) = owner {
                    token.extend(quote! {
                        if #var_name.owner() != #owner.key() {
                            return Err(ErrorCode::TokenConstraintViolated.into());
                        }
                    });
                }
            }
            AccountType::Mint { .. } => {}
            AccountType::Other { ref targets, .. } => {
                let basic_error: Expr = parse_quote!(ErrorCode::HasOneConstraint);
                let targets = targets.iter().map(|(target, error)| {
                    let target = &target;
                    let error = error.as_ref().unwrap_or(&basic_error);

                    quote! {
                        if &#var_name.#target != #target.key() {
                            return Err(#error.into());
                        }
                    }
                });
                token.extend(targets);
            }
        }
        Ok(token)
    }

    fn needs_return_bump(&self) -> bool {
        let has_pda = self.pda.is_some();
        let pda_has_no_bump = self
            .pda
            .as_ref()
            .map(|el| el.bump.is_none())
            .unwrap_or_default();
        let is_init_if_needed = self
            .init
            .as_ref()
            .map(|el| el.is_init_if_needed)
            .unwrap_or_default();

        has_pda && (pda_has_no_bump || is_init_if_needed)
    }

    pub fn generate(self) -> Result<TokenStream, syn::Error> {
        let mut token = TokenStream::new();
        let name = &self.account.name;
        let account_ty = self.account.get_ty();
        let pda_bump = format_ident!("{}_bump", name);

        let return_ty = if self.needs_return_bump() {
            quote!((#name, #pda_bump))
        } else {
            quote!(#name)
        };

        let account_checks_token = if let Some(ref init_ctx) = self.init {
            let signers = if self.pda.is_some() {
                quote!(Some(&[signer]))
            } else {
                quote!(None)
            };
            let init_token = self.get_init_token(init_ctx, signers)?;
            let init_account_token = if let Some(ref pda_ctx) = self.pda {
                let pda_token = self.get_pda(pda_ctx, init_ctx.is_init_if_needed, false)?;
                let seeds_token = self.get_signer_init(pda_ctx)?;
                quote! {
                    #pda_token
                    #seeds_token
                    let #name = { #init_token };
                }
            } else {
                quote! {
                    let #name: #account_ty = {
                        #init_token
                    };
                }
            };

            if init_ctx.is_init_if_needed {
                let account_token = self.account_token()?;
                quote! {
                    let #return_ty = if !#name.is_owned_by(&Pubkey::default()) {
                        #account_token
                        #return_ty
                    }else {
                        #init_account_token
                        #return_ty
                    };
                }
            } else {
                quote!(#init_account_token)
            }
        } else {
            self.account_token()?
        };

        if self.account.meta.is_optional {
            token.extend(quote! {
                let #return_ty = if #name.key() == program_id {
                    None
                } else {
                    #account_checks_token
                    Some(#return_ty)
                };
            });
        } else {
            token.extend(account_checks_token);
        };

        Ok(token)
    }
}

/*
ConstraintAssociatedToken::Mint(ident) => {
    if let Some(seeds) = self.seeds.as_mut() {
        let SeedsExpr::Punctuated(expr) = seeds else {
            return Err(syn::Error::new_spanned(
                &seeds,
                "Seeds expr cannot be used in this context.",
            ));
        };

        expr.insert(2, parse_quote!(#ident));
    } else {
        self.seeds = Some(SeedsExpr::Punctuated(
            parse_quote!(token_program.key().as_ref(), #ident),
        ));
        self.program_id = Some(parse_quote!(AtaTokenProgram::ID))
    }
}
ConstraintAssociatedToken::Authority(ident) => {
    if let Some(seeds) = self.seeds.as_mut() {
        let SeedsExpr::Punctuated(expr) = seeds else {
            return Err(syn::Error::new_spanned(
                &seeds,
                "Seeds expr cannot be used in this context.",
            ));
        };

        expr.insert(0, parse_quote!(#ident));
    } else {
        self.seeds = Some(SeedsExpr::Punctuated(
            parse_quote!(#ident, token_program.key().as_ref()),
        ));
        self.program_id = Some(parse_quote!(AtaTokenProgram::ID))
    }
}

*/