From 226360ecb9584f3f5564af72edba26003a641be6 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Sat, 12 Oct 2024 10:14:40 +0200 Subject: [PATCH] Prettify votes_needed --- packages/cw3/src/proposal.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/packages/cw3/src/proposal.rs b/packages/cw3/src/proposal.rs index a9a86ae80..1eb63a6cd 100644 --- a/packages/cw3/src/proposal.rs +++ b/packages/cw3/src/proposal.rs @@ -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, @@ -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 @@ -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)));