|
@@ -316,7 +316,7 @@ fn generate_constraint_init_group(f: &Field, c: &ConstraintInitGroup) -> proc_ma
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
- generate_init(f, seeds_with_nonce, payer, &c.space, &c.kind)
|
|
|
+ generate_init(f, c.if_needed, seeds_with_nonce, payer, &c.space, &c.kind)
|
|
|
}
|
|
|
|
|
|
fn generate_constraint_seeds(f: &Field, c: &ConstraintSeedsGroup) -> proc_macro2::TokenStream {
|
|
@@ -397,8 +397,10 @@ fn generate_constraint_associated_token(
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+// `if_needed` is set if account allocation and initialization is optional.
|
|
|
pub fn generate_init(
|
|
|
f: &Field,
|
|
|
+ if_needed: bool,
|
|
|
seeds_with_nonce: proc_macro2::TokenStream,
|
|
|
payer: proc_macro2::TokenStream,
|
|
|
space: &Option<Expr>,
|
|
@@ -407,6 +409,11 @@ pub fn generate_init(
|
|
|
let field = &f.ident;
|
|
|
let ty_decl = f.ty_decl();
|
|
|
let from_account_info = f.from_account_info_unchecked(Some(kind));
|
|
|
+ let if_needed = if if_needed {
|
|
|
+ quote! {true}
|
|
|
+ } else {
|
|
|
+ quote! {false}
|
|
|
+ };
|
|
|
match kind {
|
|
|
InitKind::Token { owner, mint } => {
|
|
|
let create_account = generate_create_account(
|
|
@@ -417,22 +424,25 @@ pub fn generate_init(
|
|
|
);
|
|
|
quote! {
|
|
|
let #field: #ty_decl = {
|
|
|
- // Define payer variable.
|
|
|
- #payer
|
|
|
-
|
|
|
- // Create the account with the system program.
|
|
|
- #create_account
|
|
|
-
|
|
|
- // Initialize the token account.
|
|
|
- let cpi_program = token_program.to_account_info();
|
|
|
- let accounts = anchor_spl::token::InitializeAccount {
|
|
|
- account: #field.to_account_info(),
|
|
|
- mint: #mint.to_account_info(),
|
|
|
- authority: #owner.to_account_info(),
|
|
|
- rent: rent.to_account_info(),
|
|
|
- };
|
|
|
- let cpi_ctx = CpiContext::new(cpi_program, accounts);
|
|
|
- anchor_spl::token::initialize_account(cpi_ctx)?;
|
|
|
+ if !#if_needed || #field.to_account_info().owner == &anchor_lang::solana_program::system_program::ID {
|
|
|
+ // Define payer variable.
|
|
|
+ #payer
|
|
|
+
|
|
|
+ // Create the account with the system program.
|
|
|
+ #create_account
|
|
|
+
|
|
|
+ // Initialize the token account.
|
|
|
+ let cpi_program = token_program.to_account_info();
|
|
|
+ let accounts = anchor_spl::token::InitializeAccount {
|
|
|
+ account: #field.to_account_info(),
|
|
|
+ mint: #mint.to_account_info(),
|
|
|
+ authority: #owner.to_account_info(),
|
|
|
+ rent: rent.to_account_info(),
|
|
|
+ };
|
|
|
+ let cpi_ctx = CpiContext::new(cpi_program, accounts);
|
|
|
+ anchor_spl::token::initialize_account(cpi_ctx)?;
|
|
|
+ }
|
|
|
+
|
|
|
let pa: #ty_decl = #from_account_info;
|
|
|
pa
|
|
|
};
|
|
@@ -441,20 +451,22 @@ pub fn generate_init(
|
|
|
InitKind::AssociatedToken { owner, mint } => {
|
|
|
quote! {
|
|
|
let #field: #ty_decl = {
|
|
|
- #payer
|
|
|
-
|
|
|
- let cpi_program = associated_token_program.to_account_info();
|
|
|
- let cpi_accounts = anchor_spl::associated_token::Create {
|
|
|
- payer: payer.to_account_info(),
|
|
|
- associated_token: #field.to_account_info(),
|
|
|
- authority: #owner.to_account_info(),
|
|
|
- mint: #mint.to_account_info(),
|
|
|
- system_program: system_program.to_account_info(),
|
|
|
- token_program: token_program.to_account_info(),
|
|
|
- rent: rent.to_account_info(),
|
|
|
- };
|
|
|
- let cpi_ctx = CpiContext::new(cpi_program, cpi_accounts);
|
|
|
- anchor_spl::associated_token::create(cpi_ctx)?;
|
|
|
+ if !#if_needed || #field.to_account_info().owner == &anchor_lang::solana_program::system_program::ID {
|
|
|
+ #payer
|
|
|
+
|
|
|
+ let cpi_program = associated_token_program.to_account_info();
|
|
|
+ let cpi_accounts = anchor_spl::associated_token::Create {
|
|
|
+ payer: payer.to_account_info(),
|
|
|
+ associated_token: #field.to_account_info(),
|
|
|
+ authority: #owner.to_account_info(),
|
|
|
+ mint: #mint.to_account_info(),
|
|
|
+ system_program: system_program.to_account_info(),
|
|
|
+ token_program: token_program.to_account_info(),
|
|
|
+ rent: rent.to_account_info(),
|
|
|
+ };
|
|
|
+ let cpi_ctx = CpiContext::new(cpi_program, cpi_accounts);
|
|
|
+ anchor_spl::associated_token::create(cpi_ctx)?;
|
|
|
+ }
|
|
|
let pa: #ty_decl = #from_account_info;
|
|
|
pa
|
|
|
};
|
|
@@ -477,20 +489,22 @@ pub fn generate_init(
|
|
|
};
|
|
|
quote! {
|
|
|
let #field: #ty_decl = {
|
|
|
- // Define payer variable.
|
|
|
- #payer
|
|
|
-
|
|
|
- // Create the account with the system program.
|
|
|
- #create_account
|
|
|
-
|
|
|
- // Initialize the mint account.
|
|
|
- let cpi_program = token_program.to_account_info();
|
|
|
- let accounts = anchor_spl::token::InitializeMint {
|
|
|
- mint: #field.to_account_info(),
|
|
|
- rent: rent.to_account_info(),
|
|
|
- };
|
|
|
- let cpi_ctx = CpiContext::new(cpi_program, accounts);
|
|
|
- anchor_spl::token::initialize_mint(cpi_ctx, #decimals, &#owner.to_account_info().key, #freeze_authority)?;
|
|
|
+ if !#if_needed || #field.to_account_info().owner == &anchor_lang::solana_program::system_program::ID {
|
|
|
+ // Define payer variable.
|
|
|
+ #payer
|
|
|
+
|
|
|
+ // Create the account with the system program.
|
|
|
+ #create_account
|
|
|
+
|
|
|
+ // Initialize the mint account.
|
|
|
+ let cpi_program = token_program.to_account_info();
|
|
|
+ let accounts = anchor_spl::token::InitializeMint {
|
|
|
+ mint: #field.to_account_info(),
|
|
|
+ rent: rent.to_account_info(),
|
|
|
+ };
|
|
|
+ let cpi_ctx = CpiContext::new(cpi_program, accounts);
|
|
|
+ anchor_spl::token::initialize_mint(cpi_ctx, #decimals, &#owner.to_account_info().key, #freeze_authority)?;
|
|
|
+ }
|
|
|
let pa: #ty_decl = #from_account_info;
|
|
|
pa
|
|
|
};
|
|
@@ -535,9 +549,11 @@ pub fn generate_init(
|
|
|
generate_create_account(field, quote! {space}, owner, seeds_with_nonce);
|
|
|
quote! {
|
|
|
let #field = {
|
|
|
- #space
|
|
|
- #payer
|
|
|
- #create_account
|
|
|
+ if !#if_needed || #field.to_account_info().owner == &anchor_lang::solana_program::system_program::ID {
|
|
|
+ #space
|
|
|
+ #payer
|
|
|
+ #create_account
|
|
|
+ }
|
|
|
let pa: #ty_decl = #from_account_info;
|
|
|
pa
|
|
|
};
|