Skip to main content

sss_token/instructions/
update_metadata.rs

1use anchor_lang::prelude::*;
2
3use crate::errors::SssError;
4use crate::events::MetadataUpdated;
5use crate::state::*;
6use crate::utils::require_master_authority;
7
8#[derive(AnchorSerialize, AnchorDeserialize)]
9pub struct UpdateMetadataParams {
10    pub name: Option<String>,
11    pub symbol: Option<String>,
12    pub uri: Option<String>,
13}
14
15#[derive(Accounts)]
16pub struct UpdateMetadata<'info> {
17    pub authority: Signer<'info>,
18
19    #[account(
20        mut,
21        seeds = [StablecoinConfig::SEED_PREFIX, config.mint.as_ref()],
22        bump = config.bump,
23    )]
24    pub config: Account<'info, StablecoinConfig>,
25
26    #[account(
27        seeds = [RoleRegistry::SEED_PREFIX, config.key().as_ref()],
28        bump = role_registry.bump,
29        constraint = role_registry.config == config.key() @ SssError::InvalidAuthority,
30    )]
31    pub role_registry: Account<'info, RoleRegistry>,
32}
33
34pub fn handler(ctx: Context<UpdateMetadata>, params: UpdateMetadataParams) -> Result<()> {
35    require_master_authority(&ctx.accounts.role_registry, &ctx.accounts.authority.key())?;
36
37    if let Some(ref name) = params.name {
38        require!(
39            !name.is_empty() && name.len() <= StablecoinConfig::MAX_NAME_LEN,
40            SssError::NameTooLong
41        );
42    }
43    if let Some(ref symbol) = params.symbol {
44        require!(
45            !symbol.is_empty() && symbol.len() <= StablecoinConfig::MAX_SYMBOL_LEN,
46            SssError::SymbolTooLong
47        );
48    }
49    if let Some(ref uri) = params.uri {
50        require!(
51            !uri.is_empty() && uri.len() <= StablecoinConfig::MAX_URI_LEN,
52            SssError::UriTooLong
53        );
54    }
55
56    let clock = Clock::get()?;
57    let config = &mut ctx.accounts.config;
58
59    if let Some(name) = params.name {
60        config.name = name;
61    }
62    if let Some(symbol) = params.symbol {
63        config.symbol = symbol;
64    }
65    if let Some(uri) = params.uri {
66        config.uri = uri;
67    }
68
69    config.updated_at = clock.unix_timestamp;
70
71    emit!(MetadataUpdated {
72        config: config.key(),
73        timestamp: clock.unix_timestamp,
74    });
75
76    Ok(())
77}