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
{
TENSOR_CNFT_listState: [
{
rentPayer: '82CUQbRjC2XAcwGumAPDtdzTvYTJ1bpNdMHXTjiDXea',
pubkey: '79ZwK3dqpGN9MMEFz4srdWhrh2M1QDcF2KdC6sGvSRdH',
owner: '82CUQbRjC2XAcwGumAPDtdzTvYTJ1bpNdMHXTjiDXea',
assetId: 'CHCd7ThcHoTE12nerNgwow1azhULh2ZchXSxSTCzBeBH',
amount: 300000000
},
{
rentPayer: 'DCcUCSp9GAB5D1i6EXNciDie9sDFuuVpn4UwrFbR7V8m',
pubkey: '7GUKcbY1osAm6irYBtDP3otNb34egYoEcYU7TAGDXJeH',
owner: 'DCcUCSp9GAB5D1i6EXNciDie9sDFuuVpn4UwrFbR7V8m',
assetId: '3dDMC6BNUDuvKBffgWgKAXgtaDrpxn2FqCYFVn6VFh2i',
amount: 340000000
},
{
rentPayer: 'En9ji95Pn6WLu2WWZJKcweN6NpAA2r9ckN8yjPoDAvAJ',
pubkey: '3sHBCmhwiiiFyVZ46xy3PBxBmPmXMAcQWaJUXiA3cf7i',
owner: 'En9ji95Pn6WLu2WWZJKcweN6NpAA2r9ckN8yjPoDAvAJ',
assetId: 'CtAbEwu2cqYTev7vNozHVxauPwYvLCoTBY5b8es54mVz',
amount: 350000000
},
{
rentPayer: 'FBZzrBUk5hXiHWpCRcquR1fD13ug9XTxPtf99yd2MF25',
pubkey: '2447Xn14pe8KEbCNQMnmN6HZtrBMSemvSV14Sp5ZVTfu',
owner: 'FBZzrBUk5hXiHWpCRcquR1fD13ug9XTxPtf99yd2MF25',
assetId: 'BADdShUsP64nBpiDNFtooTECL3ky91CAsD9FV59yDcHT',
amount: 320000000
}
]
}
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')
{
Tensor_SingleListing: [
{
nftMint: 'HAArUyS59aBoHWB5XPap4uauJtqwxHn2ZMqLdViyFKj6',
owner: 'HBFxRkcjB6MTazJFSgNKXLG2mzDW9EUoeKtpNRrhWhVD',
price: 160800000000,
pubkey: '7gQ9HNk2NcGgn2z7spMjYUNrB5JPyRHNAZtqFfMjhrE7'
},
{
nftMint: 'J4oaV9XiH4eqLaoMYBGjbw6LhXbinwakNpdR9JTMMXYr',
owner: 'DFzWH1SUCnQv6Lq9Dxc5kh5iKztxkzhfCWSYMKfsqTzq',
price: 196000000000,
pubkey: 'CuJNbJNVdG4pKjiUafpLx2VtqFWdw6bPAd5o3uDDDeqk'
},
{
nftMint: 'JBfGGLQ7rW9x8tfs5R4Ta9ZRBU2oxKKBSv9tD6eWvWkV',
owner: 'DkFw7SDw7TpNrpDDbetvNoAfRbjaxJEYLV62fgLbz55M',
price: 173700000000,
pubkey: '659WrEpDGH4RzheAqVyZJWjpbomPgsDkTGCXNhrfpziK'
},
{
nftMint: 'HeEJ7jHBynTjfNmouMsCEx6drbBsgrdYrfKbeaCjsUNc',
owner: 'CYXPrpHMiRvUoAZzQHgth17Lb7LEbj8nh9zfrXqa3FDM',
price: 160540000000,
pubkey: '5uhgb9yW7P5XRDZhoSGuTkJHqdnpxHyNiD77X5SjWeEG'
},
{
nftMint: 'J5Dai1wD7sMhMw8bY9jLTa2BtkQCkfnX4qeaPZzAGCKK',
owner: 'ETN6xMmnftpcxroSinABZuAJM6XAganqRvNcbTcHV9s',
price: 186500000000,
pubkey: '6vrGhM7BmCMGcWQX9wSW87MkLQWqDkMsQdEAjniwsBFA'
},
{
nftMint: 'HLCTwXVmi114J4nWphD2vimwCbhat9cxDmJ4Rv6rSEqr',
owner: '2a8eghHjgRSCvGGD4XcD54sdWyJd5aX6tMWrosBzRnFD',
price: 398000000000,
pubkey: 'ABKceAbuoq6hb3QDd1PLHYmQijSwQEqKPh1tzGCM3iVV'
},
{
nftMint: 'HetJzowAkDyJJXcwSVExmfcoEzGzwwAYHz5thbpmjrr6',
owner: 'EJhytiNULnEZFV6nUguMpuwAuTZyMtGDLcnPzK8SCDGW',
price: 246000000000,
pubkey: 'Ftc3iAxEc8BR9Lc4MZFHrTSi7Vb3QixBwRMHp7Q4CQpY'
},
{
nftMint: 'HwDUYXX6PoXFxMqb8CShiSWWR5bSbhHaorwY7wFhQ6wj',
owner: 'HxaXScmaM9rTDAra2oFB5xv8VUMQMFqUwXmNvg79uADK',
price: 195000000000,
pubkey: 'ADUQm3PNUfmhJy9BmXBVkW6w7AFHKKtg2Ws4XHXqDHx2'
}
]
}
Last updated