alloy_primitives/utils/
units.rs

1use crate::{I256, ParseSignedError, U256};
2use alloc::string::{String, ToString};
3use core::fmt;
4
5const MAX_U64_EXPONENT: u8 = 19;
6
7/// Converts the input to a U256 and converts from Ether to Wei.
8///
9/// # Examples
10///
11/// ```
12/// use alloy_primitives::{
13///     U256,
14///     utils::{Unit, parse_ether},
15/// };
16///
17/// let eth = Unit::ETHER.wei();
18/// assert_eq!(parse_ether("1").unwrap(), eth);
19/// ```
20pub fn parse_ether(eth: &str) -> Result<U256, UnitsError> {
21    ParseUnits::parse_units(eth, Unit::ETHER).map(Into::into)
22}
23
24/// Parses a decimal number and multiplies it with 10^units.
25///
26/// # Examples
27///
28/// ```
29/// use alloy_primitives::{U256, utils::parse_units};
30///
31/// let amount_in_eth = U256::from_str_radix("15230001000000000000", 10).unwrap();
32/// let amount_in_gwei = U256::from_str_radix("15230001000", 10).unwrap();
33/// let amount_in_wei = U256::from_str_radix("15230001000", 10).unwrap();
34/// assert_eq!(
35///     amount_in_eth,
36///     <U256 as From<_>>::from(parse_units("15.230001000000000000", "ether").unwrap())
37/// );
38/// assert_eq!(
39///     amount_in_gwei,
40///     <U256 as From<_>>::from(parse_units("15.230001000000000000", "gwei").unwrap())
41/// );
42/// assert_eq!(amount_in_wei, <U256 as From<_>>::from(parse_units("15230001000", "wei").unwrap()));
43/// ```
44///
45/// Example of trying to parse decimal WEI, which should fail, as WEI is the smallest
46/// ETH denominator. 1 ETH = 10^18 WEI.
47///
48/// ```should_panic
49/// use alloy_primitives::{U256, utils::parse_units};
50/// let amount_in_wei = U256::from_str_radix("15230001000", 10).unwrap();
51/// assert_eq!(
52///     amount_in_wei,
53///     <U256 as From<_>>::from(parse_units("15.230001000000000000", "wei").unwrap())
54/// );
55/// ```
56pub fn parse_units<K, E>(amount: &str, units: K) -> Result<ParseUnits, UnitsError>
57where
58    K: TryInto<Unit, Error = E>,
59    UnitsError: From<E>,
60{
61    ParseUnits::parse_units(amount, units.try_into()?)
62}
63
64/// Formats the given number of Wei as an Ether amount.
65///
66/// # Examples
67///
68/// ```
69/// use alloy_primitives::{U256, utils::format_ether};
70///
71/// let eth = format_ether(1395633240123456000_u128);
72/// assert_eq!(format_ether(1395633240123456000_u128), "1.395633240123456000");
73/// ```
74pub fn format_ether<T: Into<ParseUnits>>(amount: T) -> String {
75    amount.into().format_units(Unit::ETHER)
76}
77
78/// Formats the given number of Wei as the given unit.
79///
80/// # Examples
81///
82/// ```
83/// use alloy_primitives::{U256, utils::format_units};
84///
85/// let eth = U256::from_str_radix("1395633240123456000", 10).unwrap();
86/// assert_eq!(format_units(eth, "eth").unwrap(), "1.395633240123456000");
87///
88/// assert_eq!(format_units(i64::MIN, "gwei").unwrap(), "-9223372036.854775808");
89///
90/// assert_eq!(format_units(i128::MIN, 36).unwrap(), "-170.141183460469231731687303715884105728");
91/// ```
92pub fn format_units<T, K, E>(amount: T, units: K) -> Result<String, UnitsError>
93where
94    T: Into<ParseUnits>,
95    K: TryInto<Unit, Error = E>,
96    UnitsError: From<E>,
97{
98    units.try_into().map(|units| amount.into().format_units(units)).map_err(UnitsError::from)
99}
100
101/// Formats the given number of Wei as the given unit with a custom decimal separator.
102pub fn format_units_with<T, K, E>(
103    amount: T,
104    units: K,
105    separator: DecimalSeparator,
106) -> Result<String, UnitsError>
107where
108    T: Into<ParseUnits>,
109    K: TryInto<Unit, Error = E>,
110    UnitsError: From<E>,
111{
112    units
113        .try_into()
114        .map(|units| amount.into().format_units_with(units, separator))
115        .map_err(UnitsError::from)
116}
117
118/// Error type for [`Unit`]-related operations.
119#[derive(Debug)]
120pub enum UnitsError {
121    /// The provided units are not recognized.
122    InvalidUnit(String),
123    /// Overflow when parsing a signed number.
124    ParseSigned(ParseSignedError),
125}
126
127impl core::error::Error for UnitsError {
128    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
129        match self {
130            Self::InvalidUnit(_) => None,
131            Self::ParseSigned(e) => Some(e),
132        }
133    }
134}
135
136impl fmt::Display for UnitsError {
137    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
138        match self {
139            Self::InvalidUnit(s) => write!(f, "{s:?} is not a valid unit"),
140            Self::ParseSigned(e) => e.fmt(f),
141        }
142    }
143}
144
145impl From<ruint::ParseError> for UnitsError {
146    fn from(value: ruint::ParseError) -> Self {
147        Self::ParseSigned(value.into())
148    }
149}
150
151impl From<ParseSignedError> for UnitsError {
152    fn from(value: ParseSignedError) -> Self {
153        Self::ParseSigned(value)
154    }
155}
156
157/// This enum holds the numeric types that a possible to be returned by `parse_units` and
158/// that are taken by `format_units`.
159#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
160pub enum ParseUnits {
161    /// Unsigned 256-bit integer.
162    U256(U256),
163    /// Signed 256-bit integer.
164    I256(I256),
165}
166
167impl From<ParseUnits> for U256 {
168    #[inline]
169    fn from(value: ParseUnits) -> Self {
170        value.get_absolute()
171    }
172}
173
174impl From<ParseUnits> for I256 {
175    #[inline]
176    fn from(value: ParseUnits) -> Self {
177        value.get_signed()
178    }
179}
180
181impl fmt::Display for ParseUnits {
182    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
183        match self {
184            Self::U256(val) => val.fmt(f),
185            Self::I256(val) => val.fmt(f),
186        }
187    }
188}
189
190macro_rules! impl_from_integers {
191    ($convert:ident($($t:ty),* $(,)?)) => {$(
192        impl From<$t> for ParseUnits {
193            fn from(value: $t) -> Self {
194                Self::$convert($convert::try_from(value).unwrap())
195            }
196        }
197    )*}
198}
199
200impl_from_integers!(U256(u8, u16, u32, u64, u128, usize, U256));
201impl_from_integers!(I256(i8, i16, i32, i64, i128, isize, I256));
202
203macro_rules! impl_try_into_absolute {
204    ($($t:ty),* $(,)?) => { $(
205        impl TryFrom<ParseUnits> for $t {
206            type Error = <$t as TryFrom<U256>>::Error;
207
208            fn try_from(value: ParseUnits) -> Result<Self, Self::Error> {
209                <$t>::try_from(value.get_absolute())
210            }
211        }
212    )* };
213}
214
215impl_try_into_absolute!(u64, u128);
216
217/// Decimal separator for number formatting
218#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
219pub enum DecimalSeparator {
220    /// Use comma as decimal separator
221    Comma,
222    /// Use period as decimal separator
223    #[default]
224    Period,
225}
226
227impl DecimalSeparator {
228    /// Returns the character used as decimal separator
229    #[inline]
230    pub const fn separator(&self) -> char {
231        match self {
232            Self::Comma => ',',
233            Self::Period => '.',
234        }
235    }
236}
237
238impl ParseUnits {
239    /// Parses a decimal number and multiplies it with 10^units.
240    ///
241    /// See [`parse_units`] for more information.
242    #[allow(clippy::self_named_constructors)]
243    pub fn parse_units(amount: &str, unit: Unit) -> Result<Self, UnitsError> {
244        let exponent = unit.get() as usize;
245
246        let mut amount = amount.to_string();
247        let negative = amount.starts_with('-');
248        let dec_len = if let Some(di) = amount.find('.') {
249            amount.remove(di);
250            amount[di..].len()
251        } else {
252            0
253        };
254        let amount = amount.as_str();
255
256        if dec_len > exponent {
257            // Truncate the decimal part if it is longer than the exponent
258            let amount = &amount[..(amount.len() - (dec_len - exponent))];
259            if negative {
260                // Edge case: We have removed the entire number and only the negative sign is left.
261                //            Return 0 as a I256 given the input was signed.
262                if amount == "-" {
263                    Ok(Self::I256(I256::ZERO))
264                } else {
265                    Ok(Self::I256(I256::from_dec_str(amount)?))
266                }
267            } else {
268                Ok(Self::U256(U256::from_str_radix(amount, 10)?))
269            }
270        } else if negative {
271            // Edge case: Only a negative sign was given, return 0 as a I256 given the input was
272            // signed.
273            if amount == "-" {
274                Ok(Self::I256(I256::ZERO))
275            } else {
276                let mut n = I256::from_dec_str(amount)?;
277                n *= I256::try_from(10u8)
278                    .unwrap()
279                    .checked_pow(U256::from(exponent - dec_len))
280                    .ok_or(UnitsError::ParseSigned(ParseSignedError::IntegerOverflow))?;
281                Ok(Self::I256(n))
282            }
283        } else {
284            let mut a_uint = U256::from_str_radix(amount, 10)?;
285            a_uint *= U256::from(10)
286                .checked_pow(U256::from(exponent - dec_len))
287                .ok_or(UnitsError::ParseSigned(ParseSignedError::IntegerOverflow))?;
288            Ok(Self::U256(a_uint))
289        }
290    }
291
292    /// Formats the given number of Wei as the given unit.
293    pub fn format_units_with(&self, mut unit: Unit, separator: DecimalSeparator) -> String {
294        // Edge case: If the number is signed and the unit is the largest possible unit, we need to
295        //            subtract 1 from the unit to avoid overflow.
296        if self.is_signed() && unit == Unit::MAX {
297            unit = Unit::new(Unit::MAX.get() - 1).unwrap();
298        }
299        let units = unit.get() as usize;
300        let exp10 = unit.wei();
301
302        // TODO: `decimals` are formatted twice because U256 does not support alignment
303        // (`:0>width`).
304        match *self {
305            Self::U256(amount) => {
306                let integer = amount / exp10;
307                let decimals = (amount % exp10).to_string();
308                format!("{integer}{}{decimals:0>units$}", separator.separator())
309            }
310            Self::I256(amount) => {
311                let exp10 = I256::from_raw(exp10);
312                let sign = if amount.is_negative() { "-" } else { "" };
313                let integer = (amount / exp10).twos_complement();
314                let decimals = ((amount % exp10).twos_complement()).to_string();
315                format!("{sign}{integer}{}{decimals:0>units$}", separator.separator())
316            }
317        }
318    }
319
320    /// Formats the given number of Wei as the given unit.
321    ///
322    /// See [`format_units`] for more information.
323    pub fn format_units(&self, unit: Unit) -> String {
324        self.format_units_with(unit, DecimalSeparator::Period)
325    }
326
327    /// Returns `true` if the number is signed.
328    #[inline]
329    pub const fn is_signed(&self) -> bool {
330        matches!(self, Self::I256(_))
331    }
332
333    /// Returns `true` if the number is unsigned.
334    #[inline]
335    pub const fn is_unsigned(&self) -> bool {
336        matches!(self, Self::U256(_))
337    }
338
339    /// Returns `true` if the number is negative.
340    #[inline]
341    pub const fn is_negative(&self) -> bool {
342        match self {
343            Self::U256(_) => false,
344            Self::I256(n) => n.is_negative(),
345        }
346    }
347
348    /// Returns `true` if the number is positive.
349    #[inline]
350    pub const fn is_positive(&self) -> bool {
351        match self {
352            Self::U256(_) => true,
353            Self::I256(n) => n.is_positive(),
354        }
355    }
356
357    /// Returns `true` if the number is zero.
358    #[inline]
359    pub fn is_zero(&self) -> bool {
360        match self {
361            Self::U256(n) => n.is_zero(),
362            Self::I256(n) => n.is_zero(),
363        }
364    }
365
366    /// Returns the absolute value of the number.
367    #[inline]
368    pub const fn get_absolute(self) -> U256 {
369        match self {
370            Self::U256(n) => n,
371            Self::I256(n) => n.into_raw(),
372        }
373    }
374
375    /// Returns the signed value of the number.
376    #[inline]
377    pub const fn get_signed(self) -> I256 {
378        match self {
379            Self::U256(n) => I256::from_raw(n),
380            Self::I256(n) => n,
381        }
382    }
383}
384
385/// Ethereum unit. Always less than [`77`](Unit::MAX).
386#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
387pub struct Unit(u8);
388
389impl fmt::Display for Unit {
390    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
391        self.get().fmt(f)
392    }
393}
394
395impl TryFrom<u8> for Unit {
396    type Error = UnitsError;
397
398    fn try_from(value: u8) -> Result<Self, Self::Error> {
399        Self::new(value).ok_or_else(|| UnitsError::InvalidUnit(value.to_string()))
400    }
401}
402
403impl TryFrom<String> for Unit {
404    type Error = UnitsError;
405
406    fn try_from(value: String) -> Result<Self, Self::Error> {
407        value.parse()
408    }
409}
410
411impl<'a> TryFrom<&'a String> for Unit {
412    type Error = UnitsError;
413
414    fn try_from(value: &'a String) -> Result<Self, Self::Error> {
415        value.parse()
416    }
417}
418
419impl TryFrom<&str> for Unit {
420    type Error = UnitsError;
421
422    fn try_from(value: &str) -> Result<Self, Self::Error> {
423        value.parse()
424    }
425}
426
427impl core::str::FromStr for Unit {
428    type Err = UnitsError;
429
430    fn from_str(s: &str) -> Result<Self, Self::Err> {
431        if let Ok(unit) = crate::U8::from_str(s) {
432            return Self::new(unit.to()).ok_or_else(|| UnitsError::InvalidUnit(s.to_string()));
433        }
434
435        Ok(match s.to_ascii_lowercase().as_str() {
436            "eth" | "ether" => Self::ETHER,
437            "pwei" | "milli" | "milliether" | "finney" => Self::PWEI,
438            "twei" | "micro" | "microether" | "szabo" => Self::TWEI,
439            "gwei" | "nano" | "nanoether" | "shannon" => Self::GWEI,
440            "mwei" | "pico" | "picoether" | "lovelace" => Self::MWEI,
441            "kwei" | "femto" | "femtoether" | "babbage" => Self::KWEI,
442            "wei" => Self::WEI,
443            _ => return Err(UnitsError::InvalidUnit(s.to_string())),
444        })
445    }
446}
447
448impl Unit {
449    /// Wei is equivalent to 1 wei.
450    pub const WEI: Self = unsafe { Self::new_unchecked(0) };
451
452    /// Kwei is equivalent to 1e3 wei.
453    pub const KWEI: Self = unsafe { Self::new_unchecked(3) };
454
455    /// Mwei is equivalent to 1e6 wei.
456    pub const MWEI: Self = unsafe { Self::new_unchecked(6) };
457
458    /// Gwei is equivalent to 1e9 wei.
459    pub const GWEI: Self = unsafe { Self::new_unchecked(9) };
460
461    /// Twei is equivalent to 1e12 wei.
462    pub const TWEI: Self = unsafe { Self::new_unchecked(12) };
463
464    /// Pwei is equivalent to 1e15 wei.
465    pub const PWEI: Self = unsafe { Self::new_unchecked(15) };
466
467    /// Ether is equivalent to 1e18 wei.
468    pub const ETHER: Self = unsafe { Self::new_unchecked(18) };
469
470    /// The smallest unit.
471    pub const MIN: Self = Self::WEI;
472    /// The largest unit.
473    pub const MAX: Self = unsafe { Self::new_unchecked(77) };
474
475    /// Creates a new `Unit` instance, checking for overflow.
476    #[inline]
477    pub const fn new(units: u8) -> Option<Self> {
478        if units <= Self::MAX.get() {
479            // SAFETY: `units` is contained in the valid range.
480            Some(unsafe { Self::new_unchecked(units) })
481        } else {
482            None
483        }
484    }
485
486    /// Creates a new `Unit` instance.
487    ///
488    /// # Safety
489    ///
490    /// `x` must be less than [`Unit::MAX`].
491    #[inline]
492    pub const unsafe fn new_unchecked(x: u8) -> Self {
493        Self(x)
494    }
495
496    /// Returns `10^self`, which is the number of Wei in this unit.
497    ///
498    /// # Examples
499    ///
500    /// ```
501    /// use alloy_primitives::{U256, utils::Unit};
502    ///
503    /// assert_eq!(U256::from(1u128), Unit::WEI.wei());
504    /// assert_eq!(U256::from(1_000u128), Unit::KWEI.wei());
505    /// assert_eq!(U256::from(1_000_000u128), Unit::MWEI.wei());
506    /// assert_eq!(U256::from(1_000_000_000u128), Unit::GWEI.wei());
507    /// assert_eq!(U256::from(1_000_000_000_000u128), Unit::TWEI.wei());
508    /// assert_eq!(U256::from(1_000_000_000_000_000u128), Unit::PWEI.wei());
509    /// assert_eq!(U256::from(1_000_000_000_000_000_000u128), Unit::ETHER.wei());
510    /// ```
511    #[inline]
512    pub fn wei(self) -> U256 {
513        if self.get() <= MAX_U64_EXPONENT {
514            self.wei_const()
515        } else {
516            U256::from(10u8).pow(U256::from(self.get()))
517        }
518    }
519
520    /// Returns `10^self`, which is the number of Wei in this unit.
521    ///
522    /// # Panics
523    ///
524    /// Panics if `10^self` would overflow a `u64` (`self > 19`). If this can happen, use
525    /// [`wei`](Self::wei) instead.
526    #[inline]
527    pub const fn wei_const(self) -> U256 {
528        if self.get() > MAX_U64_EXPONENT {
529            panic!("overflow")
530        }
531        U256::from_limbs([10u64.pow(self.get() as u32), 0, 0, 0])
532    }
533
534    /// Returns the numeric value of the unit.
535    #[inline]
536    pub const fn get(self) -> u8 {
537        self.0
538    }
539}
540
541#[cfg(test)]
542mod tests {
543    use super::*;
544
545    #[test]
546    fn unit_values() {
547        assert_eq!(Unit::WEI.get(), 0);
548        assert_eq!(Unit::KWEI.get(), 3);
549        assert_eq!(Unit::MWEI.get(), 6);
550        assert_eq!(Unit::GWEI.get(), 9);
551        assert_eq!(Unit::TWEI.get(), 12);
552        assert_eq!(Unit::PWEI.get(), 15);
553        assert_eq!(Unit::ETHER.get(), 18);
554        assert_eq!(Unit::new(10).unwrap().get(), 10);
555        assert_eq!(Unit::new(20).unwrap().get(), 20);
556    }
557
558    #[test]
559    fn unit_wei() {
560        let assert = |unit: Unit| {
561            let wei = unit.wei();
562            assert_eq!(wei.to::<u128>(), 10u128.pow(unit.get() as u32));
563            assert_eq!(wei, U256::from(10u8).pow(U256::from(unit.get())));
564        };
565        assert(Unit::WEI);
566        assert(Unit::KWEI);
567        assert(Unit::MWEI);
568        assert(Unit::GWEI);
569        assert(Unit::TWEI);
570        assert(Unit::PWEI);
571        assert(Unit::ETHER);
572        assert(Unit::new(10).unwrap());
573        assert(Unit::new(20).unwrap());
574    }
575
576    #[test]
577    fn parse() {
578        assert_eq!(Unit::try_from("wei").unwrap(), Unit::WEI);
579        assert_eq!(Unit::try_from("kwei").unwrap(), Unit::KWEI);
580        assert_eq!(Unit::try_from("mwei").unwrap(), Unit::MWEI);
581        assert_eq!(Unit::try_from("gwei").unwrap(), Unit::GWEI);
582        assert_eq!(Unit::try_from("twei").unwrap(), Unit::TWEI);
583        assert_eq!(Unit::try_from("pwei").unwrap(), Unit::PWEI);
584        assert_eq!(Unit::try_from("ether").unwrap(), Unit::ETHER);
585    }
586
587    #[test]
588    fn wei_in_ether() {
589        assert_eq!(Unit::ETHER.wei(), U256::from(1e18 as u64));
590    }
591
592    #[test]
593    fn test_format_ether_unsigned() {
594        let eth = format_ether(Unit::ETHER.wei());
595        assert_eq!(eth.parse::<f64>().unwrap() as u64, 1);
596
597        let eth = format_ether(1395633240123456000_u128);
598        assert_eq!(eth.parse::<f64>().unwrap(), 1.395633240123456);
599
600        let eth = format_ether(U256::from_str_radix("1395633240123456000", 10).unwrap());
601        assert_eq!(eth.parse::<f64>().unwrap(), 1.395633240123456);
602
603        let eth = format_ether(U256::from_str_radix("1395633240123456789", 10).unwrap());
604        assert_eq!(eth, "1.395633240123456789");
605
606        let eth = format_ether(U256::from_str_radix("1005633240123456789", 10).unwrap());
607        assert_eq!(eth, "1.005633240123456789");
608
609        let eth = format_ether(u16::MAX);
610        assert_eq!(eth, "0.000000000000065535");
611
612        // Note: This covers usize on 32 bit systems.
613        let eth = format_ether(u32::MAX);
614        assert_eq!(eth, "0.000000004294967295");
615
616        // Note: This covers usize on 64 bit systems.
617        let eth = format_ether(u64::MAX);
618        assert_eq!(eth, "18.446744073709551615");
619    }
620
621    #[test]
622    fn test_format_ether_signed() {
623        let eth = format_ether(I256::from_dec_str("-1395633240123456000").unwrap());
624        assert_eq!(eth.parse::<f64>().unwrap(), -1.395633240123456);
625
626        let eth = format_ether(I256::from_dec_str("-1395633240123456789").unwrap());
627        assert_eq!(eth, "-1.395633240123456789");
628
629        let eth = format_ether(I256::from_dec_str("1005633240123456789").unwrap());
630        assert_eq!(eth, "1.005633240123456789");
631
632        let eth = format_ether(i8::MIN);
633        assert_eq!(eth, "-0.000000000000000128");
634
635        let eth = format_ether(i8::MAX);
636        assert_eq!(eth, "0.000000000000000127");
637
638        let eth = format_ether(i16::MIN);
639        assert_eq!(eth, "-0.000000000000032768");
640
641        // Note: This covers isize on 32 bit systems.
642        let eth = format_ether(i32::MIN);
643        assert_eq!(eth, "-0.000000002147483648");
644
645        // Note: This covers isize on 64 bit systems.
646        let eth = format_ether(i64::MIN);
647        assert_eq!(eth, "-9.223372036854775808");
648    }
649
650    #[test]
651    fn test_format_units_unsigned() {
652        let gwei_in_ether = format_units(Unit::ETHER.wei(), 9).unwrap();
653        assert_eq!(gwei_in_ether.parse::<f64>().unwrap() as u64, 1e9 as u64);
654
655        let eth = format_units(Unit::ETHER.wei(), "ether").unwrap();
656        assert_eq!(eth.parse::<f64>().unwrap() as u64, 1);
657
658        let eth = format_units(1395633240123456000_u128, "ether").unwrap();
659        assert_eq!(eth.parse::<f64>().unwrap(), 1.395633240123456);
660
661        let eth = format_units(U256::from_str_radix("1395633240123456000", 10).unwrap(), "ether")
662            .unwrap();
663        assert_eq!(eth.parse::<f64>().unwrap(), 1.395633240123456);
664
665        let eth = format_units(U256::from_str_radix("1395633240123456789", 10).unwrap(), "ether")
666            .unwrap();
667        assert_eq!(eth, "1.395633240123456789");
668
669        let eth = format_units(U256::from_str_radix("1005633240123456789", 10).unwrap(), "ether")
670            .unwrap();
671        assert_eq!(eth, "1.005633240123456789");
672
673        let eth = format_units(u8::MAX, 4).unwrap();
674        assert_eq!(eth, "0.0255");
675
676        let eth = format_units(u16::MAX, "ether").unwrap();
677        assert_eq!(eth, "0.000000000000065535");
678
679        // Note: This covers usize on 32 bit systems.
680        let eth = format_units(u32::MAX, 18).unwrap();
681        assert_eq!(eth, "0.000000004294967295");
682
683        // Note: This covers usize on 64 bit systems.
684        let eth = format_units(u64::MAX, "gwei").unwrap();
685        assert_eq!(eth, "18446744073.709551615");
686
687        let eth = format_units(u128::MAX, 36).unwrap();
688        assert_eq!(eth, "340.282366920938463463374607431768211455");
689
690        let eth = format_units(U256::MAX, 77).unwrap();
691        assert_eq!(
692            eth,
693            "1.15792089237316195423570985008687907853269984665640564039457584007913129639935"
694        );
695
696        let _err = format_units(U256::MAX, 78).unwrap_err();
697        let _err = format_units(U256::MAX, 79).unwrap_err();
698    }
699
700    #[test]
701    fn test_format_units_signed() {
702        let eth =
703            format_units(I256::from_dec_str("-1395633240123456000").unwrap(), "ether").unwrap();
704        assert_eq!(eth.parse::<f64>().unwrap(), -1.395633240123456);
705
706        let eth =
707            format_units(I256::from_dec_str("-1395633240123456789").unwrap(), "ether").unwrap();
708        assert_eq!(eth, "-1.395633240123456789");
709
710        let eth =
711            format_units(I256::from_dec_str("1005633240123456789").unwrap(), "ether").unwrap();
712        assert_eq!(eth, "1.005633240123456789");
713
714        let eth = format_units(i8::MIN, 4).unwrap();
715        assert_eq!(eth, "-0.0128");
716        assert_eq!(eth.parse::<f64>().unwrap(), -0.0128_f64);
717
718        let eth = format_units(i8::MAX, 4).unwrap();
719        assert_eq!(eth, "0.0127");
720        assert_eq!(eth.parse::<f64>().unwrap(), 0.0127);
721
722        let eth = format_units(i16::MIN, "ether").unwrap();
723        assert_eq!(eth, "-0.000000000000032768");
724
725        // Note: This covers isize on 32 bit systems.
726        let eth = format_units(i32::MIN, 18).unwrap();
727        assert_eq!(eth, "-0.000000002147483648");
728
729        // Note: This covers isize on 64 bit systems.
730        let eth = format_units(i64::MIN, "gwei").unwrap();
731        assert_eq!(eth, "-9223372036.854775808");
732
733        let eth = format_units(i128::MIN, 36).unwrap();
734        assert_eq!(eth, "-170.141183460469231731687303715884105728");
735
736        let eth = format_units(I256::MIN, 76).unwrap();
737        let min = "-5.7896044618658097711785492504343953926634992332820282019728792003956564819968";
738        assert_eq!(eth, min);
739        // doesn't error
740        let eth = format_units(I256::MIN, 77).unwrap();
741        assert_eq!(eth, min);
742
743        let _err = format_units(I256::MIN, 78).unwrap_err();
744        let _err = format_units(I256::MIN, 79).unwrap_err();
745    }
746
747    #[test]
748    fn parse_large_units() {
749        let decimals = 27u8;
750        let val = "10.55";
751
752        let n: U256 = parse_units(val, decimals).unwrap().into();
753        assert_eq!(n.to_string(), "10550000000000000000000000000");
754    }
755
756    #[test]
757    fn test_parse_units() {
758        let gwei: U256 = parse_units("1.5", 9).unwrap().into();
759        assert_eq!(gwei, U256::from(15e8 as u64));
760
761        let token: U256 = parse_units("1163.56926418", 8).unwrap().into();
762        assert_eq!(token, U256::from(116356926418u64));
763
764        let eth_dec_float: U256 = parse_units("1.39563324", "ether").unwrap().into();
765        assert_eq!(eth_dec_float, U256::from_str_radix("1395633240000000000", 10).unwrap());
766
767        let eth_dec_string: U256 = parse_units("1.39563324", "ether").unwrap().into();
768        assert_eq!(eth_dec_string, U256::from_str_radix("1395633240000000000", 10).unwrap());
769
770        let eth: U256 = parse_units("1", "ether").unwrap().into();
771        assert_eq!(eth, Unit::ETHER.wei());
772
773        let val: U256 = parse_units("2.3", "ether").unwrap().into();
774        assert_eq!(val, U256::from_str_radix("2300000000000000000", 10).unwrap());
775
776        let n: U256 = parse_units(".2", 2).unwrap().into();
777        assert_eq!(n, U256::from(20), "leading dot");
778
779        let n: U256 = parse_units("333.21", 2).unwrap().into();
780        assert_eq!(n, U256::from(33321), "trailing dot");
781
782        let n: U256 = parse_units("98766", 16).unwrap().into();
783        assert_eq!(n, U256::from_str_radix("987660000000000000000", 10).unwrap(), "no dot");
784
785        let n: U256 = parse_units("3_3_0", 3).unwrap().into();
786        assert_eq!(n, U256::from(330000), "underscore");
787
788        let n: U256 = parse_units("330", 0).unwrap().into();
789        assert_eq!(n, U256::from(330), "zero decimals");
790
791        let n: U256 = parse_units(".1234", 3).unwrap().into();
792        assert_eq!(n, U256::from(123), "truncate too many decimals");
793
794        assert!(parse_units("1", 80).is_err(), "overflow");
795
796        let two_e30 = U256::from(2) * U256::from_limbs([0x4674edea40000000, 0xc9f2c9cd0, 0x0, 0x0]);
797        let n: U256 = parse_units("2", 30).unwrap().into();
798        assert_eq!(n, two_e30, "2e30");
799
800        let n: U256 = parse_units(".33_319_2", 0).unwrap().into();
801        assert_eq!(n, U256::ZERO, "mix");
802
803        let n: U256 = parse_units("", 3).unwrap().into();
804        assert_eq!(n, U256::ZERO, "empty");
805    }
806
807    #[test]
808    fn test_signed_parse_units() {
809        let gwei: I256 = parse_units("-1.5", 9).unwrap().into();
810        assert_eq!(gwei.as_i64(), -15e8 as i64);
811
812        let token: I256 = parse_units("-1163.56926418", 8).unwrap().into();
813        assert_eq!(token.as_i64(), -116356926418);
814
815        let eth_dec_float: I256 = parse_units("-1.39563324", "ether").unwrap().into();
816        assert_eq!(eth_dec_float, I256::from_dec_str("-1395633240000000000").unwrap());
817
818        let eth_dec_string: I256 = parse_units("-1.39563324", "ether").unwrap().into();
819        assert_eq!(eth_dec_string, I256::from_dec_str("-1395633240000000000").unwrap());
820
821        let eth: I256 = parse_units("-1", "ether").unwrap().into();
822        assert_eq!(eth, I256::from_raw(Unit::ETHER.wei()) * I256::MINUS_ONE);
823
824        let val: I256 = parse_units("-2.3", "ether").unwrap().into();
825        assert_eq!(val, I256::from_dec_str("-2300000000000000000").unwrap());
826
827        let n: I256 = parse_units("-.2", 2).unwrap().into();
828        assert_eq!(n, I256::try_from(-20).unwrap(), "leading dot");
829
830        let n: I256 = parse_units("-333.21", 2).unwrap().into();
831        assert_eq!(n, I256::try_from(-33321).unwrap(), "trailing dot");
832
833        let n: I256 = parse_units("-98766", 16).unwrap().into();
834        assert_eq!(n, I256::from_dec_str("-987660000000000000000").unwrap(), "no dot");
835
836        let n: I256 = parse_units("-3_3_0", 3).unwrap().into();
837        assert_eq!(n, I256::try_from(-330000).unwrap(), "underscore");
838
839        let n: I256 = parse_units("-330", 0).unwrap().into();
840        assert_eq!(n, I256::try_from(-330).unwrap(), "zero decimals");
841
842        let n: I256 = parse_units("-.1234", 3).unwrap().into();
843        assert_eq!(n, I256::try_from(-123).unwrap(), "truncate too many decimals");
844
845        assert!(parse_units("-1", 80).is_err(), "overflow");
846
847        let two_e30 = I256::try_from(-2).unwrap()
848            * I256::from_raw(U256::from_limbs([0x4674edea40000000, 0xc9f2c9cd0, 0x0, 0x0]));
849        let n: I256 = parse_units("-2", 30).unwrap().into();
850        assert_eq!(n, two_e30, "-2e30");
851
852        let n: I256 = parse_units("-.33_319_2", 0).unwrap().into();
853        assert_eq!(n, I256::ZERO, "mix");
854
855        let n: I256 = parse_units("-", 3).unwrap().into();
856        assert_eq!(n, I256::ZERO, "empty");
857    }
858}