Get DAO Token Owners

Fetch all token owners of a DAO.

Each DAO is represented by a realm id. Given a realm address we can fetch all the token owners who are part of that DAO. For this, we need to focus on TokenOwnerRecordV1 and TokenOwnerRecordV2 accounts.

Few fields of our interest are

  • governingTokenMint: Token address which governs the DAO.

  • governingTokenDepositAmount: How much tokens have been deposited by the user.

  • governingTokenOwner: The user who owns the governing token.

You can run this code in replit to see it in action.

// Node doesn't implement fetch so we have to import it
import fetch from "node-fetch";
const SHYFT_API_KEY = "YOUR-KEY"

async function fetchGraphQL(query, variables = {}, name = "MyQuery") {
  const result = await fetch(
    `https://programs.shyft.to/v0/graphql/?api_key=${SHYFT_API_KEY}`,
    {
      method: "POST",
      body: JSON.stringify({
        query: query,
        variables: variables,
        operationName: name
      })
    }
  );

  return await result.json();
}

async function getRealmsForWallet(wallet) {
  const query = `
    query MyQuery {
      GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_TokenOwnerRecordV1(
        where: {realm: {_eq: ${JSON.stringify(wallet)}}}
      ) {
        governingTokenDepositAmount
        governingTokenMint
        governingTokenOwner
        pubkey
      }
      GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_TokenOwnerRecordV2(
        where: {realm: {_eq: ${JSON.stringify(wallet)}}}
      ) {
        governingTokenDepositAmount
        governingTokenMint
        governingTokenOwner
        pubkey
      }
    }
  `;

  const { errors, data } = await fetchGraphQL(query);

  if (errors) {
    // handle those errors like a pro
    console.error(errors);
  }
  
  // do something great with this precious data
  return data;
}

//Get users for Grape DAO
const daos = await getRealmsForWallet("By2sVGZXwfQq6rAiAM3rNPJ9iQfb5e2QhnF4YjJ4Bip")

console.dir(daos, {depth: null})

Last updated