The Hedera NFT Utilities SDK for JavaScript is a set of tools and resources developers can use to build NFT-related software or perform NFT-related actions. The goal of the NFT Utilities SDK is to make it easier for builders and artists to implement standard NFT features, such as calculating risk and rarity, or perform NFT tasks, such as verifying NFT metadata against a schema specification like HIP412.
By providing several helpful NFT utilities, we want to support the Hedera NFT community in building faster, more, and better NFT projects. Instead of building your own NFT tooling, you can make use of the building blocks provided by the Hedera NFT Utilities SDK.
The project aims to evolve the Hedera NFT Utilities JavaScript SDK based on community feedback to make it an excellent tool for NFT creators on the Hedera network.
In this blog post, you'll learn:
The Hedera NFT Utilities SDK consists of three core functionalities. Here's a quick overview:
Let's take a look at the details of each package. But first, how do you install the Hedera NFT Utilities SDK?
Because the package is created with JavaScript, you can use NPM or Yarn package managers to install the SDK.
npm i -s @hashgraph/nft-utilities
Next, you can import the functionality you need. Let's look at each functionality individually to see what you can do and how it works.
The SDK offers multiple validator functions. Let's look at the main function you can use to validate metadata against the HIP412 standard.1. "validator()"
const { validator, defaultVersion } = require('@hashgraph/nft-utilities'); // Valid metadata instance const metadataInstance = { "creator": "HANGRY BARBOONS", "description": "HANGRY BARBOONS are 4,444 unique citizens from the United Hashgraph of Planet Earth. Designed and illustrated by President HANGRY.", "format": "none", "name": "HANGRY BARBOON #2343", "image": "ipfs://QmaHVnnp7qAmGADa3tQfWVNxxZDRmTL5r6jKrAo16mSd5y/2343.png", "type": "image/png", "properties": { "edition": 2343 }, "attributes": [ { "trait_type": "Background", "value": "Yellow" }, { "trait_type": "Fur", "value": "Gold" }, { "trait_type": "Clothing", "value": "Floral Jacket" }, { "trait_type": "Mouth", "value": "Tongue" }, { "trait_type": "Sing", "value": "None" } ] } const results = validator(metadataInstance); // by default: verifies metadata against [email protected] console.log(results); /* Output: { errors: [], warnings: [] } */
The validator function accepts stringified metadata and a version number. The version number refers to a specific version of HIP412 you want to verify your metadata against. Currently, you can import the "defaultVersion" parameter, which refers to the latest version of the HIP412 specification (HIP412 v2.0.0).
The validator function outputs both errors and warnings. Errors refer to mistakes against the HIP412 standard, while warnings are related to additional properties on the JSON object not specified by the HIP412 metadata standard. According to the standard, any additional properties should be included in the "properties" object to keep the JSON object tidy and easy to interpret.
2. "localValidation()"
const { localValidation } = require('@hashgraph/nft-utilities'); const absolutePathToFiles = "/Users/myUser/nft-utilities/examples/local-metadata-validator/files"; localValidation(absolutePathToFiles); /* Output: Found 6 for directory: /Users/myUser/nft-utilities/examples/local-metadata-validator/files Found 5 files with the .json extension { "nft1.json": {"errors":[],"warnings":[]}, "nft2.json":{"errors":[{"type":"schema","msg":"requires property 'image'","path":"instance"},{"type":"schema","msg":"requires property 'type'","path":"instance"}],"warnings":[]}, "nft3.json":{"errors":[],"warnings":[]}, "nft4.json":{"errors":[],"warnings":[]}, "nft5.json":{"errors":[],"warnings":[]} } */
The local validator allows you to verify a folder on your machine containing multiple JSON metadata files. For instance, you have created a new NFT project, and you want to make sure all metadata files are correctly formatted according to the HIP412 standard. You can use the function by passing an absolute path to the folder containing the JSON files.
The output format is the same for the "localValidation" function, returning errors and warnings for each file the function finds in the specified folder.
The risk score calculation functions allow you to calculate the risk score for a token based on the token information by looking at the keys that are set for the token. The presence of each key has an associated risk. For instance, the presence of an admin key is one of the highest risks for a token holder. The admin key can update the properties of the token or delete the token. To calculate the risk score, each key or property receives a "weight". By adding all weights, you get a final risk score for a token. Here's the overview of all the weights.
const defaultWeights = { keys: { admin_key: 200, wipe_key: 200, freeze_key: 50, supply_key: 20, kyc_key: 50, pause_key: 50, fee_schedule_key: 40 }, properties: { supply_type_infinite: 20 } }
First, let's look at the different keys and why they received their respective weight.
Next, let's look at property-specific weights:
Next, the risk level is determined based on the total risk score. For instance, a token with a risk score of 199 will receive a risk level of "MEDIUM". Here's the distribution of risk levels.
const defaultRiskLevels = { NORISK: 0, LOW: 40, MEDIUM: 199, HIGH: 2000 };
There are two functions you can use to calculate the risk score. You can either input the token data yourself or provide a token ID. When you provide a token ID, the function will look up the token metadata on the mainnet and calculate the risk score and level.
const { calculateRiskScoreFromData, calculateRiskScoreFromTokenId } = require('@hashgraph/nft-utilities'); const results = await calculateRiskScoreFromTokenId("0.0.1270555"); console.log(results); /* Output: { riskScore: 20, riskLevel: 'LOW' } */
The "calculateRarity" function allows you to calculate the rarity of a folder on your machine containing JSON metadata files for an NFT collection. The function looks at the traits listed in the "attributes" property and their respective values to calculate the rarity score for each NFT (metadata file). This package uses the trait normalization rarity scoring model because it's the fairest model to calculate rarity. The model works by dividing the number one by the division of the number of NFTs with a specific trait value and the number of NFTs with the most common trait value for that trait. Here's the formula:
1 / (# of NFTs with trait value / # of NFTs with most common trait value)
Import the function and pass it an absolute path to the folder containing JSON metadata files.
const { calculateRarity } = require('@hashgraph/nft-utilities'); const absolutePathToFiles = "/Users/myUser/nft-utilities/examples/rarity-score-calculation/files"; const results = calculateRarity(absolutePathToFiles); console.log(results);
The function will output the rarity for each file. The output looks like this.
[ { rarity: '5.50', NFT: 1, filename: 'nft1.json' }, { rarity: '6.00', NFT: 2, filename: 'nft2.json' }, { rarity: '5.50', NFT: 3, filename: 'nft3.json' }, { rarity: '5.50', NFT: 4, filename: 'nft4.json' }, { rarity: '11.50', NFT: 5, filename: 'nft5.json' } ]
If you have feature requests or want to submit feedback, please open an issue on the GitHub repository for the project.