triggr-program 0.1.1

Created with Anchor
Documentation
pub use crate::state::*;
use anchor_lang::{prelude::*, system_program::System};

#[derive(Accounts)]
#[instruction(tirgger_index: u64, conditions: AdjacencyTree)]
pub struct UpdateTrigger<'info> {
    #[account(mut)]
    signer: Signer<'info>,

    #[account(mut, seeds = ["user".as_bytes(), &signer.key().to_bytes()], bump)]
    user: Account<'info, User>,

    #[account(
        mut, 
        seeds = ["trigger".as_bytes(), &signer.key().to_bytes()[..], &tirgger_index.to_le_bytes()], 
        bump, 
        realloc = 8 + Trigger::get_size(&conditions),
        realloc::payer = signer,
        realloc::zero = true
    )]
    trigger: Account<'info, Trigger>,

    system_program: Program<'info, System>,
}

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

    // todo: When conditions are updated enable the status of the trigger to also be updated. 
    // If status is set to Active then the trigger should be added to the user's active_triggers list.
    // the size of the user's account doesn't need to be updated since the 32 bytes of the trigger's account were added on creation
    // and will be deleted on closing of the trigger account. 

    ctx.accounts.trigger.conditions = conditions;

    Ok(())
}