Fetching Trees

getTree

Get a Tree by its ID.

const tree = await hatsSubgraphClient.getTree({
    chainId,
    treeId,
    props,
});

Arguments:

{
    chainId: number;
    treeId: number;
    props: TreePropsConfig;
}
  • chainId - ID of the chain to fetch from.

  • treeId - ID of the Tree to fetch (Tree's top-hat domain - first 4 bytes of the top-hat ID).

  • props - Tree's properties to fetch, including the ones of nested objects. Check the TreePropsConfig type for the available properties and query filters.

Response:

Tree

A Tree object, containing the chosen properties.

Example:

// get a tree's top-hat
const res = await client.getTree({
  chainId: 10, // optimism
  treeId: 1, 
  props: {
    hats: { // get the tree's hats
      props: {}, // for each hat, include only its ID
      filters: { first: 1 }, // fetch only the first hat (the top-hat)
    },
  },
});

getTreesByIds

Get Trees by their IDs.

const trees = await hatsSubgraphClient.getTreesByIds({
    chainId,
    treeIds,
    props,
});

Arguments:

{
    chainId: number;
    treeIds: number[];
    props: TreePropsConfig;
}
  • chainId - ID of the chain to fetch from.

  • treeIds - IDs of the Trees to fetch (Tree's top-hat domain - first 4 bytes of the top-hat ID).

  • props - Tree's properties to fetch, including the ones of nested objects. Check the TreePropsConfig type for the available properties and query filters.

Response:

Tree[]

An array of Tree objects, containing the chosen properties.

Example:

const res = await client.getTreesByIds({
  chainId: 10, // optimism
  treeIds: [1, 2],
  props: {
    hats: { // for each tree, fetch its hats
      props: {
        status: true, // for each hat, include its status (active/inactive)
      },
      filters: { first: 200 }, // fetch only the first 200 hats
    },
  },
});

getTreesPaginated

Paginate over Trees.

const trees = await hatsSubgraphClient.getTreesPaginated({
    chainId,
    props,
    page,
    perPage,
});

Arguments:

{
    chainId: number;
    props: TreePropsConfig;
    page: number;
    perPage: number;
}
  • chainId - ID of the chain to fetch from.

  • props - Tree's properties to fetch, including the ones of nested objects. Check the TreePropsConfig type for the available properties and query filters.

  • page - Number of page to fetch.

  • perPage - Number of Trees to fetch in each page.

Response:

Tree[]

An array of Tree objects, containing the chosen properties.

Example:

const res = await client.getTreesPaginated({
  chainId: 10, // optimism
  props: {
    hats: { // for each tree, get its hats
      props: {}, // for each hat, include only its ID 
      filters: { first: 1 }, // fetch only the first hat of every tree (the top-hat)
    },
  },
  page: 3, // get the third page
  perPage: 10, // each page contains 10 trees
});

Last updated