refactor: remove bitcoin amount from Amount

This commit is contained in:
thesimplekid
2023-12-17 23:17:47 +00:00
parent e76a8b9130
commit 5c5d0bf888
6 changed files with 28 additions and 51 deletions

View File

@@ -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)
);
}

View File

@@ -260,9 +260,8 @@ impl<C: Client> Wallet<C> {
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
);
}

View File

@@ -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<Self> {
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<u64> for Amount {
fn from(value: u64) -> Self {
Self(bitcoin::Amount::from_sat(value))
Self(value)
}
}
impl From<Amount> 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<I: Iterator<Item = Self>>(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<Amount> = [8, 2, 1].iter().map(|a| Amount::from_sat(*a)).collect();
assert_eq!(Amount::from_sat(11).split(), amounts);
let amounts: Vec<Amount> = [8, 2, 1].iter().map(|a| Amount::from(*a)).collect();
assert_eq!(Amount::from(11).split(), amounts);
let amounts: Vec<Amount> = [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);
}
}

View File

@@ -184,13 +184,7 @@ pub mod wallet {
/// Blank Outputs used for NUT-08 change
pub fn blank(keyset_id: Id, fee_reserve: Amount) -> Result<Self, wallet::Error> {
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);
}
}

View File

@@ -151,11 +151,11 @@ impl<'de> serde::de::Deserialize<'de> for KeysResponse {
let mut keys: BTreeMap<Amount, PublicKey> = BTreeMap::new();
while let Some((a, k)) = m.next_entry::<String, String>()? {
let amount = a.parse();
let amount = a.parse::<u64>();
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);
}

View File

@@ -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();