Get listings for a collection across marketplaces
Fetch active listings for a collection across multiple marketplaces in a single call.
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;
}Last updated