dpp/util/
units.rs

1#[macro_export]
2macro_rules! dash_to_credits {
3    // The macro takes a string literal representing the Dash amount.
4    ($dash:expr) => {{
5        let dash_str = stringify!($dash);
6
7        // Parsing the input string to separate the whole and fractional parts.
8        let parts: Vec<&str> = dash_str.split('.').collect();
9        let mut credits: u128 = 0;
10
11        // Process the whole number part if it exists.
12        if let Some(whole) = parts.get(0) {
13            if let Ok(whole_number) = whole.parse::<u128>() {
14                credits += whole_number * 100_000_000_000; // Whole Dash amount to credits
15            }
16        }
17
18        // Process the fractional part if it exists.
19        if let Some(fraction) = parts.get(1) {
20            let fraction_length = fraction.len();
21            let fraction_number = fraction.parse::<u128>().unwrap_or(0);
22            // Calculate the multiplier based on the number of digits in the fraction.
23            let multiplier = 10u128.pow(11 - fraction_length as u32);
24            credits += fraction_number * multiplier; // Fractional Dash to credits
25        }
26
27        credits as u64
28    }};
29}
30
31#[macro_export]
32macro_rules! dash_to_duffs {
33    // The macro takes a string literal representing the Dash amount.
34    ($dash:expr) => {{
35        let dash_str = stringify!($dash);
36
37        // Parsing the input string to separate the whole and fractional parts.
38        let parts: Vec<&str> = dash_str.split('.').collect();
39        let mut credits: u128 = 0;
40
41        // Process the whole number part if it exists.
42        if let Some(whole) = parts.get(0) {
43            if let Ok(whole_number) = whole.parse::<u128>() {
44                credits += whole_number * 100_000_000; // Whole Dash amount to credits
45            }
46        }
47
48        // Process the fractional part if it exists.
49        if let Some(fraction) = parts.get(1) {
50            let fraction_length = fraction.len();
51            let fraction_number = fraction.parse::<u128>().unwrap_or(0);
52            // Calculate the multiplier based on the number of digits in the fraction.
53            let multiplier = 10u128.pow(8 - fraction_length as u32);
54            credits += fraction_number * multiplier; // Fractional Dash to credits
55        }
56
57        credits as u64
58    }};
59}