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 #[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 #[account(
40 mut,
41 address = config.mint,
42 constraint = mint.owner == &token_program.key() @ SssError::InvalidAuthority,
43 )]
44 pub mint: UncheckedAccount<'info>,
45
46 #[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 #[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 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 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(), },
122 signer_seeds,
123 ),
124 amount,
125 )?;
126
127 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(), },
136 signer_seeds,
137 ),
138 amount,
139 )?;
140
141 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}