pub fn parse_trx(s: &str) -> Result<Trx, AmountError>Expand description
Parse a decimal TRX string (e.g. "1.5" or "100") into a Trx amount.
Free-function alias for str::parse::<Trx>(), mirroring
alloy’s parse_ether
for callers who prefer that style.
The accepted syntax mirrors alloy’s parse_units with 6 decimal places:
leading decimal points and _ separators are accepted, an empty string is
zero, and fractional digits beyond sun precision are truncated. Negative
values remain invalid because native TRX amounts are non-negative.
use tronz_primitives::{Trx, parse_trx};
assert_eq!(parse_trx("1.5").unwrap().as_sun(), 1_500_000);
assert_eq!(parse_trx(".5").unwrap().as_sun(), 500_000);
assert_eq!(parse_trx("0.000001").unwrap().as_sun(), 1);
assert_eq!(parse_trx("1.0000009").unwrap().as_sun(), 1_000_000);
assert_eq!("1".parse::<Trx>().unwrap().as_sun(), 1_000_000);
assert!(parse_trx("-1").is_err());