1use crate::{I256, ParseSignedError, U256};
2use alloc::string::{String, ToString};
3use core::fmt;
4
5const MAX_U64_EXPONENT: u8 = 19;
6
7pub fn parse_ether(eth: &str) -> Result<U256, UnitsError> {
21 ParseUnits::parse_units(eth, Unit::ETHER).map(Into::into)
22}
23
24pub 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
64pub fn format_ether<T: Into<ParseUnits>>(amount: T) -> String {
75 amount.into().format_units(Unit::ETHER)
76}
77
78pub 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
101pub 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#[derive(Debug)]
120pub enum UnitsError {
121 InvalidUnit(String),
123 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#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
160pub enum ParseUnits {
161 U256(U256),
163 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#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
219pub enum DecimalSeparator {
220 Comma,
222 #[default]
224 Period,
225}
226
227impl DecimalSeparator {
228 #[inline]
230 pub const fn separator(&self) -> char {
231 match self {
232 Self::Comma => ',',
233 Self::Period => '.',
234 }
235 }
236}
237
238impl ParseUnits {
239 #[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 let amount = &amount[..(amount.len() - (dec_len - exponent))];
259 if negative {
260 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 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 pub fn format_units_with(&self, mut unit: Unit, separator: DecimalSeparator) -> String {
294 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 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 pub fn format_units(&self, unit: Unit) -> String {
324 self.format_units_with(unit, DecimalSeparator::Period)
325 }
326
327 #[inline]
329 pub const fn is_signed(&self) -> bool {
330 matches!(self, Self::I256(_))
331 }
332
333 #[inline]
335 pub const fn is_unsigned(&self) -> bool {
336 matches!(self, Self::U256(_))
337 }
338
339 #[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 #[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 #[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 #[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 #[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#[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 pub const WEI: Self = unsafe { Self::new_unchecked(0) };
451
452 pub const KWEI: Self = unsafe { Self::new_unchecked(3) };
454
455 pub const MWEI: Self = unsafe { Self::new_unchecked(6) };
457
458 pub const GWEI: Self = unsafe { Self::new_unchecked(9) };
460
461 pub const TWEI: Self = unsafe { Self::new_unchecked(12) };
463
464 pub const PWEI: Self = unsafe { Self::new_unchecked(15) };
466
467 pub const ETHER: Self = unsafe { Self::new_unchecked(18) };
469
470 pub const MIN: Self = Self::WEI;
472 pub const MAX: Self = unsafe { Self::new_unchecked(77) };
474
475 #[inline]
477 pub const fn new(units: u8) -> Option<Self> {
478 if units <= Self::MAX.get() {
479 Some(unsafe { Self::new_unchecked(units) })
481 } else {
482 None
483 }
484 }
485
486 #[inline]
492 pub const unsafe fn new_unchecked(x: u8) -> Self {
493 Self(x)
494 }
495
496 #[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 #[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 #[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 let eth = format_ether(u32::MAX);
614 assert_eq!(eth, "0.000000004294967295");
615
616 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 let eth = format_ether(i32::MIN);
643 assert_eq!(eth, "-0.000000002147483648");
644
645 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 let eth = format_units(u32::MAX, 18).unwrap();
681 assert_eq!(eth, "0.000000004294967295");
682
683 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 let eth = format_units(i32::MIN, 18).unwrap();
727 assert_eq!(eth, "-0.000000002147483648");
728
729 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 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}