pub struct TokenAttributesMapper<SA, A = CurrentStorage>where
SA: StorageMapperApi,{ /* private fields */ }Expand description
Specialized mapper for managing NFT/SFT token attributes with efficient storage and bidirectional lookup capabilities. Maps token attributes to nonces and vice versa, enabling efficient queries by both token nonce and attribute values.
§Storage Layout
The mapper uses a sophisticated multi-key storage layout for efficient lookups:
base_key + ".counter" → u8 // Global token ID counter
base_key + ".mapping" + token_id → u8 // Token ID to internal mapping ID
base_key + ".attr" + mapping + nonce → T // Token attributes by nonce
base_key + ".nonce" + mapping + attr → u64 // Nonce lookup by attributes§Main Operations
§Attribute Management
- Set: Store attributes for token nonce with
set() - Update: Modify existing attributes with
update() - Clear: Remove attributes with
clear() - Get: Retrieve attributes by nonce with
get_attributes()
§Bidirectional Lookup
- Nonce by Attributes: Find nonce for specific attributes with
get_nonce() - Attributes by Nonce: Find attributes for specific nonce with
get_attributes() - Existence Checks: Verify presence with
has_attributes(),has_nonce()
§Internal Mapping
- Space Optimization: Uses u8 internal IDs to reduce storage keys
- Counter Management: Automatically assigns mapping IDs up to 255 tokens
- Collision Avoidance: Each token ID gets unique mapping space
§Trade-offs
Advantages:
- Bidirectional lookup (nonce ↔ attributes) in O(1) time
- Space-efficient storage with internal mapping compression
- Prevents duplicate attribute sets per token
- Supports any encodable attribute type
Limitations:
- Maximum 255 different token IDs per mapper instance
- Attributes cannot be changed once set (use update carefully)
- Complex storage layout increases implementation overhead
- No iteration capabilities over stored mappings
Implementations§
Source§impl<SA> TokenAttributesMapper<SA, CurrentStorage>where
SA: StorageMapperApi,
impl<SA> TokenAttributesMapper<SA, CurrentStorage>where
SA: StorageMapperApi,
pub fn set<T: TopEncode + TopDecode + NestedEncode + NestedDecode, M: ManagedTypeApi>( &self, token_id: &EsdtTokenIdentifier<M>, token_nonce: u64, attributes: &T, )
Sourcepub fn update<T: TopEncode + TopDecode + NestedEncode + NestedDecode, M: ManagedTypeApi>(
&self,
token_id: &EsdtTokenIdentifier<M>,
token_nonce: u64,
attributes: &T,
)
pub fn update<T: TopEncode + TopDecode + NestedEncode + NestedDecode, M: ManagedTypeApi>( &self, token_id: &EsdtTokenIdentifier<M>, token_nonce: u64, attributes: &T, )
Use carefully. Update should be used mainly when backed up by the protocol.
pub fn clear<T: TopEncode + TopDecode + NestedEncode + NestedDecode, M: ManagedTypeApi>( &self, token_id: &EsdtTokenIdentifier<M>, token_nonce: u64, )
Source§impl<SA, A> TokenAttributesMapper<SA, A>where
SA: StorageMapperApi,
A: StorageAddress<SA>,
impl<SA, A> TokenAttributesMapper<SA, A>where
SA: StorageMapperApi,
A: StorageAddress<SA>,
pub fn has_attributes<M: ManagedTypeApi>( &self, token_id: &EsdtTokenIdentifier<M>, token_nonce: u64, ) -> bool
pub fn has_nonce<T: TopEncode + TopDecode + NestedEncode + NestedDecode, M: ManagedTypeApi>( &self, token_id: &EsdtTokenIdentifier<M>, attr: &T, ) -> bool
pub fn get_attributes<T: TopEncode + TopDecode + NestedEncode + NestedDecode, M: ManagedTypeApi>( &self, token_id: &EsdtTokenIdentifier<M>, token_nonce: u64, ) -> T
pub fn get_nonce<T: TopEncode + TopDecode + NestedEncode + NestedDecode, M: ManagedTypeApi>( &self, token_id: &EsdtTokenIdentifier<M>, attr: &T, ) -> u64
Trait Implementations§
Source§impl<SA> StorageMapper<SA> for TokenAttributesMapper<SA, CurrentStorage>where
SA: StorageMapperApi,
impl<SA> StorageMapper<SA> for TokenAttributesMapper<SA, CurrentStorage>where
SA: StorageMapperApi,
Source§fn new(base_key: StorageKey<SA>) -> Self
fn new(base_key: StorageKey<SA>) -> Self
Will be called automatically by the
#[storage_mapper] annotation generated code.Source§impl<SA> StorageMapperFromAddress<SA> for TokenAttributesMapper<SA, ManagedAddress<SA>>where
SA: StorageMapperApi,
impl<SA> StorageMapperFromAddress<SA> for TokenAttributesMapper<SA, ManagedAddress<SA>>where
SA: StorageMapperApi,
Source§fn new_from_address(
address: ManagedAddress<SA>,
base_key: StorageKey<SA>,
) -> Self
fn new_from_address( address: ManagedAddress<SA>, base_key: StorageKey<SA>, ) -> Self
Will be called automatically by the
#[storage_mapper_from_address]
annotation generated code.Auto Trait Implementations§
impl<SA, A> Freeze for TokenAttributesMapper<SA, A>
impl<SA, A> RefUnwindSafe for TokenAttributesMapper<SA, A>where
A: RefUnwindSafe,
SA: RefUnwindSafe,
<SA as HandleTypeInfo>::ManagedBufferHandle: RefUnwindSafe,
impl<SA, A> Send for TokenAttributesMapper<SA, A>
impl<SA, A> Sync for TokenAttributesMapper<SA, A>
impl<SA, A> Unpin for TokenAttributesMapper<SA, A>
impl<SA, A> UnsafeUnpin for TokenAttributesMapper<SA, A>
impl<SA, A> UnwindSafe for TokenAttributesMapper<SA, A>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more