Просмотр исходного кода

replaced to_apo with decode_base10

lunar-mining 4 лет назад
Родитель
Сommit
77803f1040
3 измененных файлов с 77 добавлено и 62 удалено
  1. 3 3
      src/bin/darkfid.rs
  2. 1 1
      src/util/mod.rs
  3. 73 58
      src/util/parse.rs

+ 3 - 3
src/bin/darkfid.rs

@@ -17,7 +17,7 @@ use drk::{
         rpcserver::{listen_and_serve, RequestHandler, RpcServerConfig},
     },
     serial::{deserialize, serialize},
-    util::{assign_id, decimals, expand_path, join_config_path, to_apo, TokenList},
+    util::{assign_id, decimals, decode_base10, expand_path, join_config_path, TokenList},
     wallet::WalletDb,
     Result,
 };
@@ -300,11 +300,11 @@ impl Darkfid {
             return JsonResult::Err(jsonerr(InvalidParams, None, id));
         }
 
-        let amount = amount.as_f64().unwrap();
+        let amount = amount.as_str().unwrap();
 
         // TODO: get rid of these unwraps
         let decimals = decimals(network, token, self.tokenlist.clone()).unwrap();
-        let amount_in_apo = to_apo(amount, decimals as u32).unwrap();
+        let amount_in_apo = decode_base10(amount, decimals).unwrap();
 
         let token_id = match assign_id(&network, &token, self.tokenlist.clone()) {
             Ok(t) => t,

+ 1 - 1
src/util/mod.rs

@@ -4,6 +4,6 @@ pub mod path;
 pub mod token_list;
 
 pub use net_name::NetworkName;
-pub use parse::{assign_id, decimals, generate_id, to_apo};
+pub use parse::{assign_id, decimals, decode_base10, generate_id};
 pub use path::{expand_path, join_config_path};
 pub use token_list::TokenList;

+ 73 - 58
src/util/parse.rs

@@ -81,10 +81,10 @@ pub fn decimals(network: &str, token: &str, tokenlist: TokenList) -> Result<usiz
     }
 }
 
-pub fn to_apo(amount: f64, decimals: u32) -> Result<u64> {
-    let apo = amount as u64 * u64::pow(10, decimals as u32);
-    Ok(apo)
-}
+//pub fn to_apo(amount: f64, decimals: u32) -> Result<u64> {
+//    let apo = amount as u64 * u64::pow(10, decimals as u32);
+//    Ok(apo)
+//}
 
 pub fn symbol_to_id(token: &str, tokenlist: TokenList) -> Result<String> {
     let vec: Vec<char> = token.chars().collect();
@@ -101,64 +101,79 @@ pub fn symbol_to_id(token: &str, tokenlist: TokenList) -> Result<String> {
     }
 }
 
-//pub fn decode_base10(amount: &str, decimals: usize) -> Result<u64> {
-//    const RADIX: u32 = 10;
-//
-//    let mut input_str = amount.to_string();
-//
-//    // remove the decimal point
-//    let mut amount: String = match input_str.find(".") {
-//        Some(v) => {
-//            input_str.remove(v);
-//            input_str
-//        }
-//        None => input_str,
-//    };
-//
-//    // only digits should remain:
-//    for c in amount.chars() {
-//        if c.is_digit(RADIX) == false {
-//            // TODO: Make this an error
-//            println!("Amount is not valid digits!")
-//        }
-//    }
-//
-//    // add digits to the end if there are too few
-//    if amount.len() < decimals {
-//        loop {
-//            amount.push('0');
-//
-//            if amount.len() == decimals {
-//                break;
-//            }
-//            continue;
-//        }
-//    }
-//
-//    // remove digits from the end if there are too many
-//    if amount.len() > decimals {
-//        loop {
-//            amount.pop();
-//
-//            if amount.len() == decimals {
-//                break;
-//            }
-//            continue;
-//        }
-//    }
+pub fn decode_base10(amount: &str, decimals: usize) -> Result<u64> {
+    const RADIX: u32 = 10;
+
+    let mut input_str = amount.to_string();
+
+    // remove the decimal point
+    let mut amount: String = match input_str.find(".") {
+        Some(v) => {
+            input_str.remove(v);
+            input_str
+        }
+        None => input_str,
+    };
+
+    // only digits should remain:
+    for c in amount.chars() {
+        if c.is_digit(RADIX) == false {
+            // TODO: Make this an error
+            println!("Amount is not valid digits!")
+        }
+    }
+
+    // add digits to the end if there are too few
+    if amount.len() < decimals {
+        loop {
+            amount.push('0');
+
+            if amount.len() == decimals {
+                break;
+            }
+            continue;
+        }
+    }
+
+    // remove digits from the end if there are too many
+    if amount.len() > decimals {
+        loop {
+            amount.pop();
+
+            if amount.len() == decimals {
+                break;
+            }
+            continue;
+        }
+    }
+
+    println!("Resized amount: {}", amount);
+
+    // convert to an integer
+    let number = amount.parse::<u64>().unwrap();
+
+    Ok(number)
+}
+
+// TODO: implement this
+
+//fn encode_base10() {
+//    let input = 100000000;
+//    println!("Original input: {}", input);
+//    let mut input_str = input.to_string();
 //
-//    println!("Resized amount: {}", amount);
+//    input_str.insert(1, '.');
 //
-//    let amount_vec: Vec<u32> = vec![0; decimals];
+//    let amount = input_str.trim_end_matches('0');
 //
-//    // convert to an integer
-//    for i in amount.chars() {
-//        let digit = i.to_digit(RADIX).unwrap();
-//        amount_vec.push(digit);
-//    }
+//    let amount = if amount.ends_with('.') == true {
+//        let amount = amount.trim_end_matches('.');
+//        amount
+//    } else {
+//        amount
+//    };
 //
-//    let amount: u64 = amount_vec.drain();
-//    Ok(amount)
+//    println!("Encoded output: {}", amount);
 //}
 
 mod tests {