Introduction
Discover the excitement of collecting rare NFTs! These unique digital tokens represent ownership of digital art, collectibles, and more, and their rarity adds to their value. But how is NFT rarity calculated? It's all about supply and demand. NFTs with a limited supply are naturally more rare and valuable, as there are fewer of them available for collectors to acquire. On the other hand, NFTs with a higher supply may not be as coveted, and therefore not as valuable.
What is NFT Rarity
Rarity is a measure of how scarce or unique an NFT is, and it is an important factor that can affect the value of the NFT.
There are several ways to calculate rarity for NFTs, and the specific method used may depend on the type of NFT and the context in which it is being evaluated. Some common methods of calculating rarity include:
- Total supply: Rarity can be calculated based on the total number of units of an NFT that are available. For example, if an NFT has a total supply of 100 units, it may be considered rarer than an NFT with a total supply of 1,000 units.
- Demand: Rarity can be influenced by the demand for an NFT, which is the extent to which people are interested in acquiring it. An NFT with high demand may be considered rarer than one with low demand.
- Rarity tiers: Some NFT platforms or marketplaces may assign rarity tiers to NFTs, based on a combination of factors such as total supply, edition size, demand, and other criteria. These tiers can be used to classify NFTs as common, rare, very rare, or ultra rare, for example.
🚀 What we will be building
Today, we will create a NFT rank determiner using the newest Alchemy NFT Rarity API. The API provides us with information on the prevalence of NFT traits.
The prevalence number indicates how many percent of the collection contains certain qualities, so if the prevalence value is high, the NFT is more common, and if the prevalence value is low, the provided trait is less common in the collection, making the NFT containing trait unusual.
We will use the average approach, which involves taking the average of all trait prevalence data and then multiplying it by the total supply of the collection to calculate the rank of the NFT.
Step 1: Setting up the Environment
To store your scripts, create a new directory named alchemy-rarity-api
. Use the following command to set up the project as a npm project or a yarn project.
yarn init --yes
npm init --yes
Once the directory has been created as a npm project, we need to make a few changes within the package.json
file. Because we will be utilizing the import syntax to load ES6 modules, add the following line to your package.json
file
// package.json
{
...
"type": "module"
}
now we have to install the Alchemy SDK to easily interact with the Alchemy APIs.
To install alchemy-sdk
, run the following command.
yarn add alchemy-sdk
We are now ready to write our NFT rank determiner script. Make a new file in the project root named nft-rarity.js
.
Step 2: Initializing SDK
The first step is to import the module, which we shall accomplish simply by doing
import { Network, Alchemy } from "alchemy-sdk";
Now we must define the configuration to use the SDK; we will construct a settings object to contain our API Key and the network from which the data will be collected.
const settings = {
apiKey: "api-key-here",
network: Network.ETH_MAINNET,
};
last step is to initialize a new instance of the SDK, for this we will use the following line
const alchemy = new Alchemy(settings);
Step 3: Calling API for Rarity Details
First, we'll create two variables to store the address and token id of the NFT collection and NFT, respectively, for which we want rarity insights.
const address = "0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d"; // Bored Ape Yacht Club
const tokenID = "7717";
We require the total supply of the collection since we need the rank of the NFT, and we can easily acquire that data using the getContractMetadata
method in the NFT API.
The total supply will then be saved in a new variable.
const contractMetadata = await alchemy.nft.getContractMetadata(address);
const totalSupply = contractMetadata.totalSupply;
Now we can use the computeRarity
endpoint with the contract address and token id as inputs to receive the rarity details.
const rarity = await alchemy.nft.computeRarity(address, tokenID);
The rarity endpoints returns an array of objects containing the traitType
, prevalence
, and value
for each NFT attribute.
[
{ prevalence: 0.0626, traitType: 'Fur', value: 'Tan' },
{ prevalence: 0.0479, traitType: 'Eyes', value: 'Coins' },
{ prevalence: 0.0462, traitType: 'Earring', value: 'Gold Hoop' },
{ prevalence: 0.0163, traitType: 'Clothes', value: 'Bandolier' },
{ prevalence: 0.2272, traitType: 'Mouth', value: 'Bored' },
{ prevalence: 0.1266, traitType: 'Background', value: 'Aquamarine' }
]
Step 4: Calculating Rank
We will now calculate the rank of the NFT using the average approach, which involves taking the average of all the prevalence of the NFT attributes and multiplying it by the totalSupply
for the collection to determine the NFT's position in the collection.
we can construct a function called computeRank
and take in two arguments the rarity array and the overall supply of the collection.
then we will iterate over the array and take the average of the prevalence and then multiply it with the total supply of the collection.
const computeRank = (rarity, totalSupply) => {
let rank = 0;
for (const key in rarity) {
rank += rarity[key].prevalence;
}
rank = (rank / rarity.length) * totalSupply;
console.log(Math.round(rank));
};
Finally, we will run the function to determine the nft's rank.
computeRank(rarity, totalSupply); // 878
Entire Code:
import { Network, Alchemy } from "alchemy-sdk";
const settings = {
apiKey: "api-key-here",
network: Network.ETH_MAINNET,
};
const alchemy = new Alchemy(settings);
const address = "0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d"; // Bored Ape Yacht Club
const tokenID = "7717";
const computeRank = (rarity, totalSupply) => {
let rank = 0;
for (const key in rarity) {
rank += rarity[key].prevalence;
}
rank = (rank / rarity.length) * totalSupply;
console.log(Math.round(rank));
};
const contractMetadata = await alchemy.nft.getContractMetadata(address);
const totalSupply = contractMetadata.totalSupply;
const rarity = await alchemy.nft.computeRarity(address, tokenID);
computeRank(rarity, totalSupply); // 878
Conclusion
Congratulations! You have successfully built a NFT rank determiner using the Alchemy NFT Rarity API. By using the basic average method, you were able to determine the rank of an NFT based on its rarity.
However, it's important to note that the ranks of NFTs can vary greatly depending on people's personal preferences. What one person considers a highly prized NFT might be seen as less valuable to someone else. Therefore, it's important to remember that the rank determiner you have built is just a rough estimate, and the true value of an NFT can only be determined by the market.
Despite this, your NFT rank determiner can still be a useful tool for getting a general idea of the relative rarity and value of different NFTs. You can use it to compare the ranks of different NFTs and get a sense of which ones might be more sought after by collectors.
Top comments (0)