Get all Bids of a Collection

Getting all bids of on NFTs belonging to a specific collection

To query active bids of a collection, we will have to query data differently for Tensor cNFT and normal NFTs.

For Tensor cNFT, we have to check two fields target and targetId.

  • If target object has key assetId, then targetId points to an actual cNFT address.

  • If target object has key whitelist, then targetId points to the collection detail. (coming soon)

In this example we are checking for {target: {_has_key: "assetId"}. We will have to compare targetId against a pre defined collection mint list, which we can save before hand or fetch through DAS. For this example, we will be fetching through a DAS call.

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

Active Bids on a cNFT Collection (Compressed NFT collection)

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 getAllBidsFromCollection(collectionAddr:string) {

    const allNftMintsforCollection = await getAllMintsofColl(collectionAddr);
    // all fields can be cherry-picked as per your requirement
    const query = gql`
      query MyQuery {
        TENSOR_CNFT_bidState(
          where: {target: {_has_key: "assetId"}, targetId: {_in: ${JSON.stringify(allNftMintsforCollection)}}}
        ) {
          targetId
          target
          bidId
          owner
          cosigner
          currency
          makerBroker
          rentPayer
          pubkey
          amount
        }
      }
    `;

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

getAllBidsFromCollection('AGSmrgdkkwYRpvWXZ2kf4KoPdG5jPMj8KrNjnAgsum54')
//replace with your NFT collection address

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;
}

We can also also query the same for Non-compressed NFTs of a collection. However, this data is available in the bidState account of Tensor_Bid account. We can fetch all the NFTs belonging to a particular collection using DAS, and then use the where filter on the nftMint to get the desired result. An example is illustrated below.

We can use the _in check to to compare if a key exists in a range(array) of values

Active Bids on a NFT Collection (Non-Compressed NFT collection)

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 getAllBidsFromCollection(collectionAddr:string) {
    const allNftMintsforCollection = await getAllMintsofColl(collectionAddr);
    // all fields can be cherry-picked as per your requirement
    const query = gql`
    query MyQuery {
      tensor_bid_BidState(
        where: {nftMint: {_in: ${JSON.stringify(allNftMintsforCollection)}}}
      ) {
        bidAmount
        bidder
        expiry
        nftMint
        pubkey
      }
    }
    `;
  const response = await graphQLClient.request(query);
  console.dir(response,{depth: null});
  
}

getAllBidsFromCollection('6qxgQKV5pj4Jg4sGU7AvM2xeFwVwfGdJmXj889AyKQqu')
//replace with your NFT collection address

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;
}

Last updated