Last active
February 14, 2023 05:04
-
-
Save itsanishjain/d12e1908693172705b5c9286f6956c84 to your computer and use it in GitHub Desktop.
Creates the rarity similar to Opensea
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
- yarn init -y | |
- update package.json :) | |
{ | |
"name": "rarity", | |
"version": "1.0.0", | |
"main": "index.js", | |
"license": "MIT", | |
"type": "module", | |
"dependencies": { | |
"node-fetch": "^3.3.0" | |
} | |
} | |
- node app.js | |
*/ | |
import fetch from "node-fetch"; | |
import fs from "fs"; | |
const randomNumber = () => { | |
const min = 1; | |
const max = 10; | |
const diff = max - min; // return 5 | |
const randomNumber = Math.random(); // function returns integers between from 0 -> 1 [ex: 0.001,0.2] | |
const randomNumnerInRange = Math.floor(randomNumber * diff + min); | |
return randomNumnerInRange; | |
}; | |
const fetchMetaData = async (i) => { | |
let obj = {}; | |
const baseUrls = [ | |
"https://cloudflare-ipfs.com/ipfs/Qmdu6QgAMJqiwRcGGMadPMkkdSCDv6NoQG2neTN8fYirN9/", | |
"https://gateway.pinata.cloud/ipfs/Qmdu6QgAMJqiwRcGGMadPMkkdSCDv6NoQG2neTN8fYirN9/", | |
"https://ipfs.fleek.co/ipfs/Qmdu6QgAMJqiwRcGGMadPMkkdSCDv6NoQG2neTN8fYirN9/", | |
"https://gateway.ipfs.io/ipfs/Qmdu6QgAMJqiwRcGGMadPMkkdSCDv6NoQG2neTN8fYirN9/", | |
"https://via0.com/ipfs/Qmdu6QgAMJqiwRcGGMadPMkkdSCDv6NoQG2neTN8fYirN9/", | |
"https://ipfs.smartholdem.io/ipfs/Qmdu6QgAMJqiwRcGGMadPMkkdSCDv6NoQG2neTN8fYirN9/", | |
"https://bafybeihhf3zmdzulp5jhsc5pj463dbfuippnprdy4irjz3rijiqlf76uay.ipfs.cf-ipfs.com/", | |
"https://bafybeihhf3zmdzulp5jhsc5pj463dbfuippnprdy4irjz3rijiqlf76uay.ipfs.nftstorage.link/", | |
"https://bafybeihhf3zmdzulp5jhsc5pj463dbfuippnprdy4irjz3rijiqlf76uay.ipfs.storry.tv/", | |
"https://crustwebsites.net/ipfs/Qmdu6QgAMJqiwRcGGMadPMkkdSCDv6NoQG2neTN8fYirN9/", | |
]; | |
let baseUrl = baseUrls[i % 10] + i; | |
try { | |
const response = await fetch(baseUrl, { | |
method: "GET", | |
headers: { | |
"Content-Type": "application/json", | |
Accept: "application/json", | |
}, | |
}); | |
const data = await response.json(); | |
obj = { | |
name: data.name ? data.name : "No Name", | |
description: data.description ? data.description : "No Description", | |
attributes: data.attributes ? data.attributes : "No Attributes", | |
}; | |
} catch (error) { | |
obj = { | |
name: "No Name", | |
description: "No Description", | |
attributes: "No Attributes", | |
}; | |
console.log("ERROR In Fetching.......", error); | |
} | |
return obj; | |
}; | |
// Use to fetch the metadata of NFTs | |
const main = async () => { | |
console.time("codezup"); | |
let metadataList = []; | |
// Range of tokeIds | |
for (let i = 2; i <= 5; i++) { | |
let obj = await fetchMetaData(i); | |
console.log("OBJ -: ", i, obj); | |
metadataList.push(obj); | |
} | |
// save to file | |
fs.writeFileSync("metadata10.json", JSON.stringify(metadataList)); | |
console.timeEnd("codezup"); | |
}; | |
const findRarity = async () => { | |
// Read json file | |
let json = fs.readFileSync("metadata10.json"); | |
let metadataList = JSON.parse(json); | |
const allUniqueTraits = {}; | |
for (let i = 0; i < metadataList.length; i++) { | |
for (let j = 0; j < metadataList[i].attributes.length; j++) { | |
if (!allUniqueTraits[metadataList[i].attributes[j].value]) { | |
allUniqueTraits[metadataList[i].attributes[j].value] = 1; | |
} else { | |
allUniqueTraits[metadataList[i].attributes[j].value] += 1; | |
} | |
} | |
} | |
let totalNFTs = metadataList.length; | |
let keys = Object.keys(allUniqueTraits); | |
for (let i = 0; i < keys.length; i++) { | |
let key = keys[i]; | |
let value = allUniqueTraits[key]; | |
let percentage = (value / totalNFTs) * 100; | |
allUniqueTraits[key] = percentage; | |
} | |
console.log(keys.length); | |
const NFTs = []; | |
for (let i = 0; i < metadataList.length; i++) { | |
const obj = {}; | |
obj.name = metadataList[i].name; | |
obj.traits = []; | |
for (let j = 0; j < metadataList[i].attributes.length; j++) { | |
obj.traits.push(allUniqueTraits[metadataList[i].attributes[j].value]); | |
} | |
NFTs.push(obj); | |
} | |
const finalNFTs = []; | |
for (let i = 0; i < NFTs.length; i++) { | |
let sum = 0; | |
let final = 0; | |
const obj = {}; | |
for (let j = 0; j < NFTs[i].traits.length; j++) { | |
sum = sum + NFTs[i].traits[j]; | |
final = sum / NFTs[i].traits.length; | |
} | |
obj.name = NFTs[i].name; | |
obj.traits = final; | |
finalNFTs.push(obj); | |
} | |
finalNFTs.sort((a, b) => { | |
return a.traits - b.traits; | |
}); | |
console.log(finalNFTs.slice(0, 20)); | |
}; | |
async function master() { | |
await main(); | |
await findRarity(); | |
} | |
master(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment