Struct Amount

Source
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

Source

pub const fn zero() -> Self

Returns a zero-valued Amount.

Source

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}.

Source

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}.

Source

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}.

Source

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}.

Source

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}.

Source

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}.

Source

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}.

Source

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}.

Source

pub fn to_i64_le_bytes(self) -> [u8; 8]

Returns the Amount encoded as a signed 64-bit little-endian integer.

Source

pub const fn is_positive(self) -> bool

Returns true if self is positive and false if the Amount is zero or negative.

Source

pub const fn is_negative(self) -> bool

Returns true if self is negative and false if the Amount is zero or positive.

Source

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 successful
  • None - 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 Add<Amount> for Option<Amount>

Adds an Amount to an Option<Amount>, propagating None

Source§

type Output = Option<Amount>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Amount) -> Option<Amount>

Performs the + operation. Read more
Source§

impl Add for Amount

Adds two Amounts, checking for overflow/underflow

Source§

type Output = Option<Amount>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Amount) -> Option<Amount>

Performs the + operation. Read more
Source§

impl Clone for Amount

Source§

fn clone(&self) -> Amount

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Amount

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<&Amount> for CBOR

Source§

fn from(value: &Amount) -> Self

Converts to this type from the input type.
Source§

impl From<&Amount> for i64

Extracts the raw i64 zatoshi value from an Amount reference

Source§

fn from(amount: &Amount) -> i64

Converts to this type from the input type.
Source§

impl From<Amount> for CBOR

Source§

fn from(value: Amount) -> Self

Converts to this type from the input type.
Source§

impl From<Amount> for Envelope

Source§

fn from(value: Amount) -> Self

Converts to this type from the input type.
Source§

impl From<Amount> for i64

Extracts the raw i64 zatoshi value from an Amount

Source§

fn from(amount: Amount) -> i64

Converts to this type from the input type.
Source§

impl Mul<usize> for Amount

Multiplies an Amount by a usize factor, checking for overflow/underflow

Source§

type Output = Option<Amount>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: usize) -> Option<Amount>

Performs the * operation. Read more
Source§

impl Neg for Amount

Negates an Amount, flipping its sign

Source§

type Output = Amount

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self

Performs the unary - operation. Read more
Source§

impl Ord for Amount

Source§

fn cmp(&self, other: &Amount) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Amount

Source§

fn eq(&self, other: &Amount) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Amount

Source§

fn partial_cmp(&self, other: &Amount) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Sub<Amount> for Option<Amount>

Subtracts an Amount from an Option<Amount>, propagating None

Source§

type Output = Option<Amount>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Amount) -> Option<Amount>

Performs the - operation. Read more
Source§

impl Sub for Amount

Subtracts one Amount from another, checking for overflow/underflow

Source§

type Output = Option<Amount>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Amount) -> Option<Amount>

Performs the - operation. Read more
Source§

impl<'a> Sum<&'a Amount> for Option<Amount>

Implements std::iter::Sum for Amount references with overflow checking

Source§

fn sum<I: Iterator<Item = &'a Amount>>(iter: I) -> Self

Takes an iterator and generates Self from the elements by “summing up” the items.
Source§

impl Sum<Amount> for Option<Amount>

Implements std::iter::Sum for Amount with overflow checking

Source§

fn sum<I: Iterator<Item = Amount>>(iter: I) -> Self

Takes an iterator and generates Self from the elements by “summing up” the items.
Source§

impl TryFrom<Amount> for u64

Converts an Amount to u64, ensuring the value is non-negative

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: Amount) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<CBOR> for Amount

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(cbor: CBOR) -> Result<Self>

Performs the conversion.
Source§

impl TryFrom<Envelope> for Amount

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(envelope: Envelope) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<i64> for Amount

Converts an i64 into an Amount, with range checking

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: i64) -> Result<Self>

Performs the conversion.
Source§

impl Copy for Amount

Source§

impl Eq for Amount

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CBORDecodable for T
where T: TryFrom<CBOR, Error = Error>,

Source§

fn try_from_cbor(cbor: &CBOR) -> Result<Self, Error>

Source§

impl<T> CBOREncodable for T
where T: Into<CBOR> + Clone,

Source§

fn to_cbor(&self) -> CBOR

Converts this value to a CBOR object. Read more
Source§

fn to_cbor_data(&self) -> Vec<u8>

Converts this value directly to binary CBOR data. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> EnvelopeEncodable for T
where T: Into<Envelope> + Clone,

Source§

fn into_envelope(self) -> Envelope

Converts the value into an envelope by using its Into<Envelope> implementation.

Source§

fn to_envelope(&self) -> Envelope
where Self: Clone,

Converts a reference to this value into a Gordian Envelope. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

Source§

impl<T> CBORCodable for T

§

impl<T> ErasedDestructor for T
where T: 'static,

§

impl<T> MaybeSendSync for T