Skip to content

Introduce wrapper type for asset amounts #2532

@PhilippGackstatter

Description

@PhilippGackstatter

We should add a dedicated type that represents a valid amount in a FungibleAsset. This could be called FungibleAssetAmount or AssetAmount. The former is a bit clearer to which type it belongs while the latter is more concise. I think I'd go with the former.

It should probably have at least these parts:

#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct FungibleAssetAmount(u64);

impl FungibleAssetAmount {
    // Replaces from FungibleAsset::MAX_AMOUNT
    pub const MAX: Self = Self(2u64.pow(63) - 2u64.pow(31));

    /// Returns an error if amount exceeds Self::MAX.
    pub fn new(amount: u64) -> Result<Self, _> { ... }
}

impl From<u8> for FungibleAssetAmount { ... }
impl From<u16> for FungibleAssetAmount { ... }
impl From<u32> for FungibleAssetAmount { ... }
impl TryFrom<u64> for FungibleAssetAmount { ... }

impl core::fmt::Display for FungibleAssetAmount { ... }

We should definitely avoid constructors that take an arbitrary u64 and round it down to % FungibleAssetAmount::MAX.

The need for this came up in a few places:

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueGood for newcomersrustIssues that affect or pull requests that update Rust codestandardsRelated to standard note scripts or account components

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions