Get active listings across marketplaces for a wallet

Get NFT listings from Tensor, Sniper and ME for a wallet in a single graphQl call.

In an NFT marketplace, users list their NFTs for sale, setting prices or initiating auctions. Let’s see how we can query data from some of the top marketplaces such as Tensor, Sniper and Magic Eden.

Cross program query in action. You can query multiple marketplaces in a single call.

  • In Magic Eden, user listings are stored in the SellerTradeStateV2 and SellerTradeState account

  • For Sniper, we have the SOLNFTOrderV1 account for listings

  • Tensor Swap stores active listings in SingleListing account

With the power of Shyft’s GraphQL APIs, you can query all three marketplaces in a single call.

You can directly copy paste this code on replit and see it in action.

async function fetchGraphQL(operationsDoc, operationName, variables) {
    const result = await fetch(
      "https://programs.shyft.to/v0/graphql?api_key=YOUR-API-KEY&network=mainnet-beta",
      {
        method: "POST",
        body: JSON.stringify({
          query: operationsDoc,
          variables: variables,
          operationName: operationName
        })
      }
    );
    //network is an optional param, which can be devnet also, defaults to mainnet-beta
    return await result.json();
  }
  
  const listingsforWallet = '8pvTRunAQghgWE3sinNxqCUULaxhvXhyEknf5AQe29rB';
  //wallet address for which we are fetching the listings
  const operationsDoc = `
    query MyQuery {
      MAGIC_EDEN_V2_SellerTradeStateV2(
        where: {seller: {_eq: ${JSON.stringify(listingsforWallet)}}}
      ) {
        tokenMint
        pubkey
        seller
        buyerPrice
        expiry
      }
      Tensor_SingleListing(
        where: {owner: {_eq: ${JSON.stringify(listingsforWallet)}}}
      ) {
        owner
        nftMint
        price
      }
      SNIPER_MARKET_SOLNFTOrderV1(
        where: {owner: {_eq: ${JSON.stringify(listingsforWallet)}}}
      ) {
        nftMint
        price
        owner
      }
    }
  `; //querying multiple marketplaces with one query
  
  function fetchMyQuery() {
    return fetchGraphQL(
      operationsDoc,
      "MyQuery",
      {}
    );
  }
  
  async function startFetchMyQuery() {
    const { errors, data } = await fetchMyQuery();
  
    if (errors) {
      // handle those errors like a pro
      console.error(errors);
    }
  
    // do something great with this precious data
    console.log(data);
  }
  
  startFetchMyQuery();

Please note that the responses are sample responses, and are shortened for visibility. We can also add more accounts in the response for each program, like for example, the SellerTradeState account for Magic Eden and tensor cNFTs listState has been omitted in the above example, but they can also be added.

Moreover, a single GraphQL call returns 1000 items in response. You would also need to paginate in some cases.

Last updated