tg-voting-contract 0.17.1

Generic utils for building voting contracts for tgrade
Documentation
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use cosmwasm_std::{Decimal, StdError};
use tg3::{Status, Vote};

use crate::multitest::suite::{get_proposal_id, SuiteBuilder};
use crate::state::{RulesBuilder, Votes};
use crate::ContractError;

#[test]
fn proposal_creator_votes_automatically() {
    let rules = RulesBuilder::new()
        .with_threshold(Decimal::percent(51))
        .build();

    let mut suite = SuiteBuilder::new()
        .with_member("alice", 1)
        .with_member("bob", 2)
        .with_member("carol", 3)
        .with_rules(rules)
        .build();

    // Create proposal with 1 voting power
    let response = suite.propose("alice", "proposal", "proposal").unwrap();
    let proposal_id: u64 = get_proposal_id(&response).unwrap();

    let prop = suite.query_proposal(proposal_id).unwrap();
    assert_eq!(prop.votes, Votes::yes(1));
}

#[test]
fn simple_vote() {
    let rules = RulesBuilder::new()
        .with_threshold(Decimal::percent(51))
        .build();

    let mut suite = SuiteBuilder::new()
        .with_member("alice", 1)
        .with_member("bob", 2)
        .with_member("carol", 3)
        .with_rules(rules)
        .build();

    // Create proposal with 1 voting power
    let response = suite.propose("alice", "proposal", "proposal").unwrap();
    let proposal_id: u64 = get_proposal_id(&response).unwrap();

    // Bob votes
    suite.vote("bob", proposal_id, Vote::Yes).unwrap();

    let prop = suite.query_proposal(proposal_id).unwrap();
    assert_eq!(prop.votes.total(), 3);
}

#[test]
fn proposal_creator_cannot_vote() {
    let rules = RulesBuilder::new()
        .with_threshold(Decimal::percent(51))
        .build();

    let mut suite = SuiteBuilder::new()
        .with_member("alice", 1)
        .with_member("bob", 2)
        .with_rules(rules)
        .build();

    // Create proposal with 1 voting power
    let response = suite.propose("alice", "proposal", "proposal").unwrap();
    let proposal_id: u64 = get_proposal_id(&response).unwrap();

    // Creator cannot vote (again)
    let err = suite.vote("alice", proposal_id, Vote::Yes).unwrap_err();
    assert_eq!(ContractError::AlreadyVoted {}, err.downcast().unwrap());
}

#[test]
fn cannot_double_vote() {
    let rules = RulesBuilder::new()
        .with_threshold(Decimal::percent(51))
        .build();

    let mut suite = SuiteBuilder::new()
        .with_member("alice", 1)
        .with_member("bob", 2)
        .with_member("carol", 3)
        .with_rules(rules)
        .build();

    // Create proposal with 1 voting power
    let response = suite.propose("alice", "proposal", "proposal").unwrap();
    let proposal_id: u64 = get_proposal_id(&response).unwrap();

    // Bob votes
    suite.vote("bob", proposal_id, Vote::Yes).unwrap();

    // Bob cannot vote again
    let err = suite.vote("bob", proposal_id, Vote::Yes).unwrap_err();
    assert_eq!(ContractError::AlreadyVoted {}, err.downcast().unwrap());
}

#[test]
fn non_voters_cannot_vote() {
    let rules = RulesBuilder::new()
        .with_threshold(Decimal::percent(51))
        .build();

    let mut suite = SuiteBuilder::new()
        .with_member("alice", 1)
        .with_member("bob", 2)
        .with_rules(rules)
        .build();

    let proposal_creation_height = suite.app.block_info().height;

    // Create proposal with 1 voting power
    let response = suite.propose("alice", "proposal", "proposal").unwrap();
    let proposal_id: u64 = get_proposal_id(&response).unwrap();

    // A random tries to vote
    let err = suite.vote("some guy", proposal_id, Vote::Yes).unwrap_err();
    assert_eq!(
        ContractError::Std(StdError::GenericErr {
            msg: format!(
                "Unauthorized: wasn't member of a group at block height: {}",
                proposal_creation_height
            )
        }),
        err.downcast().unwrap()
    );
}

#[test]
fn members_with_no_voting_power_cannot_vote() {
    let rules = RulesBuilder::new()
        .with_threshold(Decimal::percent(51))
        .build();

    let mut suite = SuiteBuilder::new()
        .with_member("alice", 1)
        .with_member("bob", 2)
        .with_member("carol", 0)
        .with_rules(rules)
        .build();

    let proposal_creation_height = suite.app.block_info().height;

    // Create proposal with 1 voting power
    let response = suite.propose("alice", "proposal", "proposal").unwrap();
    let proposal_id: u64 = get_proposal_id(&response).unwrap();

    // A random tries to vote
    let err = suite.vote("carol", proposal_id, Vote::Yes).unwrap_err();
    assert_eq!(
        ContractError::Std(StdError::GenericErr {
            msg: format!(
                "Unauthorized: member didn't have voting power at block height: {}",
                proposal_creation_height
            )
        }),
        err.downcast().unwrap()
    );
}

#[test]
fn veto_counts_as_no() {
    let rules = RulesBuilder::new()
        .with_threshold(Decimal::percent(50))
        .with_quorum(Decimal::percent(10))
        .build();

    let mut suite = SuiteBuilder::new()
        .with_member("alice", 1)
        .with_member("bob", 2)
        .with_member("carol", 3)
        .with_rules(rules.clone())
        .build();

    // Create proposal with 1 voting power
    let response = suite.propose("alice", "proposal", "proposal").unwrap();
    let proposal_id: u64 = get_proposal_id(&response).unwrap();

    // Bob votes VETO - it's 33% yes votes
    suite.vote("bob", proposal_id, Vote::Veto).unwrap();

    let prop = suite.query_proposal(proposal_id).unwrap();
    assert_eq!(prop.votes.total(), 3);
    assert_eq!(prop.status, Status::Open);

    // Proposal is rejected once expired
    suite.app.advance_seconds(rules.voting_period_secs());
    let prop = suite.query_proposal(proposal_id).unwrap();
    assert_eq!(prop.votes.total(), 3);
    assert_eq!(prop.status, Status::Rejected);
}

#[test]
fn proposal_rejected_when_quorum_not_reached() {
    let rules = RulesBuilder::new()
        .with_threshold(Decimal::percent(51))
        .with_quorum(Decimal::percent(40))
        .with_allow_early(false)
        .build();

    let mut suite = SuiteBuilder::new()
        .with_member("alice", 1)
        .with_member("bob", 2)
        .with_member("carol", 3)
        .with_member("dave", 4)
        .with_rules(rules.clone())
        .build();

    // Create proposal with 2 voting power
    let response = suite.propose("bob", "proposal", "proposal").unwrap();
    let proposal_id: u64 = get_proposal_id(&response).unwrap();

    // Alice votes no. Quorum isn't reached because 3/10 voting power has been used.
    suite.vote("alice", proposal_id, Vote::No).unwrap();

    suite.app.advance_seconds(rules.voting_period_secs());
    let prop = suite.query_proposal(proposal_id).unwrap();
    assert_eq!(prop.votes.total(), 3);
    assert_eq!(prop.status, Status::Rejected);
}

#[test]
fn abstaining_can_help_reach_quorum() {
    let rules = RulesBuilder::new()
        .with_threshold(Decimal::percent(51))
        .with_quorum(Decimal::percent(40))
        .with_allow_early(false)
        .build();

    let mut suite = SuiteBuilder::new()
        .with_member("alice", 1)
        .with_member("bob", 2)
        .with_member("carol", 3)
        .with_member("dave", 4)
        .with_rules(rules.clone())
        .build();

    // Create proposal with 2 voting power
    let response = suite.propose("bob", "proposal", "proposal").unwrap();
    let proposal_id: u64 = get_proposal_id(&response).unwrap();

    // Alice votes no. Quorum isn't reached because 3/10 voting power has been used.
    suite.vote("alice", proposal_id, Vote::No).unwrap();

    // Carol abstains. Quorum is reached because 6/10 voting power has been used.
    suite.vote("carol", proposal_id, Vote::Abstain).unwrap();

    suite.app.advance_seconds(rules.voting_period_secs());
    let prop = suite.query_proposal(proposal_id).unwrap();
    assert_eq!(prop.votes.total(), 6);
    assert_eq!(prop.status, Status::Passed);
}

#[test]
fn abstaining_does_not_count_as_yes() {
    let rules = RulesBuilder::new()
        .with_threshold(Decimal::percent(51))
        .with_quorum(Decimal::percent(40))
        .with_allow_early(false)
        .build();

    let mut suite = SuiteBuilder::new()
        .with_member("alice", 1)
        .with_member("bob", 2)
        .with_member("carol", 3)
        .with_member("dave", 4)
        .with_rules(rules.clone())
        .build();

    // Create proposal with 2 voting power
    let response = suite.propose("bob", "proposal", "proposal").unwrap();
    let proposal_id: u64 = get_proposal_id(&response).unwrap();

    // Carol votes no.
    suite.vote("carol", proposal_id, Vote::No).unwrap();

    // Dave abstains. We have 2 yes, 3 no, 4 abstained. Proposal should be rejected.
    suite.vote("dave", proposal_id, Vote::Abstain).unwrap();

    suite.app.advance_seconds(rules.voting_period_secs());
    let prop = suite.query_proposal(proposal_id).unwrap();
    assert_eq!(prop.votes.total(), 9);
    assert_eq!(prop.status, Status::Rejected);
}

#[test]
fn proposal_can_be_rejected_after_voting_period() {
    let rules = RulesBuilder::new()
        .with_threshold(Decimal::percent(50))
        .with_quorum(Decimal::percent(10))
        .build();

    let mut suite = SuiteBuilder::new()
        .with_member("alice", 1)
        .with_member("bob", 2)
        .with_member("carol", 3)
        .with_rules(rules.clone())
        .build();

    // Create proposal with 1 voting power
    let response = suite.propose("alice", "proposal", "proposal").unwrap();
    let proposal_id: u64 = get_proposal_id(&response).unwrap();

    // Bob votes NO - it's 33% yes votes
    suite.vote("bob", proposal_id, Vote::No).unwrap();

    let prop = suite.query_proposal(proposal_id).unwrap();
    assert_eq!(prop.votes.total(), 3);
    assert_eq!(prop.status, Status::Open);

    // Proposal is rejected once expired
    suite.app.advance_seconds(rules.voting_period_secs());
    let prop = suite.query_proposal(proposal_id).unwrap();
    assert_eq!(prop.votes.total(), 3);
    assert_eq!(prop.status, Status::Rejected);
}

#[test]
fn passing_a_proposal_after_voting_period_works() {
    let rules = RulesBuilder::new()
        .with_threshold(Decimal::percent(50))
        .with_quorum(Decimal::percent(20))
        .build();

    let mut suite = SuiteBuilder::new()
        .with_member("alice", 4)
        .with_member("bob", 6)
        .with_rules(rules.clone())
        .build();

    // Create proposal with 4 voting power
    let response = suite.propose("alice", "proposal", "proposal").unwrap();
    let proposal_id: u64 = get_proposal_id(&response).unwrap();

    // Proposal doesn't pass early because Bob could still reject it
    let prop = suite.query_proposal(proposal_id).unwrap();
    assert_eq!(prop.votes.total(), 4);
    assert_eq!(prop.status, Status::Open);

    // Proposal is passed once voting period is over
    suite.app.advance_seconds(rules.voting_period_secs());
    let prop = suite.query_proposal(proposal_id).unwrap();
    assert_eq!(prop.votes.total(), 4);
    assert_eq!(prop.status, Status::Passed);
}

#[test]
fn expired_proposals_cannot_be_voted_on() {
    let rules = RulesBuilder::new()
        .with_threshold(Decimal::percent(51))
        .with_quorum(Decimal::percent(35))
        .build();

    let mut suite = SuiteBuilder::new()
        .with_member("alice", 1)
        .with_member("bob", 2)
        .with_rules(rules.clone())
        .build();

    // Create proposal with 1 voting power
    let response = suite.propose("alice", "cool proposal", "proposal").unwrap();
    let proposal_id: u64 = get_proposal_id(&response).unwrap();

    // Move time forward so proposal expires
    suite.app.advance_seconds(rules.voting_period_secs());

    // Bob can't vote on the expired proposal
    let err = suite.vote("bob", proposal_id, Vote::Yes).unwrap_err();
    // proposal that is open and expired is rejected
    assert_eq!(ContractError::Expired {}, err.downcast().unwrap());
}

#[test]
fn proposal_pass_on_expiration() {
    let rules = RulesBuilder::new()
        .with_threshold(Decimal::percent(51))
        .with_quorum(Decimal::percent(35))
        .build();

    let mut suite = SuiteBuilder::new()
        .with_member("alice", 1)
        .with_member("bob", 2)
        .with_rules(rules.clone())
        .build();

    // Create proposal with 1 voting power
    let response = suite.propose("alice", "cool proposal", "proposal").unwrap();
    let proposal_id: u64 = get_proposal_id(&response).unwrap();

    // Bob can vote on the proposal
    suite.vote("bob", proposal_id, Vote::Yes).unwrap();

    // Move time forward so voting ends
    suite.app.advance_seconds(rules.voting_period_secs());

    // Verify proposal is now passed
    let prop = suite.query_proposal(proposal_id).unwrap();
    assert_eq!(prop.status, Status::Passed);

    // Alice can't vote on the proposal
    let err = suite.vote("alice", proposal_id, Vote::Yes).unwrap_err();

    // cannot vote on proposal as it has expired
    assert_eq!(ContractError::Expired {}, err.downcast().unwrap());

    // But she can execute the proposal
    suite.execute_proposal("alice", proposal_id).unwrap();

    // Verify proposal is now 'executed'
    let prop = suite.query_proposal(proposal_id).unwrap();
    assert_eq!(prop.status, Status::Executed);

    // Closing should NOT be possible
    let err = suite.close("bob", proposal_id).unwrap_err();
    assert_eq!(ContractError::WrongCloseStatus {}, err.downcast().unwrap());
}