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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use crate::{CoinVoting, ConvictionVoting, PlcrVoting};
use codec::{Decode, Encode};
pub use governance_os_pallet_coin_voting::{
VoteData as CoinVoteData, VotingParameters as CoinVotingParameters,
};
pub use governance_os_pallet_conviction_voting::{
Conviction, VotingParameters as ConvictionVotingParameters,
};
pub use governance_os_pallet_plcr_voting::{
VoteData as PlcrVoteData, VotingParameters as PlcrVotingParameters,
};
use governance_os_primitives::{AccountId, Balance, BlockNumber, CurrencyId, Hash};
use governance_os_support::traits::{ProposalResult, StandardizedVoting, VotingRouter};
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use sp_runtime::{DispatchError, DispatchResult, RuntimeDebug};
use sp_std::result;
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub enum RuntimeVotingParameters {
CoinVoting(CoinVotingParameters<BlockNumber, CurrencyId>),
ConvictionVoting(ConvictionVotingParameters<BlockNumber, CurrencyId>),
PlcrVoting(PlcrVotingParameters<BlockNumber, CurrencyId>),
}
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub enum RuntimeVoteData {
CoinVoting(CoinVoteData<Balance>),
ConvictionVoting(Conviction<Balance>),
PlcrVoting(PlcrVoteData<Balance, Hash>),
}
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub enum RuntimeVotingSystemId {
CoinVoting,
ConvictionVoting,
PlcrVoting,
}
pub struct RuntimeVotingRouter;
impl VotingRouter for RuntimeVotingRouter {
type AccountId = AccountId;
type VotingSystemId = RuntimeVotingSystemId;
type Parameters = RuntimeVotingParameters;
type ProposalId = Hash;
type VoteData = RuntimeVoteData;
fn initiate(
voting_system: Self::VotingSystemId,
proposal: Self::ProposalId,
parameters: Self::Parameters,
) -> DispatchResult {
match (voting_system, parameters) {
(RuntimeVotingSystemId::CoinVoting, RuntimeVotingParameters::CoinVoting(params)) => {
CoinVoting::initiate(proposal, params)
}
(
RuntimeVotingSystemId::ConvictionVoting,
RuntimeVotingParameters::ConvictionVoting(params),
) => ConvictionVoting::initiate(proposal, params),
(RuntimeVotingSystemId::PlcrVoting, RuntimeVotingParameters::PlcrVoting(params)) => {
PlcrVoting::initiate(proposal, params)
}
_ => Err("wrong voting system, voting parameters pair".into()),
}
}
fn veto(voting_system: Self::VotingSystemId, proposal: Self::ProposalId) -> DispatchResult {
match voting_system {
RuntimeVotingSystemId::CoinVoting => CoinVoting::veto(proposal),
RuntimeVotingSystemId::ConvictionVoting => ConvictionVoting::veto(proposal),
RuntimeVotingSystemId::PlcrVoting => PlcrVoting::veto(proposal),
}
}
fn vote(
voting_system: Self::VotingSystemId,
proposal: Self::ProposalId,
voter: &Self::AccountId,
vote_data: Self::VoteData,
) -> DispatchResult {
match (voting_system, vote_data) {
(RuntimeVotingSystemId::CoinVoting, RuntimeVoteData::CoinVoting(data)) => {
CoinVoting::vote(proposal, voter, data)
}
(RuntimeVotingSystemId::ConvictionVoting, RuntimeVoteData::ConvictionVoting(data)) => {
ConvictionVoting::vote(proposal, voter, data)
}
(RuntimeVotingSystemId::PlcrVoting, RuntimeVoteData::PlcrVoting(data)) => {
PlcrVoting::vote(proposal, voter, data)
}
_ => Err("wrong voting system, vote data pair".into()),
}
}
fn close(
voting_system: Self::VotingSystemId,
proposal: Self::ProposalId,
) -> result::Result<ProposalResult, DispatchError> {
match voting_system {
RuntimeVotingSystemId::CoinVoting => CoinVoting::close(proposal),
RuntimeVotingSystemId::ConvictionVoting => ConvictionVoting::close(proposal),
RuntimeVotingSystemId::PlcrVoting => PlcrVoting::close(proposal),
}
}
}