-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: improve crypto value converter readabilty
- Loading branch information
Showing
5 changed files
with
26 additions
and
27 deletions.
There are no files selected for viewing
33 changes: 16 additions & 17 deletions
33
gemstone/src/swapper/utils.rs → .../primitives/src/crypto_value_converter.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,65 @@ | ||
use num_bigint::BigInt; | ||
use std::str::FromStr; | ||
|
||
use num_bigint::BigInt; | ||
|
||
pub mod converter { | ||
use super::*; | ||
pub struct CryptoValueConverter {} | ||
|
||
impl CryptoValueConverter { | ||
pub fn value_from(value: String, decimals: i32) -> BigInt { | ||
let decimals = decimals - 8; | ||
if decimals > 0 { | ||
BigInt::from_str(value.as_str()).unwrap() / BigInt::from(10).pow(decimals as u32) | ||
BigInt::from_str(&value).unwrap() / BigInt::from(10).pow(decimals as u32) | ||
} else { | ||
BigInt::from_str(value.as_str()).unwrap() * BigInt::from(10).pow(decimals.unsigned_abs()) | ||
BigInt::from_str(&value).unwrap() * BigInt::from(10).pow(decimals.unsigned_abs()) | ||
} | ||
} | ||
|
||
pub fn value_to(value: String, decimals: i32) -> BigInt { | ||
let decimals = decimals - 8; | ||
if decimals > 0 { | ||
BigInt::from_str(value.as_str()).unwrap() * BigInt::from(10).pow((decimals).unsigned_abs()) | ||
BigInt::from_str(&value).unwrap() * BigInt::from(10).pow((decimals).unsigned_abs()) | ||
} else { | ||
BigInt::from_str(value.as_str()).unwrap() / BigInt::from(10).pow((decimals).unsigned_abs()) | ||
BigInt::from_str(&value).unwrap() / BigInt::from(10).pow((decimals).unsigned_abs()) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::converter; | ||
use super::*; | ||
use num_bigint::BigInt; | ||
use std::str::FromStr; | ||
|
||
#[test] | ||
fn test_value_from() { | ||
let value = "1000000000".to_string(); | ||
|
||
let result = converter::value_from(value.clone(), 18); | ||
let result = CryptoValueConverter::value_from(value.clone(), 18); | ||
assert_eq!(result, BigInt::from_str("0").unwrap()); | ||
|
||
let result = converter::value_from(value.clone(), 10); | ||
let result = CryptoValueConverter::value_from(value.clone(), 10); | ||
assert_eq!(result, BigInt::from_str("10000000").unwrap()); | ||
|
||
let result = converter::value_from(value.clone(), 6); | ||
let result = CryptoValueConverter::value_from(value.clone(), 6); | ||
assert_eq!(result, BigInt::from_str("100000000000").unwrap()); | ||
|
||
let result = converter::value_from(value.clone(), 8); | ||
let result = CryptoValueConverter::value_from(value.clone(), 8); | ||
assert_eq!(result, BigInt::from(1000000000)); | ||
} | ||
|
||
#[test] | ||
fn test_value_to() { | ||
let value = "10000000".to_string(); | ||
|
||
let result = converter::value_to(value.clone(), 18); | ||
let result = CryptoValueConverter::value_to(value.clone(), 18); | ||
assert_eq!(result, BigInt::from_str("100000000000000000").unwrap()); | ||
|
||
let result = converter::value_to(value.clone(), 10); | ||
let result = CryptoValueConverter::value_to(value.clone(), 10); | ||
assert_eq!(result, BigInt::from(1000000000)); | ||
|
||
let result = converter::value_to(value.clone(), 6); | ||
let result = CryptoValueConverter::value_to(value.clone(), 6); | ||
assert_eq!(result, BigInt::from(100000)); | ||
|
||
let result = converter::value_to(value.clone(), 8); | ||
let result = CryptoValueConverter::value_to(value.clone(), 8); | ||
assert_eq!(result, BigInt::from(10000000)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters