Two instruction handlers: create_user initializes a PDA User account, and close_user closes it and returns the rent to the user.
-
create_userinitializes the PDA with Anchor'sinitconstraint:#[account( init, payer = user, space = User::DISCRIMINATOR.len() + User::INIT_SPACE, seeds = [b"USER", user.key().as_ref()], bump, )] pub user_account: Account<'info, User>,
-
close_usercloses the account using Anchor'scloseconstraint, which returns lamports to the given account:#[account( mut, seeds = [b"USER", user.key().as_ref()], bump = user_account.bump, close = user, // close account and return lamports to user )] pub user_account: Account<'info, User>,
Tests live in programs/close-account/tests/test_close_account.rs and run against litesvm. Anchor.toml's scripts.test is cargo test, so anchor test builds the program and runs the Rust tests:
anchor test