Skip to content

Commit

Permalink
lang: #[account(owner)] uses pubkey instead of account target (#691)
Browse files Browse the repository at this point in the history
  • Loading branch information
armaniferrante authored Sep 9, 2021
1 parent 4451779 commit 526de23
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 27 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ incremented for features.

## [Unreleased]

### Breaking Changes

* lang: `#[account(owner = <pubkey>)]` now requires a `Pubkey` instead of an account ([#691](https://github.com/project-serum/anchor/pull/691)).

## [0.15.0] - 2021-09-07

### Features
Expand Down
5 changes: 3 additions & 2 deletions examples/tutorial/basic-3/programs/puppet-master/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// #region core
use anchor_lang::prelude::*;
use puppet::{Puppet, SetData};
use puppet::{self, Puppet, SetData};

declare_id!("HmbTLCmaGvZhKnn1Zfa1JVnp7vkMV4DYVxPLWBVoN65L");

Expand All @@ -19,8 +19,9 @@ mod puppet_master {

#[derive(Accounts)]
pub struct PullStrings<'info> {
#[account(mut, owner = puppet_program)]
#[account(mut)]
pub puppet: Account<'info, Puppet>,
#[account(address = puppet::ID)]
pub puppet_program: AccountInfo<'info>,
}
// #endregion core
4 changes: 2 additions & 2 deletions lang/syn/src/codegen/accounts/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@ pub fn generate_constraint_raw(c: &ConstraintRaw) -> proc_macro2::TokenStream {

pub fn generate_constraint_owner(f: &Field, c: &ConstraintOwner) -> proc_macro2::TokenStream {
let ident = &f.ident;
let owner_target = c.owner_target.clone();
let owner_address = &c.owner_address;
quote! {
if #ident.to_account_info().owner != #owner_target.to_account_info().key {
if #ident.to_account_info().owner != &#owner_address {
return Err(anchor_lang::__private::ErrorCode::ConstraintOwner.into());
}
}
Expand Down
2 changes: 1 addition & 1 deletion lang/syn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ pub struct ConstraintRaw {

#[derive(Debug, Clone)]
pub struct ConstraintOwner {
pub owner_target: Expr,
pub owner_address: Expr,
}

#[derive(Debug, Clone)]
Expand Down
12 changes: 2 additions & 10 deletions lang/syn/src/parser/accounts/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ pub fn parse_token(stream: ParseStream) -> ParseResult<ConstraintToken> {
"owner" => ConstraintToken::Owner(Context::new(
span,
ConstraintOwner {
owner_target: stream.parse()?,
owner_address: stream.parse()?,
},
)),
"rent_exempt" => ConstraintToken::RentExempt(Context::new(
Expand Down Expand Up @@ -447,14 +447,6 @@ impl<'ty> ConstraintGroupBuilder<'ty> {
};
}

let (owner, pda_owner) = {
if seeds.is_some() {
(None, owner.map(|o| o.owner_target.clone()))
} else {
(owner, None)
}
};

let seeds = seeds.map(|c| ConstraintSeedsGroup {
is_init: init.is_some(),
seeds: c.seeds.clone(),
Expand Down Expand Up @@ -491,7 +483,7 @@ impl<'ty> ConstraintGroupBuilder<'ty> {
}
} else {
InitKind::Program {
owner: pda_owner.clone(),
owner: owner.as_ref().map(|o| o.owner_address.clone()),
}
},
})).transpose()?,
Expand Down
19 changes: 8 additions & 11 deletions tests/cfo/programs/cfo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ pub struct CreateOfficerToken<'info> {
payer = payer,
)]
token: Account<'info, TokenAccount>,
#[account(owner = token_program)]
#[account(owner = spl_token::ID)]
mint: AccountInfo<'info>,
#[account(mut, signer)]
payer: AccountInfo<'info>,
Expand Down Expand Up @@ -392,7 +392,7 @@ pub struct SweepFees<'info> {
officer: Account<'info, Officer>,
#[account(
mut,
owner = dex.token_program,
owner = spl_token::ID,
seeds = [officer.key().as_ref(), mint.key().as_ref()],
bump,
)]
Expand Down Expand Up @@ -423,12 +423,12 @@ pub struct SwapToUsdc<'info> {
officer: Account<'info, Officer>,
market: DexMarketAccounts<'info>,
#[account(
owner = token_program,
owner = spl_token::ID,
constraint = &officer.treasury != from_vault.key,
constraint = &officer.stake != from_vault.key,
)]
from_vault: AccountInfo<'info>,
#[account(owner = token_program)]
#[account(owner = spl_token::ID)]
quote_vault: AccountInfo<'info>,
#[account(seeds = [officer.key().as_ref(), mint::USDC.as_ref()], bump)]
usdc_vault: AccountInfo<'info>,
Expand All @@ -452,12 +452,12 @@ pub struct SwapToSrm<'info> {
officer: Account<'info, Officer>,
market: DexMarketAccounts<'info>,
#[account(
owner = token_program,
owner = spl_token::ID,
constraint = &officer.treasury != from_vault.key,
constraint = &officer.stake != from_vault.key,
)]
from_vault: AccountInfo<'info>,
#[account(owner = token_program)]
#[account(owner = spl_token::ID)]
quote_vault: AccountInfo<'info>,
#[account(
seeds = [officer.key().as_ref(), mint::SRM.as_ref()],
Expand Down Expand Up @@ -518,10 +518,7 @@ pub struct Distribute<'info> {
officer: Account<'info, Officer>,
treasury: AccountInfo<'info>,
stake: AccountInfo<'info>,
#[account(
owner = token_program,
constraint = srm_vault.mint == mint::SRM,
)]
#[account(constraint = srm_vault.mint == mint::SRM)]
srm_vault: Account<'info, TokenAccount>,
#[account(address = mint::SRM)]
mint: AccountInfo<'info>,
Expand Down Expand Up @@ -551,7 +548,7 @@ pub struct DropStakeReward<'info> {
mint: AccountInfo<'info>,
srm: DropStakeRewardPool<'info>,
msrm: DropStakeRewardPool<'info>,
#[account(owner = registry_program)]
#[account(owner = *registry_program.key)]
msrm_registrar: Box<Account<'info, Registrar>>,
#[account(address = token::ID)]
token_program: AccountInfo<'info>,
Expand Down
2 changes: 1 addition & 1 deletion tests/misc/programs/misc/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub struct Initialize<'info> {

#[derive(Accounts)]
pub struct TestOwner<'info> {
#[account(owner = misc)]
#[account(owner = *misc.key)]
pub data: AccountInfo<'info>,
pub misc: AccountInfo<'info>,
}
Expand Down

0 comments on commit 526de23

Please sign in to comment.