Skip to content

Commit 7789693

Browse files
authored
spl: Update instructions and remove rent from constraints (otter-sec#2265)
* Update spl instructions and remove the rent sys_var from constraints * Fix initialize mint * Update changelog and fix examples * Remove oversights
1 parent 9be80d4 commit 7789693

8 files changed

Lines changed: 53 additions & 76 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ The minor version will be incremented upon a breaking change and the patch versi
1717
* spl: Add `MetadataAccount` account deserialization. ([#2014](https://github.com/coral-xyz/anchor/pull/2014)).
1818
* spl: Add `update_primary_sale_happened_via_token` wrapper ([#2173](https://github.com/coral-xyz/anchor/pull/2173)).
1919
* spl: Add `sign_metadata` and `remove_creator_verification` wrappers ([#2175](https://github.com/coral-xyz/anchor/pull/2175)).
20+
* spl: Add `initialize_account3` and `initialize_mint2` ([#2265](https://github.com/coral-xyz/anchor/pull/2265)).
2021
* lang: Add parsing for consts from impl blocks for IDL PDA seeds generation ([#2128](https://github.com/coral-xyz/anchor/pull/2014))
2122
* lang: Account closing reassigns to system program and reallocates ([#2169](https://github.com/coral-xyz/anchor/pull/2169)).
2223
* ts: Add coders for SPL programs ([#2143](https://github.com/coral-xyz/anchor/pull/2143)).
@@ -39,6 +40,8 @@ The minor version will be incremented upon a breaking change and the patch versi
3940
### Breaking
4041

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

4346
## [0.25.0] - 2022-07-05
4447

lang/syn/src/codegen/accounts/constraints.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -457,14 +457,13 @@ fn generate_constraint_init_group(f: &Field, c: &ConstraintInitGroup) -> proc_ma
457457

458458
// Initialize the token account.
459459
let cpi_program = token_program.to_account_info();
460-
let accounts = anchor_spl::token::InitializeAccount {
460+
let accounts = anchor_spl::token::InitializeAccount3 {
461461
account: #field.to_account_info(),
462462
mint: #mint.to_account_info(),
463463
authority: #owner.to_account_info(),
464-
rent: rent.to_account_info(),
465464
};
466465
let cpi_ctx = anchor_lang::context::CpiContext::new(cpi_program, accounts);
467-
anchor_spl::token::initialize_account(cpi_ctx)?;
466+
anchor_spl::token::initialize_account3(cpi_ctx)?;
468467
}
469468

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

549547
// Initialize the mint account.
550548
let cpi_program = token_program.to_account_info();
551-
let accounts = anchor_spl::token::InitializeMint {
549+
let accounts = anchor_spl::token::InitializeMint2 {
552550
mint: #field.to_account_info(),
553-
rent: rent.to_account_info(),
554551
};
555552
let cpi_ctx = anchor_lang::context::CpiContext::new(cpi_program, accounts);
556-
anchor_spl::token::initialize_mint(cpi_ctx, #decimals, &#owner.key(), #freeze_authority)?;
553+
anchor_spl::token::initialize_mint2(cpi_ctx, #decimals, &#owner.key(), #freeze_authority)?;
557554
}
558555
let pa: #ty_decl = #from_account_info_unchecked;
559556
if #if_needed {

spl/src/associated_token.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ pub fn create<'info>(ctx: CpiContext<'_, '_, '_, 'info, Create<'info>>) -> Resul
2121
ctx.accounts.mint,
2222
ctx.accounts.system_program,
2323
ctx.accounts.token_program,
24-
ctx.accounts.rent,
2524
],
2625
ctx.signer_seeds,
2726
)
@@ -36,7 +35,6 @@ pub struct Create<'info> {
3635
pub mint: AccountInfo<'info>,
3736
pub system_program: AccountInfo<'info>,
3837
pub token_program: AccountInfo<'info>,
39-
pub rent: AccountInfo<'info>,
4038
}
4139

4240
#[derive(Clone)]

spl/src/token.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,23 @@ pub fn initialize_account<'a, 'b, 'c, 'info>(
142142
.map_err(Into::into)
143143
}
144144

145+
pub fn initialize_account3<'a, 'b, 'c, 'info>(
146+
ctx: CpiContext<'a, 'b, 'c, 'info, InitializeAccount3<'info>>,
147+
) -> Result<()> {
148+
let ix = spl_token::instruction::initialize_account3(
149+
&spl_token::ID,
150+
ctx.accounts.account.key,
151+
ctx.accounts.mint.key,
152+
ctx.accounts.authority.key,
153+
)?;
154+
solana_program::program::invoke_signed(
155+
&ix,
156+
&[ctx.accounts.account.clone(), ctx.accounts.mint.clone()],
157+
ctx.signer_seeds,
158+
)
159+
.map_err(Into::into)
160+
}
161+
145162
pub fn close_account<'a, 'b, 'c, 'info>(
146163
ctx: CpiContext<'a, 'b, 'c, 'info, CloseAccount<'info>>,
147164
) -> Result<()> {
@@ -229,6 +246,23 @@ pub fn initialize_mint<'a, 'b, 'c, 'info>(
229246
.map_err(Into::into)
230247
}
231248

249+
pub fn initialize_mint2<'a, 'b, 'c, 'info>(
250+
ctx: CpiContext<'a, 'b, 'c, 'info, InitializeMint2<'info>>,
251+
decimals: u8,
252+
authority: &Pubkey,
253+
freeze_authority: Option<&Pubkey>,
254+
) -> Result<()> {
255+
let ix = spl_token::instruction::initialize_mint2(
256+
&spl_token::ID,
257+
ctx.accounts.mint.key,
258+
authority,
259+
freeze_authority,
260+
decimals,
261+
)?;
262+
solana_program::program::invoke_signed(&ix, &[ctx.accounts.mint.clone()], ctx.signer_seeds)
263+
.map_err(Into::into)
264+
}
265+
232266
pub fn set_authority<'a, 'b, 'c, 'info>(
233267
ctx: CpiContext<'a, 'b, 'c, 'info, SetAuthority<'info>>,
234268
authority_type: spl_token::instruction::AuthorityType,
@@ -308,6 +342,13 @@ pub struct InitializeAccount<'info> {
308342
pub rent: AccountInfo<'info>,
309343
}
310344

345+
#[derive(Accounts)]
346+
pub struct InitializeAccount3<'info> {
347+
pub account: AccountInfo<'info>,
348+
pub mint: AccountInfo<'info>,
349+
pub authority: AccountInfo<'info>,
350+
}
351+
311352
#[derive(Accounts)]
312353
pub struct CloseAccount<'info> {
313354
pub account: AccountInfo<'info>,
@@ -335,6 +376,11 @@ pub struct InitializeMint<'info> {
335376
pub rent: AccountInfo<'info>,
336377
}
337378

379+
#[derive(Accounts)]
380+
pub struct InitializeMint2<'info> {
381+
pub mint: AccountInfo<'info>,
382+
}
383+
338384
#[derive(Accounts)]
339385
pub struct SetAuthority<'info> {
340386
pub current_authority: AccountInfo<'info>,

tests/ido-pool/programs/ido-pool/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,6 @@ pub struct InitializePool<'info> {
331331
// Programs and Sysvars
332332
pub system_program: Program<'info, System>,
333333
pub token_program: Program<'info, Token>,
334-
pub rent: Sysvar<'info, Rent>,
335334
}
336335

337336
#[derive(Accounts)]
@@ -359,7 +358,6 @@ pub struct InitUserRedeemable<'info> {
359358
// Programs and Sysvars
360359
pub system_program: Program<'info, System>,
361360
pub token_program: Program<'info, Token>,
362-
pub rent: Sysvar<'info, Rent>,
363361
}
364362

365363
#[derive(Accounts)]
@@ -418,7 +416,6 @@ pub struct InitEscrowUsdc<'info> {
418416
// Programs and Sysvars
419417
pub system_program: Program<'info, System>,
420418
pub token_program: Program<'info, Token>,
421-
pub rent: Sysvar<'info, Rent>,
422419
}
423420

424421
#[derive(Accounts)]

tests/ido-pool/tests/ido-pool.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ describe("ido-pool", () => {
124124
poolUsdc,
125125
systemProgram: anchor.web3.SystemProgram.programId,
126126
tokenProgram: TOKEN_PROGRAM_ID,
127-
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
128127
},
129128
}
130129
);
@@ -225,7 +224,6 @@ describe("ido-pool", () => {
225224
redeemableMint,
226225
systemProgram: anchor.web3.SystemProgram.programId,
227226
tokenProgram: TOKEN_PROGRAM_ID,
228-
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
229227
},
230228
}),
231229
],
@@ -326,7 +324,6 @@ describe("ido-pool", () => {
326324
redeemableMint,
327325
systemProgram: anchor.web3.SystemProgram.programId,
328326
tokenProgram: TOKEN_PROGRAM_ID,
329-
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
330327
},
331328
}),
332329
],
@@ -401,7 +398,6 @@ describe("ido-pool", () => {
401398
usdcMint,
402399
systemProgram: anchor.web3.SystemProgram.programId,
403400
tokenProgram: TOKEN_PROGRAM_ID,
404-
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
405401
},
406402
}),
407403
],

tests/misc/programs/misc/src/context.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ pub struct TestTokenSeedsInit<'info> {
3030
/// CHECK:
3131
pub authority: AccountInfo<'info>,
3232
pub system_program: Program<'info, System>,
33-
pub rent: Sysvar<'info, Rent>,
3433
pub token_program: Program<'info, Token>,
3534
}
3635

@@ -46,7 +45,6 @@ pub struct TestInitAssociatedToken<'info> {
4645
pub mint: Account<'info, Mint>,
4746
#[account(mut)]
4847
pub payer: Signer<'info>,
49-
pub rent: Sysvar<'info, Rent>,
5048
pub system_program: Program<'info, System>,
5149
pub token_program: Program<'info, Token>,
5250
pub associated_token_program: Program<'info, AssociatedToken>,
@@ -243,7 +241,6 @@ pub struct TestInitMint<'info> {
243241
pub mint: Account<'info, Mint>,
244242
#[account(mut)]
245243
pub payer: Signer<'info>,
246-
pub rent: Sysvar<'info, Rent>,
247244
pub system_program: Program<'info, System>,
248245
pub token_program: Program<'info, Token>,
249246
}
@@ -255,7 +252,6 @@ pub struct TestInitToken<'info> {
255252
pub mint: Account<'info, Mint>,
256253
#[account(mut)]
257254
pub payer: Signer<'info>,
258-
pub rent: Sysvar<'info, Rent>,
259255
pub system_program: Program<'info, System>,
260256
pub token_program: Program<'info, Token>,
261257
}
@@ -342,7 +338,6 @@ pub struct TestInitMintIfNeeded<'info> {
342338
pub mint: Account<'info, Mint>,
343339
#[account(mut)]
344340
pub payer: Signer<'info>,
345-
pub rent: Sysvar<'info, Rent>,
346341
pub system_program: Program<'info, System>,
347342
pub token_program: Program<'info, Token>,
348343
/// CHECK:
@@ -358,7 +353,6 @@ pub struct TestInitTokenIfNeeded<'info> {
358353
pub mint: Account<'info, Mint>,
359354
#[account(mut)]
360355
pub payer: Signer<'info>,
361-
pub rent: Sysvar<'info, Rent>,
362356
pub system_program: Program<'info, System>,
363357
pub token_program: Program<'info, Token>,
364358
/// CHECK:
@@ -377,7 +371,6 @@ pub struct TestInitAssociatedTokenIfNeeded<'info> {
377371
pub mint: Account<'info, Mint>,
378372
#[account(mut)]
379373
pub payer: Signer<'info>,
380-
pub rent: Sysvar<'info, Rent>,
381374
pub system_program: Program<'info, System>,
382375
pub token_program: Program<'info, Token>,
383376
pub associated_token_program: Program<'info, AssociatedToken>,

0 commit comments

Comments
 (0)