Skip to main content

Amount

Enum Amount 

Source
#[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 collections
  • Debug, 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: i64

Design 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

Fields

§amount: IOUNumber
§issuer: AccountID
§currency: Currency
§

MPT

Fields

§num_units: u64
§is_positive: bool
§mpt_id: MptId

Implementations§

Source§

impl Amount

Source

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.

Source

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§

Source§

impl Clone for Amount

Source§

fn clone(&self) -> Amount

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Amount

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

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.

Source§

type Buffer = [u8; 48]

The buffer a get_field caller allocates before invoking the host function. Each type picks its own size (an associated type, not a const, so this stays on stable Rust).
Source§

fn empty_buffer() -> Self::Buffer

Returns a zero-initialized buffer of this type’s Buffer size.
Source§

fn decode(buf: Self::Buffer, bytes_written: usize) -> Result<Self, DecodeError>

Decodes Self from the full Buffer (as written by the host, then zero-padded to the buffer’s size), given bytes_written — the number of bytes the host actually wrote. Read more
Source§

impl FromCurrentTx for Amount

Source§

impl FromLedger for Amount

Source§

impl PartialEq for Amount

Source§

fn eq(&self, other: &Amount) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Amount

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Any for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

§

fn type_name(&self) -> &'static str

§

impl<T> AnySync for T
where T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.