ohaddahan accbb54dc4 more readme.md 3 years ago
..
migrations 0d93eec785 destroy account example 3 years ago
programs 0d93eec785 destroy account example 3 years ago
tests 0d93eec785 destroy account example 3 years ago
.gitignore 0d93eec785 destroy account example 3 years ago
.prettierignore 0d93eec785 destroy account example 3 years ago
Anchor.toml b83568c680 update wallet address 3 years ago
Cargo.toml 0d93eec785 destroy account example 3 years ago
README.md accbb54dc4 more readme.md 3 years ago
package.json 0d93eec785 destroy account example 3 years ago
tsconfig.json 0d93eec785 destroy account example 3 years ago

README.md

Destroy an Account

  1. We're creating a PDA using create_user.rs instruction.

    #[account(
    init,
    seeds=[User::PREFIX.as_bytes(), user.key().as_ref()],
    payer=user,
    space=User::SIZE,
    bump
    )]
    pub user_account: Box<Account<'info, User>>,
    
  2. We're closing it using destroy_user.rs instruction, which uses Anchor AccoutClose trait.

    user_account.close(user.to_account_info())?;
    
  3. In our test destroy-an-account.ts we're using fetchNullable since we expect the account to be null prior to creation and after closing.

    const userAccountBefore = await program.account.user.fetchNullable(userAccountAddress, "processed");
    assert.equal(userAccountBefore, null);
    ...
    ...
    const userAccountAfter = await program.account.user.fetchNullable(userAccountAddress, "processed");
    assert.notEqual(userAccountAfter, null);