Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions basics/close-account/anchor/README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# Close Account

Two [instruction handlers](https://solana.com/docs/terminology#instruction-handler): `create_user` initializes a [PDA](https://solana.com/docs/terminology#program-derived-address-pda) `UserState` [account](https://solana.com/docs/terminology#account), and `close_user` closes it and returns the [rent](https://solana.com/docs/terminology#rent) to the user.
Two [instruction handlers](https://solana.com/docs/terminology#instruction-handler): `create_user` initializes a [PDA](https://solana.com/docs/terminology#program-derived-address-pda) `User` [account](https://solana.com/docs/terminology#account), and `close_user` closes it and returns the [rent](https://solana.com/docs/terminology#rent) to the user.

1. `create_user` initializes the PDA with [Anchor](https://solana.com/docs/terminology#anchor)'s `init` constraint:

```rust
#[account(
init,
payer = user,
space = UserState::DISCRIMINATOR.len() + UserState::INIT_SPACE,
space = User::DISCRIMINATOR.len() + User::INIT_SPACE,
seeds = [b"USER", user.key().as_ref()],
bump,
)]
pub user_account: Account<'info, UserState>,
pub user_account: Account<'info, User>,
```

See [`programs/close-account/src/instructions/create_user.rs`](programs/close-account/src/instructions/create_user.rs).
Expand All @@ -26,7 +26,7 @@ Two [instruction handlers](https://solana.com/docs/terminology#instruction-handl
bump = user_account.bump,
close = user, // close account and return lamports to user
)]
pub user_account: Account<'info, UserState>,
pub user_account: Account<'info, User>,
```

See [`programs/close-account/src/instructions/close_user.rs`](programs/close-account/src/instructions/close_user.rs).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct CloseUserContext<'info> {
bump = user_account.bump,
close = user, // close account and return lamports to user
)]
pub user_account: Account<'info, UserState>,
pub user_account: Account<'info, User>,
}

pub fn handle_close_user(_context: Context<CloseUserContext>) -> Result<()> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ pub struct CreateUserContext<'info> {
#[account(
init,
payer = user,
space = UserState::DISCRIMINATOR.len() + UserState::INIT_SPACE,
space = User::DISCRIMINATOR.len() + User::INIT_SPACE,
seeds = [
b"USER",
user.key().as_ref(),
],
bump
)]
pub user_account: Account<'info, UserState>,
pub user_account: Account<'info, User>,
pub system_program: Program<'info, System>,
}

pub fn handle_create_user(context: Context<CreateUserContext>, name: String) -> Result<()> {
*context.accounts.user_account = UserState {
*context.accounts.user_account = User {
bump: context.bumps.user_account,
user: context.accounts.user.key(),
name,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod user_state;
pub use user_state::*;
pub mod user;
pub use user::*;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anchor_lang::prelude::*;

#[account]
#[derive(InitSpace)] // automatically calculate the space required for the struct
pub struct UserState {
pub struct User {
pub bump: u8, // 1 byte
pub user: Pubkey, // 32 bytes
#[max_len(50)] // set a max length for the string
Expand Down
4 changes: 2 additions & 2 deletions basics/close-account/quasar/src/instructions/close_user.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use {crate::state::UserState, quasar_lang::prelude::*};
use {crate::state::User, quasar_lang::prelude::*};

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

#[inline(always)]
Expand Down
8 changes: 4 additions & 4 deletions basics/close-account/quasar/src/instructions/create_user.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {
crate::state::{UserState, UserStateInner},
crate::state::{User, UserInner},
quasar_lang::{prelude::*, sysvars::Sysvar},
};

Expand All @@ -8,8 +8,8 @@ use {
pub struct CreateUser {
#[account(mut)]
pub user: Signer,
#[account(mut, init, payer = user, address = UserState::seeds(user.address()))]
pub user_account: Account<UserState>,
#[account(mut, init, payer = user, address = User::seeds(user.address()))]
pub user_account: Account<User>,
pub system_program: Program<SystemProgram>,
}

Expand All @@ -22,7 +22,7 @@ pub fn handle_create_user(
let user_address = *accounts.user.to_account_view().address();
let rent = Rent::get()?;
accounts.user_account.set_inner(
UserStateInner { bump, user: user_address, name },
UserInner { bump, user: user_address, name },
accounts.user.to_account_view(),
rent.lamports_per_byte(),
rent.exemption_threshold_raw(),
Expand Down
2 changes: 1 addition & 1 deletion basics/close-account/quasar/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use quasar_lang::prelude::*;
/// Fixed fields (bump, user) must precede dynamic fields (name).
#[account(discriminator = 1, set_inner)]
#[seeds(b"USER", user: Address)]
pub struct UserState {
pub struct User {
pub bump: u8,
pub user: Address,
pub name: String<50>,
Expand Down
Loading