Get Active Listings of a Collection
Get active listings for a collection on Tensor using GraphQL.
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 addressActive Listings of a collection from Tensor Swap (normal NFTs)
Last updated