Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mint::freeze_authority attribute when init-ing a mint. #835

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lang/syn/src/codegen/accounts/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,13 +433,21 @@ pub fn generate_init(
};
}
}
InitKind::Mint { owner, decimals } => {
InitKind::Mint {
owner,
decimals,
freeze_authority,
} => {
let create_account = generate_create_account(
field,
quote! {anchor_spl::token::Mint::LEN},
quote! {token_program.to_account_info().key},
seeds_with_nonce,
);
let freeze_authority = match freeze_authority {
Some(fa) => quote! { Some(&#fa.key()) },
None => quote! { None },
};
quote! {
let #field: #ty_decl = {
// Define payer variable.
Expand All @@ -455,7 +463,7 @@ pub fn generate_init(
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)?;
anchor_spl::token::initialize_mint(cpi_ctx, #decimals, &#owner.to_account_info().key, #freeze_authority)?;
let pa: #ty_decl = #from_account_info;
pa
};
Expand Down
26 changes: 22 additions & 4 deletions lang/syn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ pub enum ConstraintToken {
AssociatedTokenMint(Context<ConstraintTokenMint>),
AssociatedTokenAuthority(Context<ConstraintTokenAuthority>),
MintAuthority(Context<ConstraintMintAuthority>),
MintFreezeAuthority(Context<ConstraintMintFreezeAuthority>),
MintDecimals(Context<ConstraintMintDecimals>),
Bump(Context<ConstraintTokenBump>),
}
Expand Down Expand Up @@ -658,12 +659,24 @@ pub struct ConstraintSpace {
#[derive(Debug, Clone)]
#[allow(clippy::large_enum_variant)]
pub enum InitKind {
Program { owner: Option<Expr> },
Program {
owner: Option<Expr>,
},
// Owner for token and mint represents the authority. Not to be confused
// with the owner of the AccountInfo.
Token { owner: Expr, mint: Expr },
AssociatedToken { owner: Expr, mint: Expr },
Mint { owner: Expr, decimals: Expr },
Token {
owner: Expr,
mint: Expr,
},
AssociatedToken {
owner: Expr,
mint: Expr,
},
Mint {
owner: Expr,
freeze_authority: Option<Expr>,
decimals: Expr,
},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why my rust fmt formats this different :/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you mean the order of the fields? or just the indentation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah just indentation stuff—it added newlines to Token and AssociatedToken that I wasn't expecting (my change only touches Mint).

}

#[derive(Debug, Clone)]
Expand All @@ -686,6 +699,11 @@ pub struct ConstraintMintAuthority {
mint_auth: Expr,
}

#[derive(Debug, Clone)]
pub struct ConstraintMintFreezeAuthority {
mint_freeze_auth: Expr,
}

#[derive(Debug, Clone)]
pub struct ConstraintMintDecimals {
decimals: Expr,
Expand Down
33 changes: 32 additions & 1 deletion lang/syn/src/parser/accounts/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ pub fn parse_token(stream: ParseStream) -> ParseResult<ConstraintToken> {
mint_auth: stream.parse()?,
},
)),
"freeze_authority" => ConstraintToken::MintFreezeAuthority(Context::new(
span,
ConstraintMintFreezeAuthority {
mint_freeze_auth: stream.parse()?,
},
)),
"decimals" => ConstraintToken::MintDecimals(Context::new(
span,
ConstraintMintDecimals {
Expand Down Expand Up @@ -276,6 +282,7 @@ pub struct ConstraintGroupBuilder<'ty> {
pub associated_token_mint: Option<Context<ConstraintTokenMint>>,
pub associated_token_authority: Option<Context<ConstraintTokenAuthority>>,
pub mint_authority: Option<Context<ConstraintMintAuthority>>,
pub mint_freeze_authority: Option<Context<ConstraintMintFreezeAuthority>>,
pub mint_decimals: Option<Context<ConstraintMintDecimals>>,
pub bump: Option<Context<ConstraintTokenBump>>,
}
Expand Down Expand Up @@ -305,6 +312,7 @@ impl<'ty> ConstraintGroupBuilder<'ty> {
associated_token_mint: None,
associated_token_authority: None,
mint_authority: None,
mint_freeze_authority: None,
mint_decimals: None,
bump: None,
}
Expand Down Expand Up @@ -460,6 +468,7 @@ impl<'ty> ConstraintGroupBuilder<'ty> {
associated_token_mint,
associated_token_authority,
mint_authority,
mint_freeze_authority,
mint_decimals,
bump,
} = self;
Expand Down Expand Up @@ -523,7 +532,8 @@ impl<'ty> ConstraintGroupBuilder<'ty> {
d.span(),
"authority must be provided to initialize a mint program derived address"
))
}
},
freeze_authority: mint_freeze_authority.map(|fa| fa.into_inner().mint_freeze_auth)
}
} else {
InitKind::Program {
Expand Down Expand Up @@ -570,6 +580,7 @@ impl<'ty> ConstraintGroupBuilder<'ty> {
ConstraintToken::AssociatedTokenAuthority(c) => self.add_associated_token_authority(c),
ConstraintToken::AssociatedTokenMint(c) => self.add_associated_token_mint(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),
ConstraintToken::Bump(c) => self.add_bump(c),
}
Expand Down Expand Up @@ -739,6 +750,26 @@ impl<'ty> ConstraintGroupBuilder<'ty> {
Ok(())
}

fn add_mint_freeze_authority(
&mut self,
c: Context<ConstraintMintFreezeAuthority>,
) -> ParseResult<()> {
if self.mint_freeze_authority.is_some() {
return Err(ParseError::new(
c.span(),
"mint freeze_authority already provided",
));
}
if self.init.is_none() {
return Err(ParseError::new(
c.span(),
"init must be provided before mint freeze_authority",
));
}
self.mint_freeze_authority.replace(c);
Ok(())
}

fn add_mint_decimals(&mut self, c: Context<ConstraintMintDecimals>) -> ParseResult<()> {
if self.mint_decimals.is_some() {
return Err(ParseError::new(c.span(), "mint decimals already provided"));
Expand Down
4 changes: 2 additions & 2 deletions tests/misc/programs/misc/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ pub struct TestInitZeroCopy<'info> {

#[derive(Accounts)]
pub struct TestInitMint<'info> {
#[account(init, mint::decimals = 6, mint::authority = payer, payer = payer)]
#[account(init, mint::decimals = 6, mint::authority = payer, mint::freeze_authority = payer, payer = payer)]
pub mint: Account<'info, Mint>,
#[account(signer)]
pub payer: AccountInfo<'info>,
Expand Down Expand Up @@ -218,4 +218,4 @@ pub struct TestFetchAll<'info> {
pub data: Account<'info, DataWithFilter>,
pub authority: Signer<'info>,
pub system_program: Program<'info, System>,
}
}
3 changes: 3 additions & 0 deletions tests/misc/tests/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,9 @@ describe("misc", () => {
assert.ok(
mintAccount.mintAuthority.equals(program.provider.wallet.publicKey)
);
assert.ok(
mintAccount.freezeAuthority.equals(program.provider.wallet.publicKey)
);
});

it("Can create a random mint account prefunded", async () => {
Expand Down