Skip to main content

tw_chain/utils/
mod.rs

1use std::collections::BTreeMap;
2
3use crate::constants::{D_DISPLAY_PLACES, TOTAL_TOKENS};
4use crate::primitives::asset::TokenAmount;
5
6// ------- MODS ------- //
7
8pub mod druid_utils;
9pub mod error_utils;
10pub mod script_utils;
11pub mod test_utils;
12pub mod transaction_utils;
13
14// ------- FUNCTIONS ------- //
15
16/// Determines whether the passed value is within bounds of
17/// available tokens in the supply.
18///
19/// TODO: Currently placeholder, needs to be filled in once requirements known
20pub fn is_valid_amount(_value: &TokenAmount) -> bool {
21    true
22}
23
24/// Formats an incoming value to be displayed
25///
26/// ### Arguments
27///
28/// * `value`   - Value to format for display
29pub fn format_for_display(value: &u64) -> String {
30    if value < &TOTAL_TOKENS {
31        let value_f64 = *value as f64;
32        return (value_f64 / D_DISPLAY_PLACES).to_string();
33    }
34
35    "Value out of bounds".to_string()
36}
37
38/// Create a single `BTreeMap<E, T>` struct from two `BTreeMap<E, T>` structs
39/// , summing the values of `T` for each corresponding entry `E`
40///
41/// ### Arguments
42///
43/// * `m1` - First map
44/// * `m2` - Second map
45pub fn add_btreemap<E: Ord, T: Copy + std::ops::AddAssign>(
46    m1: &mut BTreeMap<E, T>,
47    m2: BTreeMap<E, T>,
48) -> &BTreeMap<E, T> {
49    m2.into_iter().for_each(|(key, value)| {
50        m1.entry(key).and_modify(|e| *e += value).or_insert(value);
51    });
52    m1
53}