triggr-program 0.1.1

Created with Anchor
Documentation
pub use crate::state::*;
use anchor_lang::prelude::*;

#[derive(Accounts)]
#[instruction(_trigger_index: u64)]
pub struct CloseTrigger<'info> {
    // needs to be signed by authority
    #[account(mut)]
    authority: Signer<'info>,

    #[account(mut, seeds = ["payer".as_bytes(), authority.key().as_ref()], bump)]
    payer: SystemAccount<'info>,

    #[account(
        mut, 
        seeds = ["user".as_bytes(), &authority.key().to_bytes()], 
        bump, 
        realloc = 8 + User::MIN_SIZE + (user.active_triggers.len() * 32) - 32, 
        realloc::payer = authority, 
        realloc::zero = true
    )]
    user: Account<'info, User>,

    #[account(mut, close = authority, seeds = ["trigger".as_bytes(), &authority.key().to_bytes()[..], &_trigger_index.to_le_bytes()], bump )]
    trigger: Account<'info, Trigger>,

    system_program: Program<'info, System>,
}

pub fn handler(ctx: Context<CloseTrigger>, _trigger_index: u64) -> Result<()> {

    let active_triggers = &mut ctx.accounts.user.active_triggers;

    active_triggers.retain(|&x| x != ctx.accounts.trigger.key());

    ctx.accounts.user.active_triggers = active_triggers.clone();
    
    Ok(())
}