1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use crate::anchor_traits::*;
use crate::prelude::*;
use borsh::BorshSerialize;
use solana_program::pubkey::Pubkey;
use crate::get_sb_program_id;

#[repr(u32)]
#[derive(Copy, Clone)]
pub enum SwitchboardPermission {
    None = 0 << 0,
    PermitOracleHeartbeat = 1 << 0,
    PermitOracleQueueUsage = 1 << 1,
}

pub struct AttestationPermissionSet {}

#[derive(Clone, BorshSerialize, Debug)]
pub struct AttestationPermissionSetParams {
    pub permission: u8,
    pub enable: bool,
}

impl InstructionData for AttestationPermissionSetParams {}

impl Discriminator for AttestationPermissionSetParams {
    const DISCRIMINATOR: [u8; 8] = AttestationPermissionSet::DISCRIMINATOR;
}

impl Discriminator for AttestationPermissionSet {
    const DISCRIMINATOR: [u8; 8] = [211, 122, 185, 120, 129, 182, 55, 103];
}

pub struct AttestationPermissionSetAccounts {
    pub authority: Pubkey,
    pub granter: Pubkey,
    pub grantee: Pubkey,
}
impl ToAccountMetas for AttestationPermissionSetAccounts {
    fn to_account_metas(&self, _: Option<bool>) -> Vec<AccountMeta> {
        vec![
            AccountMeta::new_readonly(self.authority, true),
            AccountMeta::new_readonly(self.granter, false),
            AccountMeta::new(self.grantee, false),
        ]
    }
}

impl AttestationPermissionSet {
    pub fn build_ix(
        granter: Pubkey,
        authority: Pubkey,
        grantee: Pubkey,
        permission: SwitchboardPermission,
        enable: bool,
    ) -> Result<Instruction, OnDemandError> {
        let cluster = std::env::var("CLUSTER").unwrap_or("mainnet".to_string());
        let pid = get_sb_program_id(&cluster);
        Ok(crate::utils::build_ix(
            &pid,
            &AttestationPermissionSetAccounts {
                authority,
                granter,
                grantee,
            },
            &AttestationPermissionSetParams {
                permission: permission as u8,
                enable,
            },
        ))
    }
}