Skip to main content

stealth_vault/instructions/
execute_intent.rs

1use anchor_lang::prelude::*;
2
3use crate::{
4    constants::{INTENT_SEED, INTENT_STATUS_EXECUTED, INTENT_STATUS_PENDING, VAULT_SEED},
5    errors::StealthVaultError,
6    state::{ExecutionIntent, Vault},
7};
8
9#[derive(Accounts)]
10#[instruction(nonce: u64)]
11pub struct ExecuteIntent<'info> {
12    pub executor: Signer<'info>,
13    #[account(
14        seeds = [VAULT_SEED, vault.owner.as_ref()],
15        bump = vault.bump
16    )]
17    pub vault: Account<'info, Vault>,
18    #[account(
19        mut,
20        seeds = [INTENT_SEED, vault.key().as_ref(), &nonce.to_le_bytes()],
21        bump = intent.bump,
22        constraint = intent.vault == vault.key()
23    )]
24    pub intent: Account<'info, ExecutionIntent>,
25}
26
27pub fn execute_intent_handler(ctx: Context<ExecuteIntent>, _nonce: u64) -> Result<()> {
28    let executor = ctx.accounts.executor.key();
29    let vault = &ctx.accounts.vault;
30    let intent = &mut ctx.accounts.intent;
31
32    require!(
33        executor == vault.ephemeral_authority,
34        StealthVaultError::UnauthorizedIntentExecution
35    );
36    require!(
37        intent.status == INTENT_STATUS_PENDING,
38        StealthVaultError::IntentNotPending
39    );
40
41    intent.status = INTENT_STATUS_EXECUTED;
42    intent.executor = executor;
43    intent.executed_at = Clock::get()?.unix_timestamp;
44
45    Ok(())
46}