Skip to main content

Amount

Struct Amount 

Source
pub struct Amount { /* private fields */ }
Expand description

A quantity of a Hive asset, held as an integer count of its smallest unit.

Implementations§

Source§

impl Amount

Source

pub fn from_units(units: i64, symbol: &str, chain: Chain) -> Result<Self>

Build from a raw unit count and a symbol on the given chain.

Source

pub fn parse(s: &str, chain: Chain) -> Result<Self>

Parse Hive’s textual amount form, "1.234 HIVE".

The decimal string is converted to an integer directly. A value with more decimal places than the asset allows is an error, not a silent truncation: quietly dropping a digit from a monetary amount transfers a different sum than the caller asked for.

Source

pub fn units(&self) -> i64

The raw unit count.

Source

pub fn precision(&self) -> u8

The asset’s decimal places.

Source

pub fn symbol(&self) -> &'static str

The display symbol, e.g. HIVE.

Source

pub fn wire_symbol(&self) -> &'static str

The symbol used in the legacy binary encoding, e.g. STEEM.

Source

pub fn is_zero(&self) -> bool

Whether this is exactly zero.

Source

pub fn checked_add(&self, other: &Amount) -> Result<Amount>

Add two amounts of the same asset. Refuses a mismatch rather than coercing.

Source

pub fn checked_sub(&self, other: &Amount) -> Result<Amount>

Subtract two amounts of the same asset.

Source

pub fn to_nai_json(&self, chain: Chain) -> Result<Value>

The NAI-style JSON form used by database_api: {"amount": "1234", "precision": 3, "nai": "@@000000021"}.

Trait Implementations§

Source§

impl Clone for Amount

Source§

fn clone(&self) -> Amount

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for Amount

Source§

impl Debug for Amount

Source§

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

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

impl<'de> Deserialize<'de> for Amount

Amounts arrive from the API in two different shapes, and both are accepted.

condenser_api and the legacy forms send the textual "1.234 HIVE". database_api and account_history_api in appbase mode send the NAI object {"amount": "1234", "precision": 3, "nai": "@@000000021"}. beem handled both too, but by branching on isinstance inside Amount.__init__ across four cases.

Deserialization resolves symbols against Hive mainnet. For a testnet, decode the raw JSON and use Amount::from_units with the right chain.

Source§

fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Amount

Source§

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

Render as "1.234 HIVE", always with exactly precision decimal places.

Source§

impl Eq for Amount

Source§

impl GrapheneDeserialize for Amount

Source§

fn read_from(r: &mut Reader<'_>) -> Result<Self>

Read the 16-byte legacy asset: int64 units, uint8 precision, 7-byte NUL-padded symbol.

The symbol is resolved against the reader’s chain and cross-checked: an asset whose declared precision disagrees with the chain’s is refused rather than silently reinterpreted, because that mismatch means the bytes are not the asset they claim to be.

Source§

impl GrapheneSerialize for Amount

Source§

fn append_to(&self, out: &mut Vec<u8>) -> Result<()>

Append this value’s wire encoding to out.
Source§

fn to_wire(&self) -> Result<Vec<u8>>

Encode this value to a fresh buffer.
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 (const: unstable) · Source§

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

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

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

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

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for Amount

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
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 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 Serialize for Amount

Amounts render in Hive’s textual form, "1.234 HIVE", which is what condenser_api and network_broadcast_api accept.

Source§

fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Amount

Auto Trait Implementations§

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

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

Source§

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

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

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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

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

Source§

fn vzip(self) -> V