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 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
use cosmwasm_std::{Addr, BlockInfo, CosmosMsg, Decimal, Empty, StdResult, Storage, Uint128};
use cw_utils::Expiration;
use dao_voting::{compare_vote_count, PercentageThreshold, Status, Threshold, VoteCmp, Votes};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::{
query::ProposalResponse,
state::{CheckedDepositInfo, PROPOSAL_COUNT},
};
#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
pub struct Proposal {
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 threshold at which this proposal will pass.
pub threshold: Threshold,
/// The total amount of voting power at the time of this
/// proposal's creation.
pub total_power: Uint128,
/// The messages that will be executed should this proposal pass.
pub msgs: Vec<CosmosMsg<Empty>>,
pub status: Status,
pub votes: Votes,
pub allow_revoting: bool,
/// Information about the deposit that was sent as part of this
/// proposal. None if no deposit.
pub deposit_info: Option<CheckedDepositInfo>,
}
pub fn advance_proposal_id(store: &mut dyn Storage) -> StdResult<u64> {
let id: u64 = PROPOSAL_COUNT.load(store)? + 1;
PROPOSAL_COUNT.save(store, &id)?;
Ok(id)
}
pub fn does_vote_count_pass(
yes_votes: Uint128,
options: Uint128,
percent: PercentageThreshold,
) -> bool {
// Don't pass proposals if all the votes are abstain.
if options.is_zero() {
return false;
}
match percent {
PercentageThreshold::Majority {} => yes_votes.full_mul(2u64) > options.into(),
PercentageThreshold::Percent(percent) => {
compare_vote_count(yes_votes, VoteCmp::Geq, options, percent)
}
}
}
pub fn does_vote_count_fail(
no_votes: Uint128,
options: Uint128,
percent: PercentageThreshold,
) -> bool {
// All abstain votes should result in a rejected proposal.
if options.is_zero() {
return true;
}
match percent {
PercentageThreshold::Majority {} => {
// Fails if no votes have >= half of all votes.
no_votes.full_mul(2u64) >= options.into()
}
PercentageThreshold::Percent(percent) => compare_vote_count(
no_votes,
VoteCmp::Greater,
options,
Decimal::one() - percent,
),
}
}
impl Proposal {
/// 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) -> ProposalResponse {
self.update_status(block);
ProposalResponse { id, proposal: self }
}
/// Gets the current status of the proposal.
pub fn current_status(&self, block: &BlockInfo) -> Status {
if self.status == Status::Open && self.is_passed(block) {
Status::Passed
} else if self.status == Status::Open
&& (self.expiration.is_expired(block) || self.is_rejected(block))
{
Status::Rejected
} else {
self.status
}
}
/// Sets a proposals status to its current status.
pub fn update_status(&mut self, block: &BlockInfo) {
self.status = self.current_status(block);
}
/// Returns true iff this proposal is sure to pass (even before
/// expiration if no future sequence of possible votes can cause
/// it to fail).
pub fn is_passed(&self, block: &BlockInfo) -> bool {
// If re-voting is allowed nothing is known until the proposal
// has expired.
if self.allow_revoting && !self.expiration.is_expired(block) {
return 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 false;
}
}
match self.threshold {
Threshold::AbsolutePercentage { percentage } => {
let options = self.total_power - self.votes.abstain;
does_vote_count_pass(self.votes.yes, options, percentage)
}
Threshold::ThresholdQuorum { threshold, quorum } => {
if !does_vote_count_pass(self.votes.total(), self.total_power, quorum) {
return false;
}
if self.expiration.is_expired(block) {
// If the quorum is met and the proposal is
// expired the number of votes needed to pass a
// proposal is compared to the number of votes on
// the proposal.
let options = self.votes.total() - self.votes.abstain;
does_vote_count_pass(self.votes.yes, options, threshold)
} else {
let options = self.total_power - self.votes.abstain;
does_vote_count_pass(self.votes.yes, options, threshold)
}
}
Threshold::AbsoluteCount { threshold } => self.votes.yes >= threshold,
}
}
/// As above for the passed check, used to check if a proposal is
/// already rejected.
pub fn is_rejected(&self, block: &BlockInfo) -> 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 false;
}
match self.threshold {
Threshold::AbsolutePercentage {
percentage: percentage_needed,
} => {
let options = self.total_power - self.votes.abstain;
// If there is a 100% passing threshold..
if percentage_needed == PercentageThreshold::Percent(Decimal::percent(100)) {
if options == Uint128::zero() {
// and there are no possible votes (zero
// voting power or all abstain), then this
// proposal has been rejected.
return true;
} else {
// and there are possible votes, then this is
// rejected if there is a single no vote.
//
// We need this check becuase otherwise when
// we invert the threshold (`Decimal::one() -
// threshold`) we get a 0% requirement for no
// votes. Zero no votes do indeed meet a 0%
// threshold.
return self.votes.no >= Uint128::new(1);
}
}
does_vote_count_fail(self.votes.no, options, percentage_needed)
}
Threshold::ThresholdQuorum { threshold, quorum } => {
match (
does_vote_count_pass(self.votes.total(), self.total_power, quorum),
self.expiration.is_expired(block),
) {
// Has met quorum and is expired.
(true, true) => {
// => consider only votes cast and see if no
// votes meet threshold.
let options = self.votes.total() - self.votes.abstain;
// If there is a 100% passing threshold..
if threshold == PercentageThreshold::Percent(Decimal::percent(100)) {
if options == Uint128::zero() {
// and there are no possible votes (zero
// voting power or all abstain), then this
// proposal has been rejected.
return true;
} else {
// and there are possible votes, then this is
// rejected if there is a single no vote.
//
// We need this check becuase
// otherwise when we invert the
// threshold (`Decimal::one() -
// threshold`) we get a 0% requirement
// for no votes. Zero no votes do
// indeed meet a 0% threshold.
return self.votes.no >= Uint128::new(1);
}
}
does_vote_count_fail(self.votes.no, options, threshold)
}
// Has met quorum and is not expired.
// | Hasn't met quorum and is not expired.
(true, false) | (false, false) => {
// => consider all possible votes and see if
// no votes meet threshold.
let options = self.total_power - self.votes.abstain;
// If there is a 100% passing threshold..
if threshold == PercentageThreshold::Percent(Decimal::percent(100)) {
if options == Uint128::zero() {
// and there are no possible votes (zero
// voting power or all abstain), then this
// proposal has been rejected.
return true;
} else {
// and there are possible votes, then this is
// rejected if there is a single no vote.
//
// We need this check becuase otherwise
// when we invert the threshold
// (`Decimal::one() - threshold`) we
// get a 0% requirement for no
// votes. Zero no votes do indeed meet
// a 0% threshold.
return self.votes.no >= Uint128::new(1);
}
}
does_vote_count_fail(self.votes.no, options, threshold)
}
// Hasn't met quorum requirement and voting has closed => rejected.
(false, true) => true,
}
}
Threshold::AbsoluteCount { threshold } => {
// If all the outstanding votes voting yes would not
// cause this proposal to pass then it is rejected.
let outstanding_votes = self.total_power - self.votes.total();
self.votes.yes + outstanding_votes < threshold
}
}
}
}
#[cfg(test)]
mod test {
use super::*;
use cosmwasm_std::{testing::mock_env, Decimal};
fn setup_prop(
threshold: Threshold,
votes: Votes,
total_power: Uint128,
is_expired: bool,
min_voting_period_elapsed: bool,
allow_revoting: bool,
) -> (Proposal, BlockInfo) {
let block = mock_env().block;
let expiration = match is_expired {
true => Expiration::AtHeight(block.height - 5),
false => Expiration::AtHeight(block.height + 100),
};
let min_voting_period = match min_voting_period_elapsed {
true => Expiration::AtHeight(block.height - 5),
false => Expiration::AtHeight(block.height + 5),
};
let prop = Proposal {
title: "Demo".to_string(),
description: "Info".to_string(),
proposer: Addr::unchecked("test"),
start_height: 100,
expiration,
min_voting_period: Some(min_voting_period),
allow_revoting,
msgs: vec![],
status: Status::Open,
threshold,
total_power,
votes,
deposit_info: None,
};
(prop, block)
}
fn check_is_passed(
threshold: Threshold,
votes: Votes,
total_power: Uint128,
is_expired: bool,
min_voting_period_elapsed: bool,
allow_revoting: bool,
) -> bool {
let (prop, block) = setup_prop(
threshold,
votes,
total_power,
is_expired,
min_voting_period_elapsed,
allow_revoting,
);
prop.is_passed(&block)
}
fn check_is_rejected(
threshold: Threshold,
votes: Votes,
total_power: Uint128,
is_expired: bool,
min_voting_period_elapsed: bool,
allow_revoting: bool,
) -> bool {
let (prop, block) = setup_prop(
threshold,
votes,
total_power,
is_expired,
min_voting_period_elapsed,
allow_revoting,
);
prop.is_rejected(&block)
}
#[test]
fn test_pass_majority_percentage() {
let threshold = Threshold::AbsolutePercentage {
percentage: PercentageThreshold::Majority {},
};
let votes = Votes {
yes: Uint128::new(7),
no: Uint128::new(4),
abstain: Uint128::new(2),
};
// 15 total votes. 7 yes and 2 abstain. Majority threshold. This
// should pass.
assert!(check_is_passed(
threshold.clone(),
votes.clone(),
Uint128::new(15),
false,
true,
false,
));
// Proposal being expired should not effect those results.
assert!(check_is_passed(
threshold.clone(),
votes.clone(),
Uint128::new(15),
true,
true,
false
));
// More votes == higher threshold => not passed.
assert!(!check_is_passed(
threshold,
votes,
Uint128::new(17),
false,
true,
false
));
}
#[test]
fn test_min_voting_period_no_early_pass() {
let threshold = Threshold::AbsolutePercentage {
percentage: PercentageThreshold::Majority {},
};
let votes = Votes {
yes: Uint128::new(7),
no: Uint128::new(4),
abstain: Uint128::new(2),
};
// Does not pass if min voting period is not expired.
assert!(!check_is_passed(
threshold.clone(),
votes.clone(),
Uint128::new(15),
false,
false,
false,
));
// Should not be rejected either.
assert!(!check_is_rejected(
threshold.clone(),
votes.clone(),
Uint128::new(15),
false,
false,
false,
));
// Min voting period being expired makes this pass.
assert!(check_is_passed(
threshold,
votes,
Uint128::new(15),
false,
true,
false
));
}
#[test]
fn test_min_voting_period_early_rejection() {
let threshold = Threshold::AbsolutePercentage {
percentage: PercentageThreshold::Majority {},
};
let votes = Votes {
yes: Uint128::new(4),
no: Uint128::new(7),
abstain: Uint128::new(2),
};
// Proposal has not passed.
assert!(!check_is_passed(
threshold.clone(),
votes.clone(),
Uint128::new(15),
false,
false,
false,
));
// Should be rejected despite the min voting period not being
// passed.
assert!(check_is_rejected(
threshold,
votes,
Uint128::new(15),
false,
false,
false,
));
}
#[test]
fn test_revoting_majority_no_pass() {
// Revoting being allowed means that proposals may not be
// passed or rejected before they expire.
let threshold = Threshold::AbsolutePercentage {
percentage: PercentageThreshold::Majority {},
};
let votes = Votes {
yes: Uint128::new(7),
no: Uint128::new(4),
abstain: Uint128::new(2),
};
// 15 total votes. 7 yes and 2 abstain. Majority threshold. This
// should pass but revoting is enabled.
assert!(!check_is_passed(
threshold.clone(),
votes.clone(),
Uint128::new(15),
false,
true,
true,
));
// Proposal being expired should cause the proposal to be
// passed as votes may no longer be cast.
assert!(check_is_passed(
threshold,
votes,
Uint128::new(15),
true,
true,
true
));
}
#[test]
fn test_revoting_majority_rejection() {
// Revoting being allowed means that proposals may not be
// passed or rejected before they expire.
let threshold = Threshold::AbsolutePercentage {
percentage: PercentageThreshold::Majority {},
};
let votes = Votes {
yes: Uint128::new(4),
no: Uint128::new(7),
abstain: Uint128::new(2),
};
// Not expired, revoting allowed => no rejection.
assert!(!check_is_rejected(
threshold.clone(),
votes.clone(),
Uint128::new(15),
false,
true,
true
));
// Expired, revoting allowed => rejection.
assert!(check_is_rejected(
threshold,
votes,
Uint128::new(15),
true,
true,
true
));
}
/// Simple checks for absolute count passing and failing
/// conditions.
#[test]
fn test_absolute_count_threshold() {
let threshold = Threshold::AbsoluteCount {
threshold: Uint128::new(10),
};
assert!(check_is_passed(
threshold.clone(),
Votes {
yes: Uint128::new(10),
no: Uint128::zero(),
abstain: Uint128::zero(),
},
Uint128::new(100),
false,
true,
false
));
assert!(check_is_rejected(
threshold.clone(),
Votes {
yes: Uint128::new(9),
no: Uint128::new(1),
abstain: Uint128::zero()
},
Uint128::new(10),
false,
true,
false
));
assert!(!check_is_rejected(
threshold.clone(),
Votes {
yes: Uint128::new(9),
no: Uint128::new(1),
abstain: Uint128::zero()
},
Uint128::new(11),
false,
true,
false
));
assert!(!check_is_passed(
threshold,
Votes {
yes: Uint128::new(9),
no: Uint128::new(1),
abstain: Uint128::zero()
},
Uint128::new(11),
false,
true,
false
));
}
/// Tests that revoting works as expected with an absolute count
/// style threshold.
#[test]
fn test_absolute_count_threshold_revoting() {
let threshold = Threshold::AbsoluteCount {
threshold: Uint128::new(10),
};
assert!(!check_is_passed(
threshold.clone(),
Votes {
yes: Uint128::new(10),
no: Uint128::zero(),
abstain: Uint128::zero(),
},
Uint128::new(100),
false,
true,
true
));
assert!(check_is_passed(
threshold.clone(),
Votes {
yes: Uint128::new(10),
no: Uint128::zero(),
abstain: Uint128::zero(),
},
Uint128::new(100),
true,
true,
true
));
assert!(!check_is_rejected(
threshold.clone(),
Votes {
yes: Uint128::new(9),
no: Uint128::new(1),
abstain: Uint128::zero()
},
Uint128::new(10),
false,
true,
true
));
assert!(check_is_rejected(
threshold,
Votes {
yes: Uint128::new(9),
no: Uint128::new(1),
abstain: Uint128::zero()
},
Uint128::new(10),
true,
true,
true
));
}
#[test]
fn test_tricky_pass() {
let threshold = Threshold::AbsolutePercentage {
percentage: PercentageThreshold::Percent(Decimal::from_ratio(7u32, 13u32)),
};
let votes = Votes {
yes: Uint128::new(7),
no: Uint128::new(6),
abstain: Uint128::zero(),
};
assert!(check_is_passed(
threshold,
votes,
Uint128::new(13),
false,
true,
false
))
}
#[test]
fn test_weird_failure_rounding() {
let threshold = Threshold::AbsolutePercentage {
percentage: PercentageThreshold::Percent(Decimal::from_ratio(6u32, 13u32)),
};
let votes = Votes {
yes: Uint128::new(6),
no: Uint128::new(7),
abstain: Uint128::zero(),
};
assert!(check_is_passed(
threshold.clone(),
votes.clone(),
Uint128::new(13),
false,
true,
false
));
assert!(!check_is_rejected(
threshold,
votes,
Uint128::new(13),
false,
true,
false
));
}
#[test]
fn test_tricky_pass_majority() {
let threshold = Threshold::AbsolutePercentage {
percentage: PercentageThreshold::Majority {},
};
let votes = Votes {
yes: Uint128::new(7),
no: Uint128::new(6),
abstain: Uint128::zero(),
};
assert!(check_is_passed(
threshold.clone(),
votes.clone(),
Uint128::new(13),
false,
true,
false
));
assert!(!check_is_passed(
threshold,
votes,
Uint128::new(14),
false,
true,
false
))
}
#[test]
fn test_reject_majority_percentage() {
let percent = Threshold::AbsolutePercentage {
percentage: PercentageThreshold::Majority {},
};
// 4 YES, 7 NO, 2 ABSTAIN
let votes = Votes {
yes: Uint128::new(4),
no: Uint128::new(7),
abstain: Uint128::new(2),
};
// 15 total voting power
// 7 / (15 - 2) > 50%
// Expiry does not matter
assert!(check_is_rejected(
percent.clone(),
votes.clone(),
Uint128::new(15),
false,
true,
false,
));
assert!(check_is_rejected(
percent.clone(),
votes.clone(),
Uint128::new(15),
true,
true,
false
));
// 17 total voting power
// 7 / (17 - 2) < 50%
assert!(!check_is_rejected(
percent.clone(),
votes.clone(),
Uint128::new(17),
false,
true,
false
));
assert!(!check_is_rejected(
percent.clone(),
votes.clone(),
Uint128::new(17),
true,
true,
false
));
// Rejected if total was lower
assert!(check_is_rejected(
percent.clone(),
votes.clone(),
Uint128::new(14),
false,
true,
false
));
assert!(check_is_rejected(
percent,
votes,
Uint128::new(14),
true,
true,
false
));
}
#[test]
fn proposal_passed_quorum() {
let quorum = Threshold::ThresholdQuorum {
threshold: PercentageThreshold::Percent(Decimal::percent(50)),
quorum: PercentageThreshold::Percent(Decimal::percent(40)),
};
// all non-yes votes are counted for quorum
let passing = Votes {
yes: Uint128::new(7),
no: Uint128::new(3),
abstain: Uint128::new(2),
};
// abstain votes are not counted for threshold => yes / (yes + no + veto)
let passes_ignoring_abstain = Votes {
yes: Uint128::new(6),
no: Uint128::new(6),
abstain: Uint128::new(5),
};
// fails any way you look at it
let failing = Votes {
yes: Uint128::new(6),
no: Uint128::new(7),
abstain: Uint128::new(2),
};
// first, expired (voting period over)
// over quorum (40% of 30 = 12), over threshold (7/12 > 50%)
assert!(check_is_passed(
quorum.clone(),
passing.clone(),
Uint128::new(30),
true,
true,
false,
));
// under quorum it is not passing (40% of 33 = 13.2 > 13)
assert!(!check_is_passed(
quorum.clone(),
passing.clone(),
Uint128::new(33),
true,
true,
false
));
// over quorum, threshold passes if we ignore abstain
// 17 total votes w/ abstain => 40% quorum of 40 total
// 6 yes / (6 yes + 4 no + 2 votes) => 50% threshold
assert!(check_is_passed(
quorum.clone(),
passes_ignoring_abstain.clone(),
Uint128::new(40),
true,
true,
false,
));
// over quorum, but under threshold fails also
assert!(!check_is_passed(
quorum.clone(),
failing,
Uint128::new(20),
true,
true,
false
));
// now, check with open voting period
// would pass if closed, but fail here, as remaining votes no -> fail
assert!(!check_is_passed(
quorum.clone(),
passing.clone(),
Uint128::new(30),
false,
true,
false
));
assert!(!check_is_passed(
quorum.clone(),
passes_ignoring_abstain.clone(),
Uint128::new(40),
false,
true,
false
));
// if we have threshold * total_weight as yes votes this must pass
assert!(check_is_passed(
quorum.clone(),
passing.clone(),
Uint128::new(14),
false,
true,
false
));
// all votes have been cast, some abstain
assert!(check_is_passed(
quorum.clone(),
passes_ignoring_abstain,
Uint128::new(17),
false,
true,
false
));
// 3 votes uncast, if they all vote no, we have 7 yes, 7 no+veto, 2 abstain (out of 16)
assert!(check_is_passed(
quorum,
passing,
Uint128::new(16),
false,
true,
false
));
}
#[test]
fn proposal_rejected_quorum() {
let quorum = Threshold::ThresholdQuorum {
threshold: PercentageThreshold::Majority {},
quorum: PercentageThreshold::Percent(Decimal::percent(40)),
};
// all non-yes votes are counted for quorum
let rejecting = Votes {
yes: Uint128::new(3),
no: Uint128::new(8),
abstain: Uint128::new(2),
};
// abstain votes are not counted for threshold => yes / (yes + no)
let rejected_ignoring_abstain = Votes {
yes: Uint128::new(4),
no: Uint128::new(8),
abstain: Uint128::new(5),
};
// fails any way you look at it
let failing = Votes {
yes: Uint128::new(5),
no: Uint128::new(8),
abstain: Uint128::new(2),
};
// first, expired (voting period over)
// over quorum (40% of 30 = 12), over threshold (7/11 > 50%)
assert!(check_is_rejected(
quorum.clone(),
rejecting.clone(),
Uint128::new(30),
true,
true,
false
));
// Total power of 33. 13 total votes. 8 no votes, 3 yes, 2
// abstain. 39.3% turnout. Expired. As it is expired we see if
// the 8 no votes excede the 50% failing threshold, which they
// do.
assert!(check_is_rejected(
quorum.clone(),
rejecting.clone(),
Uint128::new(33),
true,
true,
false
));
// over quorum, threshold passes if we ignore abstain
// 17 total votes w/ abstain => 40% quorum of 40 total
// 6 no / (6 no + 4 yes + 2 votes) => 50% threshold
assert!(check_is_rejected(
quorum.clone(),
rejected_ignoring_abstain.clone(),
Uint128::new(40),
true,
true,
false
));
// Over quorum, but under threshold fails if the proposal is
// not expired. If the proposal is expired though it passes as
// the total vote count used is the number of votes, and not
// the total number of votes avaliable.
assert!(check_is_rejected(
quorum.clone(),
failing.clone(),
Uint128::new(20),
true,
true,
false
));
assert!(!check_is_rejected(
quorum.clone(),
failing,
Uint128::new(20),
false,
true,
false
));
// Voting is still open so assume rest of votes are yes
// threshold not reached
assert!(!check_is_rejected(
quorum.clone(),
rejecting.clone(),
Uint128::new(30),
false,
true,
false
));
assert!(!check_is_rejected(
quorum.clone(),
rejected_ignoring_abstain.clone(),
Uint128::new(40),
false,
true,
false
));
// if we have threshold * total_weight as no votes this must reject
assert!(check_is_rejected(
quorum.clone(),
rejecting.clone(),
Uint128::new(14),
false,
true,
false
));
// all votes have been cast, some abstain
assert!(check_is_rejected(
quorum.clone(),
rejected_ignoring_abstain,
Uint128::new(17),
false,
true,
false
));
// 3 votes uncast, if they all vote yes, we have 7 no, 7 yes+veto, 2 abstain (out of 16)
assert!(check_is_rejected(
quorum,
rejecting,
Uint128::new(16),
false,
true,
false
));
}
#[test]
fn quorum_edge_cases() {
// When we pass absolute threshold (everyone else voting no,
// we pass), but still don't hit quorum.
let quorum = Threshold::ThresholdQuorum {
threshold: PercentageThreshold::Percent(Decimal::percent(60)),
quorum: PercentageThreshold::Percent(Decimal::percent(80)),
};
// Try 9 yes, 1 no (out of 15) -> 90% voter threshold, 60%
// absolute threshold, still no quorum doesn't matter if
// expired or not.
let missing_voters = Votes {
yes: Uint128::new(9),
no: Uint128::new(1),
abstain: Uint128::new(0),
};
assert!(!check_is_passed(
quorum.clone(),
missing_voters.clone(),
Uint128::new(15),
false,
true,
false
));
assert!(!check_is_passed(
quorum.clone(),
missing_voters,
Uint128::new(15),
true,
true,
false
));
// 1 less yes, 3 vetos and this passes only when expired.
let wait_til_expired = Votes {
yes: Uint128::new(8),
no: Uint128::new(4),
abstain: Uint128::new(0),
};
assert!(!check_is_passed(
quorum.clone(),
wait_til_expired.clone(),
Uint128::new(15),
false,
true,
false
));
assert!(check_is_passed(
quorum.clone(),
wait_til_expired,
Uint128::new(15),
true,
true,
false
));
// 9 yes and 3 nos passes early
let passes_early = Votes {
yes: Uint128::new(9),
no: Uint128::new(3),
abstain: Uint128::new(0),
};
assert!(check_is_passed(
quorum.clone(),
passes_early.clone(),
Uint128::new(15),
false,
true,
false
));
assert!(check_is_passed(
quorum,
passes_early,
Uint128::new(15),
true,
true,
false
));
}
}