Skip to content

feat(asset): add AssetAmount wrapper type#2721

Open
giwaov wants to merge 1 commit into0xMiden:nextfrom
giwaov:feat/asset-amount-wrapper
Open

feat(asset): add AssetAmount wrapper type#2721
giwaov wants to merge 1 commit into0xMiden:nextfrom
giwaov:feat/asset-amount-wrapper

Conversation

@giwaov
Copy link
Copy Markdown

@giwaov giwaov commented Apr 2, 2026

Summary

Introduces a validated AssetAmount(u64) newtype that encapsulates the max-amount invariant (2^63 - 2^31) at construction time, replacing the raw u64 field inside FungibleAsset.

Changes

New: AssetAmount (asset_amount.rs)

  • MAX constant equal to 2^63 - 2^31
  • new(amount: u64) -> Result<Self, AssetError> with validation
  • as_u64() accessor
  • From<u8>, From<u16>, From<u32> (infallible small-type conversions)
  • TryFrom<u64> (delegates to new)
  • From<AssetAmount> for u64
  • Display, Serializable, Deserializable
  • Unit tests for all of the above

Modified: FungibleAsset (fungible.rs)

  • Internal amount field changed from u64 to AssetAmount
  • FungibleAsset::MAX_AMOUNT kept as a backward-compatible u64 alias pointing to AssetAmount::MAX
  • new() delegates validation to AssetAmount::new()
  • amount() still returns u64 via as_u64() to minimize downstream churn
  • add() / sub() updated to work through AssetAmount
  • Serialization writes self.amount.as_u64()

Modified: mod.rs

  • Added mod asset_amount; pub use asset_amount::AssetAmount;

Testing

  • Full workspace cargo check passes
  • Existing FungibleAsset tests are compatible (no API surface change for callers)

Closes #2532

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Introduce wrapper type for asset amounts

1 participant