API getAllTokenIds return wrong data with limit and offset

When I get nft of this contract address 0x913e00d95621579e7f5cb1ab327f8256c0f74edd in bsc with limit and offset, when offset > total, it still return data

await MoralisBSC.Web3API.token.getAllTokenIds({
  address: '0x913e00d95621579e7f5cb1ab327f8256c0f74edd',
  limit: 1,
  offset: 1500,
  order: 'DESC',
  chain: '0x38'
})

the response show that total is 1240 but this query still return value

{
  total: 1240,
  page: 0,
  page_size: 1,
  cursor: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ3aGVyZSI6eyJ0b2tlbl9hZGRyZXNzIjoiMHg5MTNlMDBkOTU2MjE1NzllN2Y1Y2IxYWIzMjdmODI1NmMwZjc0ZWRkIn0sImxpbWl0IjoxLCJvZmZzZXQiOjEsIm9yZGVyIjpbWyJibG9ja19udW1iZXJfbWludGVkIiwiREVTQyJdLFsidG9rZW5faGFzaCIsIkRFU0MiXV0sImdyb3VwIjpbInRva2VuX2hhc2giLCJ0b2tlbl9hZGRyZXNzIiwidG9rZW5faWQiLCJibG9ja19udW1iZXJfbWludGVkIl0sInBhZ2UiOjEsImtleSI6ImZmZTg0NjE2MmY2M2FiODI4MDE2NTNmMDVlODQwMGVjIiwiaWF0IjoxNjUwNDM4NTY0fQ.A173xHdpxbDo6_TyyuNFixHU1BTz_iXDm5_zrGp7Icg',
  result: [
    {
      token_hash: 'ffe846162f63ab82801653f05e8400ec',
      token_address: '0x913e00d95621579e7f5cb1ab327f8256c0f74edd',
      token_id: '943',
      block_number_minted: '15528636',
      amount: '1',
      contract_type: 'ERC721',
      name: 'CryptoGuns Squad Members',
      symbol: 'SquadMembers',
      token_uri: 'https://www.cryptoguns.io/json/pump',
      metadata: '{"name":"Pump","image":"https://www.cryptoguns.io/characters/pump.jpg","attributes":[{"trait_type":"Class","value":"Heavy"},{"trait_type":"Health","value":"1200"},{"trait_type":"Mana","value":"150"},{"trait_type":"Attack","value":"110"},{"trait_type":"Special","value":"400"}]}',
      synced_at: '2022-04-16T14:46:27.311Z'
    }
  ]
}

What I expect and what Moralis work before is when the data exceed the total, it will not return some random data like that
The same with when I got limit 500, offset 1000, it still return 500 records when it should only return 240

Hi you should look to use cursor instead.

all of our previous code is using limit and offset, I can’t easily just change this to cursor

You say this worked before with offset? It’s possible you won’t be able to use it anymore and you’ll have to use cursor. If you post fetching code snippets that use offset we can start on working on it.

This work was normal before. So I don’t know why Moralis changed that, BTW, I still can get data with offset and limit with server call (as I mentioned above). The problem is the data is wrong when it exceed the total
This is the call with offset that I use before

await MoralisBSC.Web3API.token.getAllTokenIds({
  address: '0x913e00d95621579e7f5cb1ab327f8256c0f74edd',
  limit: 1,
  offset: 1500,
  order: 'DESC',
  chain: '0x38'
})

for that request I get this total: total: 1240, meaning that it will not return anything for offset 1500 as it is bigger then 1240?

Yes, it’s what I expect. And when offset = 1000, limit = 500, it should only return 240 records, not 500 records like current

something doesn’t seem to work as expected

1 Like

Hope you guys will fix it soon !!

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

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

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()

It affects quite a lot of our function that is running on product right now so it will take a lot of our effort to change all of this and test again. And today as I check, when I call Moralis server call with offset > 500, it throws the error “offset must be less than 500”
If that can fix in this week, it will mean a lot to us

Hi,

We plan on removing the support for offset in the future.
You should migrate the code towards using the cursor.
We will be introducing an easier way to paginate over pages in the future too.

Sincerely,

Crypto Kid | Technical Support