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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{Addr, BlockInfo, StdError, StdResult, Uint128};
use cw_utils::Expiration;
use dao_voting::{
    multiple_choice::{
        CheckedMultipleChoiceOption, MultipleChoiceOptionType, MultipleChoiceVotes, VotingStrategy,
    },
    status::Status,
    voting::does_vote_count_pass,
};

use crate::query::ProposalResponse;

#[cw_serde]
pub struct MultipleChoiceProposal {
    pub title: String,
    pub description: String,
    /// The address that created this proposal.
    pub proposer: Addr,
    /// The block height at which this proposal was created. Voting
    /// power queries should query for voting power at this block
    /// height.
    pub start_height: u64,
    /// The minimum amount of time this proposal must remain open for
    /// voting. The proposal may not pass unless this is expired or
    /// None.
    pub min_voting_period: Option<Expiration>,
    /// The the time at which this proposal will expire and close for
    /// additional votes.
    pub expiration: Expiration,
    /// The options to be chosen from in the vote.
    pub choices: Vec<CheckedMultipleChoiceOption>,
    /// Prosal status (Open, rejected, executed, execution failed, closed, passed)
    pub status: Status,
    /// Voting settings (threshold, quorum, etc.)
    pub voting_strategy: VotingStrategy,
    /// The total power when the proposal started (used to calculate percentages)
    pub total_power: Uint128,
    /// The vote tally.
    pub votes: MultipleChoiceVotes,
    /// Whether DAO members are allowed to change their votes.
    /// When disabled, proposals can be executed as soon as they pass.
    /// When enabled, proposals can only be executed after the voting
    /// perid has ended and the proposal passed.
    pub allow_revoting: bool,
}

pub enum VoteResult {
    SingleWinner(CheckedMultipleChoiceOption),
    Tie,
}

impl MultipleChoiceProposal {
    /// Consumes the proposal and returns a version which may be used
    /// in a query response. The difference being that proposal
    /// statuses are only updated on vote, execute, and close
    /// events. It is possible though that since a vote has occured
    /// the proposal expiring has changed its status. This method
    /// recomputes the status so that queries get accurate
    /// information.
    pub fn into_response(mut self, block: &BlockInfo, id: u64) -> StdResult<ProposalResponse> {
        self.update_status(block)?;
        Ok(ProposalResponse { id, proposal: self })
    }

    /// Gets the current status of the proposal.
    pub fn current_status(&self, block: &BlockInfo) -> StdResult<Status> {
        if self.status == Status::Open && self.is_passed(block)? {
            Ok(Status::Passed)
        } else if self.status == Status::Open
            && (self.expiration.is_expired(block) || self.is_rejected(block)?)
        {
            Ok(Status::Rejected)
        } else {
            Ok(self.status)
        }
    }

    /// Sets a proposals status to its current status.
    pub fn update_status(&mut self, block: &BlockInfo) -> StdResult<()> {
        let new_status = self.current_status(block)?;
        self.status = new_status;
        Ok(())
    }

    /// Returns true iff this proposal is sure to pass (even before
    /// expiration if no future sequence of possible votes can cause
    /// it to fail). Passing in the case of multiple choice proposals
    /// means that quorum has been met,
    /// one of the options that is not "None of the above"
    /// has won the most votes, and there is no tie.
    pub fn is_passed(&self, block: &BlockInfo) -> StdResult<bool> {
        // If re-voting is allowed nothing is known until the proposal
        // has expired.
        if self.allow_revoting && !self.expiration.is_expired(block) {
            return Ok(false);
        }
        // If the min voting period is set and not expired the
        // proposal can not yet be passed. This gives DAO members some
        // time to remove liquidity / scheme on a recovery plan if a
        // single actor accumulates enough tokens to unilaterally pass
        // proposals.
        if let Some(min) = self.min_voting_period {
            if !min.is_expired(block) {
                return Ok(false);
            }
        }

        // Proposal can only pass if quorum has been met.
        if does_vote_count_pass(
            self.votes.total(),
            self.total_power,
            self.voting_strategy.get_quorum(),
        ) {
            let vote_result = self.calculate_vote_result()?;
            match vote_result {
                // Proposal is not passed if there is a tie.
                VoteResult::Tie => return Ok(false),
                VoteResult::SingleWinner(winning_choice) => {
                    // Proposal is not passed if winning choice is None.
                    if winning_choice.option_type != MultipleChoiceOptionType::None {
                        // If proposal is expired, quorum has been reached, and winning choice is neither tied nor None, then proposal is passed.
                        if self.expiration.is_expired(block) {
                            return Ok(true);
                        } else {
                            // If the proposal is not expired but the leading choice cannot
                            // possibly be outwon by any other choices, the proposal has passed.
                            return self.is_choice_unbeatable(&winning_choice);
                        }
                    }
                }
            }
        }
        Ok(false)
    }

    pub fn is_rejected(&self, block: &BlockInfo) -> StdResult<bool> {
        // If re-voting is allowed and the proposal is not expired no
        // information is known.
        if self.allow_revoting && !self.expiration.is_expired(block) {
            return Ok(false);
        }

        let vote_result = self.calculate_vote_result()?;
        match vote_result {
            // Proposal is rejected if there is a tie, and either the proposal is expired or
            // there is no voting power left.
            VoteResult::Tie => {
                let rejected =
                    self.expiration.is_expired(block) || self.total_power == self.votes.total();
                Ok(rejected)
            }
            VoteResult::SingleWinner(winning_choice) => {
                match (
                    does_vote_count_pass(
                        self.votes.total(),
                        self.total_power,
                        self.voting_strategy.get_quorum(),
                    ),
                    self.expiration.is_expired(block),
                ) {
                    // Quorum is met and proposal is expired.
                    (true, true) => {
                        // Proposal is rejected if "None" is the winning option.
                        if winning_choice.option_type == MultipleChoiceOptionType::None {
                            return Ok(true);
                        }
                        Ok(false)
                    }
                    // Proposal is not expired, quorum is either is met or unmet.
                    (true, false) | (false, false) => {
                        // If the proposal is not expired and the leading choice is None and it cannot
                        // possibly be outwon by any other choices, the proposal is rejected.
                        if winning_choice.option_type == MultipleChoiceOptionType::None {
                            return self.is_choice_unbeatable(&winning_choice);
                        }
                        Ok(false)
                    }
                    // Quorum is not met and proposal is expired.
                    (false, true) => Ok(true),
                }
            }
        }
    }

    /// Find the option with the highest vote weight, and note if there is a tie.
    pub fn calculate_vote_result(&self) -> StdResult<VoteResult> {
        match self.voting_strategy {
            VotingStrategy::SingleChoice { quorum: _ } => {
                // We expect to have at least 3 vote weights
                if let Some(max_weight) = self.votes.vote_weights.iter().max_by(|&a, &b| a.cmp(b)) {
                    let top_choices: Vec<(usize, &Uint128)> = self
                        .votes
                        .vote_weights
                        .iter()
                        .enumerate()
                        .filter(|x| x.1 == max_weight)
                        .collect();

                    // If more than one choice has the highest number of votes, we have a tie.
                    if top_choices.len() > 1 {
                        return Ok(VoteResult::Tie);
                    }

                    match top_choices.first() {
                        Some(winning_choice) => {
                            return Ok(VoteResult::SingleWinner(
                                self.choices[winning_choice.0].clone(),
                            ));
                        }
                        None => {
                            return Err(StdError::generic_err("no votes found"));
                        }
                    }
                }
                Err(StdError::not_found("max vote weight"))
            }
        }
    }

    /// Ensure that with the remaining vote power, the choice with the second highest votes
    /// cannot overtake the first choice.
    fn is_choice_unbeatable(
        &self,
        winning_choice: &CheckedMultipleChoiceOption,
    ) -> StdResult<bool> {
        let winning_choice_power = self.votes.vote_weights[winning_choice.index as usize];
        if let Some(second_choice_power) = self
            .votes
            .vote_weights
            .iter()
            .filter(|&x| x < &winning_choice_power)
            .max_by(|&a, &b| a.cmp(b))
        {
            // Check if the remaining vote power can be used to overtake the current winning choice.
            let remaining_vote_power = self.total_power - self.votes.total();
            match winning_choice.option_type {
                MultipleChoiceOptionType::Standard => {
                    if winning_choice_power > *second_choice_power + remaining_vote_power {
                        return Ok(true);
                    }
                }
                MultipleChoiceOptionType::None => {
                    // If the winning choice is None, and we can at most achieve a tie,
                    // this choice is unbeatable because a tie will also fail the proposal. This is why we check for '>=' in this case
                    // rather than '>'.
                    if winning_choice_power >= *second_choice_power + remaining_vote_power {
                        return Ok(true);
                    }
                }
            }
        } else {
            return Err(StdError::not_found("second highest vote weight"));
        }
        Ok(false)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use cosmwasm_std::testing::mock_env;
    use dao_voting::multiple_choice::{MultipleChoiceOption, MultipleChoiceOptions};

    fn create_proposal(
        block: &BlockInfo,
        voting_strategy: VotingStrategy,
        votes: MultipleChoiceVotes,
        total_power: Uint128,
        is_expired: bool,
        allow_revoting: bool,
    ) -> MultipleChoiceProposal {
        // The last option that gets added in into_checked is always the none of the above option
        let options = vec![
            MultipleChoiceOption {
                description: "multiple choice option 1".to_string(),
                msgs: vec![],
                title: "title".to_string(),
            },
            MultipleChoiceOption {
                description: "multiple choice option 2".to_string(),
                msgs: vec![],
                title: "title".to_string(),
            },
        ];

        let expiration: Expiration = if is_expired {
            Expiration::AtHeight(block.height - 5)
        } else {
            Expiration::AtHeight(block.height + 5)
        };

        let mc_options = MultipleChoiceOptions { options };
        MultipleChoiceProposal {
            title: "A simple text proposal".to_string(),
            description: "A simple text proposal".to_string(),
            proposer: Addr::unchecked("CREATOR"),
            start_height: mock_env().block.height,
            expiration,
            // The last option that gets added in into_checked is always the none of the above option
            choices: mc_options.into_checked().unwrap().options,
            status: Status::Open,
            voting_strategy,
            total_power,
            votes,
            allow_revoting,
            min_voting_period: None,
        }
    }

    #[test]
    fn test_majority_quorum() {
        let env = mock_env();
        let voting_strategy = VotingStrategy::SingleChoice {
            quorum: dao_voting::threshold::PercentageThreshold::Majority {},
        };

        let votes = MultipleChoiceVotes {
            vote_weights: vec![Uint128::new(1), Uint128::new(0), Uint128::new(0)],
        };

        let prop = create_proposal(
            &env.block,
            voting_strategy.clone(),
            votes,
            Uint128::new(1),
            false,
            false,
        );

        // Quorum was met and all votes were cast, should be passed.
        assert!(prop.is_passed(&env.block).unwrap());
        assert!(!prop.is_rejected(&env.block).unwrap());

        let votes = MultipleChoiceVotes {
            vote_weights: vec![Uint128::new(0), Uint128::new(0), Uint128::new(1)],
        };
        let prop = create_proposal(
            &env.block,
            voting_strategy.clone(),
            votes,
            Uint128::new(1),
            false,
            false,
        );

        // Quorum was met but none of the above won, should be rejected.
        assert!(!prop.is_passed(&env.block).unwrap());
        assert!(prop.is_rejected(&env.block).unwrap());

        let votes = MultipleChoiceVotes {
            vote_weights: vec![Uint128::new(1), Uint128::new(0), Uint128::new(0)],
        };
        let prop = create_proposal(
            &env.block,
            voting_strategy.clone(),
            votes,
            Uint128::new(100),
            false,
            false,
        );

        // Quorum was not met and is not expired, should be open.
        assert!(!prop.is_passed(&env.block).unwrap());
        assert!(!prop.is_rejected(&env.block).unwrap());

        let votes = MultipleChoiceVotes {
            vote_weights: vec![Uint128::new(1), Uint128::new(0), Uint128::new(0)],
        };
        let prop = create_proposal(
            &env.block,
            voting_strategy.clone(),
            votes,
            Uint128::new(100),
            true,
            false,
        );

        // Quorum was not met and it is expired, should be rejected.
        assert!(!prop.is_passed(&env.block).unwrap());
        assert!(prop.is_rejected(&env.block).unwrap());

        let votes = MultipleChoiceVotes {
            vote_weights: vec![Uint128::new(50), Uint128::new(50), Uint128::new(0)],
        };
        let prop = create_proposal(
            &env.block,
            voting_strategy.clone(),
            votes,
            Uint128::new(100),
            true,
            false,
        );

        // Quorum was met but it is a tie and expired, should be rejected.
        assert!(!prop.is_passed(&env.block).unwrap());
        assert!(prop.is_rejected(&env.block).unwrap());

        let votes = MultipleChoiceVotes {
            vote_weights: vec![Uint128::new(50), Uint128::new(50), Uint128::new(0)],
        };
        let prop = create_proposal(
            &env.block,
            voting_strategy,
            votes,
            Uint128::new(150),
            false,
            false,
        );

        // Quorum was met but it is a tie but not expired and still voting power remains, should be open.
        assert!(!prop.is_passed(&env.block).unwrap());
        assert!(!prop.is_rejected(&env.block).unwrap());
    }

    #[test]
    fn test_percentage_quorum() {
        let env = mock_env();
        let voting_strategy = VotingStrategy::SingleChoice {
            quorum: dao_voting::threshold::PercentageThreshold::Percent(
                cosmwasm_std::Decimal::percent(10),
            ),
        };

        let votes = MultipleChoiceVotes {
            vote_weights: vec![Uint128::new(1), Uint128::new(0), Uint128::new(0)],
        };

        let prop = create_proposal(
            &env.block,
            voting_strategy.clone(),
            votes,
            Uint128::new(1),
            false,
            false,
        );

        // Quorum was met and all votes were cast, should be passed.
        assert!(prop.is_passed(&env.block).unwrap());
        assert!(!prop.is_rejected(&env.block).unwrap());

        let votes = MultipleChoiceVotes {
            vote_weights: vec![Uint128::new(0), Uint128::new(0), Uint128::new(1)],
        };
        let prop = create_proposal(
            &env.block,
            voting_strategy.clone(),
            votes,
            Uint128::new(1),
            false,
            false,
        );

        // Quorum was met but none of the above won, should be rejected.
        assert!(!prop.is_passed(&env.block).unwrap());
        assert!(prop.is_rejected(&env.block).unwrap());

        let votes = MultipleChoiceVotes {
            vote_weights: vec![Uint128::new(1), Uint128::new(0), Uint128::new(0)],
        };
        let prop = create_proposal(
            &env.block,
            voting_strategy.clone(),
            votes,
            Uint128::new(100),
            false,
            false,
        );

        // Quorum was not met and is not expired, should be open.
        assert!(!prop.is_passed(&env.block).unwrap());
        assert!(!prop.is_rejected(&env.block).unwrap());

        let votes = MultipleChoiceVotes {
            vote_weights: vec![Uint128::new(1), Uint128::new(0), Uint128::new(0)],
        };
        let prop = create_proposal(
            &env.block,
            voting_strategy.clone(),
            votes,
            Uint128::new(101),
            true,
            false,
        );

        // Quorum was not met and it is expired, should be rejected.
        assert!(!prop.is_passed(&env.block).unwrap());
        assert!(prop.is_rejected(&env.block).unwrap());

        let votes = MultipleChoiceVotes {
            vote_weights: vec![Uint128::new(50), Uint128::new(50), Uint128::new(0)],
        };
        let prop = create_proposal(
            &env.block,
            voting_strategy.clone(),
            votes,
            Uint128::new(10000),
            true,
            false,
        );

        // Quorum was met but it is a tie and expired, should be rejected.
        assert!(!prop.is_passed(&env.block).unwrap());
        assert!(prop.is_rejected(&env.block).unwrap());

        let votes = MultipleChoiceVotes {
            vote_weights: vec![Uint128::new(50), Uint128::new(50), Uint128::new(0)],
        };
        let prop = create_proposal(
            &env.block,
            voting_strategy,
            votes,
            Uint128::new(150),
            false,
            false,
        );

        // Quorum was met but it is a tie but not expired and still voting power remains, should be open.
        assert!(!prop.is_passed(&env.block).unwrap());
        assert!(!prop.is_rejected(&env.block).unwrap());
    }

    #[test]
    fn test_unbeatable_none_option() {
        let env = mock_env();
        let voting_strategy = VotingStrategy::SingleChoice {
            quorum: dao_voting::threshold::PercentageThreshold::Percent(
                cosmwasm_std::Decimal::percent(10),
            ),
        };
        let votes = MultipleChoiceVotes {
            vote_weights: vec![Uint128::new(0), Uint128::new(50), Uint128::new(500)],
        };
        let prop = create_proposal(
            &env.block,
            voting_strategy,
            votes,
            Uint128::new(1000),
            false,
            false,
        );

        // Quorum was met but none of the above is winning, but it also can't be beat (only a tie at best), should be rejected
        assert!(!prop.is_passed(&env.block).unwrap());
        assert!(prop.is_rejected(&env.block).unwrap());
    }

    #[test]
    fn test_quorum_rounding() {
        let env = mock_env();
        let voting_strategy = VotingStrategy::SingleChoice {
            quorum: dao_voting::threshold::PercentageThreshold::Percent(
                cosmwasm_std::Decimal::percent(10),
            ),
        };
        let votes = MultipleChoiceVotes {
            vote_weights: vec![Uint128::new(10), Uint128::new(0), Uint128::new(0)],
        };
        let prop = create_proposal(
            &env.block,
            voting_strategy,
            votes,
            Uint128::new(100),
            true,
            false,
        );

        // Quorum was met and proposal expired, should pass
        assert!(prop.is_passed(&env.block).unwrap());
        assert!(!prop.is_rejected(&env.block).unwrap());

        // High Precision rounding
        let voting_strategy = VotingStrategy::SingleChoice {
            quorum: dao_voting::threshold::PercentageThreshold::Percent(
                cosmwasm_std::Decimal::percent(100),
            ),
        };

        let votes = MultipleChoiceVotes {
            vote_weights: vec![Uint128::new(999999), Uint128::new(0), Uint128::new(0)],
        };
        let prop = create_proposal(
            &env.block,
            voting_strategy,
            votes,
            Uint128::new(1000000),
            true,
            false,
        );

        // Quorum was not met and expired, should reject
        assert!(!prop.is_passed(&env.block).unwrap());
        assert!(prop.is_rejected(&env.block).unwrap());

        // High Precision rounding
        let voting_strategy = VotingStrategy::SingleChoice {
            quorum: dao_voting::threshold::PercentageThreshold::Percent(
                cosmwasm_std::Decimal::percent(99),
            ),
        };

        let votes = MultipleChoiceVotes {
            vote_weights: vec![Uint128::new(9888889), Uint128::new(0), Uint128::new(0)],
        };
        let prop = create_proposal(
            &env.block,
            voting_strategy,
            votes,
            Uint128::new(10000000),
            true,
            false,
        );

        // Quorum was not met and expired, should reject
        assert!(!prop.is_passed(&env.block).unwrap());
        assert!(prop.is_rejected(&env.block).unwrap());
    }

    #[test]
    fn test_tricky_pass() {
        let env = mock_env();
        let voting_strategy = VotingStrategy::SingleChoice {
            quorum: dao_voting::threshold::PercentageThreshold::Percent(
                cosmwasm_std::Decimal::from_ratio(7u32, 13u32),
            ),
        };
        let votes = MultipleChoiceVotes {
            vote_weights: vec![Uint128::new(7), Uint128::new(0), Uint128::new(6)],
        };
        let prop = create_proposal(
            &env.block,
            voting_strategy.clone(),
            votes.clone(),
            Uint128::new(13),
            true,
            false,
        );

        // Should pass if expired
        assert!(prop.is_passed(&env.block).unwrap());
        assert!(!prop.is_rejected(&env.block).unwrap());

        let prop = create_proposal(
            &env.block,
            voting_strategy,
            votes,
            Uint128::new(13),
            false,
            false,
        );

        // Should pass if not expired
        assert!(prop.is_passed(&env.block).unwrap());
        assert!(!prop.is_rejected(&env.block).unwrap());
    }

    #[test]
    fn test_tricky_pass_majority() {
        let env = mock_env();
        let voting_strategy = VotingStrategy::SingleChoice {
            quorum: dao_voting::threshold::PercentageThreshold::Majority {},
        };

        let votes = MultipleChoiceVotes {
            vote_weights: vec![Uint128::new(7), Uint128::new(0), Uint128::new(0)],
        };
        let prop = create_proposal(
            &env.block,
            voting_strategy.clone(),
            votes.clone(),
            Uint128::new(13),
            true,
            false,
        );

        // Should pass if majority voted
        assert!(prop.is_passed(&env.block).unwrap());
        assert!(!prop.is_rejected(&env.block).unwrap());

        let prop = create_proposal(
            &env.block,
            voting_strategy,
            votes,
            Uint128::new(14),
            true,
            false,
        );

        // Shouldn't pass if only half voted
        assert!(!prop.is_passed(&env.block).unwrap());
        assert!(prop.is_rejected(&env.block).unwrap());
    }

    #[test]
    fn test_majority_revote_pass() {
        // Revoting being allowed means that proposals may not be
        // passed or rejected before they expire.
        let env = mock_env();
        let voting_strategy = VotingStrategy::SingleChoice {
            quorum: dao_voting::threshold::PercentageThreshold::Majority {},
        };
        let votes = MultipleChoiceVotes {
            vote_weights: vec![Uint128::new(6), Uint128::new(0), Uint128::new(0)],
        };

        let prop = create_proposal(
            &env.block,
            voting_strategy.clone(),
            votes.clone(),
            Uint128::new(10),
            false,
            true,
        );
        // Quorum reached, but proposal is still active => no pass
        assert!(!prop.is_passed(&env.block).unwrap());

        let prop = create_proposal(
            &env.block,
            voting_strategy,
            votes,
            Uint128::new(10),
            true,
            true,
        );
        // Quorum reached & proposal has expired => pass
        assert!(prop.is_passed(&env.block).unwrap());
    }

    #[test]
    fn test_majority_revote_rejection() {
        // Revoting being allowed means that proposals may not be
        // passed or rejected before they expire.
        let env = mock_env();
        let voting_strategy = VotingStrategy::SingleChoice {
            quorum: dao_voting::threshold::PercentageThreshold::Majority {},
        };
        let votes = MultipleChoiceVotes {
            vote_weights: vec![Uint128::new(5), Uint128::new(5), Uint128::new(0)],
        };

        let prop = create_proposal(
            &env.block,
            voting_strategy.clone(),
            votes.clone(),
            Uint128::new(10),
            false,
            true,
        );
        // Everyone voted and proposal is in a tie...
        assert_eq!(prop.total_power, prop.votes.total());
        assert_eq!(prop.votes.vote_weights[0], prop.votes.vote_weights[1]);
        // ... but proposal is still active => no rejection
        assert!(!prop.is_rejected(&env.block).unwrap());

        let prop = create_proposal(
            &env.block,
            voting_strategy,
            votes,
            Uint128::new(10),
            true,
            true,
        );
        // Proposal has expired and ended in a tie => rejection
        assert_eq!(prop.votes.vote_weights[0], prop.votes.vote_weights[1]);
        assert!(prop.is_rejected(&env.block).unwrap());
    }

    #[test]
    fn test_percentage_revote_pass() {
        // Revoting being allowed means that proposals may not be
        // passed or rejected before they expire.
        let env = mock_env();
        let voting_strategy = VotingStrategy::SingleChoice {
            quorum: dao_voting::threshold::PercentageThreshold::Percent(
                cosmwasm_std::Decimal::percent(80),
            ),
        };

        let votes = MultipleChoiceVotes {
            vote_weights: vec![Uint128::new(81), Uint128::new(0), Uint128::new(0)],
        };

        let prop = create_proposal(
            &env.block,
            voting_strategy.clone(),
            votes.clone(),
            Uint128::new(100),
            false,
            true,
        );
        // Quorum reached, but proposal is still active => no pass
        assert!(!prop.is_passed(&env.block).unwrap());

        let prop = create_proposal(
            &env.block,
            voting_strategy,
            votes,
            Uint128::new(100),
            true,
            true,
        );
        // Quorum reached & proposal has expired => pass
        assert!(prop.is_passed(&env.block).unwrap());
    }

    #[test]
    fn test_percentage_revote_rejection() {
        // Revoting being allowed means that proposals may not be
        // passed or rejected before they expire.
        let env = mock_env();
        let voting_strategy = VotingStrategy::SingleChoice {
            quorum: dao_voting::threshold::PercentageThreshold::Percent(
                cosmwasm_std::Decimal::percent(80),
            ),
        };

        let votes = MultipleChoiceVotes {
            vote_weights: vec![Uint128::new(90), Uint128::new(0), Uint128::new(0)],
        };

        let prop = create_proposal(
            &env.block,
            voting_strategy.clone(),
            votes,
            Uint128::new(100),
            false,
            true,
        );
        // Quorum reached, but proposal is still active => no rejection
        assert!(!prop.is_rejected(&env.block).unwrap());

        let votes = MultipleChoiceVotes {
            vote_weights: vec![Uint128::new(50), Uint128::new(0), Uint128::new(0)],
        };

        let prop = create_proposal(
            &env.block,
            voting_strategy,
            votes,
            Uint128::new(100),
            true,
            true,
        );
        // No quorum reached & proposal has expired => rejection
        assert!(prop.is_rejected(&env.block).unwrap());
    }
}