Get All Proposals For DAO
Fetch all proposals for a DAO.
// 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