We will fetch all active proposals that a user can vote on. For this we will need to do following steps
Find out how many DAOs the wallet is part of from TokenOwnerRecordV1 and TokenOwnerRecordV2.
Find all governance accounts for the DAOs (realm address) from GovernanceV1 and GovernanceV2 accounts. We use the _in operator to pass in an array of realms in a single call.
Find all proposals where state = 2 for all the governance accounts. We again use the _in operator to pass in an array of governance accounts in a single call.
Here is a code snippet for you to try it out in replit.
// Node doesn't implement fetch so we have to import itimport fetch from"node-fetch";constSHYFT_API_KEY="YOUR_KEY"asyncfunctionfetchGraphQL(query, variables = {}, name ="MyQuery") {constresult=awaitfetch(`https://programs.shyft.to/v0/graphql/?api_key=${SHYFT_API_KEY}`, { method:"POST", body:JSON.stringify({ query: query, variables: variables, operationName: name }) } );returnawaitresult.json();}//We only fetch realm field, since thats what we are interested inasyncfunctiongetRealmsForWallet(wallet) {constquery=` query MyQuery { GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_TokenOwnerRecordV1( where: {governingTokenOwner: {_eq: ${JSON.stringify(wallet)}}} ) { realm } GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_TokenOwnerRecordV2( where: {governingTokenOwner: {_eq: ${JSON.stringify(wallet)}}} ) { realm } } `;const { errors,data } =awaitfetchGraphQL(query);if (errors) {// handle those errors like a proconsole.error(errors); }constrealms= []data.GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_TokenOwnerRecordV1.forEach((dao) => {realms.push(dao.realm) })data.GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_TokenOwnerRecordV2.forEach((dao) => {realms.push(dao.realm) })console.log(`Wallet is part of ${realms.length} DAOs`);// do something great with this precious datareturn realms;}asyncfunctiongetGovernanceAccountsForAllRealms(realms) {//realms should be an arrayconstquery=` query MyQuery($_in: [String!] = "") { GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_GovernanceV1( where: {realm: {_in: $_in}} ) { pubkey } GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_GovernanceV2( where: {realm: {_in: $_in}} ) { pubkey }}`constvariables= { _in: realms }const { errors,data } =awaitfetchGraphQL(query, variables);if (errors) {// handle those errors like a proconsole.error(errors); }constgovAccts= []data.GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_GovernanceV1.forEach((dao) => {govAccts.push(dao.pubkey) })data.GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_GovernanceV2.forEach((dao) => {govAccts.push(dao.pubkey) })console.log(govAccts);return govAccts;}asyncfunctiongetActiveProposals(governanceAccounts) {//governanceAccounts should be an arrayconstquery=` query MyQuery($_in: [String!] = "") { GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_ProposalV1( where: {_and: {state: {_eq: 2}, 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: {_and: {state: {_eq: 2}, 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 }}`constvariables= { _in: governanceAccounts }const { errors,data } =awaitfetchGraphQL(query, variables);if (errors) {// handle those errors like a proconsole.error(errors); }// do something great with this precious dataconsole.log(data);return data;}//Get DAOs for a walletconstrealms=awaitgetRealmsForWallet("JPQmr9p2RF3X5TuBXxn6AGcEfcsHp4ehcmzE5Ys7pZD")//Get governance accounts for all realmsconstgovernanceAccounts=awaitgetGovernanceAccountsForAllRealms(realms);constproposals=awaitgetActiveProposals(governanceAccounts);console.log(proposals)