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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use crate::dispatch::{DispatchError, Parameter};
use codec::{HasCompact, MaxEncodedLen};
use sp_arithmetic::{
traits::{SaturatedConversion, UniqueSaturatedFrom, UniqueSaturatedInto},
Perbill,
};
use sp_runtime::traits::Member;
use sp_std::prelude::*;
pub trait CurrencyToVote<B> {
fn to_vote(value: B, issuance: B) -> u64;
fn to_currency(value: u128, issuance: B) -> B;
}
pub struct U128CurrencyToVote;
impl U128CurrencyToVote {
fn factor(issuance: u128) -> u128 {
(issuance / u64::MAX as u128).max(1)
}
}
impl CurrencyToVote<u128> for U128CurrencyToVote {
fn to_vote(value: u128, issuance: u128) -> u64 {
(value / Self::factor(issuance)).saturated_into()
}
fn to_currency(value: u128, issuance: u128) -> u128 {
value.saturating_mul(Self::factor(issuance))
}
}
pub struct SaturatingCurrencyToVote;
impl<B: UniqueSaturatedInto<u64> + UniqueSaturatedFrom<u128>> CurrencyToVote<B>
for SaturatingCurrencyToVote
{
fn to_vote(value: B, _: B) -> u64 {
value.unique_saturated_into()
}
fn to_currency(value: u128, _: B) -> B {
B::unique_saturated_from(value)
}
}
pub trait VoteTally<Votes, Class> {
fn new(_: Class) -> Self;
fn ayes(&self, class: Class) -> Votes;
fn support(&self, class: Class) -> Perbill;
fn approval(&self, class: Class) -> Perbill;
#[cfg(feature = "runtime-benchmarks")]
fn unanimity(class: Class) -> Self;
#[cfg(feature = "runtime-benchmarks")]
fn rejection(class: Class) -> Self;
#[cfg(feature = "runtime-benchmarks")]
fn from_requirements(support: Perbill, approval: Perbill, class: Class) -> Self;
#[cfg(feature = "runtime-benchmarks")]
fn setup(class: Class, granularity: Perbill);
}
pub enum PollStatus<Tally, Moment, Class> {
None,
Ongoing(Tally, Class),
Completed(Moment, bool),
}
impl<Tally, Moment, Class> PollStatus<Tally, Moment, Class> {
pub fn ensure_ongoing(self) -> Option<(Tally, Class)> {
match self {
Self::Ongoing(t, c) => Some((t, c)),
_ => None,
}
}
}
pub struct ClassCountOf<P, T>(sp_std::marker::PhantomData<(P, T)>);
impl<T, P: Polling<T>> sp_runtime::traits::Get<u32> for ClassCountOf<P, T> {
fn get() -> u32 {
P::classes().len() as u32
}
}
pub trait Polling<Tally> {
type Index: Parameter + Member + Ord + PartialOrd + Copy + HasCompact + MaxEncodedLen;
type Votes: Parameter + Member + Ord + PartialOrd + Copy + HasCompact + MaxEncodedLen;
type Class: Parameter + Member + Ord + PartialOrd + MaxEncodedLen;
type Moment;
fn classes() -> Vec<Self::Class>;
fn as_ongoing(index: Self::Index) -> Option<(Tally, Self::Class)>;
fn access_poll<R>(
index: Self::Index,
f: impl FnOnce(PollStatus<&mut Tally, Self::Moment, Self::Class>) -> R,
) -> R;
fn try_access_poll<R>(
index: Self::Index,
f: impl FnOnce(PollStatus<&mut Tally, Self::Moment, Self::Class>) -> Result<R, DispatchError>,
) -> Result<R, DispatchError>;
#[cfg(feature = "runtime-benchmarks")]
fn create_ongoing(class: Self::Class) -> Result<Self::Index, ()>;
#[cfg(feature = "runtime-benchmarks")]
fn end_ongoing(index: Self::Index, approved: bool) -> Result<(), ()>;
#[cfg(feature = "runtime-benchmarks")]
fn max_ongoing() -> (Self::Class, u32) {
(Self::classes().into_iter().next().expect("Always one class"), u32::max_value())
}
}