Rarity Ranking Generator doubt

i still not seeing the metadata

You may have to force resync for token uri now is it was changed

still not working ;/

x = await Moralis.Web3API.token.getTokenIdMetadata({address:"0x7192E7CD2B1FdC9AF318276b1b38e31253d06b2A", token_id: "12", chain: "mumbai"})

=>

amount: "1"
block_number: "24192074"
block_number_minted: "24192074"
contract_type: "ERC721"
frozen: 0
is_valid: 1
metadata: "{\n  \"name\": \"WenLambo's Legendary NFT Collection #12\",\n  \"description\": \"This collection can be used as a digital asset, or to achieve the highest speeds of SmartBCH\",\n  \"image\": \"https://wenlambonft.mypinata.cloud/ipfs/QmcQjVb3yY3bx8tVMd8FfWs1VbRLHeo76C3AJKT2eaALwN/12.png\",\n  \"dna\": \"d277d3f00f93d827a3be79539f96fbd988ebf012\",\n  \"edition\": 12,\n  \"date\": 1642523624391,\n  \"attributes\": [\n    {\n      \"trait_type\": \"background\",\n      \"value\": \"rose\"\n    },\n    {\n      \"trait_type\": \"lambo_colour\",\n      \"value\": \"orange\"\n    },\n    {\n      \"trait_type\": \"lights\",\n      \"value\": \"red_light\"\n    },\n    {\n      \"trait_type\": \"hats\",\n      \"value\": \"Sasuke\"\n    },\n    {\n      \"trait_type\": \"accesories\",\n      \"value\": \"mustard\"\n    },\n    {\n      \"trait_type\": \"lights\",\n      \"value\": \"red_light\"\n    },\n    {\n      \"trait_type\": \"letters\",\n      \"value\": \"ban\"\n    }\n  ],\n  \"compiler\": \"HashLips Art Engine\"\n}"
name: "WenLambo Legendary NFT Collection"
owner_of: "0x4adb16ce845beeea4a05f764f708eec13f4bd61b"
symbol: "WLNFT"
synced_at: "2022-01-24T19:30:37.801Z"
syncing: 2
token_address: "0x7192e7cd2b1fdc9af318276b1b38e31253d06b2a"
token_id: "12"
token_uri: "https://wenlambonft.mypinata.cloud/ipfs/Qma1rEwRL4SShPXHpjCf8f8UfuC2188Jc6DqXyuJim4v2H/12.json"

now, it works, but what i need to make it work is the getalltokensid function to then store the nft collection in moralis. Is there any way to do it?

I didn’t understand exactly what you need to do

I need to make the rarity ranking page but in every step i have a problem, i don’t know if it’s my contract the problem

I will try to make the page with the bored ape collection to see if i have any problem

hey! how are you? i solved all the issues, and i ended the scripts (main.js) now, when i do node main.js the console returns me:

2500
500
ParseError: schema class name does not revalidate
at handleError (C:\Users\sebas\Desktop\tetas\node_modules\moralis\lib\node\RESTController.js:423:17)
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
code: 107

this usually means that there is a space in a name

Oh yes, it was the problem. And now, I removed the comment from the for loop at the beginning and it keeps counting up to 500 in the console, why is that?

what are those 500 iterations doing?

storing the nfts in moralis db

and what is the problem with that?

my collection is 2500 and it only stores 500

that may be because every page has only 500 items, you will have to use pagination with offset to get the other pages

you mean something like
const options = { address: “0xd…07”, chain: “bsc” };
const NFTs = await Moralis.Web3API.token.getAllTokenIds(…, …, offset: 2500)

?

something like that, but try with offset 0, than 500, than 1000, than 1500 and so on

ok, will try it, thanks

Here is my code, i don’t know what is wrong, or if it’s my contract. I tried with boredapes colection and it worked, but with mine the storing stops at 500.

const Moralis = require("moralis/node");

const serverUrl = "https://had5eajiprnq.usemoralis.com:2053/server"; //Moralis Server Url here
const appId = "Jt8UtsSLdNbmWXVSN2xMyh6EIx5Z0UJCnW2TIsCb"; //Moralis Server App ID here
Moralis.start({ serverUrl, appId });

const resolveLink = (url) => {
  if (!url || !url.includes("ipfs://")) return url;
  return url.replace("ipfs://", "https://gateway.ipfs.io/ipfs/");
};

const collectionAddress = "0xdd4250f5C9280edfeBEaF8a6CcF834Db74bFcEcb"; //Collection Address Here
const collectionName = "WenLamboLegendaryNFTCollection"; //CollectioonName Here

async function generateRarity() {
  const NFTs = await Moralis.Web3API.token.getAllTokenIds({
    chain: "polygon testnet",
    address: collectionAddress,  
  });

  const totalNum = NFTs.total;
  const pageSize = NFTs.page_size;
  console.log(totalNum);
  console.log(pageSize);
  let allNFTs = NFTs.result;

  const timer = (ms) => new Promise((res) => setTimeout(res, ms));

  for (let i = pageSize; i < totalNum; i = i + pageSize) {
    const NFTs = await Moralis.Web3API.token.getAllTokenIds({
    address: collectionAddress,
    offset: i,
   });
  allNFTs = allNFTs.concat(NFTs.result);
  await timer(6000);
  }

  let metadata = allNFTs.map((e) => JSON.parse(e.metadata).attributes);

  let tally = { TraitCount: {} };

  for (let j = 0; j < metadata.length; j++) {
    let nftTraits = metadata[j].map((e) => e.trait_type);
    let nftValues = metadata[j].map((e) => e.value);

    let numOfTraits = nftTraits.length;

    if (tally.TraitCount[numOfTraits]) {
      tally.TraitCount[numOfTraits]++;
    } else {
      tally.TraitCount[numOfTraits] = 1;
    }

    for (let i = 0; i < nftTraits.length; i++) {
      let current = nftTraits[i];
      if (tally[current]) {
        tally[current].occurences++;
      } else {
        tally[current] = { occurences: 1 };
      }

      let currentValue = nftValues[i];
      if (tally[current][currentValue]) {
        tally[current][currentValue]++;
      } else {
        tally[current][currentValue] = 1;
      }
    }
  }

  const collectionAttributes = Object.keys(tally);
  let nftArr = [];
  for (let j = 0; j < metadata.length; j++) {
    let current = metadata[j];
    let totalRarity = 0;
    for (let i = 0; i < current.length; i++) {
      let rarityScore =
        1 / (tally[current[i].trait_type][current[i].value] / totalNum);
      current[i].rarityScore = rarityScore;
      totalRarity += rarityScore;
    }

    let rarityScoreNumTraits =
      8 * (1 / (tally.TraitCount[Object.keys(current).length] / totalNum));
    current.push({
      trait_type: "TraitCount",
      value: Object.keys(current).length,
      rarityScore: rarityScoreNumTraits,
    });
    totalRarity += rarityScoreNumTraits;

    if (current.length < collectionAttributes.length) {
      let nftAttributes = current.map((e) => e.trait_type);
      let absent = collectionAttributes.filter(
        (e) => !nftAttributes.includes(e)
      );

      absent.forEach((type) => {
        let rarityScoreNull =
          1 / ((totalNum - tally[type].occurences) / totalNum);
        current.push({
          trait_type: type,
          value: null,
          rarityScore: rarityScoreNull,
        });
        totalRarity += rarityScoreNull;
      });
    }

    if (allNFTs[j].metadata) {
      allNFTs[j].metadata = JSON.parse(allNFTs[j].metadata);
      allNFTs[j].image = resolveLink(allNFTs[j].metadata.image);
    } else if (allNFTs[j].token_uri) {
      try {
        await fetch(allNFTs[j].token_uri)
          .then((response) => response.json())
          .then((data) => {
            allNFTs[j].image = resolveLink(data.image);
          });
      } catch (error) {
        console.log(error);
      }
    }

    nftArr.push({
      Attributes: current,
      Rarity: totalRarity,
      token_id: allNFTs[j].token_id,
      image: allNFTs[j].image,
    });
  }

  nftArr.sort((a, b) => b.Rarity - a.Rarity);

  for (let i = 0; i < nftArr.length; i++) {
    nftArr[i].Rank = i + 1;
    const newClass = Moralis.Object.extend(collectionName);
    const newObject = new newClass();

    newObject.set("attributes", nftArr[i].Attributes);
    newObject.set("rarity", nftArr[i].Rarity);
    newObject.set("tokenId", nftArr[i].token_id);
    newObject.set("rank", nftArr[i].Rank);
    newObject.set("image", nftArr[i].image);

    await newObject.save();
    console.log(i);
  }
  
  return true
}

generateRarity()
.then( ( result ) => { console.log( result ) } )
.catch( ( error ) => { console.log( error ) } )