# Get DAO Treasury Info

Given a DAO, we need to fetch its treasury info. In order to do this, we need the DAO's <mark style="color:yellow;">realm</mark> address. -&#x20;

* Once we have the realm address, we will fetch all the governance accounts for it.
* Treasury wallets are a <mark style="color:yellow;">PDA(program derived address)</mark> of governance pubkeys and 'native-treasury' text. Code snippet in the below section.
* Once we have the treasury wallets, we use [<mark style="color:red;">Get Portfolio</mark>](/solana-apis/wallet.md#get-portfolio) to fetch Sol, Token balances and NFTs in a single call. You can usr any other approach you want.

Here's a fully working code snippet for you to try out in <mark style="color:yellow;">replit</mark>.

#### Fetch Treasury Info For Grape Dao

{% code overflow="wrap" %}

```javascript
// Node doesn't implement fetch so we have to import it
import fetch from "node-fetch";
import { PublicKey } from "@solana/web3.js";
import { promises } from "dns";

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 getGovernanceAccountsForDAO(realmAddress) {

  //realms should be an array
  const query = `
  query MyQuery($_in: [String!] = "") {
  GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_GovernanceV1(
    where: {realm: {_eq: ${JSON.stringify(realmAddress)}}}
  ) {
    pubkey
  }
  GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_GovernanceV2(
    where: {realm: {_eq: ${JSON.stringify(realmAddress)}}}
  ) {
    pubkey
  }
}
`
  const { errors, data } = await fetchGraphQL(query);

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

  const govAccts = []
  data.GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_GovernanceV1.forEach((dao) => {
      govAccts.push(dao?.pubkey)
  })

  data.GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_GovernanceV2.forEach((dao) => {
      govAccts.push(dao?.pubkey)
  })

  console.log(govAccts);

  return govAccts;
}

function getNativeTreasuryAddress(governanceAccounts) {
  const programId = new PublicKey("GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw")
  const treasuryAddress = []
  
  governanceAccounts.forEach(async (governance) => {
    const acc = new PublicKey(governance)
    const [address] = await PublicKey.findProgramAddress(
    [Buffer.from('native-treasury'), acc.toBuffer()],
    programId
  );
    const addy = address.toBase58()
    console.log(addy)
    treasuryAddress.push(address.toBase58());
  })

  return treasuryAddress;
}

async function fetchTreasuryInfo(wallets) {
  console.time('portfolio')
  const promises = []
  //Once we have the treasury wallets, we can fetch their holdings
  wallets.map(async (wallet) => {
    promises.push(getPortfolio(wallet))
  })
  
  return await Promise.all(promises);
}

async function getPortfolio(wallet) {
  try {
    console.log('fetching portfolio for ', wallet)
    const result = await fetch(
      `https://api.shyft.to/sol/v1/wallet/get_portfolio?network=mainnet-beta&wallet=${wallet}`,
      {
        method: "GET",
        headers: {
          "Content-Type": "application/json",
          "x-api-key": SHYFT_API_KEY
        },
      }
    );
    const res = await result.json()

    return res;
    
  } catch (err) {
    console.error(err)
  }
}

async function getDaoTreasury(realmAddress) {
  
  //Get governance accounts for all realms
  const governanceAccounts = await   getGovernanceAccountsForDAO(realmAddress);

  console.log('gov accounts fetched');
  const treasuryWallets = await getNativeTreasuryAddress(governanceAccounts);
  
  console.log('treasury wallets: ', treasuryWallets);
  
  return await fetchTreasuryInfo(treasuryWallets);
}

//Grape DAO treasury
const treasury = await getDaoTreasury("By2sVGZXwfQq6rAiAM3rNPJ9iQfb5e2QhnF4YjJ4Bip");
console.dir(treasury, {depth: null})

```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.shyft.to/solana-indexers/case-studies/solana-governance-realms/get-dao-treasury-info.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
