Skip to main content

TokenAttributesMapper

Struct TokenAttributesMapper 

Source
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,

Source

pub fn set<T: TopEncode + TopDecode + NestedEncode + NestedDecode, M: ManagedTypeApi>( &self, token_id: &EsdtTokenIdentifier<M>, token_nonce: u64, attributes: &T, )

Source

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.

Source

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>,

Source

pub fn has_attributes<M: ManagedTypeApi>( &self, token_id: &EsdtTokenIdentifier<M>, token_nonce: u64, ) -> bool

Source

pub fn has_nonce<T: TopEncode + TopDecode + NestedEncode + NestedDecode, M: ManagedTypeApi>( &self, token_id: &EsdtTokenIdentifier<M>, attr: &T, ) -> bool

Source

pub fn get_attributes<T: TopEncode + TopDecode + NestedEncode + NestedDecode, M: ManagedTypeApi>( &self, token_id: &EsdtTokenIdentifier<M>, token_nonce: u64, ) -> T

Source

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,

Source§

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,

Source§

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T, U> TypeAbiFrom<TypeAbiUniversalInput<T>> for U