Skip to content

Commit

Permalink
spl: Update instructions and remove rent from constraints (#2265)
Browse files Browse the repository at this point in the history
* Update spl instructions and remove the rent sys_var from constraints

* Fix initialize mint

* Update changelog and fix examples

* Remove oversights
  • Loading branch information
Aursen authored Nov 19, 2022
1 parent 5e3ebcf commit 8ce18c3
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 76 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ The minor version will be incremented upon a breaking change and the patch versi
* spl: Add `MetadataAccount` account deserialization. ([#2014](https://github.com/coral-xyz/anchor/pull/2014)).
* spl: Add `update_primary_sale_happened_via_token` wrapper ([#2173](https://github.com/coral-xyz/anchor/pull/2173)).
* spl: Add `sign_metadata` and `remove_creator_verification` wrappers ([#2175](https://github.com/coral-xyz/anchor/pull/2175)).
* spl: Add `initialize_account3` and `initialize_mint2` ([#2265](https://github.com/coral-xyz/anchor/pull/2265)).
* lang: Add parsing for consts from impl blocks for IDL PDA seeds generation ([#2128](https://github.com/coral-xyz/anchor/pull/2014))
* lang: Account closing reassigns to system program and reallocates ([#2169](https://github.com/coral-xyz/anchor/pull/2169)).
* ts: Add coders for SPL programs ([#2143](https://github.com/coral-xyz/anchor/pull/2143)).
Expand All @@ -39,6 +40,8 @@ The minor version will be incremented upon a breaking change and the patch versi
### Breaking

* ts: SPL coders have been removed from the main Anchor package. ([#2155](https://github.com/coral-xyz/anchor/pull/2155))
* lang: Remove `rent` from constraints ([#2265](https://github.com/coral-xyz/anchor/pull/2265)).
* spl: Remove `rent` from `associated_token::Create` ([#2265](https://github.com/coral-xyz/anchor/pull/2265)).

## [0.25.0] - 2022-07-05

Expand Down
11 changes: 4 additions & 7 deletions lang/syn/src/codegen/accounts/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,14 +457,13 @@ fn generate_constraint_init_group(f: &Field, c: &ConstraintInitGroup) -> proc_ma

// Initialize the token account.
let cpi_program = token_program.to_account_info();
let accounts = anchor_spl::token::InitializeAccount {
let accounts = anchor_spl::token::InitializeAccount3 {
account: #field.to_account_info(),
mint: #mint.to_account_info(),
authority: #owner.to_account_info(),
rent: rent.to_account_info(),
};
let cpi_ctx = anchor_lang::context::CpiContext::new(cpi_program, accounts);
anchor_spl::token::initialize_account(cpi_ctx)?;
anchor_spl::token::initialize_account3(cpi_ctx)?;
}

let pa: #ty_decl = #from_account_info_unchecked;
Expand Down Expand Up @@ -497,7 +496,6 @@ fn generate_constraint_init_group(f: &Field, c: &ConstraintInitGroup) -> proc_ma
mint: #mint.to_account_info(),
system_program: system_program.to_account_info(),
token_program: token_program.to_account_info(),
rent: rent.to_account_info(),
};
let cpi_ctx = anchor_lang::context::CpiContext::new(cpi_program, cpi_accounts);
anchor_spl::associated_token::create(cpi_ctx)?;
Expand Down Expand Up @@ -548,12 +546,11 @@ fn generate_constraint_init_group(f: &Field, c: &ConstraintInitGroup) -> proc_ma

// Initialize the mint account.
let cpi_program = token_program.to_account_info();
let accounts = anchor_spl::token::InitializeMint {
let accounts = anchor_spl::token::InitializeMint2 {
mint: #field.to_account_info(),
rent: rent.to_account_info(),
};
let cpi_ctx = anchor_lang::context::CpiContext::new(cpi_program, accounts);
anchor_spl::token::initialize_mint(cpi_ctx, #decimals, &#owner.key(), #freeze_authority)?;
anchor_spl::token::initialize_mint2(cpi_ctx, #decimals, &#owner.key(), #freeze_authority)?;
}
let pa: #ty_decl = #from_account_info_unchecked;
if #if_needed {
Expand Down
2 changes: 0 additions & 2 deletions spl/src/associated_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ pub fn create<'info>(ctx: CpiContext<'_, '_, '_, 'info, Create<'info>>) -> Resul
ctx.accounts.mint,
ctx.accounts.system_program,
ctx.accounts.token_program,
ctx.accounts.rent,
],
ctx.signer_seeds,
)
Expand All @@ -36,7 +35,6 @@ pub struct Create<'info> {
pub mint: AccountInfo<'info>,
pub system_program: AccountInfo<'info>,
pub token_program: AccountInfo<'info>,
pub rent: AccountInfo<'info>,
}

#[derive(Clone)]
Expand Down
46 changes: 46 additions & 0 deletions spl/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,23 @@ pub fn initialize_account<'a, 'b, 'c, 'info>(
.map_err(Into::into)
}

pub fn initialize_account3<'a, 'b, 'c, 'info>(
ctx: CpiContext<'a, 'b, 'c, 'info, InitializeAccount3<'info>>,
) -> Result<()> {
let ix = spl_token::instruction::initialize_account3(
&spl_token::ID,
ctx.accounts.account.key,
ctx.accounts.mint.key,
ctx.accounts.authority.key,
)?;
solana_program::program::invoke_signed(
&ix,
&[ctx.accounts.account.clone(), ctx.accounts.mint.clone()],
ctx.signer_seeds,
)
.map_err(Into::into)
}

pub fn close_account<'a, 'b, 'c, 'info>(
ctx: CpiContext<'a, 'b, 'c, 'info, CloseAccount<'info>>,
) -> Result<()> {
Expand Down Expand Up @@ -229,6 +246,23 @@ pub fn initialize_mint<'a, 'b, 'c, 'info>(
.map_err(Into::into)
}

pub fn initialize_mint2<'a, 'b, 'c, 'info>(
ctx: CpiContext<'a, 'b, 'c, 'info, InitializeMint2<'info>>,
decimals: u8,
authority: &Pubkey,
freeze_authority: Option<&Pubkey>,
) -> Result<()> {
let ix = spl_token::instruction::initialize_mint2(
&spl_token::ID,
ctx.accounts.mint.key,
authority,
freeze_authority,
decimals,
)?;
solana_program::program::invoke_signed(&ix, &[ctx.accounts.mint.clone()], ctx.signer_seeds)
.map_err(Into::into)
}

pub fn set_authority<'a, 'b, 'c, 'info>(
ctx: CpiContext<'a, 'b, 'c, 'info, SetAuthority<'info>>,
authority_type: spl_token::instruction::AuthorityType,
Expand Down Expand Up @@ -308,6 +342,13 @@ pub struct InitializeAccount<'info> {
pub rent: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct InitializeAccount3<'info> {
pub account: AccountInfo<'info>,
pub mint: AccountInfo<'info>,
pub authority: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct CloseAccount<'info> {
pub account: AccountInfo<'info>,
Expand Down Expand Up @@ -335,6 +376,11 @@ pub struct InitializeMint<'info> {
pub rent: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct InitializeMint2<'info> {
pub mint: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct SetAuthority<'info> {
pub current_authority: AccountInfo<'info>,
Expand Down
3 changes: 0 additions & 3 deletions tests/ido-pool/programs/ido-pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,6 @@ pub struct InitializePool<'info> {
// Programs and Sysvars
pub system_program: Program<'info, System>,
pub token_program: Program<'info, Token>,
pub rent: Sysvar<'info, Rent>,
}

#[derive(Accounts)]
Expand Down Expand Up @@ -359,7 +358,6 @@ pub struct InitUserRedeemable<'info> {
// Programs and Sysvars
pub system_program: Program<'info, System>,
pub token_program: Program<'info, Token>,
pub rent: Sysvar<'info, Rent>,
}

#[derive(Accounts)]
Expand Down Expand Up @@ -418,7 +416,6 @@ pub struct InitEscrowUsdc<'info> {
// Programs and Sysvars
pub system_program: Program<'info, System>,
pub token_program: Program<'info, Token>,
pub rent: Sysvar<'info, Rent>,
}

#[derive(Accounts)]
Expand Down
4 changes: 0 additions & 4 deletions tests/ido-pool/tests/ido-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ describe("ido-pool", () => {
poolUsdc,
systemProgram: anchor.web3.SystemProgram.programId,
tokenProgram: TOKEN_PROGRAM_ID,
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
},
}
);
Expand Down Expand Up @@ -225,7 +224,6 @@ describe("ido-pool", () => {
redeemableMint,
systemProgram: anchor.web3.SystemProgram.programId,
tokenProgram: TOKEN_PROGRAM_ID,
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
},
}),
],
Expand Down Expand Up @@ -326,7 +324,6 @@ describe("ido-pool", () => {
redeemableMint,
systemProgram: anchor.web3.SystemProgram.programId,
tokenProgram: TOKEN_PROGRAM_ID,
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
},
}),
],
Expand Down Expand Up @@ -401,7 +398,6 @@ describe("ido-pool", () => {
usdcMint,
systemProgram: anchor.web3.SystemProgram.programId,
tokenProgram: TOKEN_PROGRAM_ID,
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
},
}),
],
Expand Down
7 changes: 0 additions & 7 deletions tests/misc/programs/misc/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ pub struct TestTokenSeedsInit<'info> {
/// CHECK:
pub authority: AccountInfo<'info>,
pub system_program: Program<'info, System>,
pub rent: Sysvar<'info, Rent>,
pub token_program: Program<'info, Token>,
}

Expand All @@ -46,7 +45,6 @@ pub struct TestInitAssociatedToken<'info> {
pub mint: Account<'info, Mint>,
#[account(mut)]
pub payer: Signer<'info>,
pub rent: Sysvar<'info, Rent>,
pub system_program: Program<'info, System>,
pub token_program: Program<'info, Token>,
pub associated_token_program: Program<'info, AssociatedToken>,
Expand Down Expand Up @@ -243,7 +241,6 @@ pub struct TestInitMint<'info> {
pub mint: Account<'info, Mint>,
#[account(mut)]
pub payer: Signer<'info>,
pub rent: Sysvar<'info, Rent>,
pub system_program: Program<'info, System>,
pub token_program: Program<'info, Token>,
}
Expand All @@ -255,7 +252,6 @@ pub struct TestInitToken<'info> {
pub mint: Account<'info, Mint>,
#[account(mut)]
pub payer: Signer<'info>,
pub rent: Sysvar<'info, Rent>,
pub system_program: Program<'info, System>,
pub token_program: Program<'info, Token>,
}
Expand Down Expand Up @@ -342,7 +338,6 @@ pub struct TestInitMintIfNeeded<'info> {
pub mint: Account<'info, Mint>,
#[account(mut)]
pub payer: Signer<'info>,
pub rent: Sysvar<'info, Rent>,
pub system_program: Program<'info, System>,
pub token_program: Program<'info, Token>,
/// CHECK:
Expand All @@ -358,7 +353,6 @@ pub struct TestInitTokenIfNeeded<'info> {
pub mint: Account<'info, Mint>,
#[account(mut)]
pub payer: Signer<'info>,
pub rent: Sysvar<'info, Rent>,
pub system_program: Program<'info, System>,
pub token_program: Program<'info, Token>,
/// CHECK:
Expand All @@ -377,7 +371,6 @@ pub struct TestInitAssociatedTokenIfNeeded<'info> {
pub mint: Account<'info, Mint>,
#[account(mut)]
pub payer: Signer<'info>,
pub rent: Sysvar<'info, Rent>,
pub system_program: Program<'info, System>,
pub token_program: Program<'info, Token>,
pub associated_token_program: Program<'info, AssociatedToken>,
Expand Down
Loading

1 comment on commit 8ce18c3

@vercel
Copy link

@vercel vercel bot commented on 8ce18c3 Nov 19, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

anchor-docs – ./

anchor-docs-git-master-200ms.vercel.app
anchor-docs-200ms.vercel.app
www.anchor-lang.com
anchor-lang.com

Please sign in to comment.