ai-registry 0.1.0

AI Registry on Solana - On-chain agent identity and knowledge management
Documentation
use ai_registry_api::{
    consts::{AGENT, FLAG_ACTIVE},
    error::AgentError,
    event::AgentClosedEvent,
    state::Agent,
};
use steel::*;

/// Process a close agent instruction
pub fn process_close_agent(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult {
    // Load accounts (signer first, following Solana best practices)
    let [controller_info, agent_info, recipient_info, system_program] = accounts else {
        return Err(ProgramError::NotEnoughAccountKeys);
    };

    // Validate accounts
    agent_info.is_writable()?.has_seeds(
        &[AGENT, agent_info.as_account::<Agent>(&ai_registry_api::ID)?.creator.as_ref()],
        &ai_registry_api::ID,
    )?;
    controller_info.is_signer()?;
    recipient_info.is_writable()?;
    system_program.is_program(&system_program::ID)?;

    // Load agent and verify controller
    let agent = agent_info.as_account::<Agent>(&ai_registry_api::ID)?;
    if controller_info.key != &agent.controller {
        return Err(AgentError::UnauthorizedController.into());
    }

    // Check if agent is active
    if agent.state_flags & FLAG_ACTIVE != 0 {
        return Err(AgentError::AgentActive.into());
    }

    // Emit event before closing
    let clock = Clock::get()?;
    let creator = agent.creator;
    let agent_key = *agent_info.key;

    AgentClosedEvent {
        creator,
        agent: agent_key,
        timestamp: clock.unix_timestamp,
    }
    .log();

    // Close the account (Steel framework helper transfers lamports automatically)
    close_account(agent_info, recipient_info)?;

    Ok(())
}