2021-08-25 10:27:27 -07:00
|
|
|
use crate::*;
|
2021-06-27 09:50:54 -07:00
|
|
|
use proc_macro2_diagnostics::SpanDiagnosticExt;
|
2021-05-30 12:59:33 -07:00
|
|
|
use quote::quote;
|
2021-06-27 13:17:05 -07:00
|
|
|
use syn::Expr;
|
2021-05-30 12:59:33 -07:00
|
|
|
|
|
|
|
pub fn generate(f: &Field) -> proc_macro2::TokenStream {
|
2021-08-08 13:20:26 -07:00
|
|
|
let constraints = linearize(&f.constraints);
|
|
|
|
|
|
|
|
let rent = constraints
|
|
|
|
.iter()
|
|
|
|
.any(|c| matches!(c, Constraint::RentExempt(ConstraintRentExempt::Enforce)))
|
|
|
|
.then(|| quote! { let __anchor_rent = Rent::get()?; })
|
|
|
|
.unwrap_or_else(|| quote! {});
|
|
|
|
|
|
|
|
let checks: Vec<proc_macro2::TokenStream> = constraints
|
2021-05-30 12:59:33 -07:00
|
|
|
.iter()
|
|
|
|
.map(|c| generate_constraint(f, c))
|
|
|
|
.collect();
|
2021-08-08 13:20:26 -07:00
|
|
|
|
2021-05-30 12:59:33 -07:00
|
|
|
quote! {
|
2021-08-08 13:20:26 -07:00
|
|
|
#rent
|
2021-05-30 12:59:33 -07:00
|
|
|
#(#checks)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn generate_composite(f: &CompositeField) -> proc_macro2::TokenStream {
|
|
|
|
let checks: Vec<proc_macro2::TokenStream> = linearize(&f.constraints)
|
|
|
|
.iter()
|
|
|
|
.filter_map(|c| match c {
|
|
|
|
Constraint::Raw(_) => Some(c),
|
|
|
|
Constraint::Literal(_) => Some(c),
|
|
|
|
_ => panic!("Invariant violation: composite constraints can only be raw or literals"),
|
|
|
|
})
|
|
|
|
.map(|c| generate_constraint_composite(f, c))
|
|
|
|
.collect();
|
|
|
|
quote! {
|
|
|
|
#(#checks)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Linearizes the constraint group so that constraints with dependencies
|
|
|
|
// run after those without.
|
|
|
|
pub fn linearize(c_group: &ConstraintGroup) -> Vec<Constraint> {
|
|
|
|
let ConstraintGroup {
|
|
|
|
init,
|
2021-08-25 10:27:27 -07:00
|
|
|
zeroed,
|
2021-05-30 12:59:33 -07:00
|
|
|
mutable,
|
|
|
|
signer,
|
2021-07-03 11:58:23 -07:00
|
|
|
has_one,
|
2021-05-30 12:59:33 -07:00
|
|
|
literal,
|
|
|
|
raw,
|
|
|
|
owner,
|
|
|
|
rent_exempt,
|
|
|
|
seeds,
|
|
|
|
executable,
|
|
|
|
state,
|
2021-06-09 15:40:43 -07:00
|
|
|
close,
|
2021-06-27 13:17:05 -07:00
|
|
|
address,
|
2021-05-30 12:59:33 -07:00
|
|
|
} = c_group.clone();
|
|
|
|
|
|
|
|
let mut constraints = Vec::new();
|
|
|
|
|
2021-08-29 14:25:38 -07:00
|
|
|
if let Some(c) = zeroed {
|
|
|
|
constraints.push(Constraint::Zeroed(c));
|
2021-06-15 17:15:51 -07:00
|
|
|
}
|
2021-05-30 12:59:33 -07:00
|
|
|
if let Some(c) = init {
|
|
|
|
constraints.push(Constraint::Init(c));
|
|
|
|
}
|
2021-08-29 14:25:38 -07:00
|
|
|
if let Some(c) = seeds {
|
|
|
|
constraints.push(Constraint::Seeds(c));
|
2021-08-25 10:27:27 -07:00
|
|
|
}
|
2021-05-30 12:59:33 -07:00
|
|
|
if let Some(c) = mutable {
|
|
|
|
constraints.push(Constraint::Mut(c));
|
|
|
|
}
|
|
|
|
if let Some(c) = signer {
|
|
|
|
constraints.push(Constraint::Signer(c));
|
|
|
|
}
|
2021-07-03 11:58:23 -07:00
|
|
|
constraints.append(&mut has_one.into_iter().map(Constraint::HasOne).collect());
|
2021-06-27 17:09:46 -07:00
|
|
|
constraints.append(&mut literal.into_iter().map(Constraint::Literal).collect());
|
|
|
|
constraints.append(&mut raw.into_iter().map(Constraint::Raw).collect());
|
2021-05-30 12:59:33 -07:00
|
|
|
if let Some(c) = owner {
|
|
|
|
constraints.push(Constraint::Owner(c));
|
|
|
|
}
|
|
|
|
if let Some(c) = rent_exempt {
|
|
|
|
constraints.push(Constraint::RentExempt(c));
|
|
|
|
}
|
|
|
|
if let Some(c) = executable {
|
|
|
|
constraints.push(Constraint::Executable(c));
|
|
|
|
}
|
|
|
|
if let Some(c) = state {
|
|
|
|
constraints.push(Constraint::State(c));
|
|
|
|
}
|
2021-06-09 15:40:43 -07:00
|
|
|
if let Some(c) = close {
|
|
|
|
constraints.push(Constraint::Close(c));
|
|
|
|
}
|
2021-06-27 13:17:05 -07:00
|
|
|
if let Some(c) = address {
|
|
|
|
constraints.push(Constraint::Address(c));
|
|
|
|
}
|
2021-05-30 12:59:33 -07:00
|
|
|
constraints
|
|
|
|
}
|
|
|
|
|
|
|
|
fn generate_constraint(f: &Field, c: &Constraint) -> proc_macro2::TokenStream {
|
|
|
|
match c {
|
|
|
|
Constraint::Init(c) => generate_constraint_init(f, c),
|
2021-08-25 10:27:27 -07:00
|
|
|
Constraint::Zeroed(c) => generate_constraint_zeroed(f, c),
|
2021-05-30 12:59:33 -07:00
|
|
|
Constraint::Mut(c) => generate_constraint_mut(f, c),
|
2021-07-03 11:58:23 -07:00
|
|
|
Constraint::HasOne(c) => generate_constraint_has_one(f, c),
|
2021-05-30 12:59:33 -07:00
|
|
|
Constraint::Signer(c) => generate_constraint_signer(f, c),
|
|
|
|
Constraint::Literal(c) => generate_constraint_literal(c),
|
|
|
|
Constraint::Raw(c) => generate_constraint_raw(c),
|
|
|
|
Constraint::Owner(c) => generate_constraint_owner(f, c),
|
|
|
|
Constraint::RentExempt(c) => generate_constraint_rent_exempt(f, c),
|
|
|
|
Constraint::Seeds(c) => generate_constraint_seeds(f, c),
|
|
|
|
Constraint::Executable(c) => generate_constraint_executable(f, c),
|
|
|
|
Constraint::State(c) => generate_constraint_state(f, c),
|
2021-06-09 15:40:43 -07:00
|
|
|
Constraint::Close(c) => generate_constraint_close(f, c),
|
2021-06-27 13:17:05 -07:00
|
|
|
Constraint::Address(c) => generate_constraint_address(f, c),
|
2021-05-30 12:59:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn generate_constraint_composite(_f: &CompositeField, c: &Constraint) -> proc_macro2::TokenStream {
|
|
|
|
match c {
|
|
|
|
Constraint::Raw(c) => generate_constraint_raw(c),
|
|
|
|
Constraint::Literal(c) => generate_constraint_literal(c),
|
|
|
|
_ => panic!("Invariant violation"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-27 13:17:05 -07:00
|
|
|
fn generate_constraint_address(f: &Field, c: &ConstraintAddress) -> proc_macro2::TokenStream {
|
|
|
|
let field = &f.ident;
|
|
|
|
let addr = &c.address;
|
|
|
|
quote! {
|
|
|
|
if #field.to_account_info().key != &#addr {
|
|
|
|
return Err(anchor_lang::__private::ErrorCode::ConstraintAddress.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-29 14:25:38 -07:00
|
|
|
pub fn generate_constraint_init(f: &Field, c: &ConstraintInitGroup) -> proc_macro2::TokenStream {
|
|
|
|
generate_constraint_init_group(f, c)
|
2021-05-30 12:59:33 -07:00
|
|
|
}
|
|
|
|
|
2021-08-29 14:25:38 -07:00
|
|
|
pub fn generate_constraint_zeroed(f: &Field, _c: &ConstraintZeroed) -> proc_macro2::TokenStream {
|
|
|
|
let field = &f.ident;
|
2021-09-07 13:06:15 -07:00
|
|
|
let ty_decl = f.ty_decl();
|
2021-09-16 15:49:24 -07:00
|
|
|
let from_account_info = f.from_account_info_unchecked(None);
|
2021-08-29 14:25:38 -07:00
|
|
|
quote! {
|
2021-09-07 13:06:15 -07:00
|
|
|
let #field: #ty_decl = {
|
2021-08-29 14:25:38 -07:00
|
|
|
let mut __data: &[u8] = &#field.try_borrow_data()?;
|
|
|
|
let mut __disc_bytes = [0u8; 8];
|
|
|
|
__disc_bytes.copy_from_slice(&__data[..8]);
|
|
|
|
let __discriminator = u64::from_le_bytes(__disc_bytes);
|
|
|
|
if __discriminator != 0 {
|
|
|
|
return Err(anchor_lang::__private::ErrorCode::ConstraintZero.into());
|
|
|
|
}
|
2021-09-07 13:06:15 -07:00
|
|
|
#from_account_info
|
2021-08-29 14:25:38 -07:00
|
|
|
};
|
|
|
|
}
|
2021-08-25 10:27:27 -07:00
|
|
|
}
|
|
|
|
|
2021-06-09 15:40:43 -07:00
|
|
|
pub fn generate_constraint_close(f: &Field, c: &ConstraintClose) -> proc_macro2::TokenStream {
|
|
|
|
let field = &f.ident;
|
|
|
|
let target = &c.sol_dest;
|
|
|
|
quote! {
|
|
|
|
if #field.to_account_info().key == #target.to_account_info().key {
|
|
|
|
return Err(anchor_lang::__private::ErrorCode::ConstraintClose.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-30 12:59:33 -07:00
|
|
|
pub fn generate_constraint_mut(f: &Field, _c: &ConstraintMut) -> proc_macro2::TokenStream {
|
|
|
|
let ident = &f.ident;
|
|
|
|
quote! {
|
|
|
|
if !#ident.to_account_info().is_writable {
|
2021-06-09 13:02:50 -07:00
|
|
|
return Err(anchor_lang::__private::ErrorCode::ConstraintMut.into());
|
2021-05-30 12:59:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-03 11:58:23 -07:00
|
|
|
pub fn generate_constraint_has_one(f: &Field, c: &ConstraintHasOne) -> proc_macro2::TokenStream {
|
2021-05-30 12:59:33 -07:00
|
|
|
let target = c.join_target.clone();
|
|
|
|
let ident = &f.ident;
|
|
|
|
let field = match &f.ty {
|
|
|
|
Ty::Loader(_) => quote! {#ident.load()?},
|
|
|
|
_ => quote! {#ident},
|
|
|
|
};
|
|
|
|
quote! {
|
|
|
|
if &#field.#target != #target.to_account_info().key {
|
2021-07-03 11:58:23 -07:00
|
|
|
return Err(anchor_lang::__private::ErrorCode::ConstraintHasOne.into());
|
2021-05-30 12:59:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn generate_constraint_signer(f: &Field, _c: &ConstraintSigner) -> proc_macro2::TokenStream {
|
|
|
|
let ident = &f.ident;
|
|
|
|
let info = match f.ty {
|
|
|
|
Ty::AccountInfo => quote! { #ident },
|
|
|
|
Ty::ProgramAccount(_) => quote! { #ident.to_account_info() },
|
2021-09-07 13:06:15 -07:00
|
|
|
Ty::Account(_) => quote! { #ident.to_account_info() },
|
2021-08-29 14:25:38 -07:00
|
|
|
Ty::Loader(_) => quote! { #ident.to_account_info() },
|
|
|
|
Ty::CpiAccount(_) => quote! { #ident.to_account_info() },
|
2021-05-30 12:59:33 -07:00
|
|
|
_ => panic!("Invalid syntax: signer cannot be specified."),
|
|
|
|
};
|
|
|
|
quote! {
|
|
|
|
// Don't enforce on CPI, since usually a program is signing and so
|
|
|
|
// the `try_accounts` deserializatoin will fail *if* the one
|
|
|
|
// tries to manually invoke it.
|
|
|
|
//
|
|
|
|
// This check will be performed on the other end of the invocation.
|
|
|
|
if cfg!(not(feature = "cpi")) {
|
|
|
|
if !#info.to_account_info().is_signer {
|
2021-06-09 13:02:50 -07:00
|
|
|
return Err(anchor_lang::__private::ErrorCode::ConstraintSigner.into());
|
2021-05-30 12:59:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn generate_constraint_literal(c: &ConstraintLiteral) -> proc_macro2::TokenStream {
|
|
|
|
let lit: proc_macro2::TokenStream = {
|
|
|
|
let lit = &c.lit;
|
2021-06-27 09:50:54 -07:00
|
|
|
let constraint = lit.value().replace("\"", "");
|
|
|
|
let message = format!(
|
|
|
|
"Deprecated. Should be used with constraint: #[account(constraint = {})]",
|
|
|
|
constraint,
|
|
|
|
);
|
|
|
|
lit.span().warning(message).emit_as_item_tokens();
|
|
|
|
constraint.parse().unwrap()
|
2021-05-30 12:59:33 -07:00
|
|
|
};
|
|
|
|
quote! {
|
|
|
|
if !(#lit) {
|
2021-06-09 13:02:50 -07:00
|
|
|
return Err(anchor_lang::__private::ErrorCode::Deprecated.into());
|
2021-05-30 12:59:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn generate_constraint_raw(c: &ConstraintRaw) -> proc_macro2::TokenStream {
|
|
|
|
let raw = &c.raw;
|
|
|
|
quote! {
|
|
|
|
if !(#raw) {
|
2021-06-09 13:02:50 -07:00
|
|
|
return Err(anchor_lang::__private::ErrorCode::ConstraintRaw.into());
|
2021-05-30 12:59:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn generate_constraint_owner(f: &Field, c: &ConstraintOwner) -> proc_macro2::TokenStream {
|
|
|
|
let ident = &f.ident;
|
2021-09-09 14:32:11 -07:00
|
|
|
let owner_address = &c.owner_address;
|
2021-05-30 12:59:33 -07:00
|
|
|
quote! {
|
2021-09-09 14:32:11 -07:00
|
|
|
if #ident.to_account_info().owner != &#owner_address {
|
2021-06-09 13:02:50 -07:00
|
|
|
return Err(anchor_lang::__private::ErrorCode::ConstraintOwner.into());
|
2021-05-30 12:59:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn generate_constraint_rent_exempt(
|
|
|
|
f: &Field,
|
|
|
|
c: &ConstraintRentExempt,
|
|
|
|
) -> proc_macro2::TokenStream {
|
|
|
|
let ident = &f.ident;
|
2021-06-27 13:17:05 -07:00
|
|
|
let info = quote! {
|
|
|
|
#ident.to_account_info()
|
2021-05-30 12:59:33 -07:00
|
|
|
};
|
|
|
|
match c {
|
|
|
|
ConstraintRentExempt::Skip => quote! {},
|
|
|
|
ConstraintRentExempt::Enforce => quote! {
|
2021-08-08 13:20:26 -07:00
|
|
|
if !__anchor_rent.is_exempt(#info.lamports(), #info.try_data_len()?) {
|
2021-06-09 13:02:50 -07:00
|
|
|
return Err(anchor_lang::__private::ErrorCode::ConstraintRentExempt.into());
|
2021-05-30 12:59:33 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-29 14:25:38 -07:00
|
|
|
fn generate_constraint_init_group(f: &Field, c: &ConstraintInitGroup) -> proc_macro2::TokenStream {
|
2021-06-15 17:15:51 -07:00
|
|
|
let payer = {
|
|
|
|
let p = &c.payer;
|
|
|
|
quote! {
|
|
|
|
let payer = #p.to_account_info();
|
2021-05-30 12:59:33 -07:00
|
|
|
}
|
2021-06-15 17:15:51 -07:00
|
|
|
};
|
2021-08-29 14:25:38 -07:00
|
|
|
|
|
|
|
let seeds_with_nonce = match &c.seeds {
|
|
|
|
None => quote! {},
|
|
|
|
Some(c) => {
|
2021-09-22 07:42:39 -07:00
|
|
|
let s = &mut c.seeds.clone();
|
|
|
|
// If the seeds came with a trailing comma, we need to chop it off
|
|
|
|
// before we interpolate them below.
|
|
|
|
if let Some(pair) = s.pop() {
|
|
|
|
s.push_value(pair.into_value());
|
|
|
|
}
|
2021-08-29 14:25:38 -07:00
|
|
|
let inner = match c.bump.as_ref() {
|
2021-08-15 16:35:53 -07:00
|
|
|
// Bump target not given. Use the canonical bump.
|
|
|
|
None => {
|
|
|
|
quote! {
|
|
|
|
[
|
|
|
|
#s,
|
|
|
|
&[
|
|
|
|
Pubkey::find_program_address(
|
|
|
|
&[#s],
|
|
|
|
program_id,
|
|
|
|
).1
|
|
|
|
]
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Bump target given. Use it.
|
|
|
|
Some(b) => quote! {
|
|
|
|
[#s, &[#b]]
|
|
|
|
},
|
2021-08-29 14:25:38 -07:00
|
|
|
};
|
|
|
|
quote! {
|
|
|
|
&#inner[..]
|
|
|
|
}
|
2021-06-15 17:15:51 -07:00
|
|
|
}
|
|
|
|
};
|
2021-09-07 13:06:15 -07:00
|
|
|
generate_init(f, seeds_with_nonce, payer, &c.space, &c.kind)
|
2021-05-30 12:59:33 -07:00
|
|
|
}
|
|
|
|
|
2021-08-29 14:25:38 -07:00
|
|
|
fn generate_constraint_seeds(f: &Field, c: &ConstraintSeedsGroup) -> proc_macro2::TokenStream {
|
2021-06-15 17:15:51 -07:00
|
|
|
let name = &f.ident;
|
2021-08-15 16:35:53 -07:00
|
|
|
let s = &c.seeds;
|
2021-07-09 18:42:05 -07:00
|
|
|
|
2021-08-29 14:25:38 -07:00
|
|
|
// If the bump is provided with init *and target*, then force it to be the
|
|
|
|
// canonical bump.
|
|
|
|
if c.is_init && c.bump.is_some() {
|
|
|
|
let b = c.bump.as_ref().unwrap();
|
2021-07-09 18:42:05 -07:00
|
|
|
quote! {
|
|
|
|
let (__program_signer, __bump) = anchor_lang::solana_program::pubkey::Pubkey::find_program_address(
|
|
|
|
&[#s],
|
|
|
|
program_id,
|
|
|
|
);
|
|
|
|
if #name.to_account_info().key != &__program_signer {
|
|
|
|
return Err(anchor_lang::__private::ErrorCode::ConstraintSeeds.into());
|
|
|
|
}
|
|
|
|
if __bump != #b {
|
|
|
|
return Err(anchor_lang::__private::ErrorCode::ConstraintSeeds.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let seeds = match c.bump.as_ref() {
|
2021-08-29 14:25:38 -07:00
|
|
|
// Bump target not given. Find it.
|
2021-07-09 18:42:05 -07:00
|
|
|
None => {
|
|
|
|
quote! {
|
2021-08-29 14:25:38 -07:00
|
|
|
[
|
|
|
|
#s,
|
|
|
|
&[
|
|
|
|
Pubkey::find_program_address(
|
|
|
|
&[#s],
|
|
|
|
program_id,
|
|
|
|
).1
|
2021-08-15 16:35:53 -07:00
|
|
|
]
|
2021-08-29 14:25:38 -07:00
|
|
|
]
|
2021-07-09 18:42:05 -07:00
|
|
|
}
|
2021-08-29 14:25:38 -07:00
|
|
|
}
|
|
|
|
// Bump target given. Use it.
|
|
|
|
Some(b) => {
|
|
|
|
quote! {
|
|
|
|
[#s, &[#b]]
|
2021-08-15 16:35:53 -07:00
|
|
|
}
|
2021-08-29 14:25:38 -07:00
|
|
|
}
|
2021-07-09 18:42:05 -07:00
|
|
|
};
|
|
|
|
quote! {
|
|
|
|
let __program_signer = Pubkey::create_program_address(
|
2021-08-15 16:35:53 -07:00
|
|
|
&#seeds[..],
|
2021-07-09 18:42:05 -07:00
|
|
|
program_id,
|
|
|
|
).map_err(|_| anchor_lang::__private::ErrorCode::ConstraintSeeds)?;
|
|
|
|
if #name.to_account_info().key != &__program_signer {
|
|
|
|
return Err(anchor_lang::__private::ErrorCode::ConstraintSeeds.into());
|
|
|
|
}
|
2021-05-30 12:59:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-07 13:06:15 -07:00
|
|
|
pub fn generate_init(
|
2021-06-15 17:15:51 -07:00
|
|
|
f: &Field,
|
|
|
|
seeds_with_nonce: proc_macro2::TokenStream,
|
|
|
|
payer: proc_macro2::TokenStream,
|
2021-06-27 13:17:05 -07:00
|
|
|
space: &Option<Expr>,
|
2021-08-29 14:25:38 -07:00
|
|
|
kind: &InitKind,
|
2021-06-15 17:15:51 -07:00
|
|
|
) -> proc_macro2::TokenStream {
|
2021-05-30 12:59:33 -07:00
|
|
|
let field = &f.ident;
|
2021-09-07 13:06:15 -07:00
|
|
|
let ty_decl = f.ty_decl();
|
2021-09-16 15:49:24 -07:00
|
|
|
let from_account_info = f.from_account_info_unchecked(Some(kind));
|
2021-06-27 13:17:05 -07:00
|
|
|
match kind {
|
2021-08-31 14:43:04 -07:00
|
|
|
InitKind::Token { owner, mint } => {
|
|
|
|
let create_account = generate_create_account(
|
|
|
|
field,
|
|
|
|
quote! {anchor_spl::token::TokenAccount::LEN},
|
|
|
|
quote! {token_program.to_account_info().key},
|
|
|
|
seeds_with_nonce,
|
|
|
|
);
|
|
|
|
quote! {
|
2021-09-07 13:06:15 -07:00
|
|
|
let #field: #ty_decl = {
|
2021-08-31 14:43:04 -07:00
|
|
|
// Define payer variable.
|
|
|
|
#payer
|
2021-06-27 13:17:05 -07:00
|
|
|
|
2021-08-31 14:43:04 -07:00
|
|
|
// 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)?;
|
2021-09-16 15:49:24 -07:00
|
|
|
let pa: #ty_decl = #from_account_info;
|
2021-09-07 13:06:15 -07:00
|
|
|
pa
|
2021-08-14 18:18:28 -07:00
|
|
|
};
|
2021-08-31 14:43:04 -07:00
|
|
|
}
|
|
|
|
}
|
2021-09-23 17:06:38 -07:00
|
|
|
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)?;
|
|
|
|
let pa: #ty_decl = #from_account_info;
|
|
|
|
pa
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2021-08-31 14:43:04 -07:00
|
|
|
InitKind::Mint { owner, decimals } => {
|
|
|
|
let create_account = generate_create_account(
|
|
|
|
field,
|
|
|
|
quote! {anchor_spl::token::Mint::LEN},
|
|
|
|
quote! {token_program.to_account_info().key},
|
|
|
|
seeds_with_nonce,
|
|
|
|
);
|
|
|
|
quote! {
|
2021-09-07 13:06:15 -07:00
|
|
|
let #field: #ty_decl = {
|
2021-08-31 14:43:04 -07:00
|
|
|
// Define payer variable.
|
|
|
|
#payer
|
2021-06-27 13:17:05 -07:00
|
|
|
|
2021-08-31 14:43:04 -07:00
|
|
|
// 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, None)?;
|
2021-09-16 15:49:24 -07:00
|
|
|
let pa: #ty_decl = #from_account_info;
|
2021-09-07 13:06:15 -07:00
|
|
|
pa
|
2021-06-27 13:17:05 -07:00
|
|
|
};
|
2021-08-31 14:43:04 -07:00
|
|
|
}
|
|
|
|
}
|
2021-08-29 14:25:38 -07:00
|
|
|
InitKind::Program { owner } => {
|
2021-08-14 18:18:28 -07:00
|
|
|
let space = match space {
|
|
|
|
// If no explicit space param was given, serialize the type to bytes
|
|
|
|
// and take the length (with +8 for the discriminator.)
|
2021-09-07 13:06:15 -07:00
|
|
|
None => {
|
|
|
|
let account_ty = f.account_ty();
|
|
|
|
match matches!(f.ty, Ty::Loader(_)) {
|
|
|
|
false => {
|
|
|
|
quote! {
|
|
|
|
let space = 8 + #account_ty::default().try_to_vec().unwrap().len();
|
|
|
|
}
|
2021-08-14 18:18:28 -07:00
|
|
|
}
|
2021-09-07 13:06:15 -07:00
|
|
|
true => {
|
|
|
|
quote! {
|
|
|
|
let space = 8 + anchor_lang::__private::bytemuck::bytes_of(&#account_ty::default()).len();
|
|
|
|
}
|
2021-08-14 18:18:28 -07:00
|
|
|
}
|
|
|
|
}
|
2021-09-07 13:06:15 -07:00
|
|
|
}
|
2021-08-14 18:18:28 -07:00
|
|
|
// Explicit account size given. Use it.
|
|
|
|
Some(s) => quote! {
|
2021-08-29 14:25:38 -07:00
|
|
|
let space = #s;
|
2021-08-14 18:18:28 -07:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-07-09 18:42:05 -07:00
|
|
|
// Owner of the account being created. If not specified,
|
|
|
|
// default to the currently executing program.
|
|
|
|
let owner = match owner {
|
|
|
|
None => quote! {
|
|
|
|
program_id
|
|
|
|
},
|
|
|
|
Some(o) => quote! {
|
|
|
|
&#o
|
|
|
|
},
|
|
|
|
};
|
2021-08-31 14:43:04 -07:00
|
|
|
let create_account =
|
|
|
|
generate_create_account(field, quote! {space}, owner, seeds_with_nonce);
|
2021-06-27 13:17:05 -07:00
|
|
|
quote! {
|
2021-07-09 18:42:05 -07:00
|
|
|
let #field = {
|
2021-06-27 13:17:05 -07:00
|
|
|
#space
|
|
|
|
#payer
|
2021-08-31 14:43:04 -07:00
|
|
|
#create_account
|
2021-09-16 15:49:24 -07:00
|
|
|
let pa: #ty_decl = #from_account_info;
|
2021-06-27 13:17:05 -07:00
|
|
|
pa
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2021-05-30 12:59:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-31 14:43:04 -07:00
|
|
|
// Generated code to create an account with with system program with the
|
|
|
|
// given `space` amount of data, owned by `owner`.
|
|
|
|
//
|
|
|
|
// `seeds_with_nonce` should be given for creating PDAs. Otherwise it's an
|
|
|
|
// empty stream.
|
|
|
|
pub fn generate_create_account(
|
|
|
|
field: &Ident,
|
|
|
|
space: proc_macro2::TokenStream,
|
|
|
|
owner: proc_macro2::TokenStream,
|
|
|
|
seeds_with_nonce: proc_macro2::TokenStream,
|
|
|
|
) -> proc_macro2::TokenStream {
|
|
|
|
quote! {
|
|
|
|
// If the account being initialized already has lamports, then
|
|
|
|
// return them all back to the payer so that the account has
|
|
|
|
// zero lamports when the system program's create instruction
|
|
|
|
// is eventually called.
|
|
|
|
let __current_lamports = #field.to_account_info().lamports();
|
|
|
|
if __current_lamports == 0 {
|
|
|
|
// Create the token account with right amount of lamports and space, and the correct owner.
|
|
|
|
let lamports = __anchor_rent.minimum_balance(#space);
|
|
|
|
anchor_lang::solana_program::program::invoke_signed(
|
|
|
|
&anchor_lang::solana_program::system_instruction::create_account(
|
|
|
|
payer.to_account_info().key,
|
|
|
|
#field.to_account_info().key,
|
|
|
|
lamports,
|
|
|
|
#space as u64,
|
|
|
|
#owner,
|
|
|
|
),
|
|
|
|
&[
|
|
|
|
payer.to_account_info(),
|
|
|
|
#field.to_account_info(),
|
2021-09-11 14:43:12 -07:00
|
|
|
system_program.to_account_info(),
|
2021-08-31 14:43:04 -07:00
|
|
|
],
|
|
|
|
&[#seeds_with_nonce],
|
|
|
|
)?;
|
|
|
|
} else {
|
|
|
|
// Fund the account for rent exemption.
|
|
|
|
let required_lamports = __anchor_rent
|
|
|
|
.minimum_balance(#space)
|
|
|
|
.max(1)
|
|
|
|
.saturating_sub(__current_lamports);
|
|
|
|
if required_lamports > 0 {
|
|
|
|
anchor_lang::solana_program::program::invoke(
|
|
|
|
&anchor_lang::solana_program::system_instruction::transfer(
|
|
|
|
payer.to_account_info().key,
|
|
|
|
#field.to_account_info().key,
|
|
|
|
required_lamports,
|
|
|
|
),
|
|
|
|
&[
|
|
|
|
payer.to_account_info(),
|
|
|
|
#field.to_account_info(),
|
2021-09-11 14:43:12 -07:00
|
|
|
system_program.to_account_info(),
|
2021-08-31 14:43:04 -07:00
|
|
|
],
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
// Allocate space.
|
|
|
|
anchor_lang::solana_program::program::invoke_signed(
|
|
|
|
&anchor_lang::solana_program::system_instruction::allocate(
|
|
|
|
#field.to_account_info().key,
|
|
|
|
#space as u64,
|
|
|
|
),
|
|
|
|
&[
|
|
|
|
#field.to_account_info(),
|
2021-09-11 14:43:12 -07:00
|
|
|
system_program.to_account_info(),
|
2021-08-31 14:43:04 -07:00
|
|
|
],
|
|
|
|
&[#seeds_with_nonce],
|
|
|
|
)?;
|
|
|
|
// Assign to the spl token program.
|
|
|
|
anchor_lang::solana_program::program::invoke_signed(
|
|
|
|
&anchor_lang::solana_program::system_instruction::assign(
|
|
|
|
#field.to_account_info().key,
|
|
|
|
#owner,
|
|
|
|
),
|
|
|
|
&[
|
|
|
|
#field.to_account_info(),
|
|
|
|
system_program.to_account_info(),
|
|
|
|
],
|
|
|
|
&[#seeds_with_nonce],
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-15 17:15:51 -07:00
|
|
|
pub fn generate_constraint_executable(
|
|
|
|
f: &Field,
|
|
|
|
_c: &ConstraintExecutable,
|
|
|
|
) -> proc_macro2::TokenStream {
|
|
|
|
let name = &f.ident;
|
|
|
|
quote! {
|
|
|
|
if !#name.to_account_info().executable {
|
|
|
|
return Err(anchor_lang::__private::ErrorCode::ConstraintExecutable.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn generate_constraint_state(f: &Field, c: &ConstraintState) -> proc_macro2::TokenStream {
|
|
|
|
let program_target = c.program_target.clone();
|
|
|
|
let ident = &f.ident;
|
|
|
|
let account_ty = match &f.ty {
|
2021-07-08 13:14:39 -07:00
|
|
|
Ty::CpiState(ty) => &ty.account_type_path,
|
2021-06-15 17:15:51 -07:00
|
|
|
_ => panic!("Invalid state constraint"),
|
|
|
|
};
|
|
|
|
quote! {
|
|
|
|
// Checks the given state account is the canonical state account for
|
|
|
|
// the target program.
|
|
|
|
if #ident.to_account_info().key != &anchor_lang::CpiState::<#account_ty>::address(#program_target.to_account_info().key) {
|
|
|
|
return Err(anchor_lang::__private::ErrorCode::ConstraintState.into());
|
|
|
|
}
|
|
|
|
if #ident.to_account_info().owner != #program_target.to_account_info().key {
|
|
|
|
return Err(anchor_lang::__private::ErrorCode::ConstraintState.into());
|
|
|
|
}
|
2021-05-30 12:59:33 -07:00
|
|
|
}
|
|
|
|
}
|