Enumeration
In the previous tutorials, you looked at ways to integrate the minting functionality into a skeleton smart contract. In order to get your NFTs to show in the wallet, you also had to deploy a patch fix that implemented one of the enumeration methods. In this tutorial, you'll expand on and finish the rest of the enumeration methods as per the standard Now you'll extend the NFT smart contract and add a couple of enumeration methods that can be used to return the contract's state.
Introductionโ
As mentioned in the Upgrade a Contract tutorial, you can deploy patches and fixes to smart contracts. This time, you'll use that knowledge to implement the nft_total_supply
, nft_tokens
and nft_supply_for_owner
enumeration functions.
To get started, either switch to the 2.minting
branch from our GitHub repository, or continue your work from the previous tutorials. If you haven't cloned it yet, refer to the Contract Architecture to check out the repository.
git checkout 2.minting
3.enumeration
branch. :::Modifications to the contractโ
Let's start by opening the src/enumeration.ts
file and locating the empty internalNftTotalSupply
function.
NFT Total Supplyโ
This function should return the total number of NFTs stored on the contract. You can easily achieve this functionality by simply returning the length of the nftMetadataById
data structure.
loading...
NFT Tokensโ
This function should return a paginated list of JsonTokens
that are stored on the contract regardless of their owners. If the user provides a from_index
parameter, you should use that as the starting point for which to start iterating through tokens; otherwise it should start from the beginning. Likewise, if the user provides a limit
parameter, the function shall stop after reaching either the limit or the end of the list.
loading...
NFT Supply For Ownerโ
This function should look for all the non-fungible tokens for a user-defined owner, and return the length of the resulting set. If there isn't a set of tokens for the provided Account ID, then the function shall return 0
.
loading...
Next, you can use the CLI to query these new methods and validate that they work correctly.
Redeploying the contractโ
Now that you've implemented the necessary logic for nft_tokens_for_owner
, it's time to build and re-deploy the contract to your account. Using the build script, deploy the contract as you did in the previous tutorials:
yarn build && near deploy --wasmFile build/nft.wasm --accountId $NFT_CONTRACT_ID
This should output a warning saying that the account has a deployed contract and will ask if you'd like to proceed. Simply type y
and hit enter.
This account already has a deployed contract [ AKJK7sCysrWrFZ976YVBnm6yzmJuKLzdAyssfzK9yLsa ]. Do you want to proceed? (y/n)
Enumerating tokensโ
Once the updated contract has been redeployed, you can test and see if these new functions work as expected.
NFT tokensโ
Let's query for a list of non-fungible tokens on the contract. Use the following command to query for the information of up to 50 NFTs starting from the 10th item:
near view $NFT_CONTRACT_ID nft_tokens '{"from_index": "10", "limit": 50}'
This command should return an output similar to the following:
Example response:
[]