Skip to content

Commit

Permalink
- Rename ata token program constraints for clarity.
Browse files Browse the repository at this point in the history
- Add token program constraint for initialized mints.
  • Loading branch information
elliotkennedy committed Apr 14, 2023
1 parent 8c3a0c2 commit e7a54fe
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 26 deletions.
2 changes: 1 addition & 1 deletion lang/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ pub enum ErrorCode {
ConstraintMintTokenProgram,
/// 2023 - A mint token program constraint was violated
#[msg("An associated token account token program constraint was violated")]
ConstraintAssociatedTokenProgram,
ConstraintAssociatedTokenTokenProgram,

// Require
/// 2500 - A require expression was violated
Expand Down
13 changes: 12 additions & 1 deletion lang/syn/src/codegen/accounts/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ fn generate_constraint_associated_token(
let token_program_optional_check = optional_check_scope.generate_check(token_program);
quote! {
#token_program_optional_check
if #name.to_account_info().owner != &#token_program.key() { return Err(anchor_lang::error::ErrorCode::ConstraintAssociatedTokenProgram.into()); }
if #name.to_account_info().owner != &#token_program.key() { return Err(anchor_lang::error::ErrorCode::ConstraintAssociatedTokenTokenProgram.into()); }
}
}
None => quote! {},
Expand Down Expand Up @@ -1027,11 +1027,22 @@ fn generate_constraint_mint(
}
None => quote! {},
};
let token_program_check = match &c.token_program {
Some(token_program) => {
let token_program_optional_check = optional_check_scope.generate_check(token_program);
quote! {
#token_program_optional_check
if #name.to_account_info().owner != &#token_program.key() { return Err(anchor_lang::error::ErrorCode::ConstraintMintTokenProgram.into()); }
}
}
None => quote! {},
};
quote! {
{
#decimal_check
#mint_authority_check
#freeze_authority_check
#token_program_check
}
}
}
Expand Down
9 changes: 2 additions & 7 deletions lang/syn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,11 +665,11 @@ pub enum ConstraintToken {
TokenTokenProgram(Context<ConstraintTokenProgram>),
AssociatedTokenMint(Context<ConstraintTokenMint>),
AssociatedTokenAuthority(Context<ConstraintTokenAuthority>),
AssociatedTokenProgram(Context<ConstraintTokenProgram>),
AssociatedTokenTokenProgram(Context<ConstraintTokenProgram>),
MintAuthority(Context<ConstraintMintAuthority>),
MintFreezeAuthority(Context<ConstraintMintFreezeAuthority>),
MintDecimals(Context<ConstraintMintDecimals>),
MintTokenProgram(Context<ConstraintMintTokenProgram>),
MintTokenProgram(Context<ConstraintTokenProgram>),
Bump(Context<ConstraintTokenBump>),
ProgramSeed(Context<ConstraintProgramSeed>),
Realloc(Context<ConstraintRealloc>),
Expand Down Expand Up @@ -855,11 +855,6 @@ pub struct ConstraintMintDecimals {
pub decimals: Expr,
}

#[derive(Debug, Clone)]
pub struct ConstraintMintTokenProgram {
token_program: Expr,
}

#[derive(Debug, Clone)]
pub struct ConstraintTokenBump {
pub bump: Option<Expr>,
Expand Down
34 changes: 17 additions & 17 deletions lang/syn/src/parser/accounts/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub fn parse_token(stream: ParseStream) -> ParseResult<ConstraintToken> {
)),
"token_program" => ConstraintToken::MintTokenProgram(Context::new(
span,
ConstraintMintTokenProgram {
ConstraintTokenProgram {
token_program: stream.parse()?,
},
)),
Expand Down Expand Up @@ -150,7 +150,7 @@ pub fn parse_token(stream: ParseStream) -> ParseResult<ConstraintToken> {
auth: stream.parse()?,
},
)),
"token_program" => ConstraintToken::AssociatedTokenProgram(Context::new(
"token_program" => ConstraintToken::AssociatedTokenTokenProgram(Context::new(
span,
ConstraintTokenProgram {
token_program: stream.parse()?,
Expand Down Expand Up @@ -353,11 +353,11 @@ pub struct ConstraintGroupBuilder<'ty> {
pub token_token_program: Option<Context<ConstraintTokenProgram>>,
pub associated_token_mint: Option<Context<ConstraintTokenMint>>,
pub associated_token_authority: Option<Context<ConstraintTokenAuthority>>,
pub associated_token_program: Option<Context<ConstraintTokenProgram>>,
pub associated_token_token_program: Option<Context<ConstraintTokenProgram>>,
pub mint_authority: Option<Context<ConstraintMintAuthority>>,
pub mint_freeze_authority: Option<Context<ConstraintMintFreezeAuthority>>,
pub mint_decimals: Option<Context<ConstraintMintDecimals>>,
pub mint_token_program: Option<Context<ConstraintMintTokenProgram>>,
pub mint_token_program: Option<Context<ConstraintTokenProgram>>,
pub bump: Option<Context<ConstraintTokenBump>>,
pub program_seed: Option<Context<ConstraintProgramSeed>>,
pub realloc: Option<Context<ConstraintRealloc>>,
Expand Down Expand Up @@ -388,7 +388,7 @@ impl<'ty> ConstraintGroupBuilder<'ty> {
token_token_program: None,
associated_token_mint: None,
associated_token_authority: None,
associated_token_program: None,
associated_token_token_program: None,
mint_authority: None,
mint_freeze_authority: None,
mint_decimals: None,
Expand Down Expand Up @@ -590,7 +590,7 @@ impl<'ty> ConstraintGroupBuilder<'ty> {
token_token_program,
associated_token_mint,
associated_token_authority,
associated_token_program,
associated_token_token_program,
mint_authority,
mint_freeze_authority,
mint_decimals,
Expand Down Expand Up @@ -630,12 +630,12 @@ impl<'ty> ConstraintGroupBuilder<'ty> {
let associated_token = match (
associated_token_mint,
associated_token_authority,
&associated_token_program,
&associated_token_token_program,
) {
(Some(mint), Some(auth), _) => Some(ConstraintAssociatedToken {
wallet: auth.into_inner().auth,
mint: mint.into_inner().mint,
token_program: associated_token_program
token_program: associated_token_token_program
.as_ref()
.map(|a| a.clone().into_inner().token_program),
}),
Expand Down Expand Up @@ -718,7 +718,7 @@ impl<'ty> ConstraintGroupBuilder<'ty> {
InitKind::AssociatedToken {
mint: at.mint.clone(),
owner: at.wallet.clone(),
token_program: associated_token_program.map(|tp| tp.into_inner().token_program),
token_program: associated_token_token_program.map(|tp| tp.into_inner().token_program),
}
} else if let Some(d) = &mint_decimals {
InitKind::Mint {
Expand Down Expand Up @@ -782,7 +782,7 @@ impl<'ty> ConstraintGroupBuilder<'ty> {
ConstraintToken::TokenTokenProgram(c) => self.add_token_token_program(c),
ConstraintToken::AssociatedTokenAuthority(c) => self.add_associated_token_authority(c),
ConstraintToken::AssociatedTokenMint(c) => self.add_associated_token_mint(c),
ConstraintToken::AssociatedTokenProgram(c) => self.add_associated_token_program(c),
ConstraintToken::AssociatedTokenTokenProgram(c) => self.add_associated_token_token_program(c),
ConstraintToken::MintAuthority(c) => self.add_mint_authority(c),
ConstraintToken::MintFreezeAuthority(c) => self.add_mint_freeze_authority(c),
ConstraintToken::MintDecimals(c) => self.add_mint_decimals(c),
Expand Down Expand Up @@ -856,10 +856,10 @@ impl<'ty> ConstraintGroupBuilder<'ty> {
"init must be provided before associated token authority",
));
}
if self.associated_token_program.is_some() {
if self.associated_token_token_program.is_some() {
return Err(ParseError::new(
c.span(),
"init must be provided before associated token program",
"init must be provided before associated token account token program",
));
}
self.init.replace(c);
Expand Down Expand Up @@ -1068,17 +1068,17 @@ impl<'ty> ConstraintGroupBuilder<'ty> {
Ok(())
}

fn add_associated_token_program(
fn add_associated_token_token_program(
&mut self,
c: Context<ConstraintTokenProgram>,
) -> ParseResult<()> {
if self.associated_token_program.is_some() {
if self.associated_token_token_program.is_some() {
return Err(ParseError::new(
c.span(),
"associated token_program already provided",
"associated token token_program already provided",
));
}
self.associated_token_program.replace(c);
self.associated_token_token_program.replace(c);
Ok(())
}

Expand Down Expand Up @@ -1114,7 +1114,7 @@ impl<'ty> ConstraintGroupBuilder<'ty> {

fn add_mint_token_program(
&mut self,
c: Context<ConstraintMintTokenProgram>,
c: Context<ConstraintTokenProgram>,
) -> ParseResult<()> {
if self.mint_token_program.is_some() {
return Err(ParseError::new(
Expand Down

0 comments on commit e7a54fe

Please sign in to comment.