spl_governance/processor/
process_set_realm_authority.rs

1//! Program state processor
2
3use {
4    crate::{
5        error::GovernanceError,
6        state::{
7            governance::assert_governance_for_realm,
8            realm::{get_realm_data_for_authority, SetRealmAuthorityAction},
9        },
10    },
11    solana_program::{
12        account_info::{next_account_info, AccountInfo},
13        entrypoint::ProgramResult,
14        pubkey::Pubkey,
15    },
16};
17
18/// Processes SetRealmAuthority instruction
19pub fn process_set_realm_authority(
20    program_id: &Pubkey,
21    accounts: &[AccountInfo],
22    action: SetRealmAuthorityAction,
23) -> ProgramResult {
24    let account_info_iter = &mut accounts.iter();
25
26    let realm_info = next_account_info(account_info_iter)?; // 0
27    let realm_authority_info = next_account_info(account_info_iter)?; // 1
28
29    let mut realm_data =
30        get_realm_data_for_authority(program_id, realm_info, realm_authority_info.key)?;
31
32    if !realm_authority_info.is_signer {
33        return Err(GovernanceError::RealmAuthorityMustSign.into());
34    }
35
36    let new_realm_authority = match action {
37        SetRealmAuthorityAction::SetUnchecked | SetRealmAuthorityAction::SetChecked => {
38            let new_realm_authority_info = next_account_info(account_info_iter)?; // 2
39
40            if action == SetRealmAuthorityAction::SetChecked {
41                // Ensure the new realm authority is one of the governances from the realm
42                assert_governance_for_realm(program_id, new_realm_authority_info, realm_info.key)?;
43            }
44
45            Some(*new_realm_authority_info.key)
46        }
47        SetRealmAuthorityAction::Remove => None,
48    };
49
50    realm_data.authority = new_realm_authority;
51
52    realm_data.serialize(&mut realm_info.data.borrow_mut()[..])?;
53
54    Ok(())
55}