Skip to main content
All CollectionsAPIWeb3 API
How to migrate to the stats endpoints for total value?
How to migrate to the stats endpoints for total value?

How to migrate to the stats endpoints for total value

John Pilla avatar
Written by John Pilla
Updated over a year ago

As of 11th September 2023, the total value returned on the endpoints will no longer be returned. This means that we deprecated the disable_total parameter and total field value. You can read about this update in the changelog.

New Endpoints:
We have introduced five new endpoints that will replace the deprecated total value:

  • Get Wallet Stats - /wallets/{address}/stats

  • Get NFT Collection Stats - /nft/{address}/stats

  • Get NFT Token Id Stats - /nft/{address}/{token_id}/stats

  • Get ERC20 Token Stats - /erc20/{address}/stats

  • Get Block Stats - /block/{block_number_or_hash}/stats

Migration Guide: The article provides a guide on how to migrate existing code to use the new stats endpoints, specifically focusing on the NFT collections stats endpoint.

  1. Define a Function:

    Define a new function to use the NFT collections stats and return the required total value.

    The response from the NFT Collection stats looks like below. For this code example, we only use the total value of NFT owners.

{ 
"total_tokens": "10007",
"owners": { "current": "10007" }, // πŸ‘ˆ required total owners value
"transfers": { "total": "24582" },
"trades": { "total": "1" }
}

The below function code will return the NFT owner's total

// app.js
const getTotalOwners = async (address, chain) => {
const url = `https://deep-index.moralis.io/api/v2.2/nft/${address}/stats?chain=${chain}`;
const response = await fetch(url, {
method: "GET",
headers: {
accept: "application/json",
"X-API-Key": "Moralis_API_KEY",
},
});

const data = await response.json();
return data.owners.current; // πŸ‘ˆ returns the total owners value
};

Integrate with Existing Code:

Now you can use the getTotalOwners function in your existing code to get the total owners and calculate the total pages.

// app.js

const Moralis = require("moralis").default;

const init = async () => {
const address = "0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB";
const chain = "0x1";
await Moralis.start({
apiKey: "Moralis_API_KEY",
});

// πŸ‘‡ using the getTotalOwners function to get the total owners value and
// calculating the total pages
const totalOwners = await getTotalOwners(address, chain);
const pageSize = 100;
const totalPages = Math.ceil(totalOwners / pageSize);
let cursor = null;
let owners = {};

for (let currentPage = 1; currentPage <= totalPages; currentPage++) { const response = await Moralis.EvmApi.nft.getNFTOwners({
address,
chain,
limit: pageSize,
cursor: cursor,
disableTotal: false,
});

console.log( `Got page ${currentPage} of ${totalPages}, ${totalOwners} total` );

for (const NFT of response.result) {
if (NFT.ownerOf.checksum in owners) {
owners[NFT.ownerOf.checksum].push({
amount: NFT.amount,
owner: NFT.ownerOf,
tokenId: NFT.tokenId,
tokenAddress: NFT.tokenAddress,
});
} else {
owners[NFT.ownerOf.checksum] = [
{
amount: NFT.amount,
owner: NFT.ownerOf,
tokenId: NFT.tokenId,
tokenAddress: NFT.tokenAddress,
},
];
}
}
cursor = response.pagination.cursor;
}

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

init();

By following the above steps, you can seamlessly integrate the new stats endpoints into your existing code. Happy coding! πŸ˜„

Did this answer your question?