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
//! Contains Dao Ids Contract definition and related abstractions.
//!
//! There is one continuous indexation of votes in the system.
//! Each new voting gets a unique across-the-system id generated by the contract.

use crate::modules::AccessControl;
use odra::{contract_env, types::Address, Sequence};

/// Dao Ids contract manages voting ids in the system.
/// Only a whitelisted account is eligible to generate ids.
///
/// For details see [DaoIdsContract]).
#[odra::module]
pub struct DaoIdsContract {
    access_control: AccessControl,
    voting_id_seq: Sequence<u32>,
}

#[odra::module]
impl DaoIdsContract {
    delegate! {
        to self.access_control {
            /// Changes the ownership of the contract. Transfers the ownership to the `owner`.
            /// Only the current owner is permitted to call this method.
            /// [`Read more`](AccessControl::propose_new_owner())
            pub fn propose_new_owner(&mut self, owner: Address);
            /// Accepts the new owner proposition. This can be called only by the proposed owner.
            /// [`Read more`](crate::modules::access_control::AccessControl::accept_new_owner())
            pub fn accept_new_owner(&mut self);
            /// Adds a new address to the whitelist.
            ///
            /// [`Read more`](AccessControl::add_to_whitelist())
            pub fn add_to_whitelist(&mut self, address: Address);
            /// Remove address from the whitelist.
            ///
            /// [`Read more`](AccessControl::remove_from_whitelist())
            pub fn remove_from_whitelist(&mut self, address: Address);
            /// Checks whether the given address is added to the whitelist.
            ///
            /// [`Read more`](AccessControl::is_whitelisted()).
            pub fn is_whitelisted(&self, address: Address) -> bool;
            /// Returns the address of the current owner.
            ///
            /// [`Read more`](AccessControl::get_owner()).
            pub fn get_owner(&self) -> Option<Address>;
        }
    }

    ///  Initializes a contract.
    ///  Sets the deployer as the owner.
    ///
    ///  see [AccessControl](AccessControl::init())
    #[odra(init)]
    pub fn init(&mut self) {
        let deployer = contract_env::caller();
        self.access_control.init(deployer);
    }

    /// Returns the next voting id in the system.
    ///
    /// # Errors
    /// Throws [`NotWhitelisted`](crate::utils::Error::NotWhitelisted) if the caller is not whitelisted.
    pub fn next_voting_id(&mut self) -> u32 {
        self.access_control.ensure_whitelisted();
        self.voting_id_seq.next_value()
    }
}