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 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();}asyncfunctiongetRealmsForWallet(wallet) {constquery=` 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 } =awaitfetchGraphQL(query);if (errors) {// handle those errors like a proconsole.error(errors); }// do something great with this precious datareturn data;}//Get users for Grape DAOconstdaos=awaitgetRealmsForWallet("By2sVGZXwfQq6rAiAM3rNPJ9iQfb5e2QhnF4YjJ4Bip")console.dir(daos, {depth:null})
{"data": {"GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_TokenOwnerRecordV2": [ {"governanceDelegate":null,"governingTokenDepositAmount":1000000,"governingTokenMint":"8upjSpvjcdpuzhfR1zriwg5NXkwDruejqNE9WNbPRtyA","governingTokenOwner":"4NruycWTZFvbaF9wjF9uRVb2wP5gqTZtUpiDZ2GyQDZv","lamports":2853600,"outstandingProposalCount":0,"realm":"By2sVGZXwfQq6rAiAM3rNPJ9iQfb5e2QhnF4YjJ4Bip","reserved":"\\x000000000000","unrelinquishedVotesCount":0,"version":0 }// More response items in the array ],"GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_TokenOwnerRecordV1": [ {"governanceDelegate":null,"governingTokenDepositAmount":0,"governingTokenMint":"8upjSpvjcdpuzhfR1zriwg5NXkwDruejqNE9WNbPRtyA","governingTokenOwner":"5s4KyZSCExZVRPg579FBTLARtpxteJf9k5UoQpsaPvoz","lamports":1962720,"outstandingProposalCount":0,"realm":"By2sVGZXwfQq6rAiAM3rNPJ9iQfb5e2QhnF4YjJ4Bip","reserved":"\\x000000000000","unrelinquishedVotesCount":0,"version":0 }// More response items in the array ] }}