Get All Proposals For DAO

Fetch all proposals for a DAO.

If we want to fetch all proposals for a DAO, we have to follow these steps

  • Get Realm id for the DAO you are interested in. You can either have it beforehand or search based on DAO name.

  • Once we have the realm address, we will fetch all the associated governance accounts.

  • We will use the _in operator to pass all governance accounts in one call and fetch all the associated proposals.

// 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 fetchProposals(governanceAccounts) {

const query = `
  query MyQuery($_in: [String!] = "") {
    GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_ProposalV1(
      where: {governance: {_in: $_in}}
    ) {
      closedAt
      descriptionLink
      draftAt
      executingAt
      executionFlags
      governance
      governingTokenMint
      instructionsCount
      instructionsExecutedCount
      instructionsNextIndex
      lamports
      maxVoteWeight
      name
      noVotesCount
      signatoriesCount
      signatoriesSignedOffCount
      signingOffAt
      state
      tokenOwnerRecord
      voteThreshold
      votingAt
      votingAtSlot
      votingCompletedAt
      yesVotesCount
    }
    GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_ProposalV2(
      where: {governance: {_in: $_in}}
    ) {
      abstainVoteWeight
      closedAt
      denyVoteWeight
      descriptionLink
      draftAt
      executingAt
      executionFlags
      governance
      governingTokenMint
      lamports
      maxVoteWeight
      maxVotingTime
      name
      options
      reserved1
      signatoriesCount
      signatoriesSignedOffCount
      signingOffAt
      startVotingAt
      state
      tokenOwnerRecord
      vetoVoteWeight
      voteThreshold
      voteType
      votingAt
      votingAtSlot
      votingCompletedAt
    }
  }
`;

  const variables = {
    _in: governanceAccounts
  }

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

  if (errors) {
    // handle those errors like a pro
    console.error(errors);
  }

  return data;
}

async function fetchGovernanceAccounts(realm) {

  //We are only interested in pubkeys of the governance accounts
  const query = `
    query MyQuery {
  GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_GovernanceV1(
    where: {realm: {_eq: ${JSON.stringify(realm)}}}
  ) {
    pubkey
  }
  GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_GovernanceV2(
    where: {realm: {_eq:${JSON.stringify(realm)}}}
  ) {
    pubkey
  }
}
  `;

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

    if (errors) {
      // handle those errors like a pro
      console.error(errors);
    }

  //Fetch all governance accounts
  const governanceAccounts = []
  data?.GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_GovernanceV1?.forEach(account => {
      governanceAccounts.push(account.pubkey);
    })

  data?.GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_GovernanceV2?.forEach(account => {
      governanceAccounts.push(account.pubkey);
    })


  return governanceAccounts;
}

//Fetch governance accounts for Grape DAO realm
const govAccounts = await fetchGovernanceAccounts("By2sVGZXwfQq6rAiAM3rNPJ9iQfb5e2QhnF4YjJ4Bip")

//Fetch all proposals for the goverance accounts
const proposals = await fetchProposals(govAccounts);
console.log(proposals);

Last updated