pub struct Trx(/* private fields */);Expand description
An amount of TRX, stored internally as i64 sun.
User-facing constructors enforce non-negative amounts. Negative values remain representable only so malformed protobuf or serialized data can be inspected and round-tripped without panicking; arithmetic operations reject them.
Implementations§
Source§impl Trx
impl Trx
Sourcepub const fn from_sun(sun: i64) -> Result<Self, AmountError>
pub const fn from_sun(sun: i64) -> Result<Self, AmountError>
Construct from a raw sun value, rejecting negatives.
Sourcepub fn checked_add(self, rhs: Trx) -> Option<Trx>
pub fn checked_add(self, rhs: Trx) -> Option<Trx>
Checked addition. Returns None on i64 overflow or if either operand
is negative.
Sourcepub fn checked_sub(self, rhs: Trx) -> Option<Trx>
pub fn checked_sub(self, rhs: Trx) -> Option<Trx>
Checked subtraction. Returns None on i64 overflow, if either operand
is negative, or if the result would be negative.
Trait Implementations§
impl Copy for Trx
Source§impl<'de> Deserialize<'de> for Trx
impl<'de> Deserialize<'de> for Trx
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Display for Trx
Formats the amount as a fixed-precision decimal TRX string, exactly (no
f64), mirroring alloy’s format_units behavior.
impl Display for Trx
Formats the amount as a fixed-precision decimal TRX string, exactly (no
f64), mirroring alloy’s format_units behavior.
use tronz_primitives::Trx;
assert_eq!(Trx::from_sun(1_500_000).unwrap().to_string(), "1.500000");
assert_eq!("100".parse::<Trx>().unwrap().to_string(), "100.000000");
assert_eq!(Trx::from_sun(1).unwrap().to_string(), "0.000001");
assert_eq!("1.5".parse::<Trx>().unwrap().to_string(), "1.500000");impl Eq for Trx
Source§impl FromStr for Trx
Parse a decimal TRX string (e.g. "1.5" or "100") into sun.
impl FromStr for Trx
Parse a decimal TRX string (e.g. "1.5" or "100") into sun.
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.
§Examples
use tronz_primitives::Trx;
assert_eq!("1".parse::<Trx>().unwrap().as_sun(), 1_000_000);
assert_eq!("1.5".parse::<Trx>().unwrap().as_sun(), 1_500_000);
assert_eq!(".5".parse::<Trx>().unwrap().as_sun(), 500_000);
assert_eq!("0.000001".parse::<Trx>().unwrap().as_sun(), 1);
assert_eq!("1.0000009".parse::<Trx>().unwrap().as_sun(), 1_000_000);
assert!("-1".parse::<Trx>().is_err());