multiversx_sc_modules/governance/
governance_proposal.rs

1multiversx_sc::imports!();
2multiversx_sc::derive_imports!();
3
4pub const MAX_GOVERNANCE_PROPOSAL_ACTIONS: usize = 4;
5pub type ProposalId = usize;
6
7pub type GovernanceActionAsMultiArg<M> =
8    MultiValue4<u64, ManagedAddress<M>, ManagedBuffer<M>, ManagedVec<M, ManagedBuffer<M>>>;
9
10#[type_abi]
11#[derive(TopEncode, TopDecode)]
12pub enum VoteType {
13    UpVote,
14    DownVote,
15    DownVetoVote,
16    AbstainVote,
17}
18
19#[type_abi]
20#[derive(TopEncode, TopDecode, PartialEq, Eq)]
21pub enum GovernanceProposalStatus {
22    None,
23    Pending,
24    Active,
25    Defeated,
26    Succeeded,
27    Queued,
28    WaitingForFees,
29}
30
31#[type_abi]
32#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, ManagedVecItem)]
33pub struct ProposalFees<M: ManagedTypeApi> {
34    pub total_amount: BigUint<M>,
35    pub entries: ManagedVec<M, FeeEntry<M>>,
36}
37
38#[type_abi]
39#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, ManagedVecItem, Clone)]
40pub struct FeeEntry<M: ManagedTypeApi> {
41    pub depositor_addr: ManagedAddress<M>,
42    pub tokens: EsdtTokenPayment<M>,
43}
44
45#[type_abi]
46#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode)]
47pub struct GovernanceAction<M: ManagedTypeApi> {
48    pub gas_limit: u64,
49    pub dest_address: ManagedAddress<M>,
50    pub function_name: ManagedBuffer<M>,
51    pub arguments: ManagedVec<M, ManagedBuffer<M>>,
52}
53
54impl<M: ManagedTypeApi> GovernanceAction<M> {
55    pub fn into_multiarg(self) -> GovernanceActionAsMultiArg<M> {
56        (
57            self.gas_limit,
58            self.dest_address,
59            self.function_name,
60            self.arguments,
61        )
62            .into()
63    }
64}
65
66#[type_abi]
67#[derive(TopEncode, TopDecode)]
68pub struct GovernanceProposal<M: ManagedTypeApi> {
69    pub proposer: ManagedAddress<M>,
70    pub actions: ArrayVec<GovernanceAction<M>, MAX_GOVERNANCE_PROPOSAL_ACTIONS>,
71    pub description: ManagedBuffer<M>,
72    pub fees: ProposalFees<M>,
73}
74
75#[type_abi]
76#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode)]
77pub struct ProposalVotes<M: ManagedTypeApi> {
78    pub up_votes: BigUint<M>,
79    pub down_votes: BigUint<M>,
80    pub down_veto_votes: BigUint<M>,
81    pub abstain_votes: BigUint<M>,
82}
83
84impl<M: ManagedTypeApi> Default for ProposalVotes<M> {
85    fn default() -> Self {
86        Self::new()
87    }
88}
89
90impl<M: ManagedTypeApi> ProposalVotes<M> {
91    pub fn new() -> Self {
92        ProposalVotes {
93            up_votes: BigUint::zero(),
94            down_votes: BigUint::zero(),
95            down_veto_votes: BigUint::zero(),
96            abstain_votes: BigUint::zero(),
97        }
98    }
99
100    pub fn get_total_votes(&self) -> BigUint<M> {
101        &self.up_votes + &self.down_votes + &self.down_veto_votes + &self.abstain_votes
102    }
103    pub fn get_up_votes_percentage(&self) -> BigUint<M> {
104        let total_votes = self.get_total_votes();
105        &self.up_votes / &total_votes
106    }
107    pub fn get_down_votes_percentage(&self) -> BigUint<M> {
108        let total_votes = self.get_total_votes();
109        &self.down_votes / &total_votes
110    }
111    pub fn get_down_veto_votes_percentage(&self) -> BigUint<M> {
112        let total_votes = self.get_total_votes();
113        &self.down_veto_votes / &total_votes
114    }
115    pub fn get_abstain_votes_percentage(&self) -> BigUint<M> {
116        let total_votes = self.get_total_votes();
117        &self.abstain_votes / &total_votes
118    }
119}