Get all Pools of a Margin Account

Get all pools attached to a particular margin account on Tensor

From Tensor, we can get all pools related to a particular margin account by filtering the margin field in Tensor_Pool account.

Account Joins can be seen in action here! We fetch pool data joined with the associated margin account.

Natively in RPC calls, you would need to fetch both pool and the margin account separately. Here we are fetching pools joined with the associated margin account.

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

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

async function getAllPoolsperMargin(marginAddr) {
    //fields can be selected as per requirement
    const query = gql`
    query MyQuery {
      Tensor_Pool(where: {margin: {_eq: ${JSON.stringify(marginAddr)}}}) {
        createdUnixSeconds
        Margin {
          poolsAttached
          lamports
        }
        nftAuthority
        nftsHeld
        orderType
        owner
        margin
      }
    }
    `;

  const response = await graphQLClient.request(query);
  console.dir(response,{depth: null});
}
getAllPoolsperMargin('11HQ8NxwY3A6D4WUoYcUM4KRUUVSVRRJXpCMe4oTp4H')
//replace with margin account address

Last updated