3

I know I can set this up with Anchor accounts and trigger a anchor function through client side code

pub fn delete_item(ctx: Context<DeleteItem>) -> Result<()> {
   Ok(())
}

pub fn add_item(ctx: Context<AddItem>) -> Result<()> {
   Ok(())
}

#[derive(Accounts)]
pub struct DeleteItem<'info> {
    #[account(mut, close = user)]
    pub item: Account<'info,Item>,
    #[account(mut)]
    pub user: Signer<'info>,
}

#[derive(Accounts)]
pub struct CreateItem<'info> {
    #[account(mut, payer=user, space=Item::LEN, seed= [some_seed], bump)]
    pub item: Account<'info,Item>,
    #[account(mut)]
    pub user: Signer<'info>,
    pub system_program: Program<'info,System>,
}

But what I would like to do is to trigger deletion or creation within another Anchor function.

pub fn another_process(ctx: Context<AnotherItem>) -> Result<()> {
   
   ///Do some things... 

   
   ///Example: now delete item PDA through rust code here..
   ///Or create another item through function here.


   Ok(())
}

Is this possible? If so, what would the code look like?

1 Answer 1

2

yes. This is possible.

As long as your program owns said account/PDA, you can delete it together with its contents.

but what do you mean by another function. Using the anchor constraint you provided when the first instruction from DeleteItem Accounts struct finishes executing, it will call the close instruction for the account.

But if you have two functions tied to the DeleteItem, you could remove the close constraint and instead use

        ctx.accounts.item.close(ctx.accounts.user.to_account_info())?;
2
  • Ok, interesting.. sorry I was updating the questions with the ability to add as well. What I meant by another function is within the function another_process can I delete a pda or create a pda there.
    – ZeroNine
    Commented Jan 15 at 19:23
  • I also saw the confusion as I had mistakenly set DeleteItem as the context for another_process. I just updated my question.
    – ZeroNine
    Commented Jan 15 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.