Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions zebra-chain/src/value_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,19 +138,27 @@ where
}
}

/// Returns the sum of all value pool balances.
pub fn total(self) -> Result<Amount<C>, amount::Error> {
self.transparent + self.sprout + self.sapling + self.orchard + self.deferred
}

/// Convert this value balance to a different ValueBalance type,
/// if it satisfies the new constraint
pub fn constrain<C2>(self) -> Result<ValueBalance<C2>, ValueBalanceError>
where
C2: Constraint,
C2: Constraint + Copy,
{
Ok(ValueBalance::<C2> {
let value_balance = ValueBalance::<C2> {
Comment on lines 148 to +152
transparent: self.transparent.constrain().map_err(Transparent)?,
sprout: self.sprout.constrain().map_err(Sprout)?,
sapling: self.sapling.constrain().map_err(Sapling)?,
orchard: self.orchard.constrain().map_err(Orchard)?,
deferred: self.deferred.constrain().map_err(Deferred)?,
})
};

value_balance.total().map_err(ValueBalanceError::Total)?;
Ok(value_balance)
Comment on lines +160 to +161
}
}

Expand Down Expand Up @@ -415,6 +423,9 @@ pub enum ValueBalanceError {
/// deferred amount error {0}
Deferred(amount::Error),

/// total amount error {0}
Total(amount::Error),

/// ValueBalance is unparsable
Unparsable,
}
Expand All @@ -427,6 +438,7 @@ impl fmt::Display for ValueBalanceError {
Sapling(e) => format!("sapling amount err: {e}"),
Orchard(e) => format!("orchard amount err: {e}"),
Deferred(e) => format!("deferred amount err: {e}"),
Total(e) => format!("total amount err: {e}"),
Unparsable => "value balance is unparsable".to_string(),
})
}
Expand Down
Loading