From c048272b16da20cd0df9c1a1efa8ca8fadfea1bc Mon Sep 17 00:00:00 2001 From: lunar-mining Date: Thu, 30 Sep 2021 17:14:10 +0200 Subject: [PATCH] util: decimal() returns usize --- src/util/parse.rs | 62 +++++++++++++++++++++++++++++++++++++++++- src/util/token_list.rs | 2 +- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/src/util/parse.rs b/src/util/parse.rs index 28b0b59ac..30850d38f 100644 --- a/src/util/parse.rs +++ b/src/util/parse.rs @@ -65,7 +65,7 @@ pub fn assign_id(network: &str, token: &str, tokenlist: TokenList) -> Result Result { +pub fn decimals(network: &str, token: &str, tokenlist: TokenList) -> Result { match NetworkName::from_str(network)? { NetworkName::Solana => match token { "solana" | "sol" => { @@ -101,6 +101,65 @@ pub fn symbol_to_id(token: &str, tokenlist: TokenList) -> Result { } } +//pub fn decode_base10(amount: &str, decimals: usize) -> Result { +// 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); +// +// let amount_vec: Vec = vec![0; decimals]; +// +// // convert to an integer +// for i in amount.chars() { +// let digit = i.to_digit(RADIX).unwrap(); +// amount_vec.push(digit); +// } +// +// Ok(amount_vec.drain()) +//} + mod tests { #[test] fn decode_base10() { @@ -110,6 +169,7 @@ mod tests { let input = "2.5"; println!("Initial input: {}", input); + let mut input_str = input.to_string(); // remove the decimal point diff --git a/src/util/token_list.rs b/src/util/token_list.rs index c0ae0e9c1..1bd902b67 100644 --- a/src/util/token_list.rs +++ b/src/util/token_list.rs @@ -29,7 +29,7 @@ impl TokenList { unreachable!(); } - pub fn search_decimal(self, symbol: &str) -> Result { + pub fn search_decimal(self, symbol: &str) -> Result { let tokens = self.tokenlist["tokens"] .as_array() .ok_or(Error::TokenParseError)?;