Skip to main content

bitcoin_units/
amount.rs

1// SPDX-License-Identifier: CC0-1.0
2
3//! Bitcoin amounts.
4//!
5//! This module mainly introduces the [Amount] and [SignedAmount] types.
6//! We refer to the documentation on the types for more information.
7
8#[cfg(feature = "alloc")]
9use alloc::string::{String, ToString};
10use core::cmp::Ordering;
11use core::convert::Infallible;
12#[cfg(feature = "alloc")]
13use core::fmt::Write as _;
14use core::str::FromStr;
15use core::{default, fmt, ops};
16
17#[cfg(feature = "serde")]
18use ::serde::{Deserialize, Serialize};
19#[cfg(feature = "arbitrary")]
20use arbitrary::{Arbitrary, Unstructured};
21
22use crate::input_string::InputString;
23use crate::internal_macros::write_err;
24#[cfg(feature = "alloc")]
25use crate::{FeeRate, Weight};
26
27/// A set of denominations in which amounts can be expressed.
28///
29/// # Examples
30/// ```
31/// # use core::str::FromStr;
32/// # use bitcoin_units::Amount;
33///
34/// assert_eq!(Amount::from_str("1 BTC").unwrap(), Amount::from_sat(100_000_000));
35/// assert_eq!(Amount::from_str("1 cBTC").unwrap(), Amount::from_sat(1_000_000));
36/// assert_eq!(Amount::from_str("1 mBTC").unwrap(), Amount::from_sat(100_000));
37/// assert_eq!(Amount::from_str("1 uBTC").unwrap(), Amount::from_sat(100));
38/// assert_eq!(Amount::from_str("10 nBTC").unwrap(), Amount::from_sat(1));
39/// assert_eq!(Amount::from_str("10000 pBTC").unwrap(), Amount::from_sat(1));
40/// assert_eq!(Amount::from_str("1 bit").unwrap(), Amount::from_sat(100));
41/// assert_eq!(Amount::from_str("1 sat").unwrap(), Amount::from_sat(1));
42/// assert_eq!(Amount::from_str("1000 msats").unwrap(), Amount::from_sat(1));
43/// ```
44#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
45#[non_exhaustive]
46pub enum Denomination {
47    /// BTC
48    Bitcoin,
49    /// cBTC
50    CentiBitcoin,
51    /// mBTC
52    MilliBitcoin,
53    /// uBTC
54    MicroBitcoin,
55    /// nBTC
56    NanoBitcoin,
57    /// pBTC
58    PicoBitcoin,
59    /// bits
60    Bit,
61    /// satoshi
62    Satoshi,
63    /// msat
64    MilliSatoshi,
65}
66
67impl Denomination {
68    /// Convenience alias for `Denomination::Bitcoin`.
69    pub const BTC: Self = Denomination::Bitcoin;
70
71    /// Convenience alias for `Denomination::Satoshi`.
72    pub const SAT: Self = Denomination::Satoshi;
73
74    /// The number of decimal places more than a satoshi.
75    fn precision(self) -> i8 {
76        match self {
77            Denomination::Bitcoin => -8,
78            Denomination::CentiBitcoin => -6,
79            Denomination::MilliBitcoin => -5,
80            Denomination::MicroBitcoin => -2,
81            Denomination::NanoBitcoin => 1,
82            Denomination::PicoBitcoin => 4,
83            Denomination::Bit => -2,
84            Denomination::Satoshi => 0,
85            Denomination::MilliSatoshi => 3,
86        }
87    }
88
89    /// Returns stringly representation of this
90    fn as_str(self) -> &'static str {
91        match self {
92            Denomination::Bitcoin => "BTC",
93            Denomination::CentiBitcoin => "cBTC",
94            Denomination::MilliBitcoin => "mBTC",
95            Denomination::MicroBitcoin => "uBTC",
96            Denomination::NanoBitcoin => "nBTC",
97            Denomination::PicoBitcoin => "pBTC",
98            Denomination::Bit => "bits",
99            Denomination::Satoshi => "satoshi",
100            Denomination::MilliSatoshi => "msat",
101        }
102    }
103
104    /// The different str forms of denominations that are recognized.
105    fn forms(s: &str) -> Option<Self> {
106        match s {
107            "BTC" | "btc" => Some(Denomination::Bitcoin),
108            "cBTC" | "cbtc" => Some(Denomination::CentiBitcoin),
109            "mBTC" | "mbtc" => Some(Denomination::MilliBitcoin),
110            "uBTC" | "ubtc" => Some(Denomination::MicroBitcoin),
111            "nBTC" | "nbtc" => Some(Denomination::NanoBitcoin),
112            "pBTC" | "pbtc" => Some(Denomination::PicoBitcoin),
113            "bit" | "bits" | "BIT" | "BITS" => Some(Denomination::Bit),
114            "SATOSHI" | "satoshi" | "SATOSHIS" | "satoshis" | "SAT" | "sat" | "SATS" | "sats" =>
115                Some(Denomination::Satoshi),
116            "mSAT" | "msat" | "mSATs" | "msats" => Some(Denomination::MilliSatoshi),
117            _ => None,
118        }
119    }
120}
121
122/// These form are ambigous and could have many meanings.  For example, M could denote Mega or Milli.
123/// If any of these forms are used, an error type PossiblyConfusingDenomination is returned.
124const CONFUSING_FORMS: [&str; 9] =
125    ["Msat", "Msats", "MSAT", "MSATS", "MSat", "MSats", "MBTC", "Mbtc", "PBTC"];
126
127impl fmt::Display for Denomination {
128    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.write_str(self.as_str()) }
129}
130
131impl FromStr for Denomination {
132    type Err = ParseDenominationError;
133
134    /// Convert from a str to Denomination.
135    ///
136    /// Any combination of upper and/or lower case, excluding uppercase of SI(m, u, n, p) is considered valid.
137    /// - Singular: BTC, mBTC, uBTC, nBTC, pBTC
138    /// - Plural or singular: sat, satoshi, bit, msat
139    ///
140    /// Due to ambiguity between mega and milli, pico and peta we prohibit usage of leading capital 'M', 'P'.
141    fn from_str(s: &str) -> Result<Self, Self::Err> {
142        use self::ParseDenominationError::*;
143
144        if CONFUSING_FORMS.contains(&s) {
145            return Err(PossiblyConfusing(PossiblyConfusingDenominationError(s.into())));
146        };
147
148        let form = self::Denomination::forms(s);
149
150        form.ok_or_else(|| Unknown(UnknownDenominationError(s.into())))
151    }
152}
153
154/// An error during amount parsing amount with denomination.
155#[derive(Debug, Clone, PartialEq, Eq)]
156#[non_exhaustive]
157pub enum ParseError {
158    /// Invalid amount.
159    Amount(ParseAmountError),
160
161    /// Invalid denomination.
162    Denomination(ParseDenominationError),
163
164    /// The denomination was not identified.
165    MissingDenomination(MissingDenominationError),
166}
167
168impl From<Infallible> for ParseError {
169    fn from(never: Infallible) -> Self { match never {} }
170}
171
172impl From<ParseAmountError> for ParseError {
173    fn from(e: ParseAmountError) -> Self { Self::Amount(e) }
174}
175
176impl From<ParseDenominationError> for ParseError {
177    fn from(e: ParseDenominationError) -> Self { Self::Denomination(e) }
178}
179
180impl From<OutOfRangeError> for ParseError {
181    fn from(e: OutOfRangeError) -> Self { Self::Amount(e.into()) }
182}
183
184impl From<TooPreciseError> for ParseError {
185    fn from(e: TooPreciseError) -> Self { Self::Amount(e.into()) }
186}
187
188impl From<MissingDigitsError> for ParseError {
189    fn from(e: MissingDigitsError) -> Self { Self::Amount(e.into()) }
190}
191
192impl From<InputTooLargeError> for ParseError {
193    fn from(e: InputTooLargeError) -> Self { Self::Amount(e.into()) }
194}
195
196impl From<InvalidCharacterError> for ParseError {
197    fn from(e: InvalidCharacterError) -> Self { Self::Amount(e.into()) }
198}
199
200impl fmt::Display for ParseError {
201    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
202        match self {
203            ParseError::Amount(error) => write_err!(f, "invalid amount"; error),
204            ParseError::Denomination(error) => write_err!(f, "invalid denomination"; error),
205            // We consider this to not be a source because it currently doesn't contain useful
206            // information
207            ParseError::MissingDenomination(_) =>
208                f.write_str("the input doesn't contain a denomination"),
209        }
210    }
211}
212
213#[cfg(feature = "std")]
214impl std::error::Error for ParseError {
215    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
216        match self {
217            ParseError::Amount(error) => Some(error),
218            ParseError::Denomination(error) => Some(error),
219            // We consider this to not be a source because it currently doesn't contain useful
220            // information
221            ParseError::MissingDenomination(_) => None,
222        }
223    }
224}
225
226/// An error during amount parsing.
227#[derive(Debug, Clone, PartialEq, Eq)]
228#[non_exhaustive]
229pub enum ParseAmountError {
230    /// The amount is too big or too small.
231    OutOfRange(OutOfRangeError),
232    /// Amount has higher precision than supported by the type.
233    TooPrecise(TooPreciseError),
234    /// A digit was expected but not found.
235    MissingDigits(MissingDigitsError),
236    /// Input string was too large.
237    InputTooLarge(InputTooLargeError),
238    /// Invalid character in input.
239    InvalidCharacter(InvalidCharacterError),
240}
241
242impl From<TooPreciseError> for ParseAmountError {
243    fn from(value: TooPreciseError) -> Self { Self::TooPrecise(value) }
244}
245
246impl From<MissingDigitsError> for ParseAmountError {
247    fn from(value: MissingDigitsError) -> Self { Self::MissingDigits(value) }
248}
249
250impl From<InputTooLargeError> for ParseAmountError {
251    fn from(value: InputTooLargeError) -> Self { Self::InputTooLarge(value) }
252}
253
254impl From<InvalidCharacterError> for ParseAmountError {
255    fn from(value: InvalidCharacterError) -> Self { Self::InvalidCharacter(value) }
256}
257
258impl From<Infallible> for ParseAmountError {
259    fn from(never: Infallible) -> Self { match never {} }
260}
261
262impl fmt::Display for ParseAmountError {
263    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
264        use ParseAmountError::*;
265
266        match *self {
267            OutOfRange(ref error) => write_err!(f, "amount out of range"; error),
268            TooPrecise(ref error) => write_err!(f, "amount has a too high precision"; error),
269            MissingDigits(ref error) => write_err!(f, "the input has too few digits"; error),
270            InputTooLarge(ref error) => write_err!(f, "the input is too large"; error),
271            InvalidCharacter(ref error) => write_err!(f, "invalid character in the input"; error),
272        }
273    }
274}
275
276#[cfg(feature = "std")]
277impl std::error::Error for ParseAmountError {
278    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
279        use ParseAmountError::*;
280
281        match *self {
282            TooPrecise(ref error) => Some(error),
283            InputTooLarge(ref error) => Some(error),
284            OutOfRange(ref error) => Some(error),
285            MissingDigits(ref error) => Some(error),
286            InvalidCharacter(ref error) => Some(error),
287        }
288    }
289}
290
291/// Returned when a parsed amount is too big or too small.
292#[derive(Debug, Copy, Clone, Eq, PartialEq)]
293pub struct OutOfRangeError {
294    is_signed: bool,
295    is_greater_than_max: bool,
296}
297
298impl OutOfRangeError {
299    /// Returns the minimum and maximum allowed values for the type that was parsed.
300    ///
301    /// This can be used to give a hint to the user which values are allowed.
302    pub fn valid_range(&self) -> (i64, u64) {
303        match self.is_signed {
304            true => (i64::MIN, i64::MAX as u64),
305            false => (0, u64::MAX),
306        }
307    }
308
309    /// Returns true if the input value was large than the maximum allowed value.
310    pub fn is_above_max(&self) -> bool { self.is_greater_than_max }
311
312    /// Returns true if the input value was smaller than the minimum allowed value.
313    pub fn is_below_min(&self) -> bool { !self.is_greater_than_max }
314
315    pub(crate) fn too_big(is_signed: bool) -> Self { Self { is_signed, is_greater_than_max: true } }
316
317    pub(crate) fn too_small() -> Self {
318        Self {
319            // implied - negative() is used for the other
320            is_signed: true,
321            is_greater_than_max: false,
322        }
323    }
324
325    pub(crate) fn negative() -> Self {
326        Self {
327            // implied - too_small() is used for the other
328            is_signed: false,
329            is_greater_than_max: false,
330        }
331    }
332}
333
334impl fmt::Display for OutOfRangeError {
335    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
336        if self.is_greater_than_max {
337            write!(f, "the amount is greater than {}", self.valid_range().1)
338        } else {
339            write!(f, "the amount is less than {}", self.valid_range().0)
340        }
341    }
342}
343
344#[cfg(feature = "std")]
345impl std::error::Error for OutOfRangeError {}
346
347impl From<OutOfRangeError> for ParseAmountError {
348    fn from(value: OutOfRangeError) -> Self { ParseAmountError::OutOfRange(value) }
349}
350
351/// Error returned when the input string has higher precision than satoshis.
352#[derive(Debug, Clone, Eq, PartialEq)]
353pub struct TooPreciseError {
354    position: usize,
355}
356
357impl fmt::Display for TooPreciseError {
358    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
359        match self.position {
360            0 => f.write_str("the amount is less than 1 satoshi but it's not zero"),
361            pos => write!(
362                f,
363                "the digits starting from position {} represent a sub-satoshi amount",
364                pos
365            ),
366        }
367    }
368}
369
370#[cfg(feature = "std")]
371impl std::error::Error for TooPreciseError {}
372
373/// Error returned when the input string is too large.
374#[derive(Debug, Clone, Eq, PartialEq)]
375pub struct InputTooLargeError {
376    len: usize,
377}
378
379impl fmt::Display for InputTooLargeError {
380    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
381        match self.len - INPUT_STRING_LEN_LIMIT {
382            1 => write!(
383                f,
384                "the input is one character longer than the maximum allowed length ({})",
385                INPUT_STRING_LEN_LIMIT
386            ),
387            n => write!(
388                f,
389                "the input is {} characters longer than the maximum allowed length ({})",
390                n, INPUT_STRING_LEN_LIMIT
391            ),
392        }
393    }
394}
395
396#[cfg(feature = "std")]
397impl std::error::Error for InputTooLargeError {}
398
399/// Error returned when digits were expected in the input but there were none.
400///
401/// In particular, this is currently returned when the string is empty or only contains the minus sign.
402#[derive(Debug, Clone, Eq, PartialEq)]
403pub struct MissingDigitsError {
404    kind: MissingDigitsKind,
405}
406
407impl fmt::Display for MissingDigitsError {
408    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
409        match self.kind {
410            MissingDigitsKind::Empty => f.write_str("the input is empty"),
411            MissingDigitsKind::OnlyMinusSign =>
412                f.write_str("there are no digits following the minus (-) sign"),
413        }
414    }
415}
416
417#[cfg(feature = "std")]
418impl std::error::Error for MissingDigitsError {}
419
420#[derive(Debug, Clone, Eq, PartialEq)]
421enum MissingDigitsKind {
422    Empty,
423    OnlyMinusSign,
424}
425
426/// Returned when the input contains an invalid character.
427#[derive(Debug, Clone, PartialEq, Eq)]
428pub struct InvalidCharacterError {
429    invalid_char: char,
430    position: usize,
431}
432
433impl fmt::Display for InvalidCharacterError {
434    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
435        match self.invalid_char {
436            '.' => f.write_str("there is more than one decimal separator (dot) in the input"),
437            '-' => f.write_str("there is more than one minus sign (-) in the input"),
438            c => write!(
439                f,
440                "the character '{}' at position {} is not a valid digit",
441                c, self.position
442            ),
443        }
444    }
445}
446
447#[cfg(feature = "std")]
448impl std::error::Error for InvalidCharacterError {}
449
450/// An error during amount parsing.
451#[derive(Debug, Clone, PartialEq, Eq)]
452#[non_exhaustive]
453pub enum ParseDenominationError {
454    /// The denomination was unknown.
455    Unknown(UnknownDenominationError),
456    /// The denomination has multiple possible interpretations.
457    PossiblyConfusing(PossiblyConfusingDenominationError),
458}
459
460impl From<Infallible> for ParseDenominationError {
461    fn from(never: Infallible) -> Self { match never {} }
462}
463
464impl fmt::Display for ParseDenominationError {
465    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
466        use ParseDenominationError::*;
467
468        match *self {
469            Unknown(ref e) => write_err!(f, "denomination parse error"; e),
470            PossiblyConfusing(ref e) => write_err!(f, "denomination parse error"; e),
471        }
472    }
473}
474
475#[cfg(feature = "std")]
476impl std::error::Error for ParseDenominationError {
477    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
478        use ParseDenominationError::*;
479
480        match *self {
481            Unknown(_) | PossiblyConfusing(_) => None,
482        }
483    }
484}
485
486/// Error returned when the denomination is empty.
487#[derive(Debug, Clone, PartialEq, Eq)]
488#[non_exhaustive]
489pub struct MissingDenominationError;
490
491/// Parsing error, unknown denomination.
492#[derive(Debug, Clone, PartialEq, Eq)]
493#[non_exhaustive]
494pub struct UnknownDenominationError(InputString);
495
496impl fmt::Display for UnknownDenominationError {
497    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
498        self.0.unknown_variant("bitcoin denomination", f)
499    }
500}
501
502#[cfg(feature = "std")]
503impl std::error::Error for UnknownDenominationError {
504    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None }
505}
506
507/// Parsing error, possibly confusing denomination.
508#[derive(Debug, Clone, PartialEq, Eq)]
509#[non_exhaustive]
510pub struct PossiblyConfusingDenominationError(InputString);
511
512impl fmt::Display for PossiblyConfusingDenominationError {
513    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
514        write!(f, "{}: possibly confusing denomination - we intentionally do not support 'M' and 'P' so as to not confuse mega/milli and peta/pico", self.0.display_cannot_parse("bitcoin denomination"))
515    }
516}
517
518#[cfg(feature = "std")]
519impl std::error::Error for PossiblyConfusingDenominationError {
520    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None }
521}
522
523/// Returns `Some(position)` if the precision is not supported.
524///
525/// The position indicates the first digit that is too precise.
526fn is_too_precise(s: &str, precision: usize) -> Option<usize> {
527    match s.find('.') {
528        Some(pos) if precision >= pos => Some(0),
529        Some(pos) => s[..pos]
530            .char_indices()
531            .rev()
532            .take(precision)
533            .find(|(_, d)| *d != '0')
534            .map(|(i, _)| i)
535            .or_else(|| {
536                s[(pos + 1)..].char_indices().find(|(_, d)| *d != '0').map(|(i, _)| i + pos + 1)
537            }),
538        None if precision >= s.len() => Some(0),
539        None => s.char_indices().rev().take(precision).find(|(_, d)| *d != '0').map(|(i, _)| i),
540    }
541}
542
543const INPUT_STRING_LEN_LIMIT: usize = 50;
544
545/// Parse decimal string in the given denomination into a satoshi value and a
546/// bool indicator for a negative amount.
547fn parse_signed_to_satoshi(
548    mut s: &str,
549    denom: Denomination,
550) -> Result<(bool, u64), InnerParseError> {
551    if s.is_empty() {
552        return Err(InnerParseError::MissingDigits(MissingDigitsError {
553            kind: MissingDigitsKind::Empty,
554        }));
555    }
556    if s.len() > INPUT_STRING_LEN_LIMIT {
557        return Err(InnerParseError::InputTooLarge(s.len()));
558    }
559
560    let is_negative = s.starts_with('-');
561    if is_negative {
562        if s.len() == 1 {
563            return Err(InnerParseError::MissingDigits(MissingDigitsError {
564                kind: MissingDigitsKind::OnlyMinusSign,
565            }));
566        }
567        s = &s[1..];
568    }
569
570    let max_decimals = {
571        // The difference in precision between native (satoshi)
572        // and desired denomination.
573        let precision_diff = -denom.precision();
574        if precision_diff <= 0 {
575            // If precision diff is negative, this means we are parsing
576            // into a less precise amount. That is not allowed unless
577            // there are no decimals and the last digits are zeroes as
578            // many as the difference in precision.
579            let last_n = precision_diff.unsigned_abs().into();
580            if let Some(position) = is_too_precise(s, last_n) {
581                match s.parse::<i64>() {
582                    Ok(0) => return Ok((is_negative, 0)),
583                    _ =>
584                        return Err(InnerParseError::TooPrecise(TooPreciseError {
585                            position: position + is_negative as usize,
586                        })),
587                }
588            }
589            s = &s[0..s.find('.').unwrap_or(s.len()) - last_n];
590            0
591        } else {
592            precision_diff
593        }
594    };
595
596    let mut decimals = None;
597    let mut value: u64 = 0; // as satoshis
598    for (i, c) in s.char_indices() {
599        match c {
600            '0'..='9' => {
601                // Do `value = 10 * value + digit`, catching overflows.
602                match 10_u64.checked_mul(value) {
603                    None => return Err(InnerParseError::Overflow { is_negative }),
604                    Some(val) => match val.checked_add((c as u8 - b'0') as u64) {
605                        None => return Err(InnerParseError::Overflow { is_negative }),
606                        Some(val) => value = val,
607                    },
608                }
609                // Increment the decimal digit counter if past decimal.
610                decimals = match decimals {
611                    None => None,
612                    Some(d) if d < max_decimals => Some(d + 1),
613                    _ =>
614                        return Err(InnerParseError::TooPrecise(TooPreciseError {
615                            position: i + is_negative as usize,
616                        })),
617                };
618            }
619            '.' => match decimals {
620                None if max_decimals <= 0 => break,
621                None => decimals = Some(0),
622                // Double decimal dot.
623                _ =>
624                    return Err(InnerParseError::InvalidCharacter(InvalidCharacterError {
625                        invalid_char: '.',
626                        position: i + is_negative as usize,
627                    })),
628            },
629            c =>
630                return Err(InnerParseError::InvalidCharacter(InvalidCharacterError {
631                    invalid_char: c,
632                    position: i + is_negative as usize,
633                })),
634        }
635    }
636
637    // Decimally shift left by `max_decimals - decimals`.
638    let scale_factor = max_decimals - decimals.unwrap_or(0);
639    for _ in 0..scale_factor {
640        value = match 10_u64.checked_mul(value) {
641            Some(v) => v,
642            None => return Err(InnerParseError::Overflow { is_negative }),
643        };
644    }
645
646    Ok((is_negative, value))
647}
648
649enum InnerParseError {
650    Overflow { is_negative: bool },
651    TooPrecise(TooPreciseError),
652    MissingDigits(MissingDigitsError),
653    InputTooLarge(usize),
654    InvalidCharacter(InvalidCharacterError),
655}
656
657impl From<Infallible> for InnerParseError {
658    fn from(never: Infallible) -> Self { match never {} }
659}
660
661impl InnerParseError {
662    fn convert(self, is_signed: bool) -> ParseAmountError {
663        match self {
664            Self::Overflow { is_negative } =>
665                OutOfRangeError { is_signed, is_greater_than_max: !is_negative }.into(),
666            Self::TooPrecise(error) => ParseAmountError::TooPrecise(error),
667            Self::MissingDigits(error) => ParseAmountError::MissingDigits(error),
668            Self::InputTooLarge(len) => ParseAmountError::InputTooLarge(InputTooLargeError { len }),
669            Self::InvalidCharacter(error) => ParseAmountError::InvalidCharacter(error),
670        }
671    }
672}
673
674fn split_amount_and_denomination(s: &str) -> Result<(&str, Denomination), ParseError> {
675    let (i, j) = if let Some(i) = s.find(' ') {
676        (i, i + 1)
677    } else {
678        let i = s
679            .find(|c: char| c.is_alphabetic())
680            .ok_or(ParseError::MissingDenomination(MissingDenominationError))?;
681        (i, i)
682    };
683    Ok((&s[..i], s[j..].parse()?))
684}
685
686/// Options given by `fmt::Formatter`
687struct FormatOptions {
688    fill: char,
689    align: Option<fmt::Alignment>,
690    width: Option<usize>,
691    precision: Option<usize>,
692    sign_plus: bool,
693    sign_aware_zero_pad: bool,
694}
695
696impl FormatOptions {
697    fn from_formatter(f: &fmt::Formatter) -> Self {
698        FormatOptions {
699            fill: f.fill(),
700            align: f.align(),
701            width: f.width(),
702            precision: f.precision(),
703            sign_plus: f.sign_plus(),
704            sign_aware_zero_pad: f.sign_aware_zero_pad(),
705        }
706    }
707}
708
709impl Default for FormatOptions {
710    fn default() -> Self {
711        FormatOptions {
712            fill: ' ',
713            align: None,
714            width: None,
715            precision: None,
716            sign_plus: false,
717            sign_aware_zero_pad: false,
718        }
719    }
720}
721
722fn dec_width(mut num: u64) -> usize {
723    let mut width = 1;
724    loop {
725        num /= 10;
726        if num == 0 {
727            break;
728        }
729        width += 1;
730    }
731    width
732}
733
734fn repeat_char(f: &mut dyn fmt::Write, c: char, count: usize) -> fmt::Result {
735    for _ in 0..count {
736        f.write_char(c)?;
737    }
738    Ok(())
739}
740
741/// Format the given satoshi amount in the given denomination.
742fn fmt_satoshi_in(
743    satoshi: u64,
744    negative: bool,
745    f: &mut dyn fmt::Write,
746    denom: Denomination,
747    show_denom: bool,
748    options: FormatOptions,
749) -> fmt::Result {
750    let precision = denom.precision();
751    // First we normalize the number:
752    // {num_before_decimal_point}{:0exp}{"." if nb_decimals > 0}{:0nb_decimals}{num_after_decimal_point}{:0trailing_decimal_zeros}
753    let mut num_after_decimal_point = 0;
754    let mut norm_nb_decimals = 0;
755    let mut num_before_decimal_point = satoshi;
756    let trailing_decimal_zeros;
757    let mut exp = 0;
758    match precision.cmp(&0) {
759        // We add the number of zeroes to the end
760        Ordering::Greater => {
761            if satoshi > 0 {
762                exp = precision as usize;
763            }
764            trailing_decimal_zeros = options.precision.unwrap_or(0);
765        }
766        Ordering::Less => {
767            let precision = precision.unsigned_abs();
768            let divisor = 10u64.pow(precision.into());
769            num_before_decimal_point = satoshi / divisor;
770            num_after_decimal_point = satoshi % divisor;
771            // normalize by stripping trailing zeros
772            if num_after_decimal_point == 0 {
773                norm_nb_decimals = 0;
774            } else {
775                norm_nb_decimals = usize::from(precision);
776                while num_after_decimal_point % 10 == 0 {
777                    norm_nb_decimals -= 1;
778                    num_after_decimal_point /= 10
779                }
780            }
781            // compute requested precision
782            let opt_precision = options.precision.unwrap_or(0);
783            trailing_decimal_zeros = opt_precision.saturating_sub(norm_nb_decimals);
784        }
785        Ordering::Equal => trailing_decimal_zeros = options.precision.unwrap_or(0),
786    }
787    let total_decimals = norm_nb_decimals + trailing_decimal_zeros;
788    // Compute expected width of the number
789    let mut num_width = if total_decimals > 0 {
790        // 1 for decimal point
791        1 + total_decimals
792    } else {
793        0
794    };
795    num_width += dec_width(num_before_decimal_point) + exp;
796    if options.sign_plus || negative {
797        num_width += 1;
798    }
799
800    if show_denom {
801        // + 1 for space
802        num_width += denom.as_str().len() + 1;
803    }
804
805    let width = options.width.unwrap_or(0);
806    let align = options.align.unwrap_or(fmt::Alignment::Right);
807    let (left_pad, pad_right) = match (num_width < width, options.sign_aware_zero_pad, align) {
808        (false, _, _) => (0, 0),
809        // Alignment is always right (ignored) when zero-padding
810        (true, true, _) | (true, false, fmt::Alignment::Right) => (width - num_width, 0),
811        (true, false, fmt::Alignment::Left) => (0, width - num_width),
812        // If the required padding is odd it needs to be skewed to the left
813        (true, false, fmt::Alignment::Center) =>
814            ((width - num_width) / 2, (width - num_width + 1) / 2),
815    };
816
817    if !options.sign_aware_zero_pad {
818        repeat_char(f, options.fill, left_pad)?;
819    }
820
821    if negative {
822        write!(f, "-")?;
823    } else if options.sign_plus {
824        write!(f, "+")?;
825    }
826
827    if options.sign_aware_zero_pad {
828        repeat_char(f, '0', left_pad)?;
829    }
830
831    write!(f, "{}", num_before_decimal_point)?;
832
833    repeat_char(f, '0', exp)?;
834
835    if total_decimals > 0 {
836        write!(f, ".")?;
837    }
838    if norm_nb_decimals > 0 {
839        write!(f, "{:0width$}", num_after_decimal_point, width = norm_nb_decimals)?;
840    }
841    repeat_char(f, '0', trailing_decimal_zeros)?;
842
843    if show_denom {
844        write!(f, " {}", denom.as_str())?;
845    }
846
847    repeat_char(f, options.fill, pad_right)?;
848    Ok(())
849}
850
851/// Amount
852///
853/// The [Amount] type can be used to express Bitcoin amounts that support
854/// arithmetic and conversion to various denominations.
855///
856///
857/// Warning!
858///
859/// This type implements several arithmetic operations from [core::ops].
860/// To prevent errors due to overflow or underflow when using these operations,
861/// it is advised to instead use the checked arithmetic methods whose names
862/// start with `checked_`.  The operations from [core::ops] that [Amount]
863/// implements will panic when overflow or underflow occurs.  Also note that
864/// since the internal representation of amounts is unsigned, subtracting below
865/// zero is considered an underflow and will cause a panic if you're not using
866/// the checked arithmetic methods.
867///
868#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
869#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
870pub struct Amount(u64);
871
872impl Amount {
873    /// The zero amount.
874    pub const ZERO: Amount = Amount(0);
875    /// Exactly one satoshi.
876    pub const ONE_SAT: Amount = Amount(1);
877    /// Exactly one bitcoin.
878    pub const ONE_BTC: Amount = Self::from_int_btc(1);
879    /// The maximum value allowed as an amount. Useful for sanity checking.
880    pub const MAX_MONEY: Amount = Self::from_int_btc(21_000_000);
881    /// The minimum value of an amount.
882    pub const MIN: Amount = Amount::ZERO;
883    /// The maximum value of an amount.
884    pub const MAX: Amount = Amount(u64::MAX);
885    /// The number of bytes that an amount contributes to the size of a transaction.
886    pub const SIZE: usize = 8; // Serialized length of a u64.
887
888    /// Create an [Amount] with satoshi precision and the given number of satoshis.
889    pub const fn from_sat(satoshi: u64) -> Amount { Amount(satoshi) }
890
891    /// Gets the number of satoshis in this [`Amount`].
892    pub fn to_sat(self) -> u64 { self.0 }
893
894    /// Convert from a value expressing bitcoins to an [Amount].
895    #[cfg(feature = "alloc")]
896    pub fn from_btc(btc: f64) -> Result<Amount, ParseAmountError> {
897        Amount::from_float_in(btc, Denomination::Bitcoin)
898    }
899
900    /// Convert from a value expressing integer values of bitcoins to an [Amount]
901    /// in const context.
902    ///
903    /// ## Panics
904    ///
905    /// The function panics if the argument multiplied by the number of sats
906    /// per bitcoin overflows a u64 type.
907    pub const fn from_int_btc(btc: u64) -> Amount {
908        match btc.checked_mul(100_000_000) {
909            Some(amount) => Amount::from_sat(amount),
910            None => {
911                // When MSRV is 1.57+ we can use `panic!()`.
912                #[allow(unconditional_panic)]
913                #[allow(clippy::let_unit_value)]
914                #[allow(clippy::out_of_bounds_indexing)]
915                let _int_overflow_converting_btc_to_sats = [(); 0][1];
916                Amount(0)
917            }
918        }
919    }
920
921    /// Parse a decimal string as a value in the given denomination.
922    ///
923    /// Note: This only parses the value string.  If you want to parse a value
924    /// with denomination, use [FromStr].
925    pub fn from_str_in(s: &str, denom: Denomination) -> Result<Amount, ParseAmountError> {
926        let (negative, satoshi) =
927            parse_signed_to_satoshi(s, denom).map_err(|error| error.convert(false))?;
928        if negative {
929            return Err(ParseAmountError::OutOfRange(OutOfRangeError::negative()));
930        }
931        Ok(Amount::from_sat(satoshi))
932    }
933
934    /// Parses amounts with denomination suffix like they are produced with
935    /// [Self::to_string_with_denomination] or with [fmt::Display].
936    /// If you want to parse only the amount without the denomination,
937    /// use [Self::from_str_in].
938    pub fn from_str_with_denomination(s: &str) -> Result<Amount, ParseError> {
939        let (amt, denom) = split_amount_and_denomination(s)?;
940        Amount::from_str_in(amt, denom).map_err(Into::into)
941    }
942
943    /// Express this [Amount] as a floating-point value in the given denomination.
944    ///
945    /// Please be aware of the risk of using floating-point numbers.
946    #[cfg(feature = "alloc")]
947    pub fn to_float_in(self, denom: Denomination) -> f64 {
948        f64::from_str(&self.to_string_in(denom)).unwrap()
949    }
950
951    /// Express this [`Amount`] as a floating-point value in Bitcoin.
952    ///
953    /// Please be aware of the risk of using floating-point numbers.
954    ///
955    /// # Examples
956    /// ```
957    /// # use bitcoin_units::amount::{Amount, Denomination};
958    /// let amount = Amount::from_sat(100_000);
959    /// assert_eq!(amount.to_btc(), amount.to_float_in(Denomination::Bitcoin))
960    /// ```
961    #[cfg(feature = "alloc")]
962    pub fn to_btc(self) -> f64 { self.to_float_in(Denomination::Bitcoin) }
963
964    /// Convert this [Amount] in floating-point notation with a given
965    /// denomination.
966    /// Can return error if the amount is too big, too precise or negative.
967    ///
968    /// Please be aware of the risk of using floating-point numbers.
969    #[cfg(feature = "alloc")]
970    pub fn from_float_in(value: f64, denom: Denomination) -> Result<Amount, ParseAmountError> {
971        if value < 0.0 {
972            return Err(OutOfRangeError::negative().into());
973        }
974        // This is inefficient, but the safest way to deal with this. The parsing logic is safe.
975        // Any performance-critical application should not be dealing with floats.
976        Amount::from_str_in(&value.to_string(), denom)
977    }
978
979    /// Create an object that implements [`fmt::Display`] using specified denomination.
980    pub fn display_in(self, denomination: Denomination) -> Display {
981        Display {
982            sats_abs: self.to_sat(),
983            is_negative: false,
984            style: DisplayStyle::FixedDenomination { denomination, show_denomination: false },
985        }
986    }
987
988    /// Create an object that implements [`fmt::Display`] dynamically selecting denomination.
989    ///
990    /// This will use BTC for values greater than or equal to 1 BTC and satoshis otherwise. To
991    /// avoid confusion the denomination is always shown.
992    pub fn display_dynamic(self) -> Display {
993        Display {
994            sats_abs: self.to_sat(),
995            is_negative: false,
996            style: DisplayStyle::DynamicDenomination,
997        }
998    }
999
1000    /// Format the value of this [Amount] in the given denomination.
1001    ///
1002    /// Does not include the denomination.
1003    #[rustfmt::skip]
1004    pub fn fmt_value_in(self, f: &mut dyn fmt::Write, denom: Denomination) -> fmt::Result {
1005        fmt_satoshi_in(self.to_sat(), false, f, denom, false, FormatOptions::default())
1006    }
1007
1008    /// Get a string number of this [Amount] in the given denomination.
1009    ///
1010    /// Does not include the denomination.
1011    #[cfg(feature = "alloc")]
1012    pub fn to_string_in(self, denom: Denomination) -> String {
1013        let mut buf = String::new();
1014        self.fmt_value_in(&mut buf, denom).unwrap();
1015        buf
1016    }
1017
1018    /// Get a formatted string of this [Amount] in the given denomination,
1019    /// suffixed with the abbreviation for the denomination.
1020    #[cfg(feature = "alloc")]
1021    pub fn to_string_with_denomination(self, denom: Denomination) -> String {
1022        let mut buf = String::new();
1023        self.fmt_value_in(&mut buf, denom).unwrap();
1024        write!(buf, " {}", denom).unwrap();
1025        buf
1026    }
1027
1028    // Some arithmetic that doesn't fit in `core::ops` traits.
1029
1030    /// Checked addition.
1031    ///
1032    /// Returns [None] if overflow occurred.
1033    pub fn checked_add(self, rhs: Amount) -> Option<Amount> {
1034        self.0.checked_add(rhs.0).map(Amount)
1035    }
1036
1037    /// Checked subtraction.
1038    ///
1039    /// Returns [None] if overflow occurred.
1040    pub fn checked_sub(self, rhs: Amount) -> Option<Amount> {
1041        self.0.checked_sub(rhs.0).map(Amount)
1042    }
1043
1044    /// Checked multiplication.
1045    ///
1046    /// Returns [None] if overflow occurred.
1047    pub fn checked_mul(self, rhs: u64) -> Option<Amount> { self.0.checked_mul(rhs).map(Amount) }
1048
1049    /// Checked integer division.
1050    ///
1051    /// Be aware that integer division loses the remainder if no exact division
1052    /// can be made.
1053    /// Returns [None] if overflow occurred.
1054    pub fn checked_div(self, rhs: u64) -> Option<Amount> { self.0.checked_div(rhs).map(Amount) }
1055
1056    /// Checked weight ceiling division.
1057    ///
1058    /// Be aware that integer division loses the remainder if no exact division
1059    /// can be made.  This method rounds up ensuring the transaction fee-rate is
1060    /// sufficient.  See also [`Amount::div_by_weight_floor`].
1061    ///
1062    /// [`None`] is returned if an overflow occurred.
1063    #[cfg(feature = "alloc")]
1064    pub fn div_by_weight_ceil(self, rhs: Weight) -> Option<FeeRate> {
1065        let sats = self.0.checked_mul(1000)?;
1066        let wu = rhs.to_wu();
1067
1068        let fee_rate = sats.checked_add(wu.checked_sub(1)?)?.checked_div(wu)?;
1069        Some(FeeRate::from_sat_per_kwu(fee_rate))
1070    }
1071
1072    /// Checked weight floor division.
1073    ///
1074    /// Be aware that integer division loses the remainder if no exact division
1075    /// can be made.  See also [`Amount::div_by_weight_ceil`].
1076    ///
1077    /// Returns [`None`] if overflow occurred.
1078    #[cfg(feature = "alloc")]
1079    pub fn div_by_weight_floor(self, rhs: Weight) -> Option<FeeRate> {
1080        let fee_rate = self.0.checked_mul(1_000)?.checked_div(rhs.to_wu())?;
1081        Some(FeeRate::from_sat_per_kwu(fee_rate))
1082    }
1083
1084    /// Checked remainder.
1085    ///
1086    /// Returns [None] if overflow occurred.
1087    pub fn checked_rem(self, rhs: u64) -> Option<Amount> { self.0.checked_rem(rhs).map(Amount) }
1088
1089    /// Unchecked addition.
1090    ///
1091    /// Computes `self + rhs`.  Panics in debug mode, wraps in release mode.
1092    pub fn unchecked_add(self, rhs: Amount) -> Amount { Self(self.0 + rhs.0) }
1093
1094    /// Unchecked subtraction.
1095    ///
1096    /// Computes `self - rhs`.  Panics in debug mode, wraps in release mode.
1097    pub fn unchecked_sub(self, rhs: Amount) -> Amount { Self(self.0 - rhs.0) }
1098
1099    /// Convert to a signed amount.
1100    pub fn to_signed(self) -> Result<SignedAmount, OutOfRangeError> {
1101        if self.to_sat() > SignedAmount::MAX.to_sat() as u64 {
1102            Err(OutOfRangeError::too_big(true))
1103        } else {
1104            Ok(SignedAmount::from_sat(self.to_sat() as i64))
1105        }
1106    }
1107}
1108
1109impl default::Default for Amount {
1110    fn default() -> Self { Amount::ZERO }
1111}
1112
1113impl fmt::Debug for Amount {
1114    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{} SAT", self.to_sat()) }
1115}
1116
1117// No one should depend on a binding contract for Display for this type.
1118// Just using Bitcoin denominated string.
1119impl fmt::Display for Amount {
1120    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1121        let satoshis = self.to_sat();
1122        let denomination = Denomination::Bitcoin;
1123        let mut format_options = FormatOptions::from_formatter(f);
1124
1125        if f.precision().is_none() && satoshis.rem_euclid(Amount::ONE_BTC.to_sat()) != 0 {
1126            format_options.precision = Some(8);
1127        }
1128
1129        fmt_satoshi_in(satoshis, false, f, denomination, true, format_options)
1130    }
1131}
1132
1133impl ops::Add for Amount {
1134    type Output = Amount;
1135
1136    fn add(self, rhs: Amount) -> Self::Output {
1137        self.checked_add(rhs).expect("Amount addition error")
1138    }
1139}
1140
1141impl ops::AddAssign for Amount {
1142    fn add_assign(&mut self, other: Amount) { *self = *self + other }
1143}
1144
1145impl ops::Sub for Amount {
1146    type Output = Amount;
1147
1148    fn sub(self, rhs: Amount) -> Self::Output {
1149        self.checked_sub(rhs).expect("Amount subtraction error")
1150    }
1151}
1152
1153impl ops::SubAssign for Amount {
1154    fn sub_assign(&mut self, other: Amount) { *self = *self - other }
1155}
1156
1157impl ops::Rem<u64> for Amount {
1158    type Output = Amount;
1159
1160    fn rem(self, modulus: u64) -> Self {
1161        self.checked_rem(modulus).expect("Amount remainder error")
1162    }
1163}
1164
1165impl ops::RemAssign<u64> for Amount {
1166    fn rem_assign(&mut self, modulus: u64) { *self = *self % modulus }
1167}
1168
1169impl ops::Mul<u64> for Amount {
1170    type Output = Amount;
1171
1172    fn mul(self, rhs: u64) -> Self::Output {
1173        self.checked_mul(rhs).expect("Amount multiplication error")
1174    }
1175}
1176
1177impl ops::MulAssign<u64> for Amount {
1178    fn mul_assign(&mut self, rhs: u64) { *self = *self * rhs }
1179}
1180
1181impl ops::Div<u64> for Amount {
1182    type Output = Amount;
1183
1184    fn div(self, rhs: u64) -> Self::Output { self.checked_div(rhs).expect("Amount division error") }
1185}
1186
1187impl ops::DivAssign<u64> for Amount {
1188    fn div_assign(&mut self, rhs: u64) { *self = *self / rhs }
1189}
1190
1191impl FromStr for Amount {
1192    type Err = ParseError;
1193
1194    fn from_str(s: &str) -> Result<Self, Self::Err> { Amount::from_str_with_denomination(s) }
1195}
1196
1197impl TryFrom<SignedAmount> for Amount {
1198    type Error = OutOfRangeError;
1199
1200    fn try_from(value: SignedAmount) -> Result<Self, Self::Error> { value.to_unsigned() }
1201}
1202
1203impl core::iter::Sum for Amount {
1204    fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
1205        let sats: u64 = iter.map(|amt| amt.0).sum();
1206        Amount::from_sat(sats)
1207    }
1208}
1209
1210/// A helper/builder that displays amount with specified settings.
1211///
1212/// This provides richer interface than `fmt::Formatter`:
1213///
1214/// * Ability to select denomination
1215/// * Show or hide denomination
1216/// * Dynamically-selected denomination - show in sats if less than 1 BTC.
1217///
1218/// However this can still be combined with `fmt::Formatter` options to precisely control zeros,
1219/// padding, alignment... The formatting works like floats from `core` but note that precision will
1220/// **never** be lossy - that means no rounding.
1221///
1222/// See [`Amount::display_in`] and [`Amount::display_dynamic`] on how to construct this.
1223#[derive(Debug, Clone)]
1224pub struct Display {
1225    /// Absolute value of satoshis to display (sign is below)
1226    sats_abs: u64,
1227    /// The sign
1228    is_negative: bool,
1229    /// How to display the value
1230    style: DisplayStyle,
1231}
1232
1233impl Display {
1234    /// Makes subsequent calls to `Display::fmt` display denomination.
1235    pub fn show_denomination(mut self) -> Self {
1236        match &mut self.style {
1237            DisplayStyle::FixedDenomination { show_denomination, .. } => *show_denomination = true,
1238            // No-op because dynamic denomination is always shown
1239            DisplayStyle::DynamicDenomination => (),
1240        }
1241        self
1242    }
1243}
1244
1245impl fmt::Display for Display {
1246    #[rustfmt::skip]
1247    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1248        let format_options = FormatOptions::from_formatter(f);
1249        match &self.style {
1250            DisplayStyle::FixedDenomination { show_denomination, denomination } => {
1251                fmt_satoshi_in(self.sats_abs, self.is_negative, f, *denomination, *show_denomination, format_options)
1252            },
1253            DisplayStyle::DynamicDenomination if self.sats_abs >= Amount::ONE_BTC.to_sat() => {
1254                fmt_satoshi_in(self.sats_abs, self.is_negative, f, Denomination::Bitcoin, true, format_options)
1255            },
1256            DisplayStyle::DynamicDenomination => {
1257                fmt_satoshi_in(self.sats_abs, self.is_negative, f, Denomination::Satoshi, true, format_options)
1258            },
1259        }
1260    }
1261}
1262
1263#[derive(Clone, Debug)]
1264enum DisplayStyle {
1265    FixedDenomination { denomination: Denomination, show_denomination: bool },
1266    DynamicDenomination,
1267}
1268
1269/// SignedAmount
1270///
1271/// The [SignedAmount] type can be used to express Bitcoin amounts that support
1272/// arithmetic and conversion to various denominations.
1273///
1274///
1275/// Warning!
1276///
1277/// This type implements several arithmetic operations from [core::ops].
1278/// To prevent errors due to overflow or underflow when using these operations,
1279/// it is advised to instead use the checked arithmetic methods whose names
1280/// start with `checked_`.  The operations from [core::ops] that [Amount]
1281/// implements will panic when overflow or underflow occurs.
1282///
1283#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
1284pub struct SignedAmount(i64);
1285
1286impl SignedAmount {
1287    /// The zero amount.
1288    pub const ZERO: SignedAmount = SignedAmount(0);
1289    /// Exactly one satoshi.
1290    pub const ONE_SAT: SignedAmount = SignedAmount(1);
1291    /// Exactly one bitcoin.
1292    pub const ONE_BTC: SignedAmount = SignedAmount(100_000_000);
1293    /// The maximum value allowed as an amount. Useful for sanity checking.
1294    pub const MAX_MONEY: SignedAmount = SignedAmount(21_000_000 * 100_000_000);
1295    /// The minimum value of an amount.
1296    pub const MIN: SignedAmount = SignedAmount(i64::MIN);
1297    /// The maximum value of an amount.
1298    pub const MAX: SignedAmount = SignedAmount(i64::MAX);
1299
1300    /// Create an [SignedAmount] with satoshi precision and the given number of satoshis.
1301    pub const fn from_sat(satoshi: i64) -> SignedAmount { SignedAmount(satoshi) }
1302
1303    /// Gets the number of satoshis in this [`SignedAmount`].
1304    pub fn to_sat(self) -> i64 { self.0 }
1305
1306    /// Convert from a value expressing bitcoins to an [SignedAmount].
1307    #[cfg(feature = "alloc")]
1308    pub fn from_btc(btc: f64) -> Result<SignedAmount, ParseAmountError> {
1309        SignedAmount::from_float_in(btc, Denomination::Bitcoin)
1310    }
1311
1312    /// Parse a decimal string as a value in the given denomination.
1313    ///
1314    /// Note: This only parses the value string.  If you want to parse a value
1315    /// with denomination, use [FromStr].
1316    pub fn from_str_in(s: &str, denom: Denomination) -> Result<SignedAmount, ParseAmountError> {
1317        match parse_signed_to_satoshi(s, denom).map_err(|error| error.convert(true))? {
1318            // (negative, amount)
1319            (false, sat) if sat > i64::MAX as u64 =>
1320                Err(ParseAmountError::OutOfRange(OutOfRangeError::too_big(true))),
1321            (false, sat) => Ok(SignedAmount(sat as i64)),
1322            (true, sat) if sat == i64::MIN.unsigned_abs() => Ok(SignedAmount(i64::MIN)),
1323            (true, sat) if sat > i64::MIN.unsigned_abs() =>
1324                Err(ParseAmountError::OutOfRange(OutOfRangeError::too_small())),
1325            (true, sat) => Ok(SignedAmount(-(sat as i64))),
1326        }
1327    }
1328
1329    /// Parses amounts with denomination suffix like they are produced with
1330    /// [Self::to_string_with_denomination] or with [fmt::Display].
1331    /// If you want to parse only the amount without the denomination,
1332    /// use [Self::from_str_in].
1333    pub fn from_str_with_denomination(s: &str) -> Result<SignedAmount, ParseError> {
1334        let (amt, denom) = split_amount_and_denomination(s)?;
1335        SignedAmount::from_str_in(amt, denom).map_err(Into::into)
1336    }
1337
1338    /// Express this [SignedAmount] as a floating-point value in the given denomination.
1339    ///
1340    /// Please be aware of the risk of using floating-point numbers.
1341    #[cfg(feature = "alloc")]
1342    pub fn to_float_in(self, denom: Denomination) -> f64 {
1343        f64::from_str(&self.to_string_in(denom)).unwrap()
1344    }
1345
1346    /// Express this [`SignedAmount`] as a floating-point value in Bitcoin.
1347    ///
1348    /// Equivalent to `to_float_in(Denomination::Bitcoin)`.
1349    ///
1350    /// Please be aware of the risk of using floating-point numbers.
1351    #[cfg(feature = "alloc")]
1352    pub fn to_btc(self) -> f64 { self.to_float_in(Denomination::Bitcoin) }
1353
1354    /// Convert this [SignedAmount] in floating-point notation with a given
1355    /// denomination.
1356    /// Can return error if the amount is too big, too precise or negative.
1357    ///
1358    /// Please be aware of the risk of using floating-point numbers.
1359    #[cfg(feature = "alloc")]
1360    pub fn from_float_in(
1361        value: f64,
1362        denom: Denomination,
1363    ) -> Result<SignedAmount, ParseAmountError> {
1364        // This is inefficient, but the safest way to deal with this. The parsing logic is safe.
1365        // Any performance-critical application should not be dealing with floats.
1366        SignedAmount::from_str_in(&value.to_string(), denom)
1367    }
1368
1369    /// Create an object that implements [`fmt::Display`] using specified denomination.
1370    pub fn display_in(self, denomination: Denomination) -> Display {
1371        Display {
1372            sats_abs: self.unsigned_abs().to_sat(),
1373            is_negative: self.is_negative(),
1374            style: DisplayStyle::FixedDenomination { denomination, show_denomination: false },
1375        }
1376    }
1377
1378    /// Create an object that implements [`fmt::Display`] dynamically selecting denomination.
1379    ///
1380    /// This will use BTC for values greater than or equal to 1 BTC and satoshis otherwise. To
1381    /// avoid confusion the denomination is always shown.
1382    pub fn display_dynamic(self) -> Display {
1383        Display {
1384            sats_abs: self.unsigned_abs().to_sat(),
1385            is_negative: self.is_negative(),
1386            style: DisplayStyle::DynamicDenomination,
1387        }
1388    }
1389
1390    /// Format the value of this [SignedAmount] in the given denomination.
1391    ///
1392    /// Does not include the denomination.
1393    #[rustfmt::skip]
1394    pub fn fmt_value_in(self, f: &mut dyn fmt::Write, denom: Denomination) -> fmt::Result {
1395        fmt_satoshi_in(self.unsigned_abs().to_sat(), self.is_negative(), f, denom, false, FormatOptions::default())
1396    }
1397
1398    /// Get a string number of this [SignedAmount] in the given denomination.
1399    ///
1400    /// Does not include the denomination.
1401    #[cfg(feature = "alloc")]
1402    pub fn to_string_in(self, denom: Denomination) -> String {
1403        let mut buf = String::new();
1404        self.fmt_value_in(&mut buf, denom).unwrap();
1405        buf
1406    }
1407
1408    /// Get a formatted string of this [SignedAmount] in the given denomination,
1409    /// suffixed with the abbreviation for the denomination.
1410    #[cfg(feature = "alloc")]
1411    pub fn to_string_with_denomination(self, denom: Denomination) -> String {
1412        let mut buf = String::new();
1413        self.fmt_value_in(&mut buf, denom).unwrap();
1414        write!(buf, " {}", denom).unwrap();
1415        buf
1416    }
1417
1418    // Some arithmetic that doesn't fit in `core::ops` traits.
1419
1420    /// Get the absolute value of this [SignedAmount].
1421    pub fn abs(self) -> SignedAmount { SignedAmount(self.0.abs()) }
1422
1423    /// Get the absolute value of this [SignedAmount] returning `Amount`.
1424    pub fn unsigned_abs(self) -> Amount { Amount(self.0.unsigned_abs()) }
1425
1426    /// Returns a number representing sign of this [SignedAmount].
1427    ///
1428    /// - `0` if the amount is zero
1429    /// - `1` if the amount is positive
1430    /// - `-1` if the amount is negative
1431    pub fn signum(self) -> i64 { self.0.signum() }
1432
1433    /// Returns `true` if this [SignedAmount] is positive and `false` if
1434    /// this [SignedAmount] is zero or negative.
1435    pub fn is_positive(self) -> bool { self.0.is_positive() }
1436
1437    /// Returns `true` if this [SignedAmount] is negative and `false` if
1438    /// this [SignedAmount] is zero or positive.
1439    pub fn is_negative(self) -> bool { self.0.is_negative() }
1440
1441    /// Get the absolute value of this [SignedAmount].
1442    /// Returns [None] if overflow occurred. (`self == MIN`)
1443    pub fn checked_abs(self) -> Option<SignedAmount> { self.0.checked_abs().map(SignedAmount) }
1444
1445    /// Checked addition.
1446    /// Returns [None] if overflow occurred.
1447    pub fn checked_add(self, rhs: SignedAmount) -> Option<SignedAmount> {
1448        self.0.checked_add(rhs.0).map(SignedAmount)
1449    }
1450
1451    /// Checked subtraction.
1452    /// Returns [None] if overflow occurred.
1453    pub fn checked_sub(self, rhs: SignedAmount) -> Option<SignedAmount> {
1454        self.0.checked_sub(rhs.0).map(SignedAmount)
1455    }
1456
1457    /// Checked multiplication.
1458    /// Returns [None] if overflow occurred.
1459    pub fn checked_mul(self, rhs: i64) -> Option<SignedAmount> {
1460        self.0.checked_mul(rhs).map(SignedAmount)
1461    }
1462
1463    /// Checked integer division.
1464    /// Be aware that integer division loses the remainder if no exact division
1465    /// can be made.
1466    /// Returns [None] if overflow occurred.
1467    pub fn checked_div(self, rhs: i64) -> Option<SignedAmount> {
1468        self.0.checked_div(rhs).map(SignedAmount)
1469    }
1470
1471    /// Checked remainder.
1472    /// Returns [None] if overflow occurred.
1473    pub fn checked_rem(self, rhs: i64) -> Option<SignedAmount> {
1474        self.0.checked_rem(rhs).map(SignedAmount)
1475    }
1476
1477    /// Unchecked addition.
1478    ///
1479    /// Computes `self + rhs`.  Panics in debug mode, wraps in release mode.
1480    pub fn unchecked_add(self, rhs: SignedAmount) -> SignedAmount { Self(self.0 + rhs.0) }
1481
1482    /// Unchecked subtraction.
1483    ///
1484    /// Computes `self - rhs`.  Panics in debug mode, wraps in release mode.
1485    pub fn unchecked_sub(self, rhs: SignedAmount) -> SignedAmount { Self(self.0 - rhs.0) }
1486
1487    /// Subtraction that doesn't allow negative [SignedAmount]s.
1488    /// Returns [None] if either [self], `rhs` or the result is strictly negative.
1489    pub fn positive_sub(self, rhs: SignedAmount) -> Option<SignedAmount> {
1490        if self.is_negative() || rhs.is_negative() || rhs > self {
1491            None
1492        } else {
1493            self.checked_sub(rhs)
1494        }
1495    }
1496
1497    /// Convert to an unsigned amount.
1498    pub fn to_unsigned(self) -> Result<Amount, OutOfRangeError> {
1499        if self.is_negative() {
1500            Err(OutOfRangeError::negative())
1501        } else {
1502            Ok(Amount::from_sat(self.to_sat() as u64))
1503        }
1504    }
1505}
1506
1507impl default::Default for SignedAmount {
1508    fn default() -> Self { SignedAmount::ZERO }
1509}
1510
1511impl fmt::Debug for SignedAmount {
1512    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1513        write!(f, "SignedAmount({} SAT)", self.to_sat())
1514    }
1515}
1516
1517// No one should depend on a binding contract for Display for this type.
1518// Just using Bitcoin denominated string.
1519impl fmt::Display for SignedAmount {
1520    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1521        self.fmt_value_in(f, Denomination::Bitcoin)?;
1522        write!(f, " {}", Denomination::Bitcoin)
1523    }
1524}
1525
1526impl ops::Add for SignedAmount {
1527    type Output = SignedAmount;
1528
1529    fn add(self, rhs: SignedAmount) -> Self::Output {
1530        self.checked_add(rhs).expect("SignedAmount addition error")
1531    }
1532}
1533
1534impl ops::AddAssign for SignedAmount {
1535    fn add_assign(&mut self, other: SignedAmount) { *self = *self + other }
1536}
1537
1538impl ops::Sub for SignedAmount {
1539    type Output = SignedAmount;
1540
1541    fn sub(self, rhs: SignedAmount) -> Self::Output {
1542        self.checked_sub(rhs).expect("SignedAmount subtraction error")
1543    }
1544}
1545
1546impl ops::SubAssign for SignedAmount {
1547    fn sub_assign(&mut self, other: SignedAmount) { *self = *self - other }
1548}
1549
1550impl ops::Rem<i64> for SignedAmount {
1551    type Output = SignedAmount;
1552
1553    fn rem(self, modulus: i64) -> Self {
1554        self.checked_rem(modulus).expect("SignedAmount remainder error")
1555    }
1556}
1557
1558impl ops::RemAssign<i64> for SignedAmount {
1559    fn rem_assign(&mut self, modulus: i64) { *self = *self % modulus }
1560}
1561
1562impl ops::Mul<i64> for SignedAmount {
1563    type Output = SignedAmount;
1564
1565    fn mul(self, rhs: i64) -> Self::Output {
1566        self.checked_mul(rhs).expect("SignedAmount multiplication error")
1567    }
1568}
1569
1570impl ops::MulAssign<i64> for SignedAmount {
1571    fn mul_assign(&mut self, rhs: i64) { *self = *self * rhs }
1572}
1573
1574impl ops::Div<i64> for SignedAmount {
1575    type Output = SignedAmount;
1576
1577    fn div(self, rhs: i64) -> Self::Output {
1578        self.checked_div(rhs).expect("SignedAmount division error")
1579    }
1580}
1581
1582impl ops::DivAssign<i64> for SignedAmount {
1583    fn div_assign(&mut self, rhs: i64) { *self = *self / rhs }
1584}
1585
1586impl ops::Neg for SignedAmount {
1587    type Output = Self;
1588
1589    fn neg(self) -> Self::Output { Self(self.0.neg()) }
1590}
1591
1592impl FromStr for SignedAmount {
1593    type Err = ParseError;
1594
1595    fn from_str(s: &str) -> Result<Self, Self::Err> { SignedAmount::from_str_with_denomination(s) }
1596}
1597
1598impl TryFrom<Amount> for SignedAmount {
1599    type Error = OutOfRangeError;
1600
1601    fn try_from(value: Amount) -> Result<Self, Self::Error> { value.to_signed() }
1602}
1603
1604impl core::iter::Sum for SignedAmount {
1605    fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
1606        let sats: i64 = iter.map(|amt| amt.0).sum();
1607        SignedAmount::from_sat(sats)
1608    }
1609}
1610
1611/// Calculate the sum over the iterator using checked arithmetic.
1612pub trait CheckedSum<R>: private::SumSeal<R> {
1613    /// Calculate the sum over the iterator using checked arithmetic. If an over or underflow would
1614    /// happen it returns `None`.
1615    fn checked_sum(self) -> Option<R>;
1616}
1617
1618impl<T> CheckedSum<Amount> for T
1619where
1620    T: Iterator<Item = Amount>,
1621{
1622    fn checked_sum(mut self) -> Option<Amount> {
1623        let first = Some(self.next().unwrap_or_default());
1624
1625        self.fold(first, |acc, item| acc.and_then(|acc| acc.checked_add(item)))
1626    }
1627}
1628
1629impl<T> CheckedSum<SignedAmount> for T
1630where
1631    T: Iterator<Item = SignedAmount>,
1632{
1633    fn checked_sum(mut self) -> Option<SignedAmount> {
1634        let first = Some(self.next().unwrap_or_default());
1635
1636        self.fold(first, |acc, item| acc.and_then(|acc| acc.checked_add(item)))
1637    }
1638}
1639
1640mod private {
1641    use super::{Amount, SignedAmount};
1642
1643    /// Used to seal the `CheckedSum` trait
1644    pub trait SumSeal<A> {}
1645
1646    impl<T> SumSeal<Amount> for T where T: Iterator<Item = Amount> {}
1647    impl<T> SumSeal<SignedAmount> for T where T: Iterator<Item = SignedAmount> {}
1648}
1649
1650#[cfg(feature = "serde")]
1651pub mod serde {
1652    // methods are implementation of a standardized serde-specific signature
1653    #![allow(missing_docs)]
1654
1655    //! This module adds serde serialization and deserialization support for Amounts.
1656    //!
1657    //! Since there is not a default way to serialize and deserialize Amounts, multiple
1658    //! ways are supported and it's up to the user to decide which serialiation to use.
1659    //! The provided modules can be used as follows:
1660    //!
1661    //! ```rust,ignore
1662    //! use serde::{Serialize, Deserialize};
1663    //! use bitcoin_units::Amount;
1664    //!
1665    //! #[derive(Serialize, Deserialize)]
1666    //! pub struct HasAmount {
1667    //!     #[serde(with = "bitcoin_units::amount::serde::as_btc")]
1668    //!     pub amount: Amount,
1669    //! }
1670    //! ```
1671
1672    use core::fmt;
1673
1674    use serde::{Deserialize, Deserializer, Serialize, Serializer};
1675
1676    #[cfg(feature = "alloc")]
1677    use super::Denomination;
1678    use super::{Amount, ParseAmountError, SignedAmount};
1679
1680    /// This trait is used only to avoid code duplication and naming collisions
1681    /// of the different serde serialization crates.
1682    pub trait SerdeAmount: Copy + Sized {
1683        fn ser_sat<S: Serializer>(self, s: S, _: private::Token) -> Result<S::Ok, S::Error>;
1684        fn des_sat<'d, D: Deserializer<'d>>(d: D, _: private::Token) -> Result<Self, D::Error>;
1685        #[cfg(feature = "alloc")]
1686        fn ser_btc<S: Serializer>(self, s: S, _: private::Token) -> Result<S::Ok, S::Error>;
1687        #[cfg(feature = "alloc")]
1688        fn des_btc<'d, D: Deserializer<'d>>(d: D, _: private::Token) -> Result<Self, D::Error>;
1689    }
1690
1691    mod private {
1692        /// Controls access to the trait methods.
1693        pub struct Token;
1694    }
1695
1696    /// This trait is only for internal Amount type serialization/deserialization
1697    pub trait SerdeAmountForOpt: Copy + Sized + SerdeAmount {
1698        fn type_prefix(_: private::Token) -> &'static str;
1699        fn ser_sat_opt<S: Serializer>(self, s: S, _: private::Token) -> Result<S::Ok, S::Error>;
1700        #[cfg(feature = "alloc")]
1701        fn ser_btc_opt<S: Serializer>(self, s: S, _: private::Token) -> Result<S::Ok, S::Error>;
1702    }
1703
1704    struct DisplayFullError(ParseAmountError);
1705
1706    #[cfg(feature = "std")]
1707    impl fmt::Display for DisplayFullError {
1708        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1709            use std::error::Error;
1710
1711            fmt::Display::fmt(&self.0, f)?;
1712            let mut source_opt = self.0.source();
1713            while let Some(source) = source_opt {
1714                write!(f, ": {}", source)?;
1715                source_opt = source.source();
1716            }
1717            Ok(())
1718        }
1719    }
1720
1721    #[cfg(not(feature = "std"))]
1722    impl fmt::Display for DisplayFullError {
1723        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(&self.0, f) }
1724    }
1725
1726    impl SerdeAmount for Amount {
1727        fn ser_sat<S: Serializer>(self, s: S, _: private::Token) -> Result<S::Ok, S::Error> {
1728            u64::serialize(&self.to_sat(), s)
1729        }
1730        fn des_sat<'d, D: Deserializer<'d>>(d: D, _: private::Token) -> Result<Self, D::Error> {
1731            Ok(Amount::from_sat(u64::deserialize(d)?))
1732        }
1733        #[cfg(feature = "alloc")]
1734        fn ser_btc<S: Serializer>(self, s: S, _: private::Token) -> Result<S::Ok, S::Error> {
1735            f64::serialize(&self.to_float_in(Denomination::Bitcoin), s)
1736        }
1737        #[cfg(feature = "alloc")]
1738        fn des_btc<'d, D: Deserializer<'d>>(d: D, _: private::Token) -> Result<Self, D::Error> {
1739            use serde::de::Error;
1740            Amount::from_btc(f64::deserialize(d)?)
1741                .map_err(DisplayFullError)
1742                .map_err(D::Error::custom)
1743        }
1744    }
1745
1746    impl SerdeAmountForOpt for Amount {
1747        fn type_prefix(_: private::Token) -> &'static str { "u" }
1748        fn ser_sat_opt<S: Serializer>(self, s: S, _: private::Token) -> Result<S::Ok, S::Error> {
1749            s.serialize_some(&self.to_sat())
1750        }
1751        #[cfg(feature = "alloc")]
1752        fn ser_btc_opt<S: Serializer>(self, s: S, _: private::Token) -> Result<S::Ok, S::Error> {
1753            s.serialize_some(&self.to_btc())
1754        }
1755    }
1756
1757    impl SerdeAmount for SignedAmount {
1758        fn ser_sat<S: Serializer>(self, s: S, _: private::Token) -> Result<S::Ok, S::Error> {
1759            i64::serialize(&self.to_sat(), s)
1760        }
1761        fn des_sat<'d, D: Deserializer<'d>>(d: D, _: private::Token) -> Result<Self, D::Error> {
1762            Ok(SignedAmount::from_sat(i64::deserialize(d)?))
1763        }
1764        #[cfg(feature = "alloc")]
1765        fn ser_btc<S: Serializer>(self, s: S, _: private::Token) -> Result<S::Ok, S::Error> {
1766            f64::serialize(&self.to_float_in(Denomination::Bitcoin), s)
1767        }
1768        #[cfg(feature = "alloc")]
1769        fn des_btc<'d, D: Deserializer<'d>>(d: D, _: private::Token) -> Result<Self, D::Error> {
1770            use serde::de::Error;
1771            SignedAmount::from_btc(f64::deserialize(d)?)
1772                .map_err(DisplayFullError)
1773                .map_err(D::Error::custom)
1774        }
1775    }
1776
1777    impl SerdeAmountForOpt for SignedAmount {
1778        fn type_prefix(_: private::Token) -> &'static str { "i" }
1779        fn ser_sat_opt<S: Serializer>(self, s: S, _: private::Token) -> Result<S::Ok, S::Error> {
1780            s.serialize_some(&self.to_sat())
1781        }
1782        #[cfg(feature = "alloc")]
1783        fn ser_btc_opt<S: Serializer>(self, s: S, _: private::Token) -> Result<S::Ok, S::Error> {
1784            s.serialize_some(&self.to_btc())
1785        }
1786    }
1787
1788    pub mod as_sat {
1789        //! Serialize and deserialize [`Amount`](crate::Amount) as real numbers denominated in satoshi.
1790        //! Use with `#[serde(with = "amount::serde::as_sat")]`.
1791        //!
1792        use serde::{Deserializer, Serializer};
1793
1794        use super::private;
1795        use crate::amount::serde::SerdeAmount;
1796
1797        pub fn serialize<A: SerdeAmount, S: Serializer>(a: &A, s: S) -> Result<S::Ok, S::Error> {
1798            a.ser_sat(s, private::Token)
1799        }
1800
1801        pub fn deserialize<'d, A: SerdeAmount, D: Deserializer<'d>>(d: D) -> Result<A, D::Error> {
1802            A::des_sat(d, private::Token)
1803        }
1804
1805        pub mod opt {
1806            //! Serialize and deserialize [`Option<Amount>`](crate::Amount) as real numbers denominated in satoshi.
1807            //! Use with `#[serde(default, with = "amount::serde::as_sat::opt")]`.
1808
1809            use core::fmt;
1810            use core::marker::PhantomData;
1811
1812            use serde::{de, Deserializer, Serializer};
1813
1814            use super::private;
1815            use crate::amount::serde::SerdeAmountForOpt;
1816
1817            pub fn serialize<A: SerdeAmountForOpt, S: Serializer>(
1818                a: &Option<A>,
1819                s: S,
1820            ) -> Result<S::Ok, S::Error> {
1821                match *a {
1822                    Some(a) => a.ser_sat_opt(s, private::Token),
1823                    None => s.serialize_none(),
1824                }
1825            }
1826
1827            pub fn deserialize<'d, A: SerdeAmountForOpt, D: Deserializer<'d>>(
1828                d: D,
1829            ) -> Result<Option<A>, D::Error> {
1830                struct VisitOptAmt<X>(PhantomData<X>);
1831
1832                impl<'de, X: SerdeAmountForOpt> de::Visitor<'de> for VisitOptAmt<X> {
1833                    type Value = Option<X>;
1834
1835                    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1836                        write!(formatter, "An Option<{}64>", X::type_prefix(private::Token))
1837                    }
1838
1839                    fn visit_none<E>(self) -> Result<Self::Value, E>
1840                    where
1841                        E: de::Error,
1842                    {
1843                        Ok(None)
1844                    }
1845                    fn visit_some<D>(self, d: D) -> Result<Self::Value, D::Error>
1846                    where
1847                        D: Deserializer<'de>,
1848                    {
1849                        Ok(Some(X::des_sat(d, private::Token)?))
1850                    }
1851                }
1852                d.deserialize_option(VisitOptAmt::<A>(PhantomData))
1853            }
1854        }
1855    }
1856
1857    #[cfg(feature = "alloc")]
1858    pub mod as_btc {
1859        //! Serialize and deserialize [`Amount`](crate::Amount) as JSON numbers denominated in BTC.
1860        //! Use with `#[serde(with = "amount::serde::as_btc")]`.
1861
1862        use serde::{Deserializer, Serializer};
1863
1864        use super::private;
1865        use crate::amount::serde::SerdeAmount;
1866
1867        pub fn serialize<A: SerdeAmount, S: Serializer>(a: &A, s: S) -> Result<S::Ok, S::Error> {
1868            a.ser_btc(s, private::Token)
1869        }
1870
1871        pub fn deserialize<'d, A: SerdeAmount, D: Deserializer<'d>>(d: D) -> Result<A, D::Error> {
1872            A::des_btc(d, private::Token)
1873        }
1874
1875        pub mod opt {
1876            //! Serialize and deserialize `Option<Amount>` as JSON numbers denominated in BTC.
1877            //! Use with `#[serde(default, with = "amount::serde::as_btc::opt")]`.
1878
1879            use core::fmt;
1880            use core::marker::PhantomData;
1881
1882            use serde::{de, Deserializer, Serializer};
1883
1884            use super::private;
1885            use crate::amount::serde::SerdeAmountForOpt;
1886
1887            pub fn serialize<A: SerdeAmountForOpt, S: Serializer>(
1888                a: &Option<A>,
1889                s: S,
1890            ) -> Result<S::Ok, S::Error> {
1891                match *a {
1892                    Some(a) => a.ser_btc_opt(s, private::Token),
1893                    None => s.serialize_none(),
1894                }
1895            }
1896
1897            pub fn deserialize<'d, A: SerdeAmountForOpt, D: Deserializer<'d>>(
1898                d: D,
1899            ) -> Result<Option<A>, D::Error> {
1900                struct VisitOptAmt<X>(PhantomData<X>);
1901
1902                impl<'de, X: SerdeAmountForOpt> de::Visitor<'de> for VisitOptAmt<X> {
1903                    type Value = Option<X>;
1904
1905                    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1906                        write!(formatter, "An Option<f64>")
1907                    }
1908
1909                    fn visit_none<E>(self) -> Result<Self::Value, E>
1910                    where
1911                        E: de::Error,
1912                    {
1913                        Ok(None)
1914                    }
1915                    fn visit_some<D>(self, d: D) -> Result<Self::Value, D::Error>
1916                    where
1917                        D: Deserializer<'de>,
1918                    {
1919                        Ok(Some(X::des_btc(d, private::Token)?))
1920                    }
1921                }
1922                d.deserialize_option(VisitOptAmt::<A>(PhantomData))
1923            }
1924        }
1925    }
1926}
1927
1928#[cfg(feature = "arbitrary")]
1929impl<'a> Arbitrary<'a> for Amount {
1930    fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
1931        let a = u64::arbitrary(u)?;
1932        Ok(Amount(a))
1933    }
1934}
1935
1936#[cfg(feature = "arbitrary")]
1937impl<'a> Arbitrary<'a> for Denomination {
1938    fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
1939        let choice = u.int_in_range(0..=5)?;
1940        match choice {
1941            0 => Ok(Denomination::Bitcoin),
1942            1 => Ok(Denomination::CentiBitcoin),
1943            2 => Ok(Denomination::MilliBitcoin),
1944            3 => Ok(Denomination::MicroBitcoin),
1945            4 => Ok(Denomination::Bit),
1946            _ => Ok(Denomination::Satoshi),
1947        }
1948    }
1949}
1950
1951#[cfg(feature = "arbitrary")]
1952impl<'a> Arbitrary<'a> for SignedAmount {
1953    fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
1954        let s = i64::arbitrary(u)?;
1955        Ok(SignedAmount(s))
1956    }
1957}
1958
1959#[cfg(kani)]
1960mod verification {
1961    use std::cmp;
1962    use std::convert::TryInto;
1963
1964    use super::*;
1965
1966    // Note regarding the `unwind` parameter: this defines how many iterations
1967    // of loops kani will unwind before handing off to the SMT solver. Basically
1968    // it should be set as low as possible such that Kani still succeeds (doesn't
1969    // return "undecidable").
1970    //
1971    // There is more info here: https://model-checking.github.io/kani/tutorial-loop-unwinding.html
1972    //
1973    // Unfortunately what it means to "loop" is pretty opaque ... in this case
1974    // there appear to be loops in memcmp, which I guess comes from assert_eq!,
1975    // though I didn't see any failures until I added the to_signed() test.
1976    // Further confusing the issue, a value of 2 works fine on my system, but on
1977    // CI it fails, so we need to set it higher.
1978    #[kani::unwind(4)]
1979    #[kani::proof]
1980    fn u_amount_add_homomorphic() {
1981        let n1 = kani::any::<u64>();
1982        let n2 = kani::any::<u64>();
1983        kani::assume(n1.checked_add(n2).is_some()); // assume we don't overflow in the actual test
1984        assert_eq!(Amount::from_sat(n1) + Amount::from_sat(n2), Amount::from_sat(n1 + n2));
1985
1986        let mut amt = Amount::from_sat(n1);
1987        amt += Amount::from_sat(n2);
1988        assert_eq!(amt, Amount::from_sat(n1 + n2));
1989
1990        let max = cmp::max(n1, n2);
1991        let min = cmp::min(n1, n2);
1992        assert_eq!(Amount::from_sat(max) - Amount::from_sat(min), Amount::from_sat(max - min));
1993
1994        let mut amt = Amount::from_sat(max);
1995        amt -= Amount::from_sat(min);
1996        assert_eq!(amt, Amount::from_sat(max - min));
1997
1998        assert_eq!(
1999            Amount::from_sat(n1).to_signed(),
2000            if n1 <= i64::MAX as u64 {
2001                Ok(SignedAmount::from_sat(n1.try_into().unwrap()))
2002            } else {
2003                Err(OutOfRangeError::too_big(true))
2004            },
2005        );
2006    }
2007
2008    #[kani::unwind(4)]
2009    #[kani::proof]
2010    fn u_amount_add_homomorphic_checked() {
2011        let n1 = kani::any::<u64>();
2012        let n2 = kani::any::<u64>();
2013        assert_eq!(
2014            Amount::from_sat(n1).checked_add(Amount::from_sat(n2)),
2015            n1.checked_add(n2).map(Amount::from_sat),
2016        );
2017        assert_eq!(
2018            Amount::from_sat(n1).checked_sub(Amount::from_sat(n2)),
2019            n1.checked_sub(n2).map(Amount::from_sat),
2020        );
2021    }
2022
2023    #[kani::unwind(4)]
2024    #[kani::proof]
2025    fn s_amount_add_homomorphic() {
2026        let n1 = kani::any::<i64>();
2027        let n2 = kani::any::<i64>();
2028        kani::assume(n1.checked_add(n2).is_some()); // assume we don't overflow in the actual test
2029        kani::assume(n1.checked_sub(n2).is_some()); // assume we don't overflow in the actual test
2030        assert_eq!(
2031            SignedAmount::from_sat(n1) + SignedAmount::from_sat(n2),
2032            SignedAmount::from_sat(n1 + n2)
2033        );
2034        assert_eq!(
2035            SignedAmount::from_sat(n1) - SignedAmount::from_sat(n2),
2036            SignedAmount::from_sat(n1 - n2)
2037        );
2038
2039        let mut amt = SignedAmount::from_sat(n1);
2040        amt += SignedAmount::from_sat(n2);
2041        assert_eq!(amt, SignedAmount::from_sat(n1 + n2));
2042        let mut amt = SignedAmount::from_sat(n1);
2043        amt -= SignedAmount::from_sat(n2);
2044        assert_eq!(amt, SignedAmount::from_sat(n1 - n2));
2045
2046        assert_eq!(
2047            SignedAmount::from_sat(n1).to_unsigned(),
2048            if n1 >= 0 {
2049                Ok(Amount::from_sat(n1.try_into().unwrap()))
2050            } else {
2051                Err(OutOfRangeError { is_signed: true, is_greater_than_max: false })
2052            },
2053        );
2054    }
2055
2056    #[kani::unwind(4)]
2057    #[kani::proof]
2058    fn s_amount_add_homomorphic_checked() {
2059        let n1 = kani::any::<i64>();
2060        let n2 = kani::any::<i64>();
2061        assert_eq!(
2062            SignedAmount::from_sat(n1).checked_add(SignedAmount::from_sat(n2)),
2063            n1.checked_add(n2).map(SignedAmount::from_sat),
2064        );
2065        assert_eq!(
2066            SignedAmount::from_sat(n1).checked_sub(SignedAmount::from_sat(n2)),
2067            n1.checked_sub(n2).map(SignedAmount::from_sat),
2068        );
2069
2070        assert_eq!(
2071            SignedAmount::from_sat(n1).positive_sub(SignedAmount::from_sat(n2)),
2072            if n1 >= 0 && n2 >= 0 && n1 >= n2 {
2073                Some(SignedAmount::from_sat(n1 - n2))
2074            } else {
2075                None
2076            },
2077        );
2078    }
2079}
2080
2081#[cfg(test)]
2082mod tests {
2083    #[cfg(feature = "alloc")]
2084    use alloc::format;
2085    #[cfg(feature = "std")]
2086    use std::panic;
2087
2088    use super::*;
2089
2090    #[test]
2091    #[cfg(feature = "alloc")]
2092    fn from_str_zero() {
2093        let denoms = ["BTC", "mBTC", "uBTC", "nBTC", "pBTC", "bits", "sats", "msats"];
2094        for denom in denoms {
2095            for v in &["0", "000"] {
2096                let s = format!("{} {}", v, denom);
2097                match Amount::from_str(&s) {
2098                    Err(e) => panic!("Failed to crate amount from {}: {:?}", s, e),
2099                    Ok(amount) => assert_eq!(amount, Amount::from_sat(0)),
2100                }
2101            }
2102
2103            let s = format!("-0 {}", denom);
2104            match Amount::from_str(&s) {
2105                Err(e) => assert_eq!(
2106                    e,
2107                    ParseError::Amount(ParseAmountError::OutOfRange(OutOfRangeError::negative()))
2108                ),
2109                Ok(_) => panic!("Unsigned amount from {}", s),
2110            }
2111            match SignedAmount::from_str(&s) {
2112                Err(e) => panic!("Failed to crate amount from {}: {:?}", s, e),
2113                Ok(amount) => assert_eq!(amount, SignedAmount::from_sat(0)),
2114            }
2115        }
2116    }
2117
2118    #[test]
2119    fn from_int_btc() {
2120        let amt = Amount::from_int_btc(2);
2121        assert_eq!(Amount::from_sat(200_000_000), amt);
2122    }
2123
2124    #[should_panic]
2125    #[test]
2126    fn from_int_btc_panic() { Amount::from_int_btc(u64::MAX); }
2127
2128    #[test]
2129    fn test_signed_amount_try_from_amount() {
2130        let ua_positive = Amount::from_sat(123);
2131        let sa_positive = SignedAmount::try_from(ua_positive).unwrap();
2132        assert_eq!(sa_positive, SignedAmount(123));
2133
2134        let ua_max = Amount::MAX;
2135        let result = SignedAmount::try_from(ua_max);
2136        assert_eq!(result, Err(OutOfRangeError { is_signed: true, is_greater_than_max: true }));
2137    }
2138
2139    #[test]
2140    fn test_amount_try_from_signed_amount() {
2141        let sa_positive = SignedAmount(123);
2142        let ua_positive = Amount::try_from(sa_positive).unwrap();
2143        assert_eq!(ua_positive, Amount::from_sat(123));
2144
2145        let sa_negative = SignedAmount(-123);
2146        let result = Amount::try_from(sa_negative);
2147        assert_eq!(result, Err(OutOfRangeError { is_signed: false, is_greater_than_max: false }));
2148    }
2149
2150    #[test]
2151    fn mul_div() {
2152        let sat = Amount::from_sat;
2153        let ssat = SignedAmount::from_sat;
2154
2155        assert_eq!(sat(14) * 3, sat(42));
2156        assert_eq!(sat(14) / 2, sat(7));
2157        assert_eq!(sat(14) % 3, sat(2));
2158        assert_eq!(ssat(-14) * 3, ssat(-42));
2159        assert_eq!(ssat(-14) / 2, ssat(-7));
2160        assert_eq!(ssat(-14) % 3, ssat(-2));
2161
2162        let mut b = ssat(30);
2163        b /= 3;
2164        assert_eq!(b, ssat(10));
2165        b %= 3;
2166        assert_eq!(b, ssat(1));
2167    }
2168
2169    #[cfg(feature = "std")]
2170    #[test]
2171    fn test_overflows() {
2172        // panic on overflow
2173        let result = panic::catch_unwind(|| Amount::MAX + Amount::from_sat(1));
2174        assert!(result.is_err());
2175        let result = panic::catch_unwind(|| Amount::from_sat(8446744073709551615) * 3);
2176        assert!(result.is_err());
2177    }
2178
2179    #[test]
2180    fn checked_arithmetic() {
2181        let sat = Amount::from_sat;
2182        let ssat = SignedAmount::from_sat;
2183
2184        assert_eq!(SignedAmount::MAX.checked_add(ssat(1)), None);
2185        assert_eq!(SignedAmount::MIN.checked_sub(ssat(1)), None);
2186        assert_eq!(Amount::MAX.checked_add(sat(1)), None);
2187        assert_eq!(Amount::MIN.checked_sub(sat(1)), None);
2188
2189        assert_eq!(sat(5).checked_div(2), Some(sat(2))); // integer division
2190        assert_eq!(ssat(-6).checked_div(2), Some(ssat(-3)));
2191    }
2192
2193    #[cfg(feature = "alloc")]
2194    #[test]
2195    fn amount_checked_div_by_weight_ceil() {
2196        let weight = Weight::from_kwu(1).unwrap();
2197        let fee_rate = Amount::from_sat(1).div_by_weight_ceil(weight).unwrap();
2198        // 1 sats / 1,000 wu = 1 sats/kwu
2199        assert_eq!(fee_rate, FeeRate::from_sat_per_kwu(1));
2200
2201        let weight = Weight::from_wu(381);
2202        let fee_rate = Amount::from_sat(329).div_by_weight_ceil(weight).unwrap();
2203        // 329 sats / 381 wu = 863.5 sats/kwu
2204        // round up to 864
2205        assert_eq!(fee_rate, FeeRate::from_sat_per_kwu(864));
2206
2207        let fee_rate = Amount::MAX.div_by_weight_ceil(weight);
2208        assert!(fee_rate.is_none());
2209
2210        let fee_rate = Amount::ONE_SAT.div_by_weight_ceil(Weight::ZERO);
2211        assert!(fee_rate.is_none());
2212    }
2213
2214    #[cfg(feature = "alloc")]
2215    #[test]
2216    fn amount_checked_div_by_weight_floor() {
2217        let weight = Weight::from_kwu(1).unwrap();
2218        let fee_rate = Amount::from_sat(1).div_by_weight_floor(weight).unwrap();
2219        // 1 sats / 1,000 wu = 1 sats/kwu
2220        assert_eq!(fee_rate, FeeRate::from_sat_per_kwu(1));
2221
2222        let weight = Weight::from_wu(381);
2223        let fee_rate = Amount::from_sat(329).div_by_weight_floor(weight).unwrap();
2224        // 329 sats / 381 wu = 863.5 sats/kwu
2225        // round down to 863
2226        assert_eq!(fee_rate, FeeRate::from_sat_per_kwu(863));
2227
2228        let fee_rate = Amount::MAX.div_by_weight_floor(weight);
2229        assert!(fee_rate.is_none());
2230
2231        let fee_rate = Amount::ONE_SAT.div_by_weight_floor(Weight::ZERO);
2232        assert!(fee_rate.is_none());
2233    }
2234
2235    #[test]
2236    #[cfg(not(debug_assertions))]
2237    fn unchecked_amount_add() {
2238        let amt = Amount::MAX.unchecked_add(Amount::ONE_SAT);
2239        assert_eq!(amt, Amount::ZERO);
2240    }
2241
2242    #[test]
2243    #[cfg(not(debug_assertions))]
2244    fn unchecked_signed_amount_add() {
2245        let signed_amt = SignedAmount::MAX.unchecked_add(SignedAmount::ONE_SAT);
2246        assert_eq!(signed_amt, SignedAmount::MIN);
2247    }
2248
2249    #[test]
2250    #[cfg(not(debug_assertions))]
2251    fn unchecked_amount_subtract() {
2252        let amt = Amount::ZERO.unchecked_sub(Amount::ONE_SAT);
2253        assert_eq!(amt, Amount::MAX);
2254    }
2255
2256    #[test]
2257    #[cfg(not(debug_assertions))]
2258    fn unchecked_signed_amount_subtract() {
2259        let signed_amt = SignedAmount::MIN.unchecked_sub(SignedAmount::ONE_SAT);
2260        assert_eq!(signed_amt, SignedAmount::MAX);
2261    }
2262
2263    #[cfg(feature = "alloc")]
2264    #[test]
2265    fn floating_point() {
2266        use super::Denomination as D;
2267        let f = Amount::from_float_in;
2268        let sf = SignedAmount::from_float_in;
2269        let sat = Amount::from_sat;
2270        let ssat = SignedAmount::from_sat;
2271
2272        assert_eq!(f(11.22, D::Bitcoin), Ok(sat(1122000000)));
2273        assert_eq!(sf(-11.22, D::MilliBitcoin), Ok(ssat(-1122000)));
2274        assert_eq!(f(11.22, D::Bit), Ok(sat(1122)));
2275        assert_eq!(sf(-1000.0, D::MilliSatoshi), Ok(ssat(-1)));
2276        assert_eq!(f(0.0001234, D::Bitcoin), Ok(sat(12340)));
2277        assert_eq!(sf(-0.00012345, D::Bitcoin), Ok(ssat(-12345)));
2278
2279        assert_eq!(f(-100.0, D::MilliSatoshi), Err(OutOfRangeError::negative().into()));
2280        assert_eq!(f(11.22, D::Satoshi), Err(TooPreciseError { position: 3 }.into()));
2281        assert_eq!(sf(-100.0, D::MilliSatoshi), Err(TooPreciseError { position: 1 }.into()));
2282        assert_eq!(f(42.123456781, D::Bitcoin), Err(TooPreciseError { position: 11 }.into()));
2283        assert_eq!(sf(-184467440738.0, D::Bitcoin), Err(OutOfRangeError::too_small().into()));
2284        assert_eq!(
2285            f(18446744073709551617.0, D::Satoshi),
2286            Err(OutOfRangeError::too_big(false).into())
2287        );
2288
2289        // Amount can be grater than the max SignedAmount.
2290        assert!(f(SignedAmount::MAX.to_float_in(D::Satoshi) + 1.0, D::Satoshi).is_ok());
2291
2292        assert_eq!(
2293            f(Amount::MAX.to_float_in(D::Satoshi) + 1.0, D::Satoshi),
2294            Err(OutOfRangeError::too_big(false).into())
2295        );
2296
2297        assert_eq!(
2298            sf(SignedAmount::MAX.to_float_in(D::Satoshi) + 1.0, D::Satoshi),
2299            Err(OutOfRangeError::too_big(true).into())
2300        );
2301
2302        let btc = move |f| SignedAmount::from_btc(f).unwrap();
2303        assert_eq!(btc(2.5).to_float_in(D::Bitcoin), 2.5);
2304        assert_eq!(btc(-2.5).to_float_in(D::MilliBitcoin), -2500.0);
2305        assert_eq!(btc(2.5).to_float_in(D::Satoshi), 250000000.0);
2306        assert_eq!(btc(-2.5).to_float_in(D::MilliSatoshi), -250000000000.0);
2307
2308        let btc = move |f| Amount::from_btc(f).unwrap();
2309        assert_eq!(&btc(0.0012).to_float_in(D::Bitcoin).to_string(), "0.0012")
2310    }
2311
2312    #[test]
2313    #[allow(clippy::inconsistent_digit_grouping)] // Group to show 100,000,000 sats per bitcoin.
2314    fn parsing() {
2315        use super::ParseAmountError as E;
2316        let btc = Denomination::Bitcoin;
2317        let sat = Denomination::Satoshi;
2318        let msat = Denomination::MilliSatoshi;
2319        let p = Amount::from_str_in;
2320        let sp = SignedAmount::from_str_in;
2321
2322        assert_eq!(
2323            p("x", btc),
2324            Err(E::from(InvalidCharacterError { invalid_char: 'x', position: 0 }))
2325        );
2326        assert_eq!(
2327            p("-", btc),
2328            Err(E::from(MissingDigitsError { kind: MissingDigitsKind::OnlyMinusSign }))
2329        );
2330        assert_eq!(
2331            sp("-", btc),
2332            Err(E::from(MissingDigitsError { kind: MissingDigitsKind::OnlyMinusSign }))
2333        );
2334        assert_eq!(
2335            p("-1.0x", btc),
2336            Err(E::from(InvalidCharacterError { invalid_char: 'x', position: 4 }))
2337        );
2338        assert_eq!(
2339            p("0.0 ", btc),
2340            Err(E::from(InvalidCharacterError { invalid_char: ' ', position: 3 }))
2341        );
2342        assert_eq!(
2343            p("0.000.000", btc),
2344            Err(E::from(InvalidCharacterError { invalid_char: '.', position: 5 }))
2345        );
2346        #[cfg(feature = "alloc")]
2347        let more_than_max = format!("1{}", Amount::MAX);
2348        #[cfg(feature = "alloc")]
2349        assert_eq!(p(&more_than_max, btc), Err(OutOfRangeError::too_big(false).into()));
2350        assert_eq!(p("0.000000042", btc), Err(TooPreciseError { position: 10 }.into()));
2351        assert_eq!(p("999.0000000", msat), Err(TooPreciseError { position: 0 }.into()));
2352        assert_eq!(p("1.0000000", msat), Err(TooPreciseError { position: 0 }.into()));
2353        assert_eq!(p("1.1", msat), Err(TooPreciseError { position: 0 }.into()));
2354        assert_eq!(p("1000.1", msat), Err(TooPreciseError { position: 5 }.into()));
2355        assert_eq!(p("1001.0000000", msat), Err(TooPreciseError { position: 3 }.into()));
2356        assert_eq!(p("1000.0000001", msat), Err(TooPreciseError { position: 11 }.into()));
2357        assert_eq!(p("1000.1000000", msat), Err(TooPreciseError { position: 5 }.into()));
2358        assert_eq!(p("1100.0000000", msat), Err(TooPreciseError { position: 1 }.into()));
2359        assert_eq!(p("10001.0000000", msat), Err(TooPreciseError { position: 4 }.into()));
2360
2361        assert_eq!(p("1", btc), Ok(Amount::from_sat(1_000_000_00)));
2362        assert_eq!(sp("-.5", btc), Ok(SignedAmount::from_sat(-500_000_00)));
2363        #[cfg(feature = "alloc")]
2364        assert_eq!(sp(&i64::MIN.to_string(), sat), Ok(SignedAmount::from_sat(i64::MIN)));
2365        assert_eq!(p("1.1", btc), Ok(Amount::from_sat(1_100_000_00)));
2366        assert_eq!(p("100", sat), Ok(Amount::from_sat(100)));
2367        assert_eq!(p("55", sat), Ok(Amount::from_sat(55)));
2368        assert_eq!(p("5500000000000000000", sat), Ok(Amount::from_sat(55_000_000_000_000_000_00)));
2369        // Should this even pass?
2370        assert_eq!(p("5500000000000000000.", sat), Ok(Amount::from_sat(55_000_000_000_000_000_00)));
2371        assert_eq!(
2372            p("12345678901.12345678", btc),
2373            Ok(Amount::from_sat(12_345_678_901__123_456_78))
2374        );
2375        assert_eq!(p("1000.0", msat), Ok(Amount::from_sat(1)));
2376        assert_eq!(p("1000.000000000000000000000000000", msat), Ok(Amount::from_sat(1)));
2377
2378        // make sure satoshi > i64::MAX is checked.
2379        #[cfg(feature = "alloc")]
2380        {
2381            let amount = Amount::from_sat(i64::MAX as u64);
2382            assert_eq!(Amount::from_str_in(&amount.to_string_in(sat), sat), Ok(amount));
2383            assert!(
2384                SignedAmount::from_str_in(&(amount + Amount(1)).to_string_in(sat), sat).is_err()
2385            );
2386            assert!(Amount::from_str_in(&(amount + Amount(1)).to_string_in(sat), sat).is_ok());
2387        }
2388
2389        assert_eq!(
2390            p("12.000", Denomination::MilliSatoshi),
2391            Err(TooPreciseError { position: 0 }.into())
2392        );
2393        // exactly 50 chars.
2394        assert_eq!(
2395            p("100000000000000.0000000000000000000000000000000000", Denomination::Bitcoin),
2396            Err(OutOfRangeError::too_big(false).into())
2397        );
2398        // more than 50 chars.
2399        assert_eq!(
2400            p("100000000000000.00000000000000000000000000000000000", Denomination::Bitcoin),
2401            Err(E::InputTooLarge(InputTooLargeError { len: 51 }))
2402        );
2403    }
2404
2405    #[test]
2406    #[cfg(feature = "alloc")]
2407    fn to_string() {
2408        use super::Denomination as D;
2409
2410        assert_eq!(Amount::ONE_BTC.to_string_in(D::Bitcoin), "1");
2411        assert_eq!(format!("{:.8}", Amount::ONE_BTC.display_in(D::Bitcoin)), "1.00000000");
2412        assert_eq!(Amount::ONE_BTC.to_string_in(D::Satoshi), "100000000");
2413        assert_eq!(Amount::ONE_SAT.to_string_in(D::Bitcoin), "0.00000001");
2414        assert_eq!(SignedAmount::from_sat(-42).to_string_in(D::Bitcoin), "-0.00000042");
2415
2416        assert_eq!(Amount::ONE_BTC.to_string_with_denomination(D::Bitcoin), "1 BTC");
2417        assert_eq!(Amount::ONE_SAT.to_string_with_denomination(D::MilliSatoshi), "1000 msat");
2418        assert_eq!(
2419            SignedAmount::ONE_BTC.to_string_with_denomination(D::Satoshi),
2420            "100000000 satoshi"
2421        );
2422        assert_eq!(Amount::ONE_SAT.to_string_with_denomination(D::Bitcoin), "0.00000001 BTC");
2423        assert_eq!(
2424            SignedAmount::from_sat(-42).to_string_with_denomination(D::Bitcoin),
2425            "-0.00000042 BTC"
2426        );
2427    }
2428
2429    // May help identify a problem sooner
2430    #[cfg(feature = "alloc")]
2431    #[test]
2432    fn test_repeat_char() {
2433        let mut buf = String::new();
2434        repeat_char(&mut buf, '0', 0).unwrap();
2435        assert_eq!(buf.len(), 0);
2436        repeat_char(&mut buf, '0', 42).unwrap();
2437        assert_eq!(buf.len(), 42);
2438        assert!(buf.chars().all(|c| c == '0'));
2439    }
2440
2441    // Creates individual test functions to make it easier to find which check failed.
2442    macro_rules! check_format_non_negative {
2443        ($denom:ident; $($test_name:ident, $val:literal, $format_string:literal, $expected:literal);* $(;)?) => {
2444            $(
2445                #[test]
2446                #[cfg(feature = "alloc")]
2447                fn $test_name() {
2448                    assert_eq!(format!($format_string, Amount::from_sat($val).display_in(Denomination::$denom)), $expected);
2449                    assert_eq!(format!($format_string, SignedAmount::from_sat($val as i64).display_in(Denomination::$denom)), $expected);
2450                }
2451            )*
2452        }
2453    }
2454
2455    macro_rules! check_format_non_negative_show_denom {
2456        ($denom:ident, $denom_suffix:literal; $($test_name:ident, $val:literal, $format_string:literal, $expected:literal);* $(;)?) => {
2457            $(
2458                #[test]
2459                #[cfg(feature = "alloc")]
2460                fn $test_name() {
2461                    assert_eq!(format!($format_string, Amount::from_sat($val).display_in(Denomination::$denom).show_denomination()), concat!($expected, $denom_suffix));
2462                    assert_eq!(format!($format_string, SignedAmount::from_sat($val as i64).display_in(Denomination::$denom).show_denomination()), concat!($expected, $denom_suffix));
2463                }
2464            )*
2465        }
2466    }
2467
2468    check_format_non_negative! {
2469        Satoshi;
2470        sat_check_fmt_non_negative_0, 0, "{}", "0";
2471        sat_check_fmt_non_negative_1, 0, "{:2}", " 0";
2472        sat_check_fmt_non_negative_2, 0, "{:02}", "00";
2473        sat_check_fmt_non_negative_3, 0, "{:.1}", "0.0";
2474        sat_check_fmt_non_negative_4, 0, "{:4.1}", " 0.0";
2475        sat_check_fmt_non_negative_5, 0, "{:04.1}", "00.0";
2476        sat_check_fmt_non_negative_6, 1, "{}", "1";
2477        sat_check_fmt_non_negative_7, 1, "{:2}", " 1";
2478        sat_check_fmt_non_negative_8, 1, "{:02}", "01";
2479        sat_check_fmt_non_negative_9, 1, "{:.1}", "1.0";
2480        sat_check_fmt_non_negative_10, 1, "{:4.1}", " 1.0";
2481        sat_check_fmt_non_negative_11, 1, "{:04.1}", "01.0";
2482        sat_check_fmt_non_negative_12, 10, "{}", "10";
2483        sat_check_fmt_non_negative_13, 10, "{:2}", "10";
2484        sat_check_fmt_non_negative_14, 10, "{:02}", "10";
2485        sat_check_fmt_non_negative_15, 10, "{:3}", " 10";
2486        sat_check_fmt_non_negative_16, 10, "{:03}", "010";
2487        sat_check_fmt_non_negative_17, 10, "{:.1}", "10.0";
2488        sat_check_fmt_non_negative_18, 10, "{:5.1}", " 10.0";
2489        sat_check_fmt_non_negative_19, 10, "{:05.1}", "010.0";
2490        sat_check_fmt_non_negative_20, 1, "{:<2}", "1 ";
2491        sat_check_fmt_non_negative_21, 1, "{:<02}", "01";
2492        sat_check_fmt_non_negative_22, 1, "{:<3.1}", "1.0";
2493        sat_check_fmt_non_negative_23, 1, "{:<4.1}", "1.0 ";
2494    }
2495
2496    check_format_non_negative_show_denom! {
2497        Satoshi, " satoshi";
2498        sat_check_fmt_non_negative_show_denom_0, 0, "{}", "0";
2499        sat_check_fmt_non_negative_show_denom_1, 0, "{:2}", "0";
2500        sat_check_fmt_non_negative_show_denom_2, 0, "{:02}", "0";
2501        sat_check_fmt_non_negative_show_denom_3, 0, "{:9}", "0";
2502        sat_check_fmt_non_negative_show_denom_4, 0, "{:09}", "0";
2503        sat_check_fmt_non_negative_show_denom_5, 0, "{:10}", " 0";
2504        sat_check_fmt_non_negative_show_denom_6, 0, "{:010}", "00";
2505        sat_check_fmt_non_negative_show_denom_7, 0, "{:.1}", "0.0";
2506        sat_check_fmt_non_negative_show_denom_8, 0, "{:11.1}", "0.0";
2507        sat_check_fmt_non_negative_show_denom_9, 0, "{:011.1}", "0.0";
2508        sat_check_fmt_non_negative_show_denom_10, 0, "{:12.1}", " 0.0";
2509        sat_check_fmt_non_negative_show_denom_11, 0, "{:012.1}", "00.0";
2510        sat_check_fmt_non_negative_show_denom_12, 1, "{}", "1";
2511        sat_check_fmt_non_negative_show_denom_13, 1, "{:10}", " 1";
2512        sat_check_fmt_non_negative_show_denom_14, 1, "{:010}", "01";
2513        sat_check_fmt_non_negative_show_denom_15, 1, "{:.1}", "1.0";
2514        sat_check_fmt_non_negative_show_denom_16, 1, "{:12.1}", " 1.0";
2515        sat_check_fmt_non_negative_show_denom_17, 1, "{:012.1}", "01.0";
2516        sat_check_fmt_non_negative_show_denom_18, 10, "{}", "10";
2517        sat_check_fmt_non_negative_show_denom_19, 10, "{:10}", "10";
2518        sat_check_fmt_non_negative_show_denom_20, 10, "{:010}", "10";
2519        sat_check_fmt_non_negative_show_denom_21, 10, "{:11}", " 10";
2520        sat_check_fmt_non_negative_show_denom_22, 10, "{:011}", "010";
2521    }
2522
2523    check_format_non_negative! {
2524        Bitcoin;
2525        btc_check_fmt_non_negative_0, 0, "{}", "0";
2526        btc_check_fmt_non_negative_1, 0, "{:2}", " 0";
2527        btc_check_fmt_non_negative_2, 0, "{:02}", "00";
2528        btc_check_fmt_non_negative_3, 0, "{:.1}", "0.0";
2529        btc_check_fmt_non_negative_4, 0, "{:4.1}", " 0.0";
2530        btc_check_fmt_non_negative_5, 0, "{:04.1}", "00.0";
2531        btc_check_fmt_non_negative_6, 1, "{}", "0.00000001";
2532        btc_check_fmt_non_negative_7, 1, "{:2}", "0.00000001";
2533        btc_check_fmt_non_negative_8, 1, "{:02}", "0.00000001";
2534        btc_check_fmt_non_negative_9, 1, "{:.1}", "0.00000001";
2535        btc_check_fmt_non_negative_10, 1, "{:11}", " 0.00000001";
2536        btc_check_fmt_non_negative_11, 1, "{:11.1}", " 0.00000001";
2537        btc_check_fmt_non_negative_12, 1, "{:011.1}", "00.00000001";
2538        btc_check_fmt_non_negative_13, 1, "{:.9}", "0.000000010";
2539        btc_check_fmt_non_negative_14, 1, "{:11.9}", "0.000000010";
2540        btc_check_fmt_non_negative_15, 1, "{:011.9}", "0.000000010";
2541        btc_check_fmt_non_negative_16, 1, "{:12.9}", " 0.000000010";
2542        btc_check_fmt_non_negative_17, 1, "{:012.9}", "00.000000010";
2543        btc_check_fmt_non_negative_18, 100_000_000, "{}", "1";
2544        btc_check_fmt_non_negative_19, 100_000_000, "{:2}", " 1";
2545        btc_check_fmt_non_negative_20, 100_000_000, "{:02}", "01";
2546        btc_check_fmt_non_negative_21, 100_000_000, "{:.1}", "1.0";
2547        btc_check_fmt_non_negative_22, 100_000_000, "{:4.1}", " 1.0";
2548        btc_check_fmt_non_negative_23, 100_000_000, "{:04.1}", "01.0";
2549        btc_check_fmt_non_negative_24, 110_000_000, "{}", "1.1";
2550        btc_check_fmt_non_negative_25, 100_000_001, "{}", "1.00000001";
2551        btc_check_fmt_non_negative_26, 100_000_001, "{:1}", "1.00000001";
2552        btc_check_fmt_non_negative_27, 100_000_001, "{:.1}", "1.00000001";
2553        btc_check_fmt_non_negative_28, 100_000_001, "{:10}", "1.00000001";
2554        btc_check_fmt_non_negative_29, 100_000_001, "{:11}", " 1.00000001";
2555        btc_check_fmt_non_negative_30, 100_000_001, "{:011}", "01.00000001";
2556        btc_check_fmt_non_negative_31, 100_000_001, "{:.8}", "1.00000001";
2557        btc_check_fmt_non_negative_32, 100_000_001, "{:.9}", "1.000000010";
2558        btc_check_fmt_non_negative_33, 100_000_001, "{:11.9}", "1.000000010";
2559        btc_check_fmt_non_negative_34, 100_000_001, "{:12.9}", " 1.000000010";
2560        btc_check_fmt_non_negative_35, 100_000_001, "{:012.9}", "01.000000010";
2561        btc_check_fmt_non_negative_36, 100_000_001, "{:+011.8}", "+1.00000001";
2562        btc_check_fmt_non_negative_37, 100_000_001, "{:+12.8}", " +1.00000001";
2563        btc_check_fmt_non_negative_38, 100_000_001, "{:+012.8}", "+01.00000001";
2564        btc_check_fmt_non_negative_39, 100_000_001, "{:+12.9}", "+1.000000010";
2565        btc_check_fmt_non_negative_40, 100_000_001, "{:+012.9}", "+1.000000010";
2566        btc_check_fmt_non_negative_41, 100_000_001, "{:+13.9}", " +1.000000010";
2567        btc_check_fmt_non_negative_42, 100_000_001, "{:+013.9}", "+01.000000010";
2568        btc_check_fmt_non_negative_43, 100_000_001, "{:<10}", "1.00000001";
2569        btc_check_fmt_non_negative_44, 100_000_001, "{:<11}", "1.00000001 ";
2570        btc_check_fmt_non_negative_45, 100_000_001, "{:<011}", "01.00000001";
2571        btc_check_fmt_non_negative_46, 100_000_001, "{:<11.9}", "1.000000010";
2572        btc_check_fmt_non_negative_47, 100_000_001, "{:<12.9}", "1.000000010 ";
2573        btc_check_fmt_non_negative_48, 100_000_001, "{:<12}", "1.00000001  ";
2574        btc_check_fmt_non_negative_49, 100_000_001, "{:^11}", "1.00000001 ";
2575        btc_check_fmt_non_negative_50, 100_000_001, "{:^11.9}", "1.000000010";
2576        btc_check_fmt_non_negative_51, 100_000_001, "{:^12.9}", "1.000000010 ";
2577        btc_check_fmt_non_negative_52, 100_000_001, "{:^12}", " 1.00000001 ";
2578        btc_check_fmt_non_negative_53, 100_000_001, "{:^12.9}", "1.000000010 ";
2579        btc_check_fmt_non_negative_54, 100_000_001, "{:^13.9}", " 1.000000010 ";
2580    }
2581
2582    check_format_non_negative_show_denom! {
2583        Bitcoin, " BTC";
2584        btc_check_fmt_non_negative_show_denom_0, 1, "{:14.1}", "0.00000001";
2585        btc_check_fmt_non_negative_show_denom_1, 1, "{:14.8}", "0.00000001";
2586        btc_check_fmt_non_negative_show_denom_2, 1, "{:15}", " 0.00000001";
2587        btc_check_fmt_non_negative_show_denom_3, 1, "{:015}", "00.00000001";
2588        btc_check_fmt_non_negative_show_denom_4, 1, "{:.9}", "0.000000010";
2589        btc_check_fmt_non_negative_show_denom_5, 1, "{:15.9}", "0.000000010";
2590        btc_check_fmt_non_negative_show_denom_6, 1, "{:16.9}", " 0.000000010";
2591        btc_check_fmt_non_negative_show_denom_7, 1, "{:016.9}", "00.000000010";
2592    }
2593
2594    check_format_non_negative_show_denom! {
2595        Bitcoin, " BTC ";
2596        btc_check_fmt_non_negative_show_denom_align_0, 1, "{:<15}", "0.00000001";
2597        btc_check_fmt_non_negative_show_denom_align_1, 1, "{:^15}", "0.00000001";
2598        btc_check_fmt_non_negative_show_denom_align_2, 1, "{:^16}", " 0.00000001";
2599    }
2600
2601    check_format_non_negative! {
2602        MilliSatoshi;
2603        msat_check_fmt_non_negative_0, 0, "{}", "0";
2604        msat_check_fmt_non_negative_1, 1, "{}", "1000";
2605        msat_check_fmt_non_negative_2, 1, "{:5}", " 1000";
2606        msat_check_fmt_non_negative_3, 1, "{:05}", "01000";
2607        msat_check_fmt_non_negative_4, 1, "{:.1}", "1000.0";
2608        msat_check_fmt_non_negative_5, 1, "{:6.1}", "1000.0";
2609        msat_check_fmt_non_negative_6, 1, "{:06.1}", "1000.0";
2610        msat_check_fmt_non_negative_7, 1, "{:7.1}", " 1000.0";
2611        msat_check_fmt_non_negative_8, 1, "{:07.1}", "01000.0";
2612    }
2613
2614    #[test]
2615    fn test_unsigned_signed_conversion() {
2616        let sa = SignedAmount::from_sat;
2617        let ua = Amount::from_sat;
2618
2619        assert_eq!(Amount::MAX.to_signed(), Err(OutOfRangeError::too_big(true)));
2620        assert_eq!(ua(i64::MAX as u64).to_signed(), Ok(sa(i64::MAX)));
2621        assert_eq!(ua(i64::MAX as u64 + 1).to_signed(), Err(OutOfRangeError::too_big(true)));
2622
2623        assert_eq!(sa(i64::MAX).to_unsigned(), Ok(ua(i64::MAX as u64)));
2624
2625        assert_eq!(sa(i64::MAX).to_unsigned().unwrap().to_signed(), Ok(sa(i64::MAX)));
2626    }
2627
2628    #[test]
2629    #[allow(clippy::inconsistent_digit_grouping)] // Group to show 100,000,000 sats per bitcoin.
2630    fn from_str() {
2631        use ParseDenominationError::*;
2632
2633        use super::ParseAmountError as E;
2634
2635        assert_eq!(
2636            Amount::from_str("x BTC"),
2637            Err(InvalidCharacterError { invalid_char: 'x', position: 0 }.into())
2638        );
2639        assert_eq!(
2640            Amount::from_str("xBTC"),
2641            Err(Unknown(UnknownDenominationError("xBTC".into())).into()),
2642        );
2643        assert_eq!(
2644            Amount::from_str("5 BTC BTC"),
2645            Err(Unknown(UnknownDenominationError("BTC BTC".into())).into()),
2646        );
2647        assert_eq!(
2648            Amount::from_str("5BTC BTC"),
2649            Err(E::from(InvalidCharacterError { invalid_char: 'B', position: 1 }).into())
2650        );
2651        assert_eq!(
2652            Amount::from_str("5 5 BTC"),
2653            Err(Unknown(UnknownDenominationError("5 BTC".into())).into()),
2654        );
2655
2656        #[track_caller]
2657        fn ok_case(s: &str, expected: Amount) {
2658            assert_eq!(Amount::from_str(s).unwrap(), expected);
2659            assert_eq!(Amount::from_str(&s.replace(' ', "")).unwrap(), expected);
2660        }
2661
2662        #[track_caller]
2663        fn case(s: &str, expected: Result<Amount, impl Into<ParseError>>) {
2664            let expected = expected.map_err(Into::into);
2665            assert_eq!(Amount::from_str(s), expected);
2666            assert_eq!(Amount::from_str(&s.replace(' ', "")), expected);
2667        }
2668
2669        #[track_caller]
2670        fn ok_scase(s: &str, expected: SignedAmount) {
2671            assert_eq!(SignedAmount::from_str(s).unwrap(), expected);
2672            assert_eq!(SignedAmount::from_str(&s.replace(' ', "")).unwrap(), expected);
2673        }
2674
2675        #[track_caller]
2676        fn scase(s: &str, expected: Result<SignedAmount, impl Into<ParseError>>) {
2677            let expected = expected.map_err(Into::into);
2678            assert_eq!(SignedAmount::from_str(s), expected);
2679            assert_eq!(SignedAmount::from_str(&s.replace(' ', "")), expected);
2680        }
2681
2682        case("5 BCH", Err(Unknown(UnknownDenominationError("BCH".into()))));
2683
2684        case("-1 BTC", Err(OutOfRangeError::negative()));
2685        case("-0.0 BTC", Err(OutOfRangeError::negative()));
2686        case("0.123456789 BTC", Err(TooPreciseError { position: 10 }));
2687        scase("-0.1 satoshi", Err(TooPreciseError { position: 3 }));
2688        case("0.123456 mBTC", Err(TooPreciseError { position: 7 }));
2689        scase("-1.001 bits", Err(TooPreciseError { position: 5 }));
2690        scase("-200000000000 BTC", Err(OutOfRangeError::too_small()));
2691        case("18446744073709551616 sat", Err(OutOfRangeError::too_big(false)));
2692
2693        ok_case(".5 bits", Amount::from_sat(50));
2694        ok_scase("-.5 bits", SignedAmount::from_sat(-50));
2695        ok_case("0.00253583 BTC", Amount::from_sat(253583));
2696        ok_scase("-5 satoshi", SignedAmount::from_sat(-5));
2697        ok_case("0.10000000 BTC", Amount::from_sat(100_000_00));
2698        ok_scase("-100 bits", SignedAmount::from_sat(-10_000));
2699        #[cfg(feature = "alloc")]
2700        ok_scase(&format!("{} SAT", i64::MIN), SignedAmount::from_sat(i64::MIN));
2701    }
2702
2703    #[cfg(feature = "alloc")]
2704    #[test]
2705    #[allow(clippy::inconsistent_digit_grouping)] // Group to show 100,000,000 sats per bitcoin.
2706    fn to_from_string_in() {
2707        use super::Denomination as D;
2708        let ua_str = Amount::from_str_in;
2709        let ua_sat = Amount::from_sat;
2710        let sa_str = SignedAmount::from_str_in;
2711        let sa_sat = SignedAmount::from_sat;
2712
2713        assert_eq!("0.5", Amount::from_sat(50).to_string_in(D::Bit));
2714        assert_eq!("-0.5", SignedAmount::from_sat(-50).to_string_in(D::Bit));
2715        assert_eq!("0.00253583", Amount::from_sat(253583).to_string_in(D::Bitcoin));
2716        assert_eq!("-5", SignedAmount::from_sat(-5).to_string_in(D::Satoshi));
2717        assert_eq!("0.1", Amount::from_sat(100_000_00).to_string_in(D::Bitcoin));
2718        assert_eq!("-100", SignedAmount::from_sat(-10_000).to_string_in(D::Bit));
2719        assert_eq!("2535830", Amount::from_sat(253583).to_string_in(D::NanoBitcoin));
2720        assert_eq!("-100000", SignedAmount::from_sat(-10_000).to_string_in(D::NanoBitcoin));
2721        assert_eq!("2535830000", Amount::from_sat(253583).to_string_in(D::PicoBitcoin));
2722        assert_eq!("-100000000", SignedAmount::from_sat(-10_000).to_string_in(D::PicoBitcoin));
2723
2724        assert_eq!("0.50", format!("{:.2}", Amount::from_sat(50).display_in(D::Bit)));
2725        assert_eq!("-0.50", format!("{:.2}", SignedAmount::from_sat(-50).display_in(D::Bit)));
2726        assert_eq!(
2727            "0.10000000",
2728            format!("{:.8}", Amount::from_sat(100_000_00).display_in(D::Bitcoin))
2729        );
2730        assert_eq!("-100.00", format!("{:.2}", SignedAmount::from_sat(-10_000).display_in(D::Bit)));
2731
2732        assert_eq!(ua_str(&ua_sat(0).to_string_in(D::Satoshi), D::Satoshi), Ok(ua_sat(0)));
2733        assert_eq!(ua_str(&ua_sat(500).to_string_in(D::Bitcoin), D::Bitcoin), Ok(ua_sat(500)));
2734        assert_eq!(
2735            ua_str(&ua_sat(21_000_000).to_string_in(D::Bit), D::Bit),
2736            Ok(ua_sat(21_000_000))
2737        );
2738        assert_eq!(
2739            ua_str(&ua_sat(1).to_string_in(D::MicroBitcoin), D::MicroBitcoin),
2740            Ok(ua_sat(1))
2741        );
2742        assert_eq!(
2743            ua_str(&ua_sat(1_000_000_000_000).to_string_in(D::MilliBitcoin), D::MilliBitcoin),
2744            Ok(ua_sat(1_000_000_000_000))
2745        );
2746        assert!(ua_str(&ua_sat(u64::MAX).to_string_in(D::MilliBitcoin), D::MilliBitcoin).is_ok());
2747
2748        assert_eq!(
2749            sa_str(&sa_sat(-1).to_string_in(D::MicroBitcoin), D::MicroBitcoin),
2750            Ok(sa_sat(-1))
2751        );
2752
2753        assert_eq!(
2754            sa_str(&sa_sat(i64::MAX).to_string_in(D::Satoshi), D::MicroBitcoin),
2755            Err(OutOfRangeError::too_big(true).into())
2756        );
2757        // Test an overflow bug in `abs()`
2758        assert_eq!(
2759            sa_str(&sa_sat(i64::MIN).to_string_in(D::Satoshi), D::MicroBitcoin),
2760            Err(OutOfRangeError::too_small().into())
2761        );
2762
2763        assert_eq!(
2764            sa_str(&sa_sat(-1).to_string_in(D::NanoBitcoin), D::NanoBitcoin),
2765            Ok(sa_sat(-1))
2766        );
2767        assert_eq!(
2768            sa_str(&sa_sat(i64::MAX).to_string_in(D::Satoshi), D::NanoBitcoin),
2769            Err(TooPreciseError { position: 18 }.into())
2770        );
2771        assert_eq!(
2772            sa_str(&sa_sat(i64::MIN).to_string_in(D::Satoshi), D::NanoBitcoin),
2773            Err(TooPreciseError { position: 19 }.into())
2774        );
2775
2776        assert_eq!(
2777            sa_str(&sa_sat(-1).to_string_in(D::PicoBitcoin), D::PicoBitcoin),
2778            Ok(sa_sat(-1))
2779        );
2780        assert_eq!(
2781            sa_str(&sa_sat(i64::MAX).to_string_in(D::Satoshi), D::PicoBitcoin),
2782            Err(TooPreciseError { position: 18 }.into())
2783        );
2784        assert_eq!(
2785            sa_str(&sa_sat(i64::MIN).to_string_in(D::Satoshi), D::PicoBitcoin),
2786            Err(TooPreciseError { position: 19 }.into())
2787        );
2788    }
2789
2790    #[cfg(feature = "alloc")]
2791    #[test]
2792    fn to_string_with_denomination_from_str_roundtrip() {
2793        use ParseDenominationError::*;
2794
2795        use super::Denomination as D;
2796
2797        let amt = Amount::from_sat(42);
2798        let denom = Amount::to_string_with_denomination;
2799        assert_eq!(Amount::from_str(&denom(amt, D::Bitcoin)), Ok(amt));
2800        assert_eq!(Amount::from_str(&denom(amt, D::MilliBitcoin)), Ok(amt));
2801        assert_eq!(Amount::from_str(&denom(amt, D::MicroBitcoin)), Ok(amt));
2802        assert_eq!(Amount::from_str(&denom(amt, D::Bit)), Ok(amt));
2803        assert_eq!(Amount::from_str(&denom(amt, D::Satoshi)), Ok(amt));
2804        assert_eq!(Amount::from_str(&denom(amt, D::NanoBitcoin)), Ok(amt));
2805        assert_eq!(Amount::from_str(&denom(amt, D::MilliSatoshi)), Ok(amt));
2806        assert_eq!(Amount::from_str(&denom(amt, D::PicoBitcoin)), Ok(amt));
2807
2808        assert_eq!(
2809            Amount::from_str("42 satoshi BTC"),
2810            Err(Unknown(UnknownDenominationError("satoshi BTC".into())).into()),
2811        );
2812        assert_eq!(
2813            SignedAmount::from_str("-42 satoshi BTC"),
2814            Err(Unknown(UnknownDenominationError("satoshi BTC".into())).into()),
2815        );
2816    }
2817
2818    #[cfg(feature = "serde")]
2819    #[test]
2820    fn serde_as_sat() {
2821        #[derive(Serialize, Deserialize, PartialEq, Debug)]
2822        struct T {
2823            #[serde(with = "crate::amount::serde::as_sat")]
2824            pub amt: Amount,
2825            #[serde(with = "crate::amount::serde::as_sat")]
2826            pub samt: SignedAmount,
2827        }
2828
2829        serde_test::assert_tokens(
2830            &T { amt: Amount::from_sat(123456789), samt: SignedAmount::from_sat(-123456789) },
2831            &[
2832                serde_test::Token::Struct { name: "T", len: 2 },
2833                serde_test::Token::Str("amt"),
2834                serde_test::Token::U64(123456789),
2835                serde_test::Token::Str("samt"),
2836                serde_test::Token::I64(-123456789),
2837                serde_test::Token::StructEnd,
2838            ],
2839        );
2840    }
2841
2842    #[cfg(feature = "serde")]
2843    #[cfg(feature = "alloc")]
2844    #[test]
2845    #[allow(clippy::inconsistent_digit_grouping)] // Group to show 100,000,000 sats per bitcoin.
2846    fn serde_as_btc() {
2847        use serde_json;
2848
2849        #[derive(Serialize, Deserialize, PartialEq, Debug)]
2850        struct T {
2851            #[serde(with = "crate::amount::serde::as_btc")]
2852            pub amt: Amount,
2853            #[serde(with = "crate::amount::serde::as_btc")]
2854            pub samt: SignedAmount,
2855        }
2856
2857        let orig = T {
2858            amt: Amount::from_sat(21_000_000__000_000_01),
2859            samt: SignedAmount::from_sat(-21_000_000__000_000_01),
2860        };
2861
2862        let json = "{\"amt\": 21000000.00000001, \
2863                    \"samt\": -21000000.00000001}";
2864        let t: T = serde_json::from_str(json).unwrap();
2865        assert_eq!(t, orig);
2866
2867        let value: serde_json::Value = serde_json::from_str(json).unwrap();
2868        assert_eq!(t, serde_json::from_value(value).unwrap());
2869
2870        // errors
2871        let t: Result<T, serde_json::Error> =
2872            serde_json::from_str("{\"amt\": 1000000.000000001, \"samt\": 1}");
2873        assert!(t
2874            .unwrap_err()
2875            .to_string()
2876            .contains(&ParseAmountError::TooPrecise(TooPreciseError { position: 16 }).to_string()));
2877        let t: Result<T, serde_json::Error> = serde_json::from_str("{\"amt\": -1, \"samt\": 1}");
2878        assert!(t.unwrap_err().to_string().contains(&OutOfRangeError::negative().to_string()));
2879    }
2880
2881    #[cfg(feature = "serde")]
2882    #[cfg(feature = "alloc")]
2883    #[test]
2884    #[allow(clippy::inconsistent_digit_grouping)] // Group to show 100,000,000 sats per bitcoin.
2885    fn serde_as_btc_opt() {
2886        use serde_json;
2887
2888        #[derive(Serialize, Deserialize, PartialEq, Debug, Eq)]
2889        struct T {
2890            #[serde(default, with = "crate::amount::serde::as_btc::opt")]
2891            pub amt: Option<Amount>,
2892            #[serde(default, with = "crate::amount::serde::as_btc::opt")]
2893            pub samt: Option<SignedAmount>,
2894        }
2895
2896        let with = T {
2897            amt: Some(Amount::from_sat(2_500_000_00)),
2898            samt: Some(SignedAmount::from_sat(-2_500_000_00)),
2899        };
2900        let without = T { amt: None, samt: None };
2901
2902        // Test Roundtripping
2903        for s in [&with, &without].iter() {
2904            let v = serde_json::to_string(s).unwrap();
2905            let w: T = serde_json::from_str(&v).unwrap();
2906            assert_eq!(w, **s);
2907        }
2908
2909        let t: T = serde_json::from_str("{\"amt\": 2.5, \"samt\": -2.5}").unwrap();
2910        assert_eq!(t, with);
2911
2912        let t: T = serde_json::from_str("{}").unwrap();
2913        assert_eq!(t, without);
2914
2915        let value_with: serde_json::Value =
2916            serde_json::from_str("{\"amt\": 2.5, \"samt\": -2.5}").unwrap();
2917        assert_eq!(with, serde_json::from_value(value_with).unwrap());
2918
2919        let value_without: serde_json::Value = serde_json::from_str("{}").unwrap();
2920        assert_eq!(without, serde_json::from_value(value_without).unwrap());
2921    }
2922
2923    #[cfg(feature = "serde")]
2924    #[cfg(feature = "alloc")]
2925    #[test]
2926    #[allow(clippy::inconsistent_digit_grouping)] // Group to show 100,000,000 sats per bitcoin.
2927    fn serde_as_sat_opt() {
2928        use serde_json;
2929
2930        #[derive(Serialize, Deserialize, PartialEq, Debug, Eq)]
2931        struct T {
2932            #[serde(default, with = "crate::amount::serde::as_sat::opt")]
2933            pub amt: Option<Amount>,
2934            #[serde(default, with = "crate::amount::serde::as_sat::opt")]
2935            pub samt: Option<SignedAmount>,
2936        }
2937
2938        let with = T {
2939            amt: Some(Amount::from_sat(2_500_000_00)),
2940            samt: Some(SignedAmount::from_sat(-2_500_000_00)),
2941        };
2942        let without = T { amt: None, samt: None };
2943
2944        // Test Roundtripping
2945        for s in [&with, &without].iter() {
2946            let v = serde_json::to_string(s).unwrap();
2947            let w: T = serde_json::from_str(&v).unwrap();
2948            assert_eq!(w, **s);
2949        }
2950
2951        let t: T = serde_json::from_str("{\"amt\": 250000000, \"samt\": -250000000}").unwrap();
2952        assert_eq!(t, with);
2953
2954        let t: T = serde_json::from_str("{}").unwrap();
2955        assert_eq!(t, without);
2956
2957        let value_with: serde_json::Value =
2958            serde_json::from_str("{\"amt\": 250000000, \"samt\": -250000000}").unwrap();
2959        assert_eq!(with, serde_json::from_value(value_with).unwrap());
2960
2961        let value_without: serde_json::Value = serde_json::from_str("{}").unwrap();
2962        assert_eq!(without, serde_json::from_value(value_without).unwrap());
2963    }
2964
2965    #[test]
2966    fn sum_amounts() {
2967        assert_eq!(Amount::from_sat(0), [].into_iter().sum::<Amount>());
2968        assert_eq!(SignedAmount::from_sat(0), [].into_iter().sum::<SignedAmount>());
2969
2970        let amounts = [Amount::from_sat(42), Amount::from_sat(1337), Amount::from_sat(21)];
2971        let sum = amounts.into_iter().sum::<Amount>();
2972        assert_eq!(Amount::from_sat(1400), sum);
2973
2974        let amounts =
2975            [SignedAmount::from_sat(-42), SignedAmount::from_sat(1337), SignedAmount::from_sat(21)];
2976        let sum = amounts.into_iter().sum::<SignedAmount>();
2977        assert_eq!(SignedAmount::from_sat(1316), sum);
2978    }
2979
2980    #[test]
2981    fn checked_sum_amounts() {
2982        assert_eq!(Some(Amount::from_sat(0)), [].into_iter().checked_sum());
2983        assert_eq!(Some(SignedAmount::from_sat(0)), [].into_iter().checked_sum());
2984
2985        let amounts = [Amount::from_sat(42), Amount::from_sat(1337), Amount::from_sat(21)];
2986        let sum = amounts.into_iter().checked_sum();
2987        assert_eq!(Some(Amount::from_sat(1400)), sum);
2988
2989        let amounts = [Amount::from_sat(u64::MAX), Amount::from_sat(1337), Amount::from_sat(21)];
2990        let sum = amounts.into_iter().checked_sum();
2991        assert_eq!(None, sum);
2992
2993        let amounts = [
2994            SignedAmount::from_sat(i64::MIN),
2995            SignedAmount::from_sat(-1),
2996            SignedAmount::from_sat(21),
2997        ];
2998        let sum = amounts.into_iter().checked_sum();
2999        assert_eq!(None, sum);
3000
3001        let amounts = [
3002            SignedAmount::from_sat(i64::MAX),
3003            SignedAmount::from_sat(1),
3004            SignedAmount::from_sat(21),
3005        ];
3006        let sum = amounts.into_iter().checked_sum();
3007        assert_eq!(None, sum);
3008
3009        let amounts =
3010            [SignedAmount::from_sat(42), SignedAmount::from_sat(3301), SignedAmount::from_sat(21)];
3011        let sum = amounts.into_iter().checked_sum();
3012        assert_eq!(Some(SignedAmount::from_sat(3364)), sum);
3013    }
3014
3015    #[test]
3016    fn denomination_string_acceptable_forms() {
3017        // Non-exhaustive list of valid forms.
3018        let valid = [
3019            "BTC", "btc", "mBTC", "mbtc", "uBTC", "ubtc", "SATOSHI", "satoshi", "SATOSHIS",
3020            "satoshis", "SAT", "sat", "SATS", "sats", "bit", "bits", "nBTC", "pBTC",
3021        ];
3022        for denom in valid.iter() {
3023            assert!(Denomination::from_str(denom).is_ok());
3024        }
3025    }
3026
3027    #[test]
3028    fn disallow_confusing_forms() {
3029        let confusing = ["Msat", "Msats", "MSAT", "MSATS", "MSat", "MSats", "MBTC", "Mbtc", "PBTC"];
3030        for denom in confusing.iter() {
3031            match Denomination::from_str(denom) {
3032                Ok(_) => panic!("from_str should error for {}", denom),
3033                Err(ParseDenominationError::PossiblyConfusing(_)) => {}
3034                Err(e) => panic!("unexpected error: {}", e),
3035            }
3036        }
3037    }
3038
3039    #[test]
3040    fn disallow_unknown_denomination() {
3041        // Non-exhaustive list of unknown forms.
3042        let unknown = ["NBTC", "UBTC", "ABC", "abc", "cBtC", "Sat", "Sats"];
3043        for denom in unknown.iter() {
3044            match Denomination::from_str(denom) {
3045                Ok(_) => panic!("from_str should error for {}", denom),
3046                Err(ParseDenominationError::Unknown(_)) => (),
3047                Err(e) => panic!("unexpected error: {}", e),
3048            }
3049        }
3050    }
3051
3052    #[test]
3053    #[cfg(feature = "alloc")]
3054    fn trailing_zeros_for_amount() {
3055        assert_eq!(format!("{}", Amount::ONE_SAT), "0.00000001 BTC");
3056        assert_eq!(format!("{}", Amount::ONE_BTC), "1 BTC");
3057        assert_eq!(format!("{}", Amount::from_sat(1)), "0.00000001 BTC");
3058        assert_eq!(format!("{}", Amount::from_sat(10)), "0.00000010 BTC");
3059        assert_eq!(format!("{:.2}", Amount::from_sat(10)), "0.0000001 BTC");
3060        assert_eq!(format!("{:.2}", Amount::from_sat(100)), "0.000001 BTC");
3061        assert_eq!(format!("{:.2}", Amount::from_sat(1000)), "0.00001 BTC");
3062        assert_eq!(format!("{:.2}", Amount::from_sat(10_000)), "0.0001 BTC");
3063        assert_eq!(format!("{:.2}", Amount::from_sat(100_000)), "0.001 BTC");
3064        assert_eq!(format!("{:.2}", Amount::from_sat(1_000_000)), "0.01 BTC");
3065        assert_eq!(format!("{:.2}", Amount::from_sat(10_000_000)), "0.10 BTC");
3066        assert_eq!(format!("{:.2}", Amount::from_sat(100_000_000)), "1.00 BTC");
3067        assert_eq!(format!("{}", Amount::from_sat(100_000_000)), "1 BTC");
3068        assert_eq!(format!("{}", Amount::from_sat(40_000_000_000)), "400 BTC");
3069        assert_eq!(format!("{:.10}", Amount::from_sat(100_000_000)), "1.0000000000 BTC");
3070        assert_eq!(format!("{}", Amount::from_sat(400_000_000_000_010)), "4000000.00000010 BTC");
3071        assert_eq!(format!("{}", Amount::from_sat(400_000_000_000_000)), "4000000 BTC");
3072    }
3073}