From 5c5d0bf8880bdf5d547011aa2bb39088bce00acb Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Sun, 17 Dec 2023 23:17:47 +0000 Subject: [PATCH] refactor: remove bitcoin amount from Amount --- crates/cashu-sdk/src/mint.rs | 10 ++++---- crates/cashu-sdk/src/wallet.rs | 5 ++-- crates/cashu/src/amount.rs | 44 +++++++++++----------------------- crates/cashu/src/nuts/nut00.rs | 14 ++++------- crates/cashu/src/nuts/nut01.rs | 4 ++-- crates/cashu/src/nuts/nut02.rs | 2 +- 6 files changed, 28 insertions(+), 51 deletions(-) diff --git a/crates/cashu-sdk/src/mint.rs b/crates/cashu-sdk/src/mint.rs index d1037ec8..ede40f31 100644 --- a/crates/cashu-sdk/src/mint.rs +++ b/crates/cashu-sdk/src/mint.rs @@ -231,13 +231,13 @@ impl Mint { pub fn verify_melt_request(&mut self, melt_request: &MeltBolt11Request) -> Result<(), Error> { let quote = self.quotes.get(&melt_request.quote).unwrap(); - let proofs_total = melt_request.proofs_amount().to_sat(); + let proofs_total = melt_request.proofs_amount(); let required_total = quote.amount + quote.fee_reserve; - if proofs_total < required_total { + if proofs_total < required_total.into() { debug!( - "Insufficient Proofs: Got: {}, Required: {}", + "Insufficient Proofs: Got: {:?}, Required: {}", proofs_total, required_total ); return Err(Error::Amount); @@ -301,8 +301,8 @@ impl Mint { change = Some(change_sigs); } else { info!( - "No change outputs provided. Burnt: {} sats", - (melt_request.proofs_amount() - total_spent).to_sat() + "No change outputs provided. Burnt: {:?} sats", + (melt_request.proofs_amount() - total_spent) ); } diff --git a/crates/cashu-sdk/src/wallet.rs b/crates/cashu-sdk/src/wallet.rs index 0673c8f5..fa1ca0c6 100644 --- a/crates/cashu-sdk/src/wallet.rs +++ b/crates/cashu-sdk/src/wallet.rs @@ -260,9 +260,8 @@ impl Wallet { if send_amount.ne(&amount) { warn!( - "Send amount proofs is {} expected {}", - send_amount.to_sat(), - amount.to_sat() + "Send amount proofs is {:?} expected {:?}", + send_amount, amount ); } diff --git a/crates/cashu/src/amount.rs b/crates/cashu/src/amount.rs index 211ba276..13a0e871 100644 --- a/crates/cashu/src/amount.rs +++ b/crates/cashu/src/amount.rs @@ -4,14 +4,14 @@ use serde::{Deserialize, Serialize}; /// Number of satoshis #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)] #[serde(transparent)] -pub struct Amount(#[serde(with = "bitcoin::amount::serde::as_sat")] bitcoin::Amount); +pub struct Amount(u64); impl Amount { - pub const ZERO: Amount = Amount(bitcoin::Amount::ZERO); + pub const ZERO: Amount = Amount(0); /// Split into parts that are powers of two pub fn split(&self) -> Vec { - let sats = self.0.to_sat(); + let sats = self.0; (0_u64..64) .rev() .filter_map(|bit| { @@ -20,22 +20,6 @@ impl Amount { }) .collect() } - - pub fn to_sat(&self) -> u64 { - self.0.to_sat() - } - - pub fn to_msat(&self) -> u64 { - self.0.to_sat() * 1000 - } - - pub fn from_sat(sat: u64) -> Self { - Self(bitcoin::Amount::from_sat(sat)) - } - - pub fn from_msat(msat: u64) -> Self { - Self(bitcoin::Amount::from_sat(msat / 1000)) - } } impl Default for Amount { @@ -46,13 +30,13 @@ impl Default for Amount { impl From for Amount { fn from(value: u64) -> Self { - Self(bitcoin::Amount::from_sat(value)) + Self(value) } } impl From for u64 { fn from(value: Amount) -> Self { - value.0.to_sat() + value.0 } } @@ -80,7 +64,7 @@ impl std::ops::Sub for Amount { impl core::iter::Sum for Amount { fn sum>(iter: I) -> Self { - let sats: u64 = iter.map(|amt| amt.0.to_sat()).sum(); + let sats: u64 = iter.map(|amt| amt.0).sum(); Amount::from(sats) } } @@ -91,18 +75,18 @@ mod tests { #[test] fn test_split_amount() { - assert_eq!(Amount::from_sat(1).split(), vec![Amount::from_sat(1)]); - assert_eq!(Amount::from_sat(2).split(), vec![Amount::from_sat(2)]); + assert_eq!(Amount::from(1).split(), vec![Amount::from(1)]); + assert_eq!(Amount::from(2).split(), vec![Amount::from(2)]); assert_eq!( - Amount::from_sat(3).split(), - vec![Amount::from_sat(2), Amount::from_sat(1)] + Amount::from(3).split(), + vec![Amount::from(2), Amount::from(1)] ); - let amounts: Vec = [8, 2, 1].iter().map(|a| Amount::from_sat(*a)).collect(); - assert_eq!(Amount::from_sat(11).split(), amounts); + let amounts: Vec = [8, 2, 1].iter().map(|a| Amount::from(*a)).collect(); + assert_eq!(Amount::from(11).split(), amounts); let amounts: Vec = [128, 64, 32, 16, 8, 4, 2, 1] .iter() - .map(|a| Amount::from_sat(*a)) + .map(|a| Amount::from(*a)) .collect(); - assert_eq!(Amount::from_sat(255).split(), amounts); + assert_eq!(Amount::from(255).split(), amounts); } } diff --git a/crates/cashu/src/nuts/nut00.rs b/crates/cashu/src/nuts/nut00.rs index c4a6fa7b..e1df11f0 100644 --- a/crates/cashu/src/nuts/nut00.rs +++ b/crates/cashu/src/nuts/nut00.rs @@ -184,13 +184,7 @@ pub mod wallet { /// Blank Outputs used for NUT-08 change pub fn blank(keyset_id: Id, fee_reserve: Amount) -> Result { - let fee_reserve = bitcoin::Amount::from_sat(fee_reserve.to_sat()); - - let count = (fee_reserve - .to_float_in(bitcoin::Denomination::Satoshi) - .log2() - .ceil() as u64) - .max(1); + let count = ((u64::from(fee_reserve) as f64).log2().ceil() as u64).max(1); let mut output = Vec::with_capacity(count as usize); @@ -300,7 +294,7 @@ pub mod wallet { } } - (amount.to_sat(), self.token[0].mint.to_string()) + (amount.into(), self.token[0].mint.to_string()) } } @@ -482,11 +476,11 @@ mod tests { #[test] fn test_blank_blinded_messages() { // TODO: Need to update id to new type in proof - let b = PreMintSecrets::blank(Id::from_str("").unwrap(), Amount::from_sat(1000)).unwrap(); + let b = PreMintSecrets::blank(Id::from_str("").unwrap(), Amount::from(1000)).unwrap(); assert_eq!(b.len(), 10); // TODO: Need to update id to new type in proof - let b = PreMintSecrets::blank(Id::from_str("").unwrap(), Amount::from_sat(1)).unwrap(); + let b = PreMintSecrets::blank(Id::from_str("").unwrap(), Amount::from(1)).unwrap(); assert_eq!(b.len(), 1); } } diff --git a/crates/cashu/src/nuts/nut01.rs b/crates/cashu/src/nuts/nut01.rs index 0d81032b..cc24fc74 100644 --- a/crates/cashu/src/nuts/nut01.rs +++ b/crates/cashu/src/nuts/nut01.rs @@ -151,11 +151,11 @@ impl<'de> serde::de::Deserialize<'de> for KeysResponse { let mut keys: BTreeMap = BTreeMap::new(); while let Some((a, k)) = m.next_entry::()? { - let amount = a.parse(); + let amount = a.parse::(); let pub_key = PublicKey::from_hex(k); if let (Ok(amount), Ok(pubkey)) = (amount, pub_key) { - let amount = Amount::from_sat(amount); + let amount = Amount::from(amount); keys.insert(amount, pubkey); } diff --git a/crates/cashu/src/nuts/nut02.rs b/crates/cashu/src/nuts/nut02.rs index abcd20df..bb9fa8fd 100644 --- a/crates/cashu/src/nuts/nut02.rs +++ b/crates/cashu/src/nuts/nut02.rs @@ -237,7 +237,7 @@ pub mod mint { engine.input(derivation_path.into().as_bytes()); for i in 0..max_order { - let amount = Amount::from_sat(2_u64.pow(i as u32)); + let amount = Amount::from(2_u64.pow(i as u32)); // Reuse midstate let mut e = engine.clone();