Skip to main content

dpp/balances/total_single_token_balance/
mod.rs

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