feat(asset): add AssetAmount wrapper type#2721
Open
giwaov wants to merge 1 commit into0xMiden:nextfrom
Open
Conversation
Introduce a validated AssetAmount(u64) newtype that encapsulates the max-amount check (2^63 - 2^31) at construction time. - Add AssetAmount with MAX constant, new(), as_u64(), From<u8/u16/u32>, TryFrom<u64>, Display, Serializable, Deserializable - Integrate into FungibleAsset: replace raw u64 amount field - Delegate validation in FungibleAsset::new() to AssetAmount::new() - Keep FungibleAsset::MAX_AMOUNT as backward-compatible alias - Keep amount() returning u64 to minimize downstream churn Closes 0xMiden#2532
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Introduces a validated
AssetAmount(u64)newtype that encapsulates the max-amount invariant (2^63 - 2^31) at construction time, replacing the rawu64field insideFungibleAsset.Changes
New:
AssetAmount(asset_amount.rs)MAXconstant equal to2^63 - 2^31new(amount: u64) -> Result<Self, AssetError>with validationas_u64()accessorFrom<u8>,From<u16>,From<u32>(infallible small-type conversions)TryFrom<u64>(delegates tonew)From<AssetAmount> for u64Display,Serializable,DeserializableModified:
FungibleAsset(fungible.rs)amountfield changed fromu64toAssetAmountFungibleAsset::MAX_AMOUNTkept as a backward-compatibleu64alias pointing toAssetAmount::MAXnew()delegates validation toAssetAmount::new()amount()still returnsu64viaas_u64()to minimize downstream churnadd()/sub()updated to work throughAssetAmountself.amount.as_u64()Modified:
mod.rsmod asset_amount; pub use asset_amount::AssetAmount;Testing
cargo checkpassesFungibleAssettests are compatible (no API surface change for callers)Closes #2532