feat: introduce TokenMetadata type for fungible faucet metadata#2380
Conversation
This adds a new TokenMetadata struct that encapsulates the metadata associated with fungible faucet accounts (token_supply, max_supply, decimals, symbol). The struct provides: - Constructors: new() and with_supply() - Accessors: token_supply(), max_supply(), decimals(), symbol() - Conversions: From<TokenMetadata> for Word and StorageSlot - Parsing: TryFrom<&StorageSlot> and TryFrom<&AccountStorage> BasicFungibleFaucet and NetworkFungibleFaucet have been refactored to use TokenMetadata internally, reducing code duplication. Closes 0xMiden#2344
051da4e to
3e470d6
Compare
All done. thank you @PhilippGackstatter 👍 |
PhilippGackstatter
left a comment
There was a problem hiding this comment.
Great work! I left a few suggestions.
| /// Token metadata for fungible faucet accounts. | ||
| /// | ||
| /// This struct encapsulates the metadata associated with a fungible token faucet: | ||
| /// - `token_supply`: The current amount of tokens issued by the faucet. | ||
| /// - `max_supply`: The maximum amount of tokens that can be issued. | ||
| /// - `decimals`: The number of decimal places for token amounts. | ||
| /// - `symbol`: The token symbol. | ||
| /// | ||
| /// The metadata is stored in a single storage slot as: | ||
| /// `[token_supply, max_supply, decimals, symbol]` | ||
| #[derive(Debug, Clone, Copy)] | ||
| pub struct TokenMetadata { |
There was a problem hiding this comment.
Since this is now documented here, I would remove the docs from BasicFungibleFaucet and instead link to TokenMetadata, something like:
/// ## Storage Layout
///
/// - [`Self::metadata_slot`]: Stores [`TokenMetadata`]
| /// Tries to create [`TokenMetadata`] from a storage slot. | ||
| fn try_from(slot: &StorageSlot) -> Result<Self, Self::Error> { | ||
| TokenMetadata::try_from_word(slot.value()) |
There was a problem hiding this comment.
We should also validate that the slot name matches.
| // TRAIT IMPLEMENTATIONS | ||
| // ================================================================================================ | ||
|
|
||
| impl From<TokenMetadata> for Word { |
There was a problem hiding this comment.
I think it would also be fine to publicly implement TryFrom<Word> for TokenMetadata.
|
|
||
| // TOKEN METADATA | ||
| // ================================================================================================ |
There was a problem hiding this comment.
Let's move static METADATA_SLOT_NAME into this file since it's only used here now.
| impl TryFrom<&StorageSlot> for TokenMetadata { | ||
| type Error = FungibleFaucetError; |
There was a problem hiding this comment.
Let's make use of this implementation and replace NetworkFungibleFaucet::try_from(&faucet)?.token_supply() in crates/miden-testing/tests/agglayer/bridge_out.rs with decoding the TokenMetadata from faucet.storage().
Same in crates/miden-testing/tests/scripts/faucet.rs, including replacing BasicFungibleFaucet::try_from.
- Move METADATA_SLOT_NAME constant to token_metadata.rs - Update BasicFungibleFaucet docs to reference TokenMetadata - Add slot name validation in TryFrom<&StorageSlot> for TokenMetadata - Implement TryFrom<Word> for TokenMetadata (public trait) - Replace NetworkFungibleFaucet/BasicFungibleFaucet::try_from with TokenMetadata in tests
c420686 to
2c4bdd6
Compare
PhilippGackstatter
left a comment
There was a problem hiding this comment.
Looks good to me!
Summary
TokenMetadatastruct to encapsulate fungible faucet metadata (token_supply,max_supply,decimals,symbol)From<TokenMetadata> for Word,From<TokenMetadata> for StorageSlot,TryFrom<&StorageSlot>,TryFrom<&AccountStorage>BasicFungibleFaucetandNetworkFungibleFaucetto useTokenMetadatainternally, reducing code duplicationTest plan
TokenMetadatavalidation and conversionsCloses #2344