Skip to main content

sss_token/instructions/
seize.rs

1use anchor_lang::prelude::*;
2use anchor_spl::{
3    token_2022::{self, Burn, FreezeAccount, MintTo, ThawAccount, Token2022},
4    token_interface::TokenAccount,
5};
6
7use crate::errors::SssError;
8use crate::events::TokensSeized;
9use crate::state::*;
10use crate::utils::require_role;
11
12#[derive(Accounts)]
13pub struct Seize<'info> {
14    pub authority: Signer<'info>,
15
16    #[account(
17        mut,
18        seeds = [StablecoinConfig::SEED_PREFIX, config.mint.as_ref()],
19        bump = config.bump,
20    )]
21    pub config: Account<'info, StablecoinConfig>,
22
23    #[account(
24        seeds = [RoleRegistry::SEED_PREFIX, config.key().as_ref()],
25        bump = role_registry.bump,
26        constraint = role_registry.config == config.key() @ SssError::InvalidAuthority,
27    )]
28    pub role_registry: Account<'info, RoleRegistry>,
29
30    /// Proves the target is blacklisted
31    #[account(
32        seeds = [BlacklistEntry::SEED_PREFIX, config.key().as_ref(), blacklist_entry.blocked_address.as_ref()],
33        bump = blacklist_entry.bump,
34        constraint = blacklist_entry.config == config.key(),
35    )]
36    pub blacklist_entry: Account<'info, BlacklistEntry>,
37
38    /// CHECK: The Token-2022 mint account. Address validated against config, owner against Token-2022.
39    #[account(
40        mut,
41        address = config.mint,
42        constraint = mint.owner == &token_program.key() @ SssError::InvalidAuthority,
43    )]
44    pub mint: UncheckedAccount<'info>,
45
46    /// The token account to seize from (must be owned by the blacklisted address)
47    #[account(
48        mut,
49        token::mint = config.mint,
50        token::authority = blacklist_entry.blocked_address,
51        token::token_program = token_program,
52    )]
53    pub from_token_account: InterfaceAccount<'info, TokenAccount>,
54
55    /// The destination token account (e.g., treasury)
56    #[account(
57        mut,
58        token::mint = config.mint,
59        token::token_program = token_program,
60    )]
61    pub to_token_account: InterfaceAccount<'info, TokenAccount>,
62
63    pub token_program: Program<'info, Token2022>,
64}
65
66pub fn handler(ctx: Context<Seize>, amount: u64) -> Result<()> {
67    let config = &ctx.accounts.config;
68
69    require!(
70        config.enable_permanent_delegate,
71        SssError::FeatureNotEnabled
72    );
73
74    require_role(
75        &ctx.accounts.role_registry,
76        &ctx.accounts.authority.key(),
77        Role::Seizer,
78    )?;
79
80    require!(amount > 0, SssError::SeizeAmountZero);
81
82    require!(
83        ctx.accounts.from_token_account.key() != ctx.accounts.to_token_account.key(),
84        SssError::SeizeSameAccount
85    );
86
87    let clock = Clock::get()?;
88    let mint_key = config.mint;
89    let signer_seeds: &[&[&[u8]]] = &[&[
90        StablecoinConfig::SEED_PREFIX,
91        mint_key.as_ref(),
92        &[config.bump],
93    ]];
94
95    require!(
96        ctx.accounts.from_token_account.amount >= amount,
97        SssError::InsufficientBalance
98    );
99
100    // Step 1: Thaw the frozen account (blacklisted accounts are frozen)
101    token_2022::thaw_account(CpiContext::new_with_signer(
102        ctx.accounts.token_program.to_account_info(),
103        ThawAccount {
104            account: ctx.accounts.from_token_account.to_account_info(),
105            mint: ctx.accounts.mint.to_account_info(),
106            authority: ctx.accounts.config.to_account_info(),
107        },
108        signer_seeds,
109    ))?;
110
111    // Step 2: Burn tokens from the seized account (config PDA is permanent delegate)
112    // Using burn+mint instead of transfer_checked avoids triggering the transfer hook,
113    // which is correct since seize is a privileged program operation, not a user transfer.
114    token_2022::burn(
115        CpiContext::new_with_signer(
116            ctx.accounts.token_program.to_account_info(),
117            Burn {
118                mint: ctx.accounts.mint.to_account_info(),
119                from: ctx.accounts.from_token_account.to_account_info(),
120                authority: ctx.accounts.config.to_account_info(), // permanent delegate
121            },
122            signer_seeds,
123        ),
124        amount,
125    )?;
126
127    // Step 3: Mint equivalent tokens to the destination (treasury)
128    token_2022::mint_to(
129        CpiContext::new_with_signer(
130            ctx.accounts.token_program.to_account_info(),
131            MintTo {
132                mint: ctx.accounts.mint.to_account_info(),
133                to: ctx.accounts.to_token_account.to_account_info(),
134                authority: ctx.accounts.config.to_account_info(), // mint authority
135            },
136            signer_seeds,
137        ),
138        amount,
139    )?;
140
141    // Step 4: Re-freeze the account (it's still blacklisted)
142    token_2022::freeze_account(CpiContext::new_with_signer(
143        ctx.accounts.token_program.to_account_info(),
144        FreezeAccount {
145            account: ctx.accounts.from_token_account.to_account_info(),
146            mint: ctx.accounts.mint.to_account_info(),
147            authority: ctx.accounts.config.to_account_info(),
148        },
149        signer_seeds,
150    ))?;
151
152    let config = &mut ctx.accounts.config;
153    config.total_seized = config
154        .total_seized
155        .checked_add(amount)
156        .ok_or(SssError::Overflow)?;
157    config.updated_at = clock.unix_timestamp;
158
159    emit!(TokensSeized {
160        config: config.key(),
161        from: ctx.accounts.from_token_account.key(),
162        amount,
163        seized_by: ctx.accounts.authority.key(),
164        timestamp: clock.unix_timestamp,
165    });
166
167    Ok(())
168}