Skip to main content

sss_token/instructions/
blacklist_add.rs

1use anchor_lang::prelude::*;
2use anchor_spl::{
3    token_2022::{self, FreezeAccount, Token2022},
4    token_interface::TokenAccount,
5};
6
7use crate::errors::SssError;
8use crate::events::BlacklistAdded;
9use crate::state::*;
10use crate::utils::require_role;
11
12#[derive(AnchorSerialize, AnchorDeserialize)]
13pub struct BlacklistAddParams {
14    pub reason: String,
15}
16
17#[derive(Accounts)]
18#[instruction(params: BlacklistAddParams)]
19pub struct BlacklistAdd<'info> {
20    #[account(mut)]
21    pub authority: Signer<'info>,
22
23    #[account(
24        mut,
25        seeds = [StablecoinConfig::SEED_PREFIX, config.mint.as_ref()],
26        bump = config.bump,
27    )]
28    pub config: Account<'info, StablecoinConfig>,
29
30    #[account(
31        seeds = [RoleRegistry::SEED_PREFIX, config.key().as_ref()],
32        bump = role_registry.bump,
33        constraint = role_registry.config == config.key() @ SssError::InvalidAuthority,
34    )]
35    pub role_registry: Account<'info, RoleRegistry>,
36
37    #[account(
38        init,
39        payer = authority,
40        space = BlacklistEntry::SPACE,
41        seeds = [BlacklistEntry::SEED_PREFIX, config.key().as_ref(), address_to_blacklist.key().as_ref()],
42        bump,
43    )]
44    pub blacklist_entry: Account<'info, BlacklistEntry>,
45
46    /// CHECK: The address being blacklisted.
47    pub address_to_blacklist: UncheckedAccount<'info>,
48
49    /// CHECK: The Token-2022 mint account. Address validated against config, owner against Token-2022.
50    #[account(
51        address = config.mint,
52        constraint = mint.owner == &token_program.key() @ SssError::InvalidAuthority,
53    )]
54    pub mint: UncheckedAccount<'info>,
55
56    /// The target's token account to freeze
57    #[account(
58        mut,
59        token::mint = config.mint,
60        token::authority = address_to_blacklist,
61        token::token_program = token_program,
62    )]
63    pub target_token_account: InterfaceAccount<'info, TokenAccount>,
64
65    pub token_program: Program<'info, Token2022>,
66    pub system_program: Program<'info, System>,
67}
68
69pub fn handler(ctx: Context<BlacklistAdd>, params: BlacklistAddParams) -> Result<()> {
70    let config = &ctx.accounts.config;
71
72    // Require SSS-2 features
73    require!(
74        config.enable_permanent_delegate,
75        SssError::BlacklistNotEnabled
76    );
77
78    require_role(
79        &ctx.accounts.role_registry,
80        &ctx.accounts.authority.key(),
81        Role::Blacklister,
82    )?;
83
84    require!(
85        params.reason.len() <= BlacklistEntry::MAX_REASON_LEN,
86        SssError::ReasonTooLong
87    );
88
89    // Cannot blacklist master authority
90    require!(
91        ctx.accounts.address_to_blacklist.key() != ctx.accounts.role_registry.master_authority,
92        SssError::CannotBlacklistAuthority
93    );
94
95    let clock = Clock::get()?;
96
97    // Create blacklist entry
98    let entry = &mut ctx.accounts.blacklist_entry;
99    entry.bump = ctx.bumps.blacklist_entry;
100    entry.config = config.key();
101    entry.blocked_address = ctx.accounts.address_to_blacklist.key();
102    entry.reason = params.reason.clone();
103    entry.blacklisted_by = ctx.accounts.authority.key();
104    entry.blacklisted_at = clock.unix_timestamp;
105
106    // Also freeze the target's token account
107    let mint_key = config.mint;
108    let signer_seeds: &[&[&[u8]]] = &[&[
109        StablecoinConfig::SEED_PREFIX,
110        mint_key.as_ref(),
111        &[config.bump],
112    ]];
113
114    token_2022::freeze_account(CpiContext::new_with_signer(
115        ctx.accounts.token_program.to_account_info(),
116        FreezeAccount {
117            account: ctx.accounts.target_token_account.to_account_info(),
118            mint: ctx.accounts.mint.to_account_info(),
119            authority: ctx.accounts.config.to_account_info(),
120        },
121        signer_seeds,
122    ))?;
123
124    emit!(BlacklistAdded {
125        config: config.key(),
126        blocked_address: ctx.accounts.address_to_blacklist.key(),
127        reason: params.reason,
128        blacklisted_by: ctx.accounts.authority.key(),
129        timestamp: clock.unix_timestamp,
130    });
131
132    let config = &mut ctx.accounts.config;
133    config.updated_at = clock.unix_timestamp;
134
135    Ok(())
136}