use crate::state::{Namespace, Profile, User};
use anchor_lang::prelude::*;
use std::convert::AsRef;
use std::str::FromStr;
use crate::constants::*;
use crate::events::{ProfileDeleted, ProfileNew};
#[derive(Accounts)]
#[instruction(namespace: String)]
pub struct CreateProfile<'info> {
#[account(
init,
seeds = [
PROFILE_PREFIX_SEED.as_bytes(),
namespace.as_bytes(),
user.to_account_info().key.as_ref()
],
bump,
payer = authority,
space = Profile::LEN
)]
pub profile: Account<'info, Profile>,
#[account(
seeds = [
USER_PREFIX_SEED.as_bytes(),
user.random_hash.as_ref(),
],
bump,
has_one = authority,
)]
pub user: Account<'info, User>,
#[account(mut)]
pub authority: Signer<'info>,
pub system_program: Program<'info, System>,
}
pub fn create_profile_handler(ctx: Context<CreateProfile>, namespace: String) -> Result<()> {
let profile = &mut ctx.accounts.profile;
profile.namespace = Namespace::from_str(&namespace).unwrap();
profile.user = *ctx.accounts.user.to_account_info().key;
emit!(ProfileNew {
profile: *profile.to_account_info().key,
namespace: profile.namespace,
user: *ctx.accounts.user.to_account_info().key,
timestamp: Clock::get()?.unix_timestamp,
});
Ok(())
}
#[derive(Accounts)]
pub struct DeleteProfile<'info> {
#[account(
mut,
seeds = [
PROFILE_PREFIX_SEED.as_bytes(),
profile.namespace.as_ref().as_bytes(),
profile.user.as_ref(),
],
bump,
has_one = user,
close = authority,
)]
pub profile: Account<'info, Profile>,
#[account(
seeds = [
USER_PREFIX_SEED.as_bytes(),
user.random_hash.as_ref(),
],
bump,
has_one = authority,
)]
pub user: Account<'info, User>,
#[account(mut)]
pub authority: Signer<'info>,
}
pub fn delete_profile_handler(ctx: Context<DeleteProfile>) -> Result<()> {
emit!(ProfileDeleted {
profile: *ctx.accounts.profile.to_account_info().key,
namespace: ctx.accounts.profile.namespace,
user: *ctx.accounts.user.to_account_info().key,
timestamp: Clock::get()?.unix_timestamp,
});
Ok(())
}