-1

I am just starting out with Rust and NEAR and trying to create a simple function that counts how many NFTs have been minted with a particular substring.

My NFTs have a token_id which contains a randomstring-tier1 or randomstring-tier2 and rather than returning the total amount minted. i want o know for each tier.

I have this very basic function that returns the total count.

    pub fn nft_total_supply(&self) -> U128 {
        //return the length of the token metadata by ID
        U128(self.token_metadata_by_id.len() as u128)
    }

But not got a good enough understanding of how I check the token_id for a particular sub-string.

Was trying

pub fn check_nft_minted_by_tier1(
    &self, 
    token_id
) -> u128 {
    if token_id.contains("tier1").count() {
        U128(tier1.len() as u128)
    }  
}

But this doesn't work.

Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77
JacksWastedLife
  • 254
  • 2
  • 15

1 Answers1

0

My personal recommendation is to have this information stored in state that way you can quickly access it with little computation and it's also scalable. If the list of NFTs grows very high, you might not have enough GAS to loop through each and check the token ID for the substring.

I would store something in state that either keeps track of the number of NFTs with each tier, or keeps track of the token IDs for each tier. That way you can expand later and maybe get the metadata as well rather than just the total number of NFTs.

This set of token IDs can be populated upon minting an NFT. At the end of the mint function, you can add a quick check to see if the token ID contains the sub-string and then add it to the list if it does. You can reference this post to see how you can check for a sub-string.

Benjamin Kurrek
  • 1,044
  • 3
  • 14