#[repr(C)]pub enum Amount {
XRP {
num_drops: i64,
},
IOU {
amount: IOUNumber,
issuer: AccountID,
currency: Currency,
},
MPT {
num_units: u64,
is_positive: bool,
mpt_id: MptId,
},
}Expand description
A zero-cost abstraction for XRPL tokens. Tokens conform to the following binary layout:
┌────────────────────────────────────────────────────────────────────────────┐
│ XRP Amount (64 bits / 8 bytes) │
├────────────────────────────────────────────────────────────────────────────┤
│ ┌────────────────────────────────────────────────────┐ │
│ ┌─┐┌─┐┌─┐ ┌─┬─┬─┬─┐ │ ┌────────────────────────────────────────────────┐ │ │
│ │0││1││0│ │0│0│0│0│ │ │ ... │ │ │
│ └─┘└─┘└─┘ └─┴─┴─┴─┘ │ └────────────────────────────────────────────────┘ │ │
│ ▲ ▲ ▲ ▲ │ Integer Drops (57 bits) │ │
│ │ │ │ │ └────────────────────────────────────────────────────┘ │
┌───┼──┘ │ └─────┐ └────────────────┐ │
│ └─────┼────────┼──────────────────┼──────────────────────────────────────────┘
│ │ │ │
┌────────────────┐ │ ┌─────────────┐ ┌──────────────────┐
│ Type Bit │ │ │ Is MPT Bit │ │ Reserved │
│(0=XRP/MPT;1=IOU│ │ │(1=MPT/0=XRP)│ └──────────────────┘
└────────────────┘ │ └─────────────┘
┌────────────────┐
│ Sign bit │
│(1 for positive)│
└────────────────┘
┌────────────────────────────────────────────────────────────────────────────┐
│ MPT Amount (264-bits/33-bytes) │
├────────────────────────────────────────────────────────────────────────────┤
│ ┌──────────┐ ┌────────────┐ ┌────────────────┐ │
│ ┌─┐┌─┐┌─┐ ┌─┬─┬─┬─┬─┐ │┌────────┐│ │ ┌────────┐ │ │ ┌────────┐ │ │
│ │0││1││1│ │0│0│0│0│0│ ││ ... ││ │ │ ... │ │ │ │ ... │ │ │
│ └─┘└─┘└─┘ └─┴─┴─┴─┴─┘ │└────────┘│ │ └────────┘ │ │ └────────┘ │ │
│ ▲ ▲ ▲ ▲ │ Amount │ │Sequence Num│ │Issuer AccountID│ │
│ │ │ │ │ │(64 bits) │ │ (32 bits) │ │ (160 bits) │ │
┌───┼──┘ │ └────┐ │ └──────────┘ └────────────┘ └────────────────┘ │
│ └─────┼───────┼──┼───────────────────────────────────────────────────────────┘
│ │ │ └───────────────┐
┌─────────────────┐│┌─────────────┐ │
│ Type Bit │││ Is MPT Bit │ │
│(0=XRP/MPT;1=IOU)│││(1=MPT/0=XRP)│ │
└─────────────────┘│└─────────────┘ │
┌────────────────┐ ┌──────────────────┐
│ Sign bit │ │ Reserved │
│(1 for positive)│ └──────────────────┘
└────────────────┘
┌────────────────────────────────────────────────────────────────────────────────┐
│ IOU Amount (384-bits/48-bytes) │
├────────────────────────────────────────────────────────────────────────────────┤
│ ┌─────────────────┐ ┌──────────────┐ ┌──────────────┐ ┌────────────────┐│
│ ┌─┐┌─┐│┌─┬─┬─┬─┬─┬─┬─┬─┐│ │┌────────────┐│ │ ┌────────┐ │ │ ┌───────┐ ││
│ │1││1│││0│0│0│0│0│0│0│0││ ││ ... ││ │ │ ... │ │ │ │ ... │ ││
│ └─┘└─┘│└─┴─┴─┴─┴─┴─┴─┴─┘│ │└────────────┘│ │ └────────┘ │ │ └───────┘ ││
│ ▲ ▲ │Exponent (8 Bits)│ │Mantissa Bits │ │Currency Code │ │Issuer AccountID││
│ │ │ └─────────────────┘ │ (54 Bits) │ │ (160 bits) │ │ (160 bits) ││
│ │ └────────────────┐ └──────────────┘ └──────────────┘ └────────────────┘│
│ │ │ │
└──┴───────────────────┴─────────────────────────────────────────────────────────┘
┌──────────────────┐┌──────────────────┐
│ Type Bit ││ Sign bit │
│(0=XRP/MPT;1=IOU) ││ (1 for positive) │
└──────────────────┘└──────────────────┘§Derived Traits
PartialEq, Eq: Enable comparisons and use in collectionsDebug, Clone: Standard traits for development and consistency
Note: Copy is intentionally not derived due to the enum’s size (48 bytes).
Variants§
XRP
Fields
num_drops: i64Design decision note: Per the pattern in Amount, we considered having this be an
unsigned u64 and adding an is_positve boolean to this variant. However, we decided to
break that pattern and instead use an i64 here for two reasons. First, this allows
simple math like add, sub, etc. to be performed in WASM without having to check for
negative values. Second, the total supply of XRP is capped at 100B XRP (100B * 1M Drops),
which fits just fine into an i64.
IOU
MPT
Implementations§
Source§impl Amount
impl Amount
Sourcepub fn to_stamount_bytes(&self) -> ([u8; 48], usize)
pub fn to_stamount_bytes(&self) -> ([u8; 48], usize)
Converts a Amount to STAmount bytes format.
All Amount types return a 48-byte array for consistency with the XRPL STAmount format. The format follows the XRPL binary layout:
- XRP: Raw drop amount with sign bit in first 8 bytes + 40 bytes padding
- MPT: Flag byte (0b_0110_0000) in byte 0, raw amount in bytes 1-9, MptId in bytes 9-33 + 15 bytes padding
- IOU: IOUNumber in first 8 bytes, Currency in bytes 8-28, AccountID in bytes 28-48
Returns a tuple of (bytes, length) where length is always 48.
Sourcepub fn from_bytes(bytes: &[u8]) -> Result<Self>
pub fn from_bytes(bytes: &[u8]) -> Result<Self>
Parses a Amount from a byte array.
The byte array can be one of three formats:
- XRP: 8 bytes
- MPT: 33 bytes
- IOU: 48 bytes
Returns Err(InvalidParams) if the byte array is not a valid Amount.
Trait Implementations§
impl Eq for Amount
Source§impl FieldDecoder for Amount
FieldDecoder for XRPL amount values. The host writes a variable number of bytes into the
fixed AMOUNT_SIZE buffer — 8 for XRP, 33 for MPT, 48 for IOU — with the remainder left as
empty_buffer()’s zero-padding, which is exactly the shape Amount::from_bytes wants, so it
reads the buffer in place with no re-slice or re-copy.
impl FieldDecoder for Amount
FieldDecoder for XRPL amount values. The host writes a variable number of bytes into the
fixed AMOUNT_SIZE buffer — 8 for XRP, 33 for MPT, 48 for IOU — with the remainder left as
empty_buffer()’s zero-padding, which is exactly the shape Amount::from_bytes wants, so it
reads the buffer in place with no re-slice or re-copy.