Skip to content

Commit 3f8084d

Browse files
author
Edward
committed
Replace magic numbers with DISCRIMINATOR + INIT_SPACE
1 parent 17db32e commit 3f8084d

8 files changed

Lines changed: 15 additions & 17 deletions

File tree

  • basics/cross-program-invocation/anchor/programs/lever/src
  • tokens
    • token-2022/transfer-hook
      • account-data-as-seed/anchor/programs/transfer-hook/src
      • counter/anchor/programs/transfer-hook/src
      • transfer-cost/anchor/programs/transfer-hook/src
      • whitelist/anchor/programs/transfer-hook/src
    • token-swap/anchor/programs/token-swap/src

basics/cross-program-invocation/anchor/programs/lever/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub mod lever {
2727

2828
#[derive(Accounts)]
2929
pub struct InitializeLever<'info> {
30-
#[account(init, payer = user, space = 8 + 8)]
30+
#[account(init, payer = user, space = PowerStatus::DISCRIMINATOR.len() + PowerStatus::INIT_SPACE)]
3131
pub power: Account<'info, PowerStatus>,
3232
#[account(mut)]
3333
pub user: Signer<'info>,
@@ -41,6 +41,7 @@ pub struct SetPowerStatus<'info> {
4141
}
4242

4343
#[account]
44+
#[derive(InitSpace)]
4445
pub struct PowerStatus {
4546
pub is_on: bool,
4647
}

tokens/token-2022/transfer-hook/account-data-as-seed/anchor/programs/transfer-hook/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ pub struct InitializeExtraAccountMetaList<'info> {
114114
)]
115115
pub extra_account_meta_list: UncheckedAccount<'info>,
116116
pub mint: InterfaceAccount<'info, Mint>,
117-
#[account(init, seeds = [b"counter", payer.key().as_ref()], bump, payer = payer, space = 16)]
117+
#[account(init, seeds = [b"counter", payer.key().as_ref()], bump, payer = payer, space = CounterAccount::DISCRIMINATOR.len() + CounterAccount::INIT_SPACE)]
118118
pub counter_account: Account<'info, CounterAccount>,
119119
pub token_program: Program<'info, Token2022>,
120120
pub associated_token_program: Program<'info, AssociatedToken>,
@@ -169,6 +169,7 @@ pub struct TransferHook<'info> {
169169
}
170170

171171
#[account]
172+
#[derive(InitSpace)]
172173
pub struct CounterAccount {
173174
counter: u64,
174175
}

tokens/token-2022/transfer-hook/counter/anchor/programs/transfer-hook/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ pub struct InitializeExtraAccountMetaList<'info> {
110110
)]
111111
pub extra_account_meta_list: AccountInfo<'info>,
112112
pub mint: InterfaceAccount<'info, Mint>,
113-
#[account(init, seeds = [b"counter"], bump, payer = payer, space = 16)]
113+
#[account(init, seeds = [b"counter"], bump, payer = payer, space = CounterAccount::DISCRIMINATOR.len() + CounterAccount::INIT_SPACE)]
114114
pub counter_account: Account<'info, CounterAccount>,
115115
pub token_program: Program<'info, Token2022>,
116116
pub associated_token_program: Program<'info, AssociatedToken>,
@@ -158,6 +158,7 @@ pub struct TransferHook<'info> {
158158
}
159159

160160
#[account]
161+
#[derive(InitSpace)]
161162
pub struct CounterAccount {
162163
counter: u64,
163164
}

tokens/token-2022/transfer-hook/transfer-cost/anchor/programs/transfer-hook/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ pub struct InitializeExtraAccountMetaList<'info> {
138138
)]
139139
pub extra_account_meta_list: UncheckedAccount<'info>,
140140
pub mint: InterfaceAccount<'info, Mint>,
141-
#[account(init, seeds = [b"counter"], bump, payer = payer, space = 9)]
141+
#[account(init, seeds = [b"counter"], bump, payer = payer, space = CounterAccount::DISCRIMINATOR.len() + CounterAccount::INIT_SPACE)]
142142
pub counter_account: Account<'info, CounterAccount>,
143143
pub system_program: Program<'info, System>,
144144
}
@@ -263,6 +263,7 @@ pub struct TransferHook<'info> {
263263
}
264264

265265
#[account]
266+
#[derive(InitSpace)]
266267
pub struct CounterAccount {
267268
counter: u8,
268269
}

tokens/token-2022/transfer-hook/whitelist/anchor/programs/transfer-hook/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub struct InitializeExtraAccountMetaList<'info> {
127127
pub extra_account_meta_list: UncheckedAccount<'info>,
128128
pub mint: InterfaceAccount<'info, Mint>,
129129
pub system_program: Program<'info, System>,
130-
#[account(init_if_needed, seeds = [b"white_list"], bump, payer = payer, space = 400)]
130+
#[account(init_if_needed, seeds = [b"white_list"], bump, payer = payer, space = WhiteList::DISCRIMINATOR.len() + WhiteList::INIT_SPACE)]
131131
pub white_list: Account<'info, WhiteList>,
132132
}
133133

@@ -187,7 +187,9 @@ pub struct AddToWhiteList<'info> {
187187
}
188188

189189
#[account]
190+
#[derive(InitSpace)]
190191
pub struct WhiteList {
191192
pub authority: Pubkey,
193+
#[max_len(11)]
192194
pub white_list: Vec<Pubkey>,
193195
}

tokens/token-swap/anchor/programs/token-swap/src/instructions/create_amm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub struct CreateAmm<'info> {
1717
#[account(
1818
init,
1919
payer = payer,
20-
space = Amm::LEN,
20+
space = Amm::DISCRIMINATOR.len() + Amm::INIT_SPACE,
2121
seeds = [
2222
id.as_ref()
2323
],

tokens/token-swap/anchor/programs/token-swap/src/instructions/create_pool.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub struct CreatePool<'info> {
3131
#[account(
3232
init,
3333
payer = payer,
34-
space = Pool::LEN,
34+
space = Pool::DISCRIMINATOR.len() + Pool::INIT_SPACE,
3535
seeds = [
3636
amm.key().as_ref(),
3737
mint_a.key().as_ref(),
Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use anchor_lang::prelude::*;
22

33
#[account]
4-
#[derive(Default)]
4+
#[derive(Default, InitSpace)]
55
pub struct Amm {
66
/// The primary key of the AMM
77
pub id: Pubkey,
@@ -13,12 +13,8 @@ pub struct Amm {
1313
pub fee: u16,
1414
}
1515

16-
impl Amm {
17-
pub const LEN: usize = 8 + 32 + 32 + 2;
18-
}
19-
2016
#[account]
21-
#[derive(Default)]
17+
#[derive(Default, InitSpace)]
2218
pub struct Pool {
2319
/// Primary key of the AMM
2420
pub amm: Pubkey,
@@ -29,7 +25,3 @@ pub struct Pool {
2925
/// Mint of token B
3026
pub mint_b: Pubkey,
3127
}
32-
33-
impl Pool {
34-
pub const LEN: usize = 8 + 32 + 32 + 32;
35-
}

0 commit comments

Comments
 (0)