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:
We should add a dedicated type that represents a valid amount in a
FungibleAsset. This could be calledFungibleAssetAmountorAssetAmount. 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:
We should definitely avoid constructors that take an arbitrary
u64and round it down to% FungibleAssetAmount::MAX.The need for this came up in a few places:
MINTnote to support public output notes #2073 (comment)Assets to the double word representation #2437 (comment)