Get ordered list of nft count

Hello everyone, I should prepare a cloud function to return a “ranking” of my users based on the NFTs they have for a specific contract. I have seen that through queries I can return ordered lists, but I have no idea how to keep track in my database of the NFTs owned (only by a specific contract) by the user. Some idea?

Depending how often you want to make that rank, you can use web3api to get the list of nfts for your contract and then to do the ranking based on that.

getNFTs also accepts a contract address as parameter, there is also a function specific to a contract address

Ok but I would like to do something a little different and more functional. I would like my server to keep track within the “User” table, or any of them through a relationship with the user, of the ids of the tokens owned. Through an array, in such a way as to be able to quickly obtain the length or in game compare if the id used are actually the ones owned, I don’t know if I have explained myself.

For now, the server will track all the nfts for all the contracts, not only for a contract. And you can process nft transfers that are synced on the server.

It may be easier to use web3api. It also can depend how often the nfts are transferred or minted in that game. You can make a ranking per hour for example and say that the ranking was done at that specific hour.

Ok but there is a way with a cloud function to write on the server a data? in this way i will execute the cloud function to retrieve all the tokens and then write the result on the server.

Yes, you can write a cloud function that collects some data and then writes it in a table in the database from where it can be retrieved later.

in what part of documentation can i find the way?

This may help you:
https://docs.moralis.io/moralis-dapp/cloud-code/cloud-functions

You also have to read about databases. To find out how to save data and get data from the database.

Ok i’ve built a cloud function to write the nft list on the server, so now i can retrieve my ordered list but i have few questions. To write with cloud function I use {useMasterKey:true}, but this mean that everybody can use the function and in some ways manipulate it. So:

  1. Can I automatize the execution of my cloud function on an emitted event (Transfer)?
  2. Can I make the cloud function only to be executed by this automatize from server?

you could check in the cloud function based on the request by who is called, and allow it to be called only by a specific user

you can add it as a job, or call it in a trigger for that Transfer event, in afterSave for example