Skip to content

Commit

Permalink
Multiply Amount with FractionalVotingPower
Browse files Browse the repository at this point in the history
  • Loading branch information
sug0 committed Sep 7, 2023
1 parent f42f6dd commit 62184e7
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions core/src/types/voting_power.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -170,6 +171,27 @@ impl Mul<&FractionalVotingPower> for FractionalVotingPower {
}
}

impl Mul<Amount> 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<FractionalVotingPower> for FractionalVotingPower {
type Output = Self;

Expand Down

0 comments on commit 62184e7

Please sign in to comment.