Skip to content

Commit

Permalink
Add a utility function to convert currency.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenlu committed Oct 30, 2023
1 parent a778964 commit 0c8d8cc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lightspark/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub enum Error {
CryptoError(CryptoError),
WebhookSignatureError,
SigningKeyNotFound,
InvalidCurrencyConversion,
}

impl fmt::Display for Error {
Expand All @@ -31,6 +32,7 @@ impl fmt::Display for Error {
write!(f, "Webhook message hash does not match signature")
}
Self::SigningKeyNotFound => write!(f, "Signing key not found"),
Self::InvalidCurrencyConversion => write!(f, "Invalid currency conversion"),
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions lightspark/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,7 @@ pub mod objects;
pub mod request;
#[cfg(feature = "base")]
pub mod types;
#[cfg(feature = "objects")]
pub mod utils;
#[cfg(feature = "webhooks")]
pub mod webhooks;
19 changes: 19 additions & 0 deletions lightspark/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use crate::{error::Error, objects::currency_amount::CurrencyAmount};

pub fn value_millisatoshi(amount: CurrencyAmount) -> Result<i64, Error> {
match amount.original_unit {
crate::objects::currency_unit::CurrencyUnit::Bitcoin => {
Ok(amount.original_value * 100_000_000_000)
}
crate::objects::currency_unit::CurrencyUnit::Satoshi => Ok(amount.original_value * 1000),
crate::objects::currency_unit::CurrencyUnit::Millisatoshi => Ok(amount.original_value),
crate::objects::currency_unit::CurrencyUnit::Usd => Err(Error::InvalidCurrencyConversion),
crate::objects::currency_unit::CurrencyUnit::Nanobitcoin => Ok(amount.original_value * 100),
crate::objects::currency_unit::CurrencyUnit::Microbitcoin => {
Ok(amount.original_value * 100_000)
}
crate::objects::currency_unit::CurrencyUnit::Millibitcoin => {
Ok(amount.original_value * 100_000_000)
}
}
}

0 comments on commit 0c8d8cc

Please sign in to comment.