Skip to content

Commit

Permalink
lang: Add mint::freeze_authority attribute for init mint (#835)
Browse files Browse the repository at this point in the history
  • Loading branch information
cqfd authored Oct 5, 2021
1 parent 8566b90 commit dbb5f48
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ incremented for features.

* cli: `target/types` directory now created on build to store a TypeScript types file for each program's IDL ([#795](https://github.com/project-serum/anchor/pull/795)).
* ts: `Program<T>` can now be typed with an IDL type ([#795](https://github.com/project-serum/anchor/pull/795)).
* lang: Add `mint::freeze_authority` keyword for mint initialization within `#[derive(Accounts)]` ([#835](https://github.com/project-serum/anchor/pull/835)).

## [0.17.0] - 2021-10-03

Expand Down
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,
},
}

#[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

0 comments on commit dbb5f48

Please sign in to comment.