Skip to main content

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    /// all the tokens held in token shielded pools (0 before protocol version 14)
13    pub total_token_shielded_pool_balances: SumTokenAmount,
14}
15
16impl fmt::Display for TotalTokensBalance {
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        writeln!(f, "TotalTokensBalance {{")?;
19        writeln!(
20            f,
21            "    total_tokens_in_platform: {},",
22            self.total_tokens_in_platform
23        )?;
24        writeln!(
25            f,
26            "    total_identity_token_balances: {},",
27            self.total_identity_token_balances
28        )?;
29        writeln!(
30            f,
31            "    total_token_shielded_pool_balances: {}",
32            self.total_token_shielded_pool_balances
33        )?;
34        write!(f, "}}")
35    }
36}
37impl TotalTokensBalance {
38    /// Is the outcome okay? basically do the values match up
39    /// Errors in case of overflow
40    pub fn ok(&self) -> Result<bool, ProtocolError> {
41        let TotalTokensBalance {
42            total_tokens_in_platform,
43            total_identity_token_balances,
44            total_token_shielded_pool_balances,
45        } = *self;
46
47        if total_tokens_in_platform < 0 {
48            return Err(ProtocolError::CriticalCorruptedCreditsCodeExecution(
49                "Tokens in platform are less than 0".to_string(),
50            ));
51        }
52
53        if total_identity_token_balances < 0 {
54            return Err(ProtocolError::CriticalCorruptedCreditsCodeExecution(
55                "Tokens in identity balances are less than 0".to_string(),
56            ));
57        }
58
59        if total_token_shielded_pool_balances < 0 {
60            return Err(ProtocolError::CriticalCorruptedCreditsCodeExecution(
61                "Tokens in shielded pools are less than 0".to_string(),
62            ));
63        }
64
65        let total_balances = total_identity_token_balances
66            .checked_add(total_token_shielded_pool_balances)
67            .ok_or_else(|| {
68                ProtocolError::CriticalCorruptedCreditsCodeExecution(
69                    "Overflow adding identity and shielded pool token balances".to_string(),
70                )
71            })?;
72
73        Ok(total_tokens_in_platform == total_balances)
74    }
75
76    /// The balance side of the conservation equation: identity balances plus shielded pool
77    /// balances. Errors on overflow.
78    pub fn total_balances(&self) -> Result<SumTokenAmount, ProtocolError> {
79        self.total_identity_token_balances
80            .checked_add(self.total_token_shielded_pool_balances)
81            .ok_or_else(|| {
82                ProtocolError::CriticalCorruptedCreditsCodeExecution(
83                    "Overflow adding identity and shielded pool token balances".to_string(),
84                )
85            })
86    }
87}