Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Offset not working #15

Open
emirhansirkeci opened this issue Apr 22, 2022 · 6 comments
Open

Offset not working #15

emirhansirkeci opened this issue Apr 22, 2022 · 6 comments

Comments

@emirhansirkeci
Copy link

image

@Deveshb15
Copy link

Same issue for me, is there a fix for this?

@emirhansirkeci
Copy link
Author

Same issue for me, is there a fix for this?

I used cursor instead of offset.

async function generateRarity() {
  const totalNum = 10000;
  let allNFTs = [];

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

  let cursor = null;
  do {
    const response = await Moralis.Web3API.token.getAllTokenIds({
      address,
      chain: "eth",
      limit: 500,
      cursor: cursor,
    });
    console.log(
      `Got page ${response.page} of ${Math.ceil(
        response.total / response.page_size
      )}, ${response.total} total`
    );
    allNFTs = allNFTs.concat(response.result);
    cursor = response.cursor;
    // await timer(1000);
    cursor = null; // disable the timer above and enable this line of code to get only 500 tokenIds;
  } while (cursor != "" && cursor != null);

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

@Deveshb15
Copy link

Same issue for me, is there a fix for this?

I used cursor instead of offset.

async function generateRarity() {
  const totalNum = 10000;
  let allNFTs = [];

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

  let cursor = null;
  do {
    const response = await Moralis.Web3API.token.getAllTokenIds({
      address,
      chain: "eth",
      limit: 500,
      cursor: cursor,
    });
    console.log(
      `Got page ${response.page} of ${Math.ceil(
        response.total / response.page_size
      )}, ${response.total} total`
    );
    allNFTs = allNFTs.concat(response.result);
    cursor = response.cursor;
    // await timer(1000);
    cursor = null; // disable the timer above and enable this line of code to get only 500 tokenIds;
  } while (cursor != "" && cursor != null);

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

Even I tried this but what if the collection has more than 10000 nfts?

@emirhansirkeci
Copy link
Author

I set totalNum to calculate rarity scores. Cursor usage is giving us the full metadata of any collection. I thing setting the variable like this after loop is completed may be way better than my codes. let totalNum = allNFTs.length;

And like that you don't have to know how many NFT's are available in collection.

@thomasansems
Copy link

#16 Created a pull request regarding the updated cursor of Moralis.

@Arkay92
Copy link

Arkay92 commented Jul 3, 2022

This works as a fix for cursor

` let allNFTs = [];

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

let cursor = null;
do {
const NFTs = await Moralis.Web3API.token.getAllTokenIds({
address: collectionAddress,
chain: "eth",
limit: 100,
cursor: cursor,
});

  let counter = 0;

  for (const nft of NFTs.result) {
        let metaData = JSON.parse(nft.metadata);
        if(metaData != null) {
           allNFTs.push(nft);
           console.log(metaData);
        }
        counter+=100;
  }

  if(counter % 200 == 0) {
      await timer(2000);
  }

  cursor = NFTs.cursor;

} while (cursor != "" && cursor != null);

let metadata = allNFTs.map((e) => {
if(JSON.parse(e.metadata)) {
return JSON.parse(e.metadata).attributes
}
});`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants