Merge rust-bitcoin/rust-bitcoin#1715: Add CentiBitcoin to denominations

39e528fdfc Add CentiBitcoin to denominations (yancy)

Pull request description:

  I was looking to maintain compatibility easily with core tests, and many of the tests use [cent](40e1c4d402/src/test/util/setup_common.h (L74)) denominations.  It would be nice to be able to use cent as a denomination in rust-bitcoin as well.

ACKs for top commit:
  apoelstra:
    ACK 39e528fdfc
  tcharding:
    ACK 39e528fdfc
  Kixunil:
    ACK 39e528fdfc

Tree-SHA512: 0514592c9924cbae2370a6186e36d5bac825be59a1fcfdb007fd1f0f08d6ff952a04c0ab0cc7736c29ff6fd0cb7ee96f27f09a79ca3f29ed5775fe3446701889
This commit is contained in:
Andrew Poelstra 2023-03-21 16:57:24 +00:00
commit d830e8d542
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 9 additions and 0 deletions

View File

@ -21,6 +21,7 @@ use crate::prelude::*;
/// # use bitcoin::Amount; /// # use bitcoin::Amount;
/// ///
/// assert_eq!(Amount::from_str("1 BTC").unwrap(), Amount::from_sat(100_000_000)); /// assert_eq!(Amount::from_str("1 BTC").unwrap(), Amount::from_sat(100_000_000));
/// assert_eq!(Amount::from_str("1 cBTC").unwrap(), Amount::from_sat(1_000_000));
/// assert_eq!(Amount::from_str("1 mBTC").unwrap(), Amount::from_sat(100_000)); /// assert_eq!(Amount::from_str("1 mBTC").unwrap(), Amount::from_sat(100_000));
/// assert_eq!(Amount::from_str("1 uBTC").unwrap(), Amount::from_sat(100)); /// assert_eq!(Amount::from_str("1 uBTC").unwrap(), Amount::from_sat(100));
/// assert_eq!(Amount::from_str("10 nBTC").unwrap(), Amount::from_sat(1)); /// assert_eq!(Amount::from_str("10 nBTC").unwrap(), Amount::from_sat(1));
@ -33,6 +34,8 @@ use crate::prelude::*;
pub enum Denomination { pub enum Denomination {
/// BTC /// BTC
Bitcoin, Bitcoin,
/// cBTC
CentiBitcoin,
/// mBTC /// mBTC
MilliBitcoin, MilliBitcoin,
/// uBTC /// uBTC
@ -54,6 +57,7 @@ impl Denomination {
fn precision(self) -> i8 { fn precision(self) -> i8 {
match self { match self {
Denomination::Bitcoin => -8, Denomination::Bitcoin => -8,
Denomination::CentiBitcoin => -6,
Denomination::MilliBitcoin => -5, Denomination::MilliBitcoin => -5,
Denomination::MicroBitcoin => -2, Denomination::MicroBitcoin => -2,
Denomination::NanoBitcoin => 1, Denomination::NanoBitcoin => 1,
@ -68,6 +72,7 @@ impl Denomination {
fn as_str(self) -> &'static str { fn as_str(self) -> &'static str {
match self { match self {
Denomination::Bitcoin => "BTC", Denomination::Bitcoin => "BTC",
Denomination::CentiBitcoin => "cBTC",
Denomination::MilliBitcoin => "mBTC", Denomination::MilliBitcoin => "mBTC",
Denomination::MicroBitcoin => "uBTC", Denomination::MicroBitcoin => "uBTC",
Denomination::NanoBitcoin => "nBTC", Denomination::NanoBitcoin => "nBTC",
@ -115,6 +120,10 @@ fn denomination_from_str(mut s: &str) -> Option<Denomination> {
return Some(Denomination::Bitcoin); return Some(Denomination::Bitcoin);
} }
if s.eq_ignore_ascii_case("cBTC") {
return Some(Denomination::CentiBitcoin);
}
if s.eq_ignore_ascii_case("mBTC") { if s.eq_ignore_ascii_case("mBTC") {
return Some(Denomination::MilliBitcoin); return Some(Denomination::MilliBitcoin);
} }