nostr_types/types/
satoshi.rs

1use derive_more::{AsMut, AsRef, Deref, Display, From, Into};
2use serde::{Deserialize, Serialize};
3#[cfg(feature = "speedy")]
4use speedy::{Readable, Writable};
5use std::ops::Add;
6
7/// Bitcoin amount measured in millisatoshi
8#[derive(
9    AsMut,
10    AsRef,
11    Clone,
12    Copy,
13    Debug,
14    Deref,
15    Deserialize,
16    Display,
17    Eq,
18    From,
19    Into,
20    Ord,
21    PartialEq,
22    PartialOrd,
23    Serialize,
24)]
25#[cfg_attr(feature = "speedy", derive(Readable, Writable))]
26pub struct MilliSatoshi(pub u64);
27
28impl MilliSatoshi {
29    // Mock data for testing
30    #[allow(dead_code)]
31    pub(crate) fn mock() -> MilliSatoshi {
32        MilliSatoshi(15423000)
33    }
34}
35
36impl Add<MilliSatoshi> for MilliSatoshi {
37    type Output = Self;
38
39    fn add(self, rhs: MilliSatoshi) -> Self::Output {
40        MilliSatoshi(self.0 + rhs.0)
41    }
42}
43
44#[cfg(test)]
45mod test {
46    use super::*;
47
48    test_serde! {MilliSatoshi, test_millisatoshi_serde}
49
50    #[test]
51    fn test_millisatoshi_math() {
52        let a = MilliSatoshi(15000);
53        let b = MilliSatoshi(3000);
54        let c = a + b;
55        assert_eq!(c.0, 18000);
56    }
57}