diff --git a/ethers-core/src/utils/units.rs b/ethers-core/src/utils/units.rs index 629908b2..82c55476 100644 --- a/ethers-core/src/utils/units.rs +++ b/ethers-core/src/utils/units.rs @@ -6,8 +6,16 @@ use std::{convert::TryFrom, fmt, str::FromStr}; pub enum Units { /// Wei is equivalent to 1 wei. Wei, + /// Kwei is equivalent to 1e3 wei. + Kwei, + /// Mwei is equivalent to 1e6 wei. + Mwei, /// Gwei is equivalent to 1e9 wei. Gwei, + /// Twei is equivalent to 1e12 wei. + Twei, + /// Pwei is equivalent to 1e15 wei. + Pwei, /// Ether is equivalent to 1e18 wei. Ether, /// Other less frequent unit sizes, equivalent to 1e{0} wei. @@ -74,7 +82,11 @@ impl FromStr for Units { fn from_str(s: &str) -> Result { Ok(match s.to_lowercase().as_str() { "eth" | "ether" => Units::Ether, - "gwei" | "nano" | "nanoether" => Units::Gwei, + "pwei" | "milli" | "milliether" | "finney" => Units::Pwei, + "twei" | "micro" | "microether" | "szabo" => Units::Twei, + "gwei" | "nano" | "nanoether" | "shannon" => Units::Gwei, + "mwei" | "pico" | "picoether" | "lovelace" => Units::Mwei, + "kwei" | "femto" | "femtoether" | "babbage" => Units::Kwei, "wei" => Units::Wei, _ => return Err(ConversionError::UnrecognizedUnits(s.to_string())), }) @@ -103,7 +115,11 @@ impl Units { pub fn as_num(&self) -> u32 { match self { Units::Wei => 0, + Units::Kwei => 3, + Units::Mwei => 6, Units::Gwei => 9, + Units::Twei => 12, + Units::Pwei => 15, Units::Ether => 18, Units::Other(inner) => *inner, } @@ -118,7 +134,11 @@ mod tests { #[test] fn test_units() { assert_eq!(Wei.as_num(), 0); + assert_eq!(Kwei.as_num(), 3); + assert_eq!(Mwei.as_num(), 6); assert_eq!(Gwei.as_num(), 9); + assert_eq!(Twei.as_num(), 12); + assert_eq!(Pwei.as_num(), 15); assert_eq!(Ether.as_num(), 18); assert_eq!(Other(10).as_num(), 10); assert_eq!(Other(20).as_num(), 20); @@ -127,15 +147,27 @@ mod tests { #[test] fn test_into() { assert_eq!(Units::try_from("wei").unwrap(), Wei); + assert_eq!(Units::try_from("kwei").unwrap(), Kwei); + assert_eq!(Units::try_from("mwei").unwrap(), Mwei); assert_eq!(Units::try_from("gwei").unwrap(), Gwei); + assert_eq!(Units::try_from("twei").unwrap(), Twei); + assert_eq!(Units::try_from("pwei").unwrap(), Pwei); assert_eq!(Units::try_from("ether").unwrap(), Ether); assert_eq!(Units::try_from("wei".to_string()).unwrap(), Wei); + assert_eq!(Units::try_from("kwei".to_string()).unwrap(), Kwei); + assert_eq!(Units::try_from("mwei".to_string()).unwrap(), Mwei); assert_eq!(Units::try_from("gwei".to_string()).unwrap(), Gwei); + assert_eq!(Units::try_from("twei".to_string()).unwrap(), Twei); + assert_eq!(Units::try_from("pwei".to_string()).unwrap(), Pwei); assert_eq!(Units::try_from("ether".to_string()).unwrap(), Ether); assert_eq!(Units::try_from(&"wei".to_string()).unwrap(), Wei); + assert_eq!(Units::try_from(&"kwei".to_string()).unwrap(), Kwei); + assert_eq!(Units::try_from(&"mwei".to_string()).unwrap(), Mwei); assert_eq!(Units::try_from(&"gwei".to_string()).unwrap(), Gwei); + assert_eq!(Units::try_from(&"twei".to_string()).unwrap(), Twei); + assert_eq!(Units::try_from(&"pwei".to_string()).unwrap(), Pwei); assert_eq!(Units::try_from(&"ether".to_string()).unwrap(), Ether); } }