Struct Amount

Source
pub struct Amount(/* private fields */);
Expand description

Number of millisatoshis.

This type represents a number of millisatoshis (thousands of satoshi) which is the base unit of the lightning network. It provides ordinary arithmetic and conversion methods.

§Invariants

This type guarantees that the amount stays less than or equal to 21 million bitcoins. However unsafe code must not rely on this, at least for now. This implies that arithmetic operations always panic on overflow.

§Display implementation

To avoid confusion, the amount is displayed with msat suffix - e.g. 42 msat. No other representations are supported yet, feel free to contribute!

Implementations§

Source§

impl Amount

Source

pub const ZERO: Amount

Zero bitcoins.

Source

pub const ONE_MSAT: Amount

One millisatoshi

Source

pub const MAX: Amount

21 million bitcoins.

Source

pub const ONE_SAT: Amount

One satoshi

Source

pub const ONE_BTC: Amount

One bitcoin

Source

pub fn from_msat(msat: u64) -> Result<Self, OverflowError>

Constructs the amount from raw millisatosis.

The value is directly converted with an overflow check.

§Errors

This method returns an error if the amount exceeds Bitcoin supply cap

§Example
let msat = ln_types::Amount::from_msat(1000).unwrap();
assert_eq!(msat, ln_types::Amount::ONE_SAT);
Source

pub fn from_sat(sat: u64) -> Result<Self, OverflowError>

Constructs the amount from raw satosis.

The value is converted with an overflow check.

§Errors

This method returns an error if the amount exceeds Bitcoin supply cap

§Example
let msat = ln_types::Amount::from_sat(100_000_000).unwrap();
assert_eq!(msat, ln_types::Amount::ONE_BTC);
Source

pub fn to_msat(self) -> u64

Converts the value to raw millisatoshis.

§Example
let msat = ln_types::Amount::ONE_SAT.to_msat();
assert_eq!(msat, 1000);
Source

pub fn to_sat(self) -> Result<u64, FractionError>

Attempts to convert the value to raw satoshis.

§Errors

This method returns an error if the number of millisatoshis isn’t rounded to thousands.

§Example
let msat = ln_types::Amount::ONE_SAT.to_sat().unwrap();
assert_eq!(msat, 1);
Source

pub fn to_sat_floor(self) -> u64

Converts to satoshis rounding down.

§Example
let msat = ln_types::Amount::ONE_MSAT.to_sat_floor();
assert_eq!(msat, 0);
Source

pub fn to_sat_ceiling(self) -> u64

Converts to satoshis rounding up.

§Example
let msat = ln_types::Amount::ONE_MSAT.to_sat_ceiling();
assert_eq!(msat, 1);
Source

pub fn to_sat_round(self) -> u64

Converts to satoshis rounding.

§Example
let msat = ln_types::Amount::ONE_MSAT.to_sat_round();
assert_eq!(msat, 0);

Trait Implementations§

Source§

impl Add for Amount

Panics on overflow

Source§

type Output = Amount

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Amount) -> Self::Output

Performs the + operation. Read more
Source§

impl AddAssign for Amount

Panics on overflow

Source§

fn add_assign(&mut self, rhs: Amount)

Performs the += operation. Read more
Source§

impl Clone for Amount

Source§

fn clone(&self) -> Amount

Returns a duplicate 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

Displays the amount followed by denomination msat.

Source§

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

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

impl Default for Amount

Source§

fn default() -> Amount

Returns the “default value” for a type. Read more
Source§

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

Available on crate feature serde only.

The value is deserialized as u64 msats.

Source§

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

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

impl Display for Amount

Displays the amount followed by denomination msat.

Source§

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

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

impl Div<u64> for Amount

Source§

type Output = Amount

The resulting type after applying the / operator.
Source§

fn div(self, rhs: u64) -> Self::Output

Performs the / operation. Read more
Source§

impl DivAssign<u64> for Amount

Source§

fn div_assign(&mut self, rhs: u64)

Performs the /= operation. Read more
Source§

impl<'a> FromSql<'a> for Amount

Available on crate feature postgres-types only.

Retrieved as i64 msats with range check

Source§

fn from_sql( ty: &Type, raw: &'a [u8], ) -> Result<Self, Box<dyn Error + Send + Sync + 'static>>

Creates a new value of this type from a buffer of data of the specified Postgres Type in its binary format. Read more
Source§

fn accepts(ty: &Type) -> bool

Determines if a value of this type can be created from the specified Postgres Type.
Source§

fn from_sql_null(ty: &Type) -> Result<Self, Box<dyn Error + Send + Sync>>

Creates a new value of this type from a NULL SQL value. Read more
Source§

fn from_sql_nullable( ty: &Type, raw: Option<&'a [u8]>, ) -> Result<Self, Box<dyn Error + Send + Sync>>

A convenience function that delegates to from_sql and from_sql_null depending on the value of raw.
Source§

impl FromStr for Amount

Accepts an unsigned integer up to 21 000 000 BTC The amount may optionally be followed by denomination msat.

Source§

type Err = ParseError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for Amount

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Mul<Amount> for u64

Panics on overflow

Source§

type Output = Amount

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Amount) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<u64> for Amount

Panics on overflow

Source§

type Output = Amount

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: u64) -> Self::Output

Performs the * operation. Read more
Source§

impl MulAssign<u64> for Amount

Panics on overflow

Source§

fn mul_assign(&mut self, rhs: u64)

Performs the *= 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 ParseArgFromStr for Amount

Available on crate feature parse_arg only.
Source§

fn describe_type<W: Write>(writer: W) -> Result

Writes human-readable description of the type to the writer. 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 Rem<u64> for Amount

Source§

type Output = Amount

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: u64) -> Self::Output

Performs the % operation. Read more
Source§

impl RemAssign<u64> for Amount

Source§

fn rem_assign(&mut self, rhs: u64)

Performs the %= operation. Read more
Source§

impl Serialize for Amount

Available on crate feature serde only.

The value is serialized as u64 msats.

Source§

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

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

impl Sub for Amount

Panics on overflow

Source§

type Output = Amount

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Amount) -> Self::Output

Performs the - operation. Read more
Source§

impl SubAssign for Amount

Panics on overflow

Source§

fn sub_assign(&mut self, rhs: Amount)

Performs the -= operation. Read more
Source§

impl ToSql for Amount

Available on crate feature postgres-types only.

Stored as i64 msats

Source§

fn to_sql( &self, ty: &Type, out: &mut BytesMut, ) -> Result<IsNull, Box<dyn Error + Send + Sync + 'static>>

Converts the value of self into the binary format of the specified Postgres Type, appending it to out. Read more
Source§

fn accepts(ty: &Type) -> bool

Determines if a value of this type can be converted to the specified Postgres Type.
Source§

fn to_sql_checked( &self, ty: &Type, out: &mut BytesMut, ) -> Result<IsNull, Box<dyn Error + Sync + Send>>

An adaptor method used internally by Rust-Postgres. Read more
Source§

fn encode_format(&self, _ty: &Type) -> Format

Specify the encode format
Source§

impl<'a> TryFrom<&'a str> for Amount

Accepts an unsigned integer up to 21 000 000 BTC The amount may optionally be followed by denomination msat.

Source§

type Error = ParseError

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

fn try_from(s: &'a str) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Amount> for Amount

Available on crate feature bitcoin-units only.
Source§

type Error = OverflowError

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<Amount> for Amount

Available on crate feature bitcoin-units only.
Source§

type Error = FractionError

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<Box<str>> for Amount

Available on crate feature alloc only.

Accepts an unsigned integer up to 21 000 000 BTC The amount may optionally be followed by denomination msat.

Source§

type Error = ParseError

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

fn try_from(s: Box<str>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<String> for Amount

Available on crate feature alloc only.

Accepts an unsigned integer up to 21 000 000 BTC The amount may optionally be followed by denomination msat.

Source§

type Error = ParseError

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

fn try_from(s: String) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl Value for Amount

Available on crate feature slog only.

Logs msats using emit_u64

Source§

fn serialize( &self, _rec: &Record<'_>, key: Key, serializer: &mut dyn Serializer, ) -> Result

Serialize self into Serializer Read more
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> BorrowToSql for T
where T: ToSql,

Source§

fn borrow_to_sql(&self) -> &dyn ToSql

Returns a reference to self as a ToSql trait object.
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<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> ParseArg for T
where T: ParseArgFromStr, <T as FromStr>::Err: Display,

Source§

type Error = ParseArgError<<T as FromStr>::Err>

Type returned in the Err variant of Result when parsing fails.
Source§

fn parse_arg(arg: &OsStr) -> Result<T, <T as ParseArg>::Error>

Parses the argument.
Source§

fn describe_type<W>(writer: W) -> Result<(), Error>
where W: Write,

Writes human-readable description of the type to the writer. Read more
Source§

fn parse_owned_arg(arg: OsString) -> Result<Self, Self::Error>

Parses the argument consuming it. 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> 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 = 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.
Source§

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

Source§

fn vzip(self) -> V

Source§

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

Source§

impl<T> FromSqlOwned for T
where T: for<'a> FromSql<'a>,