dpp/balances/total_single_token_balance/
mod.rs

1use crate::balances::credits::SignedTokenAmount;
2use crate::ProtocolError;
3#[cfg(feature = "fixtures-and-mocks")]
4use bincode::Encode;
5#[cfg(feature = "fixtures-and-mocks")]
6use platform_serialization::de::Decode;
7use std::fmt;
8
9/// A structure where the token supply and the aggregated token account balances should always be equal
10#[derive(Copy, Clone, Debug)]
11#[cfg_attr(feature = "fixtures-and-mocks", derive(Encode, Decode))]
12pub struct TotalSingleTokenBalance {
13    /// the token supply
14    pub token_supply: SignedTokenAmount,
15    /// the sum of all user account balances
16    pub aggregated_token_account_balances: SignedTokenAmount,
17}
18
19impl fmt::Display for TotalSingleTokenBalance {
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        writeln!(f, "TotalSingleTokenBalance {{")?;
22        writeln!(f, "    token_supply: {},", self.token_supply)?;
23        writeln!(
24            f,
25            "    aggregated_token_account_balances: {}",
26            self.aggregated_token_account_balances
27        )?;
28        write!(f, "}}")
29    }
30}
31impl TotalSingleTokenBalance {
32    /// Is the outcome okay? basically do the values match up
33    /// Errors in case of overflow
34    pub fn ok(&self) -> Result<bool, ProtocolError> {
35        let TotalSingleTokenBalance {
36            token_supply,
37            aggregated_token_account_balances,
38        } = *self;
39
40        if token_supply < 0 {
41            return Err(ProtocolError::Generic(
42                "Token in platform are less than 0".to_string(),
43            ));
44        }
45
46        if aggregated_token_account_balances < 0 {
47            return Err(ProtocolError::Generic(
48                "Token in aggregated identity balances are less than 0".to_string(),
49            ));
50        }
51
52        Ok(token_supply == aggregated_token_account_balances)
53    }
54}