Skip to content

Commit

Permalink
Prettify votes_needed
Browse files Browse the repository at this point in the history
  • Loading branch information
webmaster128 committed Oct 12, 2024
1 parent 17b1ac4 commit e965f70
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions packages/cw3/src/proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ use cw_utils::{Expiration, Threshold};

use crate::{DepositInfo, Status, Vote};

// we multiply by this when calculating needed_votes in order to round up properly
// Note: `10u128.pow(9)` fails as "u128::pow` is not yet stable as a const fn"
const PRECISION_FACTOR: u128 = 1_000_000_000;

#[cw_serde]
pub struct Proposal {
pub title: String,
Expand Down Expand Up @@ -155,12 +151,13 @@ impl Votes {
}
}

// this is a helper function so Decimal works with u64 rather than Uint128
// also, we must *round up* here, as we need 8, not 7 votes to reach 50% of 15 total
// This is a helper function so Decimal works with u64 rather than Uint128.
// Also, we must *round up* here, as we need 8, not 7 votes to reach 50% of 15 total.
// `percentage` must not exceed 1.0.
fn votes_needed(weight: u64, percentage: Decimal) -> u64 {
let applied = Uint128::new(PRECISION_FACTOR * weight as u128).mul_floor(percentage);
// Divide by PRECISION_FACTOR, rounding up to the nearest integer
((applied.u128() + PRECISION_FACTOR - 1) / PRECISION_FACTOR) as u64
assert!(percentage <= Decimal::one());
let out = Uint128::from(weight).mul_ceil(percentage);
out.u128() as u64 // cast is safe because percentage is <= 1.
}

// we cast a ballot with our chosen vote and a given weight
Expand Down Expand Up @@ -199,6 +196,8 @@ mod test {
// round up right over 1
assert_eq!(2, votes_needed(3, Decimal::permille(334)));
assert_eq!(11, votes_needed(30, Decimal::permille(334)));
// round up to full number of votes
assert_eq!(420, votes_needed(420, Decimal::permille(999)));

// exact matches don't round
assert_eq!(17, votes_needed(34, Decimal::percent(50)));
Expand Down

0 comments on commit e965f70

Please sign in to comment.