-
-
Notifications
You must be signed in to change notification settings - Fork 716
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d680bec
commit d44d272
Showing
6 changed files
with
140 additions
and
8 deletions.
There are no files selected for viewing
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
const basePath = process.cwd(); | ||
const fs = require("fs"); | ||
|
||
const getRarity = () => { | ||
// read json data | ||
const rawdata = fs.readFileSync(`${basePath}/build/json/_metadata.json`); | ||
const nfts = JSON.parse(rawdata); | ||
|
||
processRarity(nfts) | ||
} | ||
|
||
function processRarity(nfts) { | ||
const rarity = {} | ||
|
||
// loop through all nfts | ||
for(const nft of nfts) { | ||
// check if attributes exist | ||
if(nft?.attributes?.length > 0) { | ||
// loop through all attributes | ||
for(attribute of nft.attributes) { | ||
// add trait type to rarity object if it doesn't exist | ||
if(!rarity[attribute.trait_type]) { | ||
rarity[attribute.trait_type] = {} | ||
} | ||
// add attribute value to rarity object if it doesn't exist and set count to 0 | ||
if(!rarity[attribute.trait_type][attribute.value]) { | ||
rarity[attribute.trait_type][attribute.value] = { | ||
count: 0 | ||
} | ||
} | ||
// increment count of trait type | ||
rarity[attribute.trait_type][attribute.value].count++ | ||
// add rarity score to rarity object for each trait type | ||
rarity[attribute.trait_type][attribute.value].rarityScore = (1 / (rarity[attribute.trait_type][attribute.value].count / nfts.length)).toFixed(2) | ||
} | ||
} | ||
} | ||
|
||
// create a total rarity score for each nft by adding up all the rarity scores for each trait type | ||
nfts.map(nft => { | ||
if(nft?.attributes?.length > 0) { | ||
let totalScore = 0; | ||
for(attribute of nft.attributes) { | ||
attribute.rarity_score = rarity[attribute.trait_type][attribute.value].rarityScore | ||
totalScore += parseFloat(attribute.rarity_score) | ||
} | ||
nft.total_rarity_score = +parseFloat(totalScore).toFixed(2) | ||
} | ||
}) | ||
|
||
// sort nfts by total rarity score | ||
nfts.sort((a, b) => b.total_rarity_score - a.total_rarity_score) | ||
|
||
// add rank to nfts | ||
nfts.map((nft, index) => { | ||
nft.rank = index + 1 | ||
}) | ||
|
||
// sort nfts by edition again | ||
nfts.sort((a, b) => a.custom_fields.edition - b.custom_fields.edition) | ||
|
||
fs.writeFileSync(`${basePath}/build/json/_metadata_with_rarity.json`, JSON.stringify(nfts, null, 2)); | ||
} | ||
|
||
getRarity(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
const basePath = process.cwd(); | ||
const fs = require("fs"); | ||
|
||
// initialize readline to prompt user for input | ||
const readline = require("readline"); | ||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
}); | ||
const prompt = (query) => new Promise((resolve) => rl.question(query, resolve)); | ||
|
||
(async () => { | ||
try { | ||
// read json data | ||
const rawdata = fs.readFileSync( | ||
`${basePath}/build/json/_metadata_with_rarity.json` | ||
); | ||
const nfts = JSON.parse(rawdata); | ||
|
||
// prompt user to choose how to list nfts | ||
// 1. get top ## nfts | ||
// 2. get a specific nft by edition | ||
const choice = await prompt( | ||
"Enter 1 to get top ## NFTs by rarity or 2 to get a specific NFTs rarity: " | ||
); | ||
|
||
if (choice === "1") { | ||
const top = await prompt("Enter the number of NFTs you want to get: "); | ||
const sortedNfts = nfts.sort( | ||
(a, b) => b.total_rarity_score - a.total_rarity_score | ||
); | ||
const topNfts = sortedNfts.slice(0, top); | ||
console.log( | ||
topNfts.map(({ rank, total_rarity_score, name }) => { | ||
return { | ||
name, | ||
rank, | ||
total_rarity_score, | ||
}; | ||
}) | ||
); | ||
} else if (choice === "2") { | ||
const nftEdition = await prompt("Enter the NFT Edition: "); | ||
const nft = nfts.find((nft) => nft.custom_fields.edition === +nftEdition); | ||
console.log({ | ||
name: nft.name, | ||
rank: nft.rank, | ||
total_rarity_score: nft.total_rarity_score, | ||
}); | ||
} else { | ||
console.log("Invalid choice. Enter either 1 or 2."); | ||
} | ||
|
||
// close readline | ||
rl.close(); | ||
} catch (e) { | ||
console.error("unable to prompt", e); | ||
} | ||
})(); |