nostr_types/types/
satoshi.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use derive_more::{AsMut, AsRef, Deref, Display, From, Into};
use serde::{Deserialize, Serialize};
#[cfg(feature = "speedy")]
use speedy::{Readable, Writable};
use std::ops::Add;

/// Bitcoin amount measured in millisatoshi
#[derive(
    AsMut,
    AsRef,
    Clone,
    Copy,
    Debug,
    Deref,
    Deserialize,
    Display,
    Eq,
    From,
    Into,
    Ord,
    PartialEq,
    PartialOrd,
    Serialize,
)]
#[cfg_attr(feature = "speedy", derive(Readable, Writable))]
pub struct MilliSatoshi(pub u64);

impl MilliSatoshi {
    // Mock data for testing
    #[allow(dead_code)]
    pub(crate) fn mock() -> MilliSatoshi {
        MilliSatoshi(15423000)
    }
}

impl Add<MilliSatoshi> for MilliSatoshi {
    type Output = Self;

    fn add(self, rhs: MilliSatoshi) -> Self::Output {
        MilliSatoshi(self.0 + rhs.0)
    }
}

#[cfg(test)]
mod test {
    use super::*;

    test_serde! {MilliSatoshi, test_millisatoshi_serde}

    #[test]
    fn test_millisatoshi_math() {
        let a = MilliSatoshi(15000);
        let b = MilliSatoshi(3000);
        let c = a + b;
        assert_eq!(c.0, 18000);
    }
}