Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to_unit into amount module #381

Merged
merged 2 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions crates/cdk-cln/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ pub enum Error {
/// Cln Rpc Error
#[error(transparent)]
ClnRpc(#[from] cln_rpc::RpcError),
/// Amount Error
#[error(transparent)]
Amount(#[from] cdk::amount::Error),
}

impl From<Error> for cdk::cdk_lightning::Error {
Expand Down
5 changes: 2 additions & 3 deletions crates/cdk-cln/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ use std::sync::Arc;
use std::time::Duration;

use async_trait::async_trait;
use cdk::amount::Amount;
use cdk::amount::{to_unit, Amount};
use cdk::cdk_lightning::{
self, to_unit, CreateInvoiceResponse, MintLightning, PayInvoiceResponse, PaymentQuoteResponse,
Settings,
self, CreateInvoiceResponse, MintLightning, PayInvoiceResponse, PaymentQuoteResponse, Settings,
};
use cdk::mint::FeeReserve;
use cdk::nuts::{
Expand Down
5 changes: 2 additions & 3 deletions crates/cdk-fake-wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ use std::sync::Arc;
use async_trait::async_trait;
use bitcoin::hashes::{sha256, Hash};
use bitcoin::secp256k1::{Secp256k1, SecretKey};
use cdk::amount::Amount;
use cdk::amount::{to_unit, Amount};
use cdk::cdk_lightning::{
self, to_unit, CreateInvoiceResponse, MintLightning, PayInvoiceResponse, PaymentQuoteResponse,
Settings,
self, CreateInvoiceResponse, MintLightning, PayInvoiceResponse, PaymentQuoteResponse, Settings,
};
use cdk::mint;
use cdk::mint::FeeReserve;
Expand Down
5 changes: 2 additions & 3 deletions crates/cdk-lnbits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ use std::sync::Arc;
use anyhow::anyhow;
use async_trait::async_trait;
use axum::Router;
use cdk::amount::Amount;
use cdk::amount::{to_unit, Amount, MSAT_IN_SAT};
use cdk::cdk_lightning::{
self, to_unit, CreateInvoiceResponse, MintLightning, PayInvoiceResponse, PaymentQuoteResponse,
Settings, MSAT_IN_SAT,
self, CreateInvoiceResponse, MintLightning, PayInvoiceResponse, PaymentQuoteResponse, Settings,
};
use cdk::mint::FeeReserve;
use cdk::nuts::{
Expand Down
5 changes: 2 additions & 3 deletions crates/cdk-lnd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ use std::sync::Arc;

use anyhow::anyhow;
use async_trait::async_trait;
use cdk::amount::Amount;
use cdk::amount::{to_unit, Amount, MSAT_IN_SAT};
use cdk::cdk_lightning::{
self, to_unit, CreateInvoiceResponse, MintLightning, PayInvoiceResponse, PaymentQuoteResponse,
Settings, MSAT_IN_SAT,
self, CreateInvoiceResponse, MintLightning, PayInvoiceResponse, PaymentQuoteResponse, Settings,
};
use cdk::mint::FeeReserve;
use cdk::nuts::{
Expand Down
5 changes: 2 additions & 3 deletions crates/cdk-phoenixd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ use std::sync::Arc;
use anyhow::anyhow;
use async_trait::async_trait;
use axum::Router;
use cdk::amount::Amount;
use cdk::amount::{to_unit, Amount, MSAT_IN_SAT};
use cdk::cdk_lightning::{
self, to_unit, CreateInvoiceResponse, MintLightning, PayInvoiceResponse, PaymentQuoteResponse,
Settings, MSAT_IN_SAT,
self, CreateInvoiceResponse, MintLightning, PayInvoiceResponse, PaymentQuoteResponse, Settings,
};
use cdk::mint::FeeReserve;
use cdk::nuts::{
Expand Down
72 changes: 72 additions & 0 deletions crates/cdk/src/amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use std::fmt;
use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::nuts::CurrencyUnit;

/// Amount Error
#[derive(Debug, Error)]
pub enum Error {
Expand All @@ -17,6 +19,9 @@ pub enum Error {
/// Amount overflow
#[error("Amount Overflow")]
AmountOverflow,
/// Cannot convert units
#[error("Cannot convert units")]
CannotConvertUnits,
}

/// Amount can be any unit
Expand Down Expand Up @@ -217,6 +222,30 @@ pub enum SplitTarget {
Values(Vec<Amount>),
}

/// Msats in sat
pub const MSAT_IN_SAT: u64 = 1000;

/// Helper function to convert units
pub fn to_unit<T>(
amount: T,
current_unit: &CurrencyUnit,
target_unit: &CurrencyUnit,
) -> Result<Amount, Error>
where
T: Into<u64>,
{
let amount = amount.into();
match (current_unit, target_unit) {
(CurrencyUnit::Sat, CurrencyUnit::Sat) => Ok(amount.into()),
(CurrencyUnit::Msat, CurrencyUnit::Msat) => Ok(amount.into()),
(CurrencyUnit::Sat, CurrencyUnit::Msat) => Ok((amount * MSAT_IN_SAT).into()),
(CurrencyUnit::Msat, CurrencyUnit::Sat) => Ok((amount / MSAT_IN_SAT).into()),
(CurrencyUnit::Usd, CurrencyUnit::Usd) => Ok(amount.into()),
(CurrencyUnit::Eur, CurrencyUnit::Eur) => Ok(amount.into()),
_ => Err(Error::CannotConvertUnits),
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -341,4 +370,47 @@ mod tests {

assert_eq!(total, 10001.into());
}

#[test]
fn test_amount_to_unit() {
let amount = Amount::from(1000);
let current_unit = CurrencyUnit::Sat;
let target_unit = CurrencyUnit::Msat;

let converted = to_unit(amount, &current_unit, &target_unit).unwrap();

assert_eq!(converted, 1000000.into());

let amount = Amount::from(1000);
let current_unit = CurrencyUnit::Msat;
let target_unit = CurrencyUnit::Sat;

let converted = to_unit(amount, &current_unit, &target_unit).unwrap();

assert_eq!(converted, 1.into());

let amount = Amount::from(1);
let current_unit = CurrencyUnit::Usd;
let target_unit = CurrencyUnit::Usd;

let converted = to_unit(amount, &current_unit, &target_unit).unwrap();

assert_eq!(converted, 1.into());

let amount = Amount::from(1);
let current_unit = CurrencyUnit::Eur;
let target_unit = CurrencyUnit::Eur;

let converted = to_unit(amount, &current_unit, &target_unit).unwrap();

assert_eq!(converted, 1.into());

let amount = Amount::from(1);
let current_unit = CurrencyUnit::Sat;
let target_unit = CurrencyUnit::Eur;

let converted = to_unit(amount, &current_unit, &target_unit);

assert!(converted.is_err());
}
}
30 changes: 3 additions & 27 deletions crates/cdk/src/cdk_lightning/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ pub enum Error {
/// Parse Error
#[error(transparent)]
Parse(#[from] ParseOrSemanticError),
/// Cannot convert units
#[error("Cannot convert units")]
CannotConvertUnits,
/// Amount Error
#[error(transparent)]
Amount(#[from] crate::amount::Error),
}

/// MintLighting Trait
Expand Down Expand Up @@ -151,27 +151,3 @@ pub struct Settings {
/// Invoice Description supported
pub invoice_description: bool,
}

/// Msats in sat
pub const MSAT_IN_SAT: u64 = 1000;

/// Helper function to convert units
pub fn to_unit<T>(
amount: T,
current_unit: &CurrencyUnit,
target_unit: &CurrencyUnit,
) -> Result<Amount, Error>
where
T: Into<u64>,
{
let amount = amount.into();
match (current_unit, target_unit) {
(CurrencyUnit::Sat, CurrencyUnit::Sat) => Ok(amount.into()),
(CurrencyUnit::Msat, CurrencyUnit::Msat) => Ok(amount.into()),
(CurrencyUnit::Sat, CurrencyUnit::Msat) => Ok((amount * MSAT_IN_SAT).into()),
(CurrencyUnit::Msat, CurrencyUnit::Sat) => Ok((amount / MSAT_IN_SAT).into()),
(CurrencyUnit::Usd, CurrencyUnit::Usd) => Ok(amount.into()),
(CurrencyUnit::Eur, CurrencyUnit::Eur) => Ok(amount.into()),
_ => Err(Error::CannotConvertUnits),
}
}
4 changes: 2 additions & 2 deletions crates/cdk/src/mint/melt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use crate::dhke::hash_to_curve;
use crate::nuts::nut11::enforce_sig_flag;
use crate::nuts::nut11::EnforceSigFlag;
use crate::{
cdk_lightning::to_unit, mint::SigFlag, nuts::Id, nuts::MeltQuoteState, types::LnKey,
util::unix_time, Amount, Error,
amount::to_unit, mint::SigFlag, nuts::Id, nuts::MeltQuoteState, types::LnKey, util::unix_time,
Amount, Error,
};

use super::nut05::MeltBolt11Response;
Expand Down
Loading