From 62184e70b266aa724c62d3c0d2cced6ab34878f6 Mon Sep 17 00:00:00 2001 From: Tiago Carvalho Date: Fri, 1 Sep 2023 10:09:04 +0100 Subject: [PATCH] Multiply Amount with FractionalVotingPower --- core/src/types/voting_power.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/core/src/types/voting_power.rs b/core/src/types/voting_power.rs index 946e08b834..844df5811b 100644 --- a/core/src/types/voting_power.rs +++ b/core/src/types/voting_power.rs @@ -12,6 +12,7 @@ use num_traits::ops::checked::CheckedAdd; use serde::de::Visitor; use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; +use crate::types::token::Amount; use crate::types::uint::Uint; /// Namada voting power, normalized to the range `0 - 2^32`. @@ -170,6 +171,27 @@ impl Mul<&FractionalVotingPower> for FractionalVotingPower { } } +impl Mul for FractionalVotingPower { + type Output = Amount; + + fn mul(self, rhs: Amount) -> Self::Output { + self * &rhs + } +} + +impl Mul<&Amount> for FractionalVotingPower { + type Output = Amount; + + fn mul(self, &rhs: &Amount) -> Self::Output { + let whole: Uint = rhs.into(); + let fraction = (self.0 * whole).to_integer(); + match Amount::from_uint(fraction, 0u8) { + Ok(amount) => amount, + _ => unreachable!(), + } + } +} + impl Add for FractionalVotingPower { type Output = Self;