|
|
@@ -1,13 +1,14 @@
|
|
|
+use log::debug;
|
|
|
+use sha2::{Digest, Sha256};
|
|
|
+use std::iter::FromIterator;
|
|
|
+use std::str::FromStr;
|
|
|
+
|
|
|
use crate::{
|
|
|
serial::{deserialize, serialize},
|
|
|
util::{NetworkName, TokenList},
|
|
|
Error, Result,
|
|
|
};
|
|
|
|
|
|
-use log::debug;
|
|
|
-use sha2::{Digest, Sha256};
|
|
|
-use std::str::FromStr;
|
|
|
-
|
|
|
// hash the external token ID and NetworkName param.
|
|
|
// if fails, change the last 4 bytes and hash it again. keep repeating until it works.
|
|
|
pub fn generate_id(tkn_str: &str, network: &NetworkName) -> Result<jubjub::Fr> {
|
|
|
@@ -101,163 +102,98 @@ 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;
|
|
|
+fn is_digit(c: char) -> bool {
|
|
|
+ ('0'..='9').contains(&c)
|
|
|
+}
|
|
|
|
|
|
- let mut input_str = amount.to_string();
|
|
|
+fn char_eq(a: char, b: char) -> bool {
|
|
|
+ a == b
|
|
|
+}
|
|
|
|
|
|
- // remove the decimal point
|
|
|
- let mut amount: String = match input_str.find(".") {
|
|
|
- Some(v) => {
|
|
|
- input_str.remove(v);
|
|
|
- input_str
|
|
|
- }
|
|
|
- None => input_str,
|
|
|
- };
|
|
|
+pub fn decode_base10(amount: &str, decimal_places: usize, strict: bool) -> Result<u64> {
|
|
|
+ let mut s: Vec<char> = amount.to_string().chars().collect();
|
|
|
|
|
|
- // 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!")
|
|
|
- }
|
|
|
+ // Get rid of the decimal point:
|
|
|
+ let point: usize;
|
|
|
+ if let Some(p) = amount.find('.') {
|
|
|
+ s.remove(p);
|
|
|
+ point = p;
|
|
|
+ } else {
|
|
|
+ point = s.len();
|
|
|
}
|
|
|
|
|
|
- // add digits to the end if there are too few
|
|
|
- if amount.len() < decimals {
|
|
|
- loop {
|
|
|
- amount.push('0');
|
|
|
-
|
|
|
- if amount.len() == decimals {
|
|
|
- break;
|
|
|
- }
|
|
|
- continue;
|
|
|
+ // Only digits should remain
|
|
|
+ for i in &s {
|
|
|
+ if !is_digit(*i) {
|
|
|
+ return Err(Error::ParseFailed("Found non-digits"));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // remove digits from the end if there are too many
|
|
|
- if amount.len() > decimals {
|
|
|
- loop {
|
|
|
- amount.pop();
|
|
|
+ // Add digits to the end if there are too few:
|
|
|
+ let actual_places = s.len() - point;
|
|
|
+ if actual_places < decimal_places {
|
|
|
+ s.extend(vec!['0'; decimal_places - actual_places])
|
|
|
+ }
|
|
|
|
|
|
- if amount.len() == decimals {
|
|
|
+ // Remove digits from the end if there are too many:
|
|
|
+ let mut round = false;
|
|
|
+ if actual_places > decimal_places {
|
|
|
+ let end = point + decimal_places;
|
|
|
+ for i in &s[end..s.len()] {
|
|
|
+ if !char_eq(*i, '0') {
|
|
|
+ round = true;
|
|
|
break;
|
|
|
}
|
|
|
- continue;
|
|
|
}
|
|
|
+ s.truncate(end);
|
|
|
}
|
|
|
|
|
|
- println!("Resized amount: {}", amount);
|
|
|
+ if strict && round {
|
|
|
+ return Err(Error::ParseFailed("Would end up rounding while strict"));
|
|
|
+ }
|
|
|
|
|
|
- // convert to an integer
|
|
|
- let number = amount.parse::<u64>().unwrap();
|
|
|
+ // Convert to an integer
|
|
|
+ let number = u64::from_str(&String::from_iter(&s))?;
|
|
|
|
|
|
- Ok(number)
|
|
|
+ // Round and return
|
|
|
+ if round && number == u64::MAX {
|
|
|
+ return Err(Error::ParseFailed("u64 overflow"));
|
|
|
+ }
|
|
|
+
|
|
|
+ Ok(number + round as u64)
|
|
|
}
|
|
|
|
|
|
-// TODO: implement this
|
|
|
+pub fn encode_base10(amount: u64, decimal_places: usize) -> String {
|
|
|
+ let mut s: Vec<char> = format!("{:0width$}", amount, width = 1 + decimal_places)
|
|
|
+ .chars()
|
|
|
+ .collect();
|
|
|
+ s.insert(s.len() - decimal_places, '.');
|
|
|
|
|
|
-//fn encode_base10() {
|
|
|
-// let input = 100000000;
|
|
|
-// println!("Original input: {}", input);
|
|
|
-// let mut input_str = input.to_string();
|
|
|
-//
|
|
|
-// input_str.insert(1, '.');
|
|
|
-//
|
|
|
-// let amount = input_str.trim_end_matches('0');
|
|
|
-//
|
|
|
-// let amount = if amount.ends_with('.') == true {
|
|
|
-// let amount = amount.trim_end_matches('.');
|
|
|
-// amount
|
|
|
-// } else {
|
|
|
-// amount
|
|
|
-// };
|
|
|
-//
|
|
|
-// println!("Encoded output: {}", amount);
|
|
|
-//}
|
|
|
+ String::from_iter(&s)
|
|
|
+ .trim_end_matches('0')
|
|
|
+ .trim_end_matches('.')
|
|
|
+ .to_string()
|
|
|
+}
|
|
|
|
|
|
+#[allow(unused_imports)]
|
|
|
mod tests {
|
|
|
+ use crate::util::decode_base10;
|
|
|
+ use crate::util::encode_base10;
|
|
|
#[test]
|
|
|
- fn decode_base10() {
|
|
|
- const RADIX: u32 = 10;
|
|
|
- // TODO: this number varies per token
|
|
|
- let decimal_places = 10;
|
|
|
-
|
|
|
- let input = "2.5";
|
|
|
- println!("Initial input: {}", input);
|
|
|
-
|
|
|
- let mut input_str = input.to_string();
|
|
|
-
|
|
|
- // remove the decimal point
|
|
|
- let mut amount: String = match input_str.find(".") {
|
|
|
- Some(v) => {
|
|
|
- input_str.remove(v);
|
|
|
- input_str
|
|
|
- }
|
|
|
- None => {
|
|
|
- print!("Number isn't a float");
|
|
|
- input_str
|
|
|
- }
|
|
|
- };
|
|
|
-
|
|
|
- println!("Removed decimal point: {}", amount);
|
|
|
-
|
|
|
- // only digits should remain:
|
|
|
- for c in amount.chars() {
|
|
|
- if c.is_digit(RADIX) == false {
|
|
|
- println!("Amount is not valid digits!")
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // add digits to the end if there are too few
|
|
|
- if amount.len() < decimal_places {
|
|
|
- loop {
|
|
|
- amount.push('0');
|
|
|
-
|
|
|
- if amount.len() == decimal_places {
|
|
|
- break;
|
|
|
- }
|
|
|
- continue;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // remove digits from the end if there are too many
|
|
|
- if amount.len() > decimal_places {
|
|
|
- loop {
|
|
|
- amount.pop();
|
|
|
-
|
|
|
- if amount.len() == decimal_places {
|
|
|
- break;
|
|
|
- }
|
|
|
- continue;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- println!("Resized amount: {}", amount);
|
|
|
-
|
|
|
- // convert to an integer
|
|
|
- let number = amount.parse::<u64>().unwrap();
|
|
|
-
|
|
|
- println!("The final number: {:?}", number);
|
|
|
+ fn test_decode_base10() {
|
|
|
+ assert_eq!(124, decode_base10("12.33", 1, false).unwrap());
|
|
|
+ assert_eq!(1233000, decode_base10("12.33", 5, false).unwrap());
|
|
|
+ assert_eq!(1200000, decode_base10("12.", 5, false).unwrap());
|
|
|
+ assert_eq!(1200000, decode_base10("12", 5, false).unwrap());
|
|
|
+ assert!(decode_base10("12.33", 1, true).is_err());
|
|
|
}
|
|
|
|
|
|
#[test]
|
|
|
- fn encode_base10() {
|
|
|
- let input = 100000000;
|
|
|
- println!("Original input: {}", input);
|
|
|
- let mut input_str = input.to_string();
|
|
|
-
|
|
|
- input_str.insert(1, '.');
|
|
|
-
|
|
|
- let amount = input_str.trim_end_matches('0');
|
|
|
-
|
|
|
- let amount = if amount.ends_with('.') == true {
|
|
|
- let amount = amount.trim_end_matches('.');
|
|
|
- amount
|
|
|
- } else {
|
|
|
- amount
|
|
|
- };
|
|
|
-
|
|
|
- println!("Encoded output: {}", amount);
|
|
|
+ fn test_encode_base10() {
|
|
|
+ assert_eq!("23.4321111", &encode_base10(234321111, 7));
|
|
|
+ assert_eq!("23432111.1", &encode_base10(234321111, 1));
|
|
|
+ assert_eq!("234321.1", &encode_base10(2343211, 1));
|
|
|
+ assert_eq!("2343211", &encode_base10(2343211, 0));
|
|
|
+ assert_eq!("0.00002343", &encode_base10(2343, 8));
|
|
|
}
|
|
|
}
|