Skip to content

Commit

Permalink
lang: Consistent init constraints (#641)
Browse files Browse the repository at this point in the history
  • Loading branch information
armaniferrante committed Aug 29, 2021
1 parent 2604a44 commit 75c2085
Show file tree
Hide file tree
Showing 26 changed files with 487 additions and 408 deletions.
14 changes: 9 additions & 5 deletions CHANGELOG.md
Expand Up @@ -14,13 +14,17 @@ incremented for features.
### Features

* lang: Ignore `Unnamed` structs instead of panic ([#605](https://github.com/project-serum/anchor/pull/605)).
* lang: Add constraints for initializing mint accounts as pdas, `#[account(init, seeds = [...], mint::decimals = <expr>, mint::authority = <expr>)]` ([#](https://github.com/project-serum/anchor/pull/562)).
* lang: Add constraints for initializing mint accounts as pdas, `#[account(init, seeds = [...], mint::decimals = <expr>, mint::authority = <expr>)]` ([#562](https://github.com/project-serum/anchor/pull/562)).

### Breaking Changes

* lang: Change `#[account(init, seeds = [...], token = <expr>, authority = <expr>)]` to `#[account(init, token::mint = <expr> token::authority = <expr>)]` ([#](https://github.com/project-serum/anchor/pull/562)).
* lang: `#[associated]` and `#[account(associated = <target>, with = <target>)]` are both removed.
* cli: Removed `anchor launch` command
* lang: Change `#[account(init, seeds = [...], token = <expr>, authority = <expr>)]` to `#[account(init, token::mint = <expr> token::authority = <expr>)]` ([#562](https://github.com/project-serum/anchor/pull/562)).
* lang: `#[associated]` and `#[account(associated = <target>, with = <target>)]` are both removed ([#612](https://github.com/project-serum/anchor/pull/612)).
* cli: Removed `anchor launch` command ([#634](https://github.com/project-serum/anchor/pull/634)).
* lang: `#[account(init)]` now creates the account inside the same instruction to be consistent with initializing PDAs. To maintain the old behavior of `init`, replace it with `#[account(zero)]` ([#641](https://github.com/project-serum/anchor/pull/641)).
* lang: `bump` must be provided when using the `seeds` constraint. This has been added as an extra safety constraint to ensure that whenever a PDA is initialized via a constraint the bump used is the one created by `Pubkey::find_program_address` ([#641](https://github.com/project-serum/anchor/pull/641)).
* lang: `try_from_init` has been removed from `Loader`, `ProgramAccount`, and `CpiAccount` and replaced with `try_from_unchecked` ([#641](https://github.com/project-serum/anchor/pull/641)).
* lang: Remove `AccountsInit` trait ([#641](https://github.com/project-serum/anchor/pull/641)).

## [0.13.2] - 2021-08-11

Expand All @@ -32,7 +36,7 @@ incremented for features.

### Features

* cli: Programs embedded into genesis during tests will produce program logs.
* cli: Programs embedded into genesis during tests will produce program logs ([#594](https://github.com/project-serum/anchor/pull/594)).

### Fixes

Expand Down
20 changes: 10 additions & 10 deletions examples/cashiers-check/programs/cashiers-check/src/lib.rs
Expand Up @@ -86,15 +86,15 @@ pub struct CreateCheck<'info> {
#[account(zero)]
check: ProgramAccount<'info, Check>,
// Check's token vault.
#[account(mut, "&vault.owner == check_signer.key")]
#[account(mut, constraint = &vault.owner == check_signer.key)]
vault: CpiAccount<'info, TokenAccount>,
// Program derived address for the check.
check_signer: AccountInfo<'info>,
// Token account the check is made from.
#[account(mut, has_one = owner)]
from: CpiAccount<'info, TokenAccount>,
// Token account the check is made to.
#[account("from.mint == to.mint")]
#[account(constraint = from.mint == to.mint)]
to: CpiAccount<'info, TokenAccount>,
// Owner of the `from` token account.
owner: AccountInfo<'info>,
Expand All @@ -121,10 +121,10 @@ pub struct CashCheck<'info> {
check: ProgramAccount<'info, Check>,
#[account(mut)]
vault: AccountInfo<'info>,
#[account(seeds = [
check.to_account_info().key.as_ref(),
&[check.nonce],
])]
#[account(
seeds = [check.to_account_info().key.as_ref()],
bump = check.nonce,
)]
check_signer: AccountInfo<'info>,
#[account(mut, has_one = owner)]
to: CpiAccount<'info, TokenAccount>,
Expand All @@ -139,10 +139,10 @@ pub struct CancelCheck<'info> {
check: ProgramAccount<'info, Check>,
#[account(mut)]
vault: AccountInfo<'info>,
#[account(seeds = [
check.to_account_info().key.as_ref(),
&[check.nonce],
])]
#[account(
seeds = [check.to_account_info().key.as_ref()],
bump = check.nonce,
)]
check_signer: AccountInfo<'info>,
#[account(mut, has_one = owner)]
from: CpiAccount<'info, TokenAccount>,
Expand Down
18 changes: 14 additions & 4 deletions examples/cfo/programs/cfo/src/lib.rs
Expand Up @@ -383,7 +383,10 @@ pub struct SetDistribution<'info> {

#[derive(Accounts)]
pub struct SweepFees<'info> {
#[account(seeds = [dex.dex_program.key.as_ref(), &[officer.bumps.bump]])]
#[account(
seeds = [dex.dex_program.key.as_ref()],
bump = officer.bumps.bump,
)]
officer: ProgramAccount<'info, Officer>,
#[account(
mut,
Expand Down Expand Up @@ -411,7 +414,10 @@ pub struct Dex<'info> {

#[derive(Accounts)]
pub struct SwapToUsdc<'info> {
#[account(seeds = [dex_program.key().as_ref(), &[officer.bumps.bump]])]
#[account(
seeds = [dex_program.key().as_ref()],
bump = officer.bumps.bump,
)]
officer: ProgramAccount<'info, Officer>,
market: DexMarketAccounts<'info>,
#[account(
Expand All @@ -437,7 +443,10 @@ pub struct SwapToUsdc<'info> {

#[derive(Accounts)]
pub struct SwapToSrm<'info> {
#[account(seeds = [dex_program.key().as_ref(), &[officer.bumps.bump]])]
#[account(
seeds = [dex_program.key().as_ref()],
bump = officer.bumps.bump,
)]
officer: ProgramAccount<'info, Officer>,
market: DexMarketAccounts<'info>,
#[account(
Expand Down Expand Up @@ -529,7 +538,8 @@ pub struct DropStakeReward<'info> {
)]
officer: ProgramAccount<'info, Officer>,
#[account(
seeds = [b"stake", officer.key().as_ref(), &[officer.bumps.stake]]
seeds = [b"stake", officer.key().as_ref()],
bump = officer.bumps.stake,
)]
stake: CpiAccount<'info, TokenAccount>,
#[cfg_attr(
Expand Down
3 changes: 2 additions & 1 deletion examples/chat/programs/chat/src/lib.rs
Expand Up @@ -60,7 +60,8 @@ pub struct CreateChatRoom<'info> {
#[derive(Accounts)]
pub struct SendMessage<'info> {
#[account(
seeds = [authority.key().as_ref(), &[user.bump]],
seeds = [authority.key().as_ref()],
bump = user.bump,
has_one = authority,
)]
user: ProgramAccount<'info, User>,
Expand Down
20 changes: 16 additions & 4 deletions examples/ido-pool/programs/ido-pool/src/lib.rs
Expand Up @@ -232,7 +232,10 @@ impl<'info> InitializePool<'info> {
pub struct ExchangeUsdcForRedeemable<'info> {
#[account(has_one = redeemable_mint, has_one = pool_usdc)]
pub pool_account: ProgramAccount<'info, PoolAccount>,
#[account(seeds = [pool_account.watermelon_mint.as_ref(), &[pool_account.nonce]])]
#[account(
seeds = [pool_account.watermelon_mint.as_ref()],
bump = pool_account.nonce,
)]
pool_signer: AccountInfo<'info>,
#[account(
mut,
Expand All @@ -256,7 +259,10 @@ pub struct ExchangeUsdcForRedeemable<'info> {
pub struct ExchangeRedeemableForUsdc<'info> {
#[account(has_one = redeemable_mint, has_one = pool_usdc)]
pub pool_account: ProgramAccount<'info, PoolAccount>,
#[account(seeds = [pool_account.watermelon_mint.as_ref(), &[pool_account.nonce]])]
#[account(
seeds = [pool_account.watermelon_mint.as_ref()],
bump = pool_account.nonce,
)]
pool_signer: AccountInfo<'info>,
#[account(
mut,
Expand All @@ -280,7 +286,10 @@ pub struct ExchangeRedeemableForUsdc<'info> {
pub struct ExchangeRedeemableForWatermelon<'info> {
#[account(has_one = redeemable_mint, has_one = pool_watermelon)]
pub pool_account: ProgramAccount<'info, PoolAccount>,
#[account(seeds = [pool_account.watermelon_mint.as_ref(), &[pool_account.nonce]])]
#[account(
seeds = [pool_account.watermelon_mint.as_ref()],
bump = pool_account.nonce,
)]
pool_signer: AccountInfo<'info>,
#[account(
mut,
Expand All @@ -304,7 +313,10 @@ pub struct ExchangeRedeemableForWatermelon<'info> {
pub struct WithdrawPoolUsdc<'info> {
#[account(has_one = pool_usdc, has_one = distribution_authority)]
pub pool_account: ProgramAccount<'info, PoolAccount>,
#[account(seeds = [pool_account.watermelon_mint.as_ref(), &[pool_account.nonce]])]
#[account(
seeds = [pool_account.watermelon_mint.as_ref()],
bump = pool_account.nonce,
)]
pub pool_signer: AccountInfo<'info>,
#[account(mut, constraint = pool_usdc.owner == *pool_signer.key)]
pub pool_usdc: CpiAccount<'info, TokenAccount>,
Expand Down
16 changes: 11 additions & 5 deletions examples/lockup/programs/lockup/src/lib.rs
Expand Up @@ -217,7 +217,7 @@ pub struct CreateVesting<'info> {
#[account(signer)]
depositor_authority: AccountInfo<'info>,
// Misc.
#[account("token_program.key == &token::ID")]
#[account(constraint = token_program.key == &token::ID)]
token_program: AccountInfo<'info>,
clock: Sysvar<'info, Clock>,
}
Expand Down Expand Up @@ -251,13 +251,16 @@ pub struct Withdraw<'info> {
beneficiary: AccountInfo<'info>,
#[account(mut)]
vault: CpiAccount<'info, TokenAccount>,
#[account(seeds = [vesting.to_account_info().key.as_ref(), &[vesting.nonce]])]
#[account(
seeds = [vesting.to_account_info().key.as_ref()],
bump = vesting.nonce,
)]
vesting_signer: AccountInfo<'info>,
// Withdraw receiving target..
#[account(mut)]
token: CpiAccount<'info, TokenAccount>,
// Misc.
#[account("token_program.key == &token::ID")]
#[account(constraint = token_program.key == &token::ID)]
token_program: AccountInfo<'info>,
clock: Sysvar<'info, Clock>,
}
Expand All @@ -282,9 +285,12 @@ pub struct WhitelistTransfer<'info> {
// Whitelist interface.
#[account(mut, has_one = beneficiary, has_one = vault)]
vesting: ProgramAccount<'info, Vesting>,
#[account(mut, "&vault.owner == vesting_signer.key")]
#[account(mut, constraint = &vault.owner == vesting_signer.key)]
vault: CpiAccount<'info, TokenAccount>,
#[account(seeds = [vesting.to_account_info().key.as_ref(), &[vesting.nonce]])]
#[account(
seeds = [vesting.to_account_info().key.as_ref()],
bump = vesting.nonce,
)]
vesting_signer: AccountInfo<'info>,
#[account("token_program.key == &token::ID")]
token_program: AccountInfo<'info>,
Expand Down

0 comments on commit 75c2085

Please sign in to comment.