-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Paresing appdata with json format v3 (#11)
* Parsion appdata with json format v3 * silence clippy * clippy corrections * nlordell's comments
- Loading branch information
Showing
5 changed files
with
148 additions
and
8 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use primitive_types::U256; | ||
use serde::{de, Deserializer, Serializer}; | ||
use std::fmt; | ||
|
||
// Code copied from here: https://github.com/cowprotocol/services/blob/main/crates/model/src/u256_decimal.rs | ||
// It was copied, as we prefer to not depend on such a big project. | ||
|
||
pub struct DecimalU256; | ||
|
||
pub fn serialize<S>(value: &U256, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
serializer.serialize_str(&value.to_string()) | ||
} | ||
|
||
pub fn deserialize<'de, D>(deserializer: D) -> Result<U256, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
struct Visitor {} | ||
impl<'de> de::Visitor<'de> for Visitor { | ||
type Value = U256; | ||
|
||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | ||
write!(formatter, "a u256 encoded as a decimal encoded string") | ||
} | ||
|
||
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E> | ||
where | ||
E: de::Error, | ||
{ | ||
U256::from_dec_str(s).map_err(|err| { | ||
de::Error::custom(format!("failed to decode {:?} as decimal u256: {}", s, err)) | ||
}) | ||
} | ||
} | ||
|
||
deserializer.deserialize_str(Visitor {}) | ||
} | ||
|
||
/// Converts an amount of units of an ERC20 token with the specified amount of | ||
/// decimals into its decimal representation as a string. | ||
/// | ||
pub fn format_units(amount: U256, decimals: usize) -> String { | ||
let str_amount = amount.to_string(); | ||
if decimals == 0 { | ||
str_amount | ||
} else if str_amount.len() <= decimals { | ||
format!("0.{:0>pad_left$}", str_amount, pad_left = decimals) | ||
} else { | ||
format!( | ||
"{}.{}", | ||
&str_amount[0..str_amount.len() - decimals], | ||
&str_amount[str_amount.len() - decimals..] | ||
) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_format_units() { | ||
assert_eq!(format_units(1_337u64.into(), 0), "1337"); | ||
assert_eq!(format_units(0u64.into(), 0), "0"); | ||
assert_eq!(format_units(0u64.into(), 1), "0.0"); | ||
assert_eq!(format_units(1u64.into(), 6), "0.000001"); | ||
assert_eq!(format_units(999_999u64.into(), 6), "0.999999"); | ||
assert_eq!(format_units(1_000_000u64.into(), 6), "1.000000"); | ||
assert_eq!(format_units(1_337_000u64.into(), 6), "1.337000"); | ||
assert_eq!( | ||
format_units(1_337_000_004_200u64.into(), 6), | ||
"1337000.004200" | ||
) | ||
} | ||
} |
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