dpp/balances/total_tokens_balance/
mod.rs

1use crate::balances::credits::SumTokenAmount;
2use crate::ProtocolError;
3use std::fmt;
4
5/// The outcome of verifying token balances
6#[derive(Copy, Clone, Debug)]
7pub struct TotalTokensBalance {
8    /// all the tokens in platform
9    pub total_tokens_in_platform: SumTokenAmount,
10    /// all the tokens in identity token balances
11    pub total_identity_token_balances: SumTokenAmount,
12}
13
14impl fmt::Display for TotalTokensBalance {
15    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16        writeln!(f, "TotalTokensBalance {{")?;
17        writeln!(
18            f,
19            "    total_tokens_in_platform: {},",
20            self.total_tokens_in_platform
21        )?;
22        writeln!(
23            f,
24            "    total_identity_token_balances: {}",
25            self.total_identity_token_balances
26        )?;
27        write!(f, "}}")
28    }
29}
30impl TotalTokensBalance {
31    /// Is the outcome okay? basically do the values match up
32    /// Errors in case of overflow
33    pub fn ok(&self) -> Result<bool, ProtocolError> {
34        let TotalTokensBalance {
35            total_tokens_in_platform,
36            total_identity_token_balances,
37        } = *self;
38
39        if total_tokens_in_platform < 0 {
40            return Err(ProtocolError::CriticalCorruptedCreditsCodeExecution(
41                "Tokens in platform are less than 0".to_string(),
42            ));
43        }
44
45        if total_identity_token_balances < 0 {
46            return Err(ProtocolError::CriticalCorruptedCreditsCodeExecution(
47                "Tokens in identity balances are less than 0".to_string(),
48            ));
49        }
50
51        Ok(total_tokens_in_platform == total_identity_token_balances)
52    }
53}