Hello,
We have written a cloud function that performs and aggregation on the PolygonNFTTransfers to calculate the number of NFT tokens held by a user, and return sorted by the top holders by count.
The problem is that the total number of owned NFTs that we come up with for a certain contract and owner address is different than when calling the Web3 API.
The chain: Polygon Mumbai
The contract address: 0xa5ce390be1d19cc41ecb26c3174e99ead3a3e45a
The owner address: 0x2cdfc342877b19e9385f72408de34d2c5fd06dfc
We get totalNftrees = 30 from our cloud function.
And we get 36 from the Web3 API, with this request: https://deep-index.moralis.io/api/v2/0x2cdfc342877b19e9385f72408de34d2c5fd06dfc/nft/0xa5ce390be1d19cc41ecb26c3174e99ead3a3e45a?chain=mumbai&format=decimal
Are we doing something wrong? Can it be that the PolygonNFTTransfers table is out of sync with the chain?
This is the cloud function that we use:
Moralis.Cloud.define("topNFTreeHolders", async (request) => {
const query = new Moralis.Query("PolygonNFTTransfers")
const pipeline = [
{
match: {
token_address: "0xa5ce390be1d19cc41ecb26c3174e99ead3a3e45a".toLowerCase(),
}
},
{
project: {
party: [{
address: "$from_address",
amount: -1,
sent: 1,
}, {
address: "$to_address",
amount: 1,
received: 1,
}]
}
},
{
unwind: {
path: "$party",
}
},
{
group: {
objectId: '$party.address',
totalNftrees: { $sum: "$party.amount" },
totalNftreesSent: { $sum: "$party.sent" },
totalNftreesReceived: { $sum: "$party.received" },
// tokens: { $push: "$$ROOT" },
}
},
// {
// lookup: {
// from: "_User",
// localField: "address",
// foreignField: "ethAddress",
// // foreignField: "accounts",
// as: "user"
// }
// },
{ sort: { totalNftrees: -1 } }
];
return query.aggregate(pipeline);
});