Skip to content

Commit d5791de

Browse files
chore(close-account): rename UserState to User
Applies the repo-wide convention that state structs do not carry the 'State' suffix. The struct is the data; 'state' is implicit in being an on-chain account (compare: Offer, not OfferState; Counter, not CounterState). Affected: - Anchor: programs/close-account/src/state/user_state.rs renamed to state/user.rs; pub struct UserState -> pub struct User; module pub mod user_state; -> pub mod user;. All callers updated. - Quasar: src/state.rs pub struct UserState -> pub struct User; the set_inner-generated UserStateInner becomes UserInner. All callers updated. - basics/close-account/anchor/README.md updated to match. Not changed: - state.rs / state/ module and directory names keep their names. The 'state' module is a fine container for state types; only the *struct* names are being renamed. Verification: cargo check clean on both basics/close-account/anchor and basics/close-account/quasar.
1 parent 34b9660 commit d5791de

8 files changed

Lines changed: 18 additions & 18 deletions

File tree

basics/close-account/anchor/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
# Close Account
22

3-
Two instruction handlers: `create_user` initializes a PDA `UserState` account, and `close_user` closes it and returns the rent to the user.
3+
Two instruction handlers: `create_user` initializes a PDA `User` account, and `close_user` closes it and returns the rent to the user.
44

55
1. `create_user` initializes the PDA with Anchor's `init` constraint:
66

77
```rust
88
#[account(
99
init,
1010
payer = user,
11-
space = UserState::DISCRIMINATOR.len() + UserState::INIT_SPACE,
11+
space = User::DISCRIMINATOR.len() + User::INIT_SPACE,
1212
seeds = [b"USER", user.key().as_ref()],
1313
bump,
1414
)]
15-
pub user_account: Account<'info, UserState>,
15+
pub user_account: Account<'info, User>,
1616
```
1717

1818
See [`programs/close-account/src/instructions/create_user.rs`](programs/close-account/src/instructions/create_user.rs).
@@ -26,7 +26,7 @@ Two instruction handlers: `create_user` initializes a PDA `UserState` account, a
2626
bump = user_account.bump,
2727
close = user, // close account and return lamports to user
2828
)]
29-
pub user_account: Account<'info, UserState>,
29+
pub user_account: Account<'info, User>,
3030
```
3131

3232
See [`programs/close-account/src/instructions/close_user.rs`](programs/close-account/src/instructions/close_user.rs).

basics/close-account/anchor/programs/close-account/src/instructions/close_user.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub struct CloseUserContext<'info> {
1515
bump = user_account.bump,
1616
close = user, // close account and return lamports to user
1717
)]
18-
pub user_account: Account<'info, UserState>,
18+
pub user_account: Account<'info, User>,
1919
}
2020

2121
pub fn handle_close_user(_context: Context<CloseUserContext>) -> Result<()> {

basics/close-account/anchor/programs/close-account/src/instructions/create_user.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ pub struct CreateUserContext<'info> {
99
#[account(
1010
init,
1111
payer = user,
12-
space = UserState::DISCRIMINATOR.len() + UserState::INIT_SPACE,
12+
space = User::DISCRIMINATOR.len() + User::INIT_SPACE,
1313
seeds = [
1414
b"USER",
1515
user.key().as_ref(),
1616
],
1717
bump
1818
)]
19-
pub user_account: Account<'info, UserState>,
19+
pub user_account: Account<'info, User>,
2020
pub system_program: Program<'info, System>,
2121
}
2222

2323
pub fn handle_create_user(context: Context<CreateUserContext>, name: String) -> Result<()> {
24-
*context.accounts.user_account = UserState {
24+
*context.accounts.user_account = User {
2525
bump: context.bumps.user_account,
2626
user: context.accounts.user.key(),
2727
name,
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
pub mod user_state;
2-
pub use user_state::*;
1+
pub mod user;
2+
pub use user::*;

basics/close-account/anchor/programs/close-account/src/state/user_state.rs renamed to basics/close-account/anchor/programs/close-account/src/state/user.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use anchor_lang::prelude::*;
22

33
#[account]
44
#[derive(InitSpace)] // automatically calculate the space required for the struct
5-
pub struct UserState {
5+
pub struct User {
66
pub bump: u8, // 1 byte
77
pub user: Pubkey, // 32 bytes
88
#[max_len(50)] // set a max length for the string

basics/close-account/quasar/src/instructions/close_user.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use {crate::state::UserState, quasar_lang::prelude::*};
1+
use {crate::state::User, quasar_lang::prelude::*};
22

33
/// Accounts for closing a user account.
44
/// The `close(dest = user)` attribute mirrors Anchor's `close = user`: at the
@@ -9,7 +9,7 @@ pub struct CloseUser {
99
#[account(mut)]
1010
pub user: Signer,
1111
#[account(mut, close(dest = user))]
12-
pub user_account: Account<UserState>,
12+
pub user_account: Account<User>,
1313
}
1414

1515
#[inline(always)]

basics/close-account/quasar/src/instructions/create_user.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use {
2-
crate::state::{UserState, UserStateInner},
2+
crate::state::{User, UserInner},
33
quasar_lang::{prelude::*, sysvars::Sysvar},
44
};
55

@@ -8,8 +8,8 @@ use {
88
pub struct CreateUser {
99
#[account(mut)]
1010
pub user: Signer,
11-
#[account(mut, init, payer = user, address = UserState::seeds(user.address()))]
12-
pub user_account: Account<UserState>,
11+
#[account(mut, init, payer = user, address = User::seeds(user.address()))]
12+
pub user_account: Account<User>,
1313
pub system_program: Program<SystemProgram>,
1414
}
1515

@@ -22,7 +22,7 @@ pub fn handle_create_user(
2222
let user_address = *accounts.user.to_account_view().address();
2323
let rent = Rent::get()?;
2424
accounts.user_account.set_inner(
25-
UserStateInner { bump, user: user_address, name },
25+
UserInner { bump, user: user_address, name },
2626
accounts.user.to_account_view(),
2727
rent.lamports_per_byte(),
2828
rent.exemption_threshold_raw(),

basics/close-account/quasar/src/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use quasar_lang::prelude::*;
44
/// Fixed fields (bump, user) must precede dynamic fields (name).
55
#[account(discriminator = 1, set_inner)]
66
#[seeds(b"USER", user: Address)]
7-
pub struct UserState {
7+
pub struct User {
88
pub bump: u8,
99
pub user: Address,
1010
pub name: String<50>,

0 commit comments

Comments
 (0)