pub struct Amount(/* private fields */);
Expand description
A type-safe representation of a ZCash amount in zatoshis (zats).
Amount
represents a monetary value in the Zcash cryptocurrency, stored
internally as a signed 64-bit integer count of zatoshis. One ZEC equals
100,000,000 zatoshis (1 ZEC = 10^8 zats), similar to Bitcoin’s satoshis.
The signed representation allows for representing both positive amounts (payments received) and negative amounts (payments sent) in transaction and balance calculations.
§Zcash Concept Relation
In Zcash, monetary values are represented in two units:
- ZEC: The main unit of currency (analogous to dollars)
- zatoshis (zats): The smallest indivisible unit (analogous to cents)
Amount enforces the protocol limit of 21 million total ZEC, preventing overflow or underflow in calculations with proper error handling.
§Data Preservation
The Amount
type preserves the exact zatoshi values from wallet data,
maintaining precise balances and transaction amounts during wallet migration.
When displayed, values are formatted as ZEC with decimal places.
§Examples
// Create an amount of 1.5 ZEC (150,000,000 zatoshis)
let amount = Amount::from_u64(150_000_000)?;
// Check if the amount is positive
assert!(amount.is_positive());
// Convert to raw zatoshi value
let zats: i64 = amount.into();
assert_eq!(zats, 150_000_000);
FIXME: Amounts in the zewif format should never be negative; negative values are only used transiently in the protocol.
Implementations§
Source§impl Amount
impl Amount
Sourcepub const fn const_from_i64(amount: i64) -> Self
pub const fn const_from_i64(amount: i64) -> Self
Creates a constant Amount from an i64.
Panics: if the amount is outside the range {-MAX_BALANCE..MAX_BALANCE}
.
Sourcepub const fn const_from_u64(amount: u64) -> Self
pub const fn const_from_u64(amount: u64) -> Self
Creates a constant Amount from a u64.
Panics: if the amount is outside the range {0..MAX_BALANCE}
.
Sourcepub fn from_i64(amount: i64) -> Result<Self>
pub fn from_i64(amount: i64) -> Result<Self>
Creates an Amount from an i64.
Returns an error if the amount is outside the range {-MAX_BALANCE..MAX_BALANCE}
.
Sourcepub fn from_nonnegative_i64(amount: i64) -> Result<Self>
pub fn from_nonnegative_i64(amount: i64) -> Result<Self>
Creates a non-negative Amount from an i64.
Returns an error if the amount is outside the range {0..MAX_BALANCE}
.
Sourcepub fn from_u64(amount: u64) -> Result<Self>
pub fn from_u64(amount: u64) -> Result<Self>
Creates an Amount from a u64.
Returns an error if the amount is outside the range {0..MAX_MONEY}
.
Sourcepub fn from_i64_le_bytes(bytes: [u8; 8]) -> Result<Self>
pub fn from_i64_le_bytes(bytes: [u8; 8]) -> Result<Self>
Reads an Amount from a signed 64-bit little-endian integer.
Returns an error if the amount is outside the range {-MAX_BALANCE..MAX_BALANCE}
.
Sourcepub fn from_nonnegative_i64_le_bytes(bytes: [u8; 8]) -> Result<Self>
pub fn from_nonnegative_i64_le_bytes(bytes: [u8; 8]) -> Result<Self>
Reads a non-negative Amount from a signed 64-bit little-endian integer.
Returns an error if the amount is outside the range {0..MAX_BALANCE}
.
Sourcepub fn from_u64_le_bytes(bytes: [u8; 8]) -> Result<Self>
pub fn from_u64_le_bytes(bytes: [u8; 8]) -> Result<Self>
Reads an Amount from an unsigned 64-bit little-endian integer.
Returns an error if the amount is outside the range {0..MAX_BALANCE}
.
Sourcepub fn to_i64_le_bytes(self) -> [u8; 8]
pub fn to_i64_le_bytes(self) -> [u8; 8]
Returns the Amount encoded as a signed 64-bit little-endian integer.
Sourcepub const fn is_positive(self) -> bool
pub const fn is_positive(self) -> bool
Returns true
if self
is positive and false
if the Amount is zero or
negative.
Sourcepub const fn is_negative(self) -> bool
pub const fn is_negative(self) -> bool
Returns true
if self
is negative and false
if the Amount is zero or
positive.
Sourcepub fn sum<I: IntoIterator<Item = Amount>>(values: I) -> Option<Amount>
pub fn sum<I: IntoIterator<Item = Amount>>(values: I) -> Option<Amount>
Sums a collection of Amount values with overflow checking.
This helper method safely adds a collection of Amounts, returning None if any intermediate calculation would exceed the valid Amount range.
§Arguments
values
- An iterable collection of Amount values to sum
§Returns
Some(Amount)
- The sum if all operations were successfulNone
- If any intermediate sum would exceed MAX_BALANCE
§Examples
// Sum several ZEC amounts
let amounts = vec![
Amount::from_u64(100_000_000)?, // 1 ZEC
Amount::from_u64(50_000_000)?, // 0.5 ZEC
Amount::from_u64(25_000_000)?, // 0.25 ZEC
];
let total = Amount::sum(amounts).unwrap();
let total_zats: i64 = total.into();
assert_eq!(total_zats, 175_000_000); // 1.75 ZEC
Trait Implementations§
Source§impl Mul<usize> for Amount
Multiplies an Amount by a usize factor, checking for overflow/underflow
impl Mul<usize> for Amount
Multiplies an Amount by a usize factor, checking for overflow/underflow
Source§impl Ord for Amount
impl Ord for Amount
Source§impl PartialOrd for Amount
impl PartialOrd for Amount
Source§impl Sub<Amount> for Option<Amount>
Subtracts an Amount from an Option<Amount>
, propagating None
impl Sub<Amount> for Option<Amount>
Subtracts an Amount from an Option<Amount>
, propagating None
Source§impl<'a> Sum<&'a Amount> for Option<Amount>
Implements std::iter::Sum for Amount references with overflow checking
impl<'a> Sum<&'a Amount> for Option<Amount>
Implements std::iter::Sum for Amount references with overflow checking
Source§impl Sum<Amount> for Option<Amount>
Implements std::iter::Sum for Amount with overflow checking
impl Sum<Amount> for Option<Amount>
Implements std::iter::Sum for Amount with overflow checking
impl Copy for Amount
impl Eq for Amount
impl StructuralPartialEq for Amount
Auto Trait Implementations§
impl Freeze for Amount
impl RefUnwindSafe for Amount
impl Send for Amount
impl Sync for Amount
impl Unpin for Amount
impl UnwindSafe for Amount
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CBORDecodable for T
impl<T> CBORDecodable for T
Source§impl<T> CBOREncodable for T
impl<T> CBOREncodable for T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> EnvelopeEncodable for T
impl<T> EnvelopeEncodable for T
Source§fn into_envelope(self) -> Envelope
fn into_envelope(self) -> Envelope
Converts the value into an envelope by using its Into<Envelope>
implementation.
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more