getNFTsForContract Offset Not Working

API Response ( PFA ) :

API Request : const fetchNFTsForContract = async () => {
const options = {
chain: “bsc testnet”,
address: “",
token_address: "
”,
/limit: 1,
offset: 1,
/
};

Above Request Hide the limit and offset options. So it will return whole records ( now only have 2 records ) .

But i want only one record ( latest one ) . In this case i used offset and limit.

Only limit option are working offset option are not working. Kindly tell what i am wrong.

I think that is is a problem on our side, I didn’t get the expected output for this query (this is from another forum thread):

x = await Moralis.Web3API.token.getAllTokenIds({
  address: '0x913e00d95621579e7f5cb1ab327f8256c0f74edd',
  limit: 500,
  offset: 2000,
  order: 'DESC',
  chain: '0x38'
})

can you paste the query that you use and that doesn’t return the expected data?

Our Query - getNFTsForContract.

const fetchNFTsForContract = async () => {
const options = {
chain: “bsc testnet”,
address: “xxxx”,
token_address: “xxxx”,
limit: 1,
offset: 1,

  };

  const polygonNFTs = await Web3Api.account.getNFTsForContract(options);

can you give a more exact example with an address and a token address?

const fetchNFTsForContract = async () => {
const options = {
chain: “bsc testnet”,
address: “0x66CAD4cB71902b77615Cb44C6e1526341acf0155”,
token_address: “0x7F3C0A4Aa2ED97E17a5031b199ac541648fF267f”,
limit: 1,
offset: 1,
};

const polygonNFTs = await Web3Api.account.getNFTsForContract(options);

ok, it looks like it doesn’t advance to page 1 in this case

in case that you run that server side, in node js, you could make REST api requests and use the cursor, until we fix it

Okay. Please fix and let us know ASAP. We are looking on browser side ( REACT ).

Can you please share node js example code ? we try to implement.

here you have a python example on how cursor can be used:
https://docs.moralis.io/misc/rate-limit#example-of-how-to-use-cursor-python

I have written this example (https://github.com/menezesphill/moralis-snapshot) showing how to use cursors to fetch data from a collection by looping through all result pages.

update, found out how to use cursor with Moralis SDK, this is an example for node js:

const  Moralis = require('moralis/node')
const serverUrl = 'https://sadfadfa:2053/server'
const appId = 'fasdfasf'
const contractAddress = '4324234234'
async function getAllOwners() {
  await Moralis.start({serverUrl: serverUrl, appId: appId})
  let cursor = null
  let owners = {}
  do {
    const response = await Moralis.Web3API.token.getNFTOwners({ address: contractAddress, chain: 'eth', limit: 500, cursor: cursor  })
    console.log(`Got page ${response.page} of ${Math.ceil(response.total / response.page_size)}, ${response.total} total`)
    for (const owner of response.result) {
      owners[owner.owner_of] = {amount: owner.amount, owner: owner.owner_of, tokenId: owner.token_id, tokenAddress: owner.token_address}
    }
    cursor = response.cursor
  } while (cursor != '' && cursor != null)

  console.log('owners:', owners, 'total owners:', Object.keys(owners).length)
}

getAllOwners()
1 Like

How would you use cursor here when using API endpoint?

Awesome thank you for clarifying this!

You can do something like:

const contractAddress = "0x18f36d3222324ddac1efd0a524aabfb9d77d8041"

  async function getAllOwners() {
    let cursor = ""
    let owners = {}
    do {
      const { data } = await axios(`https://deep-index.moralis.io/api/v2/nft/${contractAddress}/owners?chain=eth&cursor=${cursor}`, {
        headers: {
          'x-api-key': '' 
        }
      })

      console.log(`Got page ${data.page} of ${Math.ceil(data.total / data.page_size)}, ${data.total} total`)
      for (const owner of data.result) {
        owners[owner.owner_of] = {amount: owner.amount, owner: owner.owner_of, tokenId: owner.token_id, tokenAddress: owner.token_address}
      }
      cursor = data.cursor
    } while (cursor != '' && cursor != null)
  
    console.log('owners:', owners, 'total owners:', Object.keys(owners).length)
  }

Ahh ok it’s working if I do NFT transactions but cursor isn’t a field in the response when doing ERC20 transfers

yes, it seems to be a different problem with ERC20 transfers where cursor is not present now

the offset problem for ERC20 token transfers is fixed now, just adding this information here

Hi glad.
How did you implement pagination when thee are more than 500 NFTs.
Could you tell me about your experience in implementing pagination?

Hello, the code snippet I posted above is an example of looping past 500 results.

Otherwise you can manually iterate using cursor with pagination e.g. with buttons.

Hi @alex
Thank you so much for your information.