Skip to main content

sss_token/instructions/
accept_authority.rs

1use anchor_lang::prelude::*;
2
3use crate::errors::SssError;
4use crate::events::AuthorityTransferred;
5use crate::state::*;
6
7#[derive(Accounts)]
8pub struct AcceptAuthority<'info> {
9    pub authority: Signer<'info>,
10
11    #[account(
12        mut,
13        seeds = [StablecoinConfig::SEED_PREFIX, config.mint.as_ref()],
14        bump = config.bump,
15    )]
16    pub config: Account<'info, StablecoinConfig>,
17
18    #[account(
19        mut,
20        seeds = [RoleRegistry::SEED_PREFIX, config.key().as_ref()],
21        bump = role_registry.bump,
22        constraint = role_registry.config == config.key() @ SssError::InvalidAuthority,
23    )]
24    pub role_registry: Account<'info, RoleRegistry>,
25}
26
27pub fn handler(ctx: Context<AcceptAuthority>) -> Result<()> {
28    let pending_authority = ctx.accounts.config.pending_authority;
29    require!(
30        pending_authority != Pubkey::default(),
31        SssError::NoPendingAuthority
32    );
33    require!(
34        ctx.accounts.authority.key() == pending_authority,
35        SssError::NotPendingAuthority
36    );
37
38    let clock = Clock::get()?;
39    let old_authority = ctx.accounts.config.master_authority;
40    let new_authority = ctx.accounts.authority.key();
41
42    let config = &mut ctx.accounts.config;
43    config.master_authority = new_authority;
44    config.pending_authority = Pubkey::default();
45    config.updated_at = clock.unix_timestamp;
46
47    let role_registry = &mut ctx.accounts.role_registry;
48    role_registry.master_authority = new_authority;
49
50    if role_registry.pauser == old_authority {
51        role_registry.pauser = new_authority;
52    }
53    if role_registry.blacklister == old_authority {
54        role_registry.blacklister = new_authority;
55    }
56    if role_registry.seizer == old_authority {
57        role_registry.seizer = new_authority;
58    }
59
60    emit!(AuthorityTransferred {
61        config: config.key(),
62        old_authority,
63        new_authority,
64        timestamp: clock.unix_timestamp,
65    });
66
67    Ok(())
68}