Get listings for a collection across marketplaces

Fetch active listings for a collection across multiple marketplaces in a single call.

In order to query active listings for a collection, we would first need a mint list (list of addresses of all NFTs belonging to that collection). We can save it before hand or fetch it through DAS. For this example we will be fetching the data through DAS.

GraphQL API also allows you to add multiple filters, along with checks for a range of values for particular fields. Suppose, we are filtering the nftMint, or the owner field, we can check if the nftMint or owner, belongs to a range (array) of values instead of just one particular value.

So to achieve the above objective, we will be requiring two simple steps

  • We will get a list of all mint addresses for a collection using DAS.

  • We will then use the array of mint addresses to filter the listing across the three marketplaces.

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

const endpoint = `https://programs.shyft.to/v0/graphql?api_key=YOUR-API-KEY`; //Shyft's gQl endpoint

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

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

async function getAllListingsForCollection(collectionAddr:string) {

    const allNftMintsforCollection = await getAllMintsofColl(collectionAddr);
    const query = gql`
    query MyQuery {
        MAGIC_EDEN_V2_SellerTradeStateV2(
          where: {tokenMint: {_in: ${JSON.stringify(allNftMintsforCollection)}}}
        ) {
          tokenMint
          seller
          buyerPrice
        }
        Tensor_SingleListing(
          where: {nftMint: {_in: ${JSON.stringify(allNftMintsforCollection)}}}
        ) {
          owner
          nftMint
          price
        }
        SNIPER_MARKET_SOLNFTOrderV1(
          where: {nftMint: {_in: ${JSON.stringify(allNftMintsforCollection)}}}
        ) {
          nftMint
          price
          owner
        }
      }
    `;

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

//MadLads collection address
getAllListingsForCollection('J1S9H3QjnRtBbbuD4HjPV6RpRhwuk4zKbxsnCHuTgh9w')

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

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

      console.log(`fetching ${page} from DAS`);
      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;
}

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

Please note that, like filtering we can also limit the number of results we want in the response. In the above case we have limited the listings from each marketplace to 1, however this can be removed to fetch more listings for that particular collection from the marketplaces.

Last updated