Get Active Listings of a Collection

Get active listings for a collection on Tensor using GraphQL.

To query active listings of a collection, we will need to check all listing accounts and see if the listed NFT/cNFT belongs to a pre-defined collection mint list. First thing is to have a mint list of the collection you are interested in. For the sake of this example we will use a DAS call to fetch it.

You can also use our MintList tool to get a collection's mint list.

For Tensor cNFT, we will be checking for assetId field. For Tensor Swap program we will be using nftMint field.

The key is to use GraphQL _in filter which checks if the NFT address is present in the mint list.

Active listings of a collection from Tensor cNFT

import { gql, GraphQLClient } from "graphql-request";
import { Network, ShyftSdk } from '@shyft-to/js';

const endpoint = `https://programs.shyft.to/v0/graphql/?api_key=YOUR-KEY`;

const graphQLClient = new GraphQLClient(endpoint, {
  method: `POST`,
  jsonSerializer: {
    parse: JSON.parse,
    stringify: JSON.stringify,
  },
});

const shyft = new ShyftSdk({ apiKey: YOUR-KEY, network: Network.Mainnet }); //Initialize Shyft SDK to use DAS


async function getAllListFromCollection(collectionAddr:string) {

    const allNftMintsforCollection = await getAllMintsofColl(collectionAddr); //getting all NFT mints from a collection
    
    const query = gql`
      query MyQuery {
        TENSOR_CNFT_listState(where: {assetId: {_in: ${JSON.stringify(allNftMintsforCollection)}}}) {
          rentPayer
          pubkey
          owner
          assetId
          amount
        }
      }
    `;

  const response = await graphQLClient.request(query);
  console.dir(response,{depth: null});
  
}

async function getAllMintsofColl(collectionAddr:string) { //get all NFTs from a specific collection
    let page = 1;
    const assets = [];

    while (page > 0) {
      const assetsByGroup = await shyft.rpc.getAssetsByGroup({
        groupKey: 'collection',
        groupValue: collectionAddr,
        page,
        limit: 1000,
      });

      assets.push(...assetsByGroup.items);
      page = assetsByGroup.total !== 1000 ? -1 : page + 1;
    }

    console.log('Total NFT ', assets.length);
    const nftAddresses = assets.map((nft) => nft.id);
    return nftAddresses;
}

getAllListFromCollection('8Hbfu77utvPYTU3ngHis81nnomFW9tQj2yC54EH9B9Q9')
//replace with your NFT collection address

Active Listings of a collection from Tensor Swap (normal NFTs)

import { gql, GraphQLClient } from "graphql-request";
import { Network, ShyftSdk } from '@shyft-to/js';

const endpoint = `https://programs.shyft.to/v0/graphql/?api_key=YOUR-KEY`;

const graphQLClient = new GraphQLClient(endpoint, {
  method: `POST`,
  jsonSerializer: {
    parse: JSON.parse,
    stringify: JSON.stringify,
  },
});

const shyft = new ShyftSdk({ apiKey: YOUR-KEY, network: Network.Mainnet }); //Initialize Shyft SDK to use DAS

async function getAllListFromCollection(collectionAddr:string) {

    const allNftMintsforCollection = await getAllMintsofColl(collectionAddr); //getting all NFT mints from a collection
    
    const query = gql`
      query MyQuery {
        Tensor_SingleListing(where: {nftMint: {_in: ${JSON.stringify(allNftMintsforCollection)}}}) {
          nftMint
          owner
          price
          pubkey
        }
      }
    `;

  const response = await graphQLClient.request(query);
  console.dir(response,{depth: null});
  
}

async function getAllMintsofColl(collectionAddr:string) { //get all NFTs from a specific collection
    let page = 1;
    const assets = [];

    while (page > 0) {
      const assetsByGroup = await shyft.rpc.getAssetsByGroup({
        groupKey: 'collection',
        groupValue: collectionAddr,
        page,
        limit: 1000,
      });

      assets.push(...assetsByGroup.items);
      page = assetsByGroup.total !== 1000 ? -1 : page + 1;
    }

    console.log('Total NFT ', assets.length);
    const nftAddresses = assets.map((nft) => nft.id);
    return nftAddresses;
}

//MadLads NFT collection
getAllListFromCollection('J1S9H3QjnRtBbbuD4HjPV6RpRhwuk4zKbxsnCHuTgh9w')

Last updated