Skip to main content

cow_settlement_interface/
role.rs

1//! Transferable authority roles stored in the settlement state PDA.
2
3use solana_program_error::ProgramError;
4
5/// A transferable authority stored in the state PDA.
6///
7/// The discriminant is the wire value carried by the authority-transfer
8/// instruction (see [`transfer_authority`](crate::instruction::transfer_authority)).
9#[derive(Clone, Copy, Debug, Eq, PartialEq, num_enum::TryFromPrimitive)]
10#[cfg_attr(feature = "cli", derive(clap::ValueEnum))]
11#[repr(u8)]
12#[num_enum(error_type(name = ProgramError, constructor = Role::unknown_role))]
13pub enum Role {
14    /// The account authorized to add and remove solvers and to transfer roles.
15    /// It is the highest authority: it may transfer any role.
16    Manager = 0,
17    /// The account authorized to close buffer accounts and reclaim their rent,
18    /// choosing where that rent goes.
19    ReclaimAuthority,
20    /// The account authorized to place orders that sell the protocol's own
21    /// buffer balances (e.g., fee withdrawals). It chooses the destination, the
22    /// bought token, and the price of those orders.
23    SelfOrderAuthority,
24}
25
26impl Role {
27    /// Every [`Role`] variant, in discriminant order.
28    pub const ALL: [Self; 3] = [
29        Role::Manager,
30        Role::ReclaimAuthority,
31        Role::SelfOrderAuthority,
32    ];
33
34    /// The single wire byte that selects this role in the authority-transfer
35    /// instruction.
36    pub fn discriminator(self) -> u8 {
37        self as u8
38    }
39
40    fn unknown_role(_: u8) -> ProgramError {
41        ProgramError::InvalidInstructionData
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn role_try_from_partitions_all_bytes() {
51        for i in u8::MIN..=u8::MAX {
52            match Role::try_from(i) {
53                Ok(role) => assert_eq!(role as u8, i),
54                Err(err) => assert_eq!(err, ProgramError::InvalidInstructionData),
55            }
56        }
57    }
58
59    #[test]
60    fn role_try_from_matches_manager() {
61        assert_eq!(Role::try_from(0), Ok(Role::Manager));
62    }
63
64    #[test]
65    fn all_roles_lists_every_role_in_discriminator_order() {
66        // The roles `try_from` accepts, discovered independently of `Role::ALL`.
67        // The scan runs over ascending bytes, so this is every role that exists,
68        // in discriminant order.
69        let every_role: Vec<Role> = (u8::MIN..=u8::MAX)
70            .filter_map(|byte| Role::try_from(byte).ok())
71            .collect();
72
73        assert_eq!(Role::ALL.as_slice(), every_role.as_slice());
74    }
75}