How to get the last sale price of an NFT

This tutorial will walk you through the steps to get the last sale price of an NFT.

Don’t have an API key?

Start using this API in your app today.

NFTs can be listed and sold on marketplaces such as OpenSea, LookRare etc. For an NFT listed on a marketplace, when it is sold, it has a sellerFee which is paid to the wallet address of the Seller, protocolFee which is sent to the NFT Marketplace such as OpenSea, and royaltyFee, which is paid to the original Address of the NFT collection whenever the NFT is re-sold. The royalty fee applies if the NFT was originally sold as part of a collection where a royalty fee is taken by the collection owner. A royalty fee thus, does not apply to every NFT collection. These three fees when summed up give us the price at which the NFT was sold.

📘

SDK Method

The tutorial uses the getNftSales API.

Why is this important?

One use case of fetching the last sale price of an NFT is so that developers can easily build dashboards.

Data Analysts can infer the total cost of sales that have been made by a seller, the total number of sales made in a collection or even the royalty earned by the NFT creator.

In this example, we will use node.

Initialize the Project

  1. From your command line, create a new project directory and cd into it:
mkdir get-NFT-last-sale-price
cd get-NFT-last-sale-price
  1. Open the project in your favourite IDE or Text editor and initialize an npm project in the terminal by running the command:
npm init -y
  1. Install the Axios library
npm install axios

Code

  1. Create a file named index.js

This is a small script that demos the use of the getNftSales API. See SDK version.

There are options that you can parse as arguments to the getNftSales API, you can see the list in the docs. In this example script, we request for sales information on a BoredApeYachtClub tokenID 39 by parsing the contract address and tokenID as arguments.

Note that, as part of the arguments given in the params for the call, you can parse a Seller Address or a Buyer Address. You cannot parse a tokenID without specifying the token contract.

The options fromBlock and toBlock are used to specially request the sales information at a particular block height.

const axios = require("axios");

const options = {
  method: "GET",
  url: "https://eth-mainnet.g.alchemy.com/nft/v2/your-api-key/getNFTSales",
  params: {
    fromBlock: "0",
    toBlock: "latest",
    order: "asc",
    contractAddress: "0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d",
    tokenId: "39",
  },
  headers: { accept: "application/json" },
};

axios
  .request(options)
  .then(function (response) {
    console.log(response.data.nftSales)
  })
  .catch(function (error) {
    console.error(error);
  });

Run the Code

  1. Run the script using node
node index.js

Response

You will see the following response logged to the console:

[
  {
    marketplace: 'looksrare',
    marketplaceAddress: '0x59728544b08ab483533076417fbbb2fd0b17ce3a',
    contractAddress: '0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d',
    tokenId: '39',
    quantity: '1',
    buyerAddress: '0xc9ff792d842ce164478d810437834725e4aa330e',
    sellerAddress: '0xa0b6a11a2a10c8bec8c3a123fbc9c185a58d1ab5',
    taker: 'BUYER',
    sellerFee: {
      amount: '70192500000000000000',
      tokenAddress: '0x',
      symbol: 'WETH',
      decimals: 18
    },
    protocolFee: { amount: '1470000000000000000', symbol: 'WETH', decimals: 18 },
    royaltyFee: { amount: '1837500000000000000', symbol: 'WETH', decimals: 18 },
    blockNumber: 13985060,
    logIndex: 9,
    bundleIndex: 0,
    transactionHash: '0xa1844c796d425879aa9347abd7db00014ef8501a0d67eda7bd5ee9914a4aae75'
  }
]

The critical information logged to the console are the tokenID, which we want to find the last price at which it was sold, the 3 different prices which we will learn about in the next paragraph, the collection contract address, the transaction hash. You can read more about transactions in our docs.

As part of the responses logged to the console, we have the 3 different fees listed at the beginning of this article as the fees returned when a NFT Sale is made. They are:

  • sellerFee which is paid to the EOA of the Seller.

  • marketplaceFee which is sent to the NFT Marketplace such as OpenSea. Also called protocolFee when you use the getSales API.

  • royaltyFee, which is paid to the original Address of the NFT collection, when applicable.

Each fee in the response object has a it’s name listed as the key, and the returned values are: amount, symbol and decimals.

...
"sellerFee": {
        "amount": "70192500000000000000",
        "tokenAddress": "0x",
        "symbol": "ETH",
        "decimals": 18
      },
"protocolFee": {
        "amount": "1470000000000000000",
        "symbol": "ETH",
        "decimals": 18
      },
"royaltyFee": {
        "amount": "1837500000000000000",
        "symbol": "ETH",
        "decimals": 18
      },
...

The amount is the value paid to the seller, in the case of the seller response. As well as having the amount paid to the marketplace (such as OpenSea), and the last amount is the royalty fee.

The symbol is ETH, which is Ether, held in the marketplace contract.

The decimals is the number of decimal places by which we calculate the amount.
i.e `amount * 10^-18

📘

Calculating the last sale price of the NFT

To get the final value at which the NFT was sold, add the 3 different fees: sellerFee, protocolFee and royaltyFee and calculate the number of decimal places:

(70192500000000000000 + 1470000000000000000 + 160000000000000000) * 10^-18

Calculate the last sale price of the NFT

To get the final value at which the NFT was sold, add the 3 different fees: sellerFee, protocolFee and royaltyFee and calculate the number of decimal places:

(70192500000000000000 + 1470000000000000000 + 1837500000000000000) * 10^-18

This will return: 73.5 ETH, as the last Sale price of this NFT.

The process above can be achieved via the script by simply mapping over the responses and calculating the sum. The response object amount is a string and can be converted to an integer using parseInt. Carrying out a sum operation on the response object without first converting it to an integer will result to a string concatenation.

//add a function before the axios request to calculate the NFT sales price.
const nftSalePrice = (x, y, z) => {
  const price = (x + y + z) * 10 ** -18;
  console.log(price);
};


//map over the response in the axios request to return the NFT sales price
response.data.nftSales.map((value) => {
      nftSalePrice(
        parseInt(value.sellerFee.amount),
        parseInt(value.protocolFee.amount),
        parseInt(value.royaltyFee.amount)
      );
    });

That is how to calculate the last sale price of an NFT using the getNftSales using the Alchemy API.

You can verify the last sale information for this particular tokenID 39.

Check out the getNftSales API endpoint to find code for implementation in other programming languages.

If you enjoyed this tutorial, give us a tweet @AlchemyPlatform.

Don't forget to join our Discord server to meet other blockchain devs, builders, and entrepreneurs.