How to Get the Transaction History of an NFT

How to Get the Transaction History of an NFT

Whether you’re developing an NFT price estimator, an analytics platform, or a marketplace similar to OpenSea using NFT development services, you’ll likely want to track down an NFT’s ownership history and show it to your clients.

An NFT’s ownership history can be found in a few different ways. Parsing every transaction made on the blockchain since the genesis block, searching for those connected to the NFT, and understandably presenting the results for humans is one method. This approach usually requires a significant investment of time and engineering resources.

Prerequisites

Install npm and Node.js(> 14) on your local computer.

You can find steps to install node and npm if you still need to install them. Use the following command in your terminal to find out your Node version:

node -v

Create a Node Project

The getAssetTransfers function will be used to obtain the transfer history of a specific NFT. This function accepts several necessary and optional arguments. We will make use of the following arguments in our example:

from block: The block from which the transfer history should be traced. This will be set to 0x0, or the genesis block.

contract addresses: A list of the contract addresses for which we wish to look up the history of transfers. This will just be the primary BAYC contract in our situation.

category: A list of transaction types that we wish to monitor. We simply want to monitor erc721 and token transactions in our situation.

excludeZeroValue: Used to filter out transfers with zero value. Since we are open to any transfer, we shall set this to false.

const { Alchemy, Network, fromHex } = require('alchemy-sdk');
const config = {
  apiKey: 'alchemy api key',
  network: Network.ETH_MAINNET,
};
const alchemy = new Alchemy(config);const main = async () => {
  const address = ['0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D'];

  const response = await alchemy.core.getAssetTransfers({
    fromBlock: '0x0',
    contractAddresses: address,
    category: ['erc721'],
    excludeZeroValue: false,
  });  const nftId = 1;
  let txns = response.transfers.filter(
    (txn) => fromHex(txn.erc721TokenId) === nftId
  );
  console.log(txns);
};const getNftTxn = async () => {
  try {
    await main();
  } catch (error) {
    console.log(error);

  }
};getNftTxn();

Conclusion

Tracking an NFT’s transaction history is essential for developing price estimators, analytics platforms, or marketplaces. Using Alchemy’s API and the getAssetTransfers function, you can efficiently retrieve an NFT’s ownership history without parsing all blockchain transactions since the genesis block.

In this guide, we showed how to set up a Node.js project and use Alchemy’s SDK to fetch NFT transfer data. By specifying parameters like the starting block, contract addresses, and transaction categories, you can obtain precise transaction histories for your NFTs.

This approach streamlines the process, allowing you to provide valuable insights to your users while focusing on building robust NFT platforms. In case you are looking for NFT development services, take a look at our talent pool of NFT developers who are skilled in addressing diverse industry demands.