> ## Documentation Index
> Fetch the complete documentation index at: https://docs.orbitflare.com/llms.txt
> Use this file to discover all available pages before exploring further.

# getProgramAccounts

> Returns all accounts owned by a program

## Parameters

<ResponseField name="programId" type="string" required>
  Program public key (base-58 encoded)
</ResponseField>

<ResponseField name="config" type="object" optional>
  <Expandable title="config fields">
    <ResponseField name="commitment" type="string" optional>
      Commitment level (processed, confirmed, finalized)
    </ResponseField>

    <ResponseField name="encoding" type="string" optional>
      Encoding format for account data (base58, base64, jsonParsed)
    </ResponseField>

    <ResponseField name="dataSlice" type="object" optional>
      <ResponseField name="offset" type="number">
        Offset into account data to start reading
      </ResponseField>

      <ResponseField name="length" type="number">
        Length of data to read
      </ResponseField>
    </ResponseField>

    <ResponseField name="filters" type="array" optional>
      Array of filter objects to apply to accounts

      <ResponseField name="memcmp" type="object" optional>
        <ResponseField name="offset" type="number">
          Offset into account data to compare
        </ResponseField>

        <ResponseField name="bytes" type="string">
          Base-58 encoded bytes to match
        </ResponseField>
      </ResponseField>

      <ResponseField name="dataSize" type="number" optional>
        Account data size to match
      </ResponseField>
    </ResponseField>
  </Expandable>
</ResponseField>

## Response

<ResponseField name="result" type="array">
  Array of account information objects

  <ResponseField name="pubkey" type="string">
    Account public key (base-58 encoded)
  </ResponseField>

  <ResponseField name="account" type="object">
    <ResponseField name="data" type="array">
      Account data in the specified encoding
    </ResponseField>

    <ResponseField name="executable" type="boolean">
      Whether the account is executable
    </ResponseField>

    <ResponseField name="lamports" type="number">
      Account balance in lamports
    </ResponseField>

    <ResponseField name="owner" type="string">
      Account owner (base-58 encoded)
    </ResponseField>

    <ResponseField name="rentEpoch" type="number">
      Epoch at which the account will next owe rent
    </ResponseField>
  </ResponseField>
</ResponseField>

## Code Examples

### Basic Request

```bash theme={null}
curl https://fra.rpc.orbitflare.com?api_key=YOUR-API-KEY -X POST -H "Content-Type: application/json" -d '{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "getProgramAccounts",
  "params": [
    "PROGRAM_ID"
  ]
}'
```

### Request with Filters

```bash theme={null}
curl https://fra.rpc.orbitflare.com?api_key=YOUR-API-KEY -X POST -H "Content-Type: application/json" -d '{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "getProgramAccounts",
  "params": [
    "PROGRAM_ID",
    {
      "filters": [
        {
          "dataSize": 165
        },
        {
          "memcmp": {
            "offset": 0,
            "bytes": "BASE58_ENCODED_BYTES"
          }
        }
      ]
    }
  ]
}'
```

### Using web3.js

```typescript theme={null}
import { Connection, PublicKey } from '@solana/web3.js';

const connection = new Connection('https://fra.rpc.orbitflare.com?api_key=YOUR-API-KEY');

// Get all program accounts
const accounts = await connection.getProgramAccounts(
  new PublicKey('PROGRAM_ID')
);
console.log('Accounts:', accounts);

// Get filtered program accounts
async function getFilteredProgramAccounts(
  programId: PublicKey,
  filters: any[]
) {
  const accounts = await connection.getProgramAccounts(programId, {
    filters
  });
  
  return accounts.map(({ pubkey, account }) => ({
    pubkey: pubkey.toBase58(),
    data: account.data,
    lamports: account.lamports,
    owner: account.owner.toBase58(),
    executable: account.executable
  }));
}
```

## Notes

1. Returns all accounts owned by a specific program
2. Filters can be applied to narrow down results
3. Account data can be sliced to reduce response size
4. The response is immediate as it reads from the current state
5. Different encodings can be specified for account data

## Best Practices

1. Use filters to efficiently query specific accounts
2. Consider using dataSlice to reduce response size
3. Cache results when appropriate to reduce RPC load
4. Handle large result sets with pagination
5. Use appropriate encoding based on your needs

## Common Errors

| Code   | Message                         | Solution                                     |
| ------ | ------------------------------- | -------------------------------------------- |
| -32601 | Method not found                | Verify you're connected to a Solana RPC node |
| -32602 | Invalid params                  | Check programId and config parameters        |
| -32007 | Account information unavailable | Node may be bootstrapping or syncing         |

## Use Cases

1. **Program Account Analysis**
   ```typescript theme={null}
   interface ProgramAccountAnalysis {
     pubkey: string;
     dataSize: number;
     dataHash: string;
     lamports: number;
     metadata: {
       timestamp: number;
     };
   }

   async function analyzeProgramAccounts(
     programId: PublicKey,
     filters?: any[]
   ): Promise<ProgramAccountAnalysis[]> {
     const accounts = await connection.getProgramAccounts(programId, {
       filters,
       encoding: 'base64'
     });
     
     return accounts.map(({ pubkey, account }) => {
       const data = account.data[0];
       const dataSize = Buffer.from(data, 'base64').length;
       
       return {
         pubkey: pubkey.toBase58(),
         dataSize,
         dataHash: require('crypto')
           .createHash('sha256')
           .update(data)
           .digest('hex'),
         lamports: account.lamports,
         metadata: {
           timestamp: Date.now()
         }
       };
     });
   }
   ```

2. **Program Account Monitoring**
   ```typescript theme={null}
   interface ProgramAccountChange {
     pubkey: string;
     type: 'created' | 'deleted' | 'modified';
     previous: any;
     current: any;
     difference: number;
   }

   class ProgramAccountMonitor {
     private previousAccounts: Map<string, any> = new Map();
     
     async monitorProgramAccounts(
       programId: PublicKey,
       filters?: any[],
       interval: number = 5000
     ): Promise<ProgramAccountChange[]> {
       const accounts = await connection.getProgramAccounts(programId, {
         filters
       });
       const changes: ProgramAccountChange[] = [];
       
       accounts.forEach(({ pubkey, account }) => {
         const address = pubkey.toBase58();
         const current = account;
         const previous = this.previousAccounts.get(address);
         
         if (!previous && current) {
           changes.push({
             pubkey: address,
             type: 'created',
             previous: null,
             current,
             difference: current.lamports
           });
         } else if (previous && !current) {
           changes.push({
             pubkey: address,
             type: 'deleted',
             previous,
             current: null,
             difference: -previous.lamports
           });
         } else if (previous && current) {
           const difference = current.lamports - previous.lamports;
           if (difference !== 0 || current.data[0] !== previous.data[0]) {
             changes.push({
               pubkey: address,
               type: 'modified',
               previous,
               current,
               difference
             });
           }
         }
         
         this.previousAccounts.set(address, current);
       });
       
       return changes;
     }
   }
   ```

3. **Program Account Pagination**
   ```typescript theme={null}
   interface ProgramAccountPage {
     accounts: Array<{
       pubkey: string;
       data: any;
       lamports: number;
     }>;
     total: number;
     page: number;
     pageSize: number;
     metadata: {
       timestamp: number;
     };
   }

   async function getProgramAccountPage(
     programId: PublicKey,
     page: number = 1,
     pageSize: number = 100,
     filters?: any[]
   ): Promise<ProgramAccountPage> {
     const accounts = await connection.getProgramAccounts(programId, {
       filters,
       encoding: 'base64'
     });
     
     const start = (page - 1) * pageSize;
     const end = start + pageSize;
     const pageAccounts = accounts.slice(start, end);
     
     return {
       accounts: pageAccounts.map(({ pubkey, account }) => ({
         pubkey: pubkey.toBase58(),
         data: account.data[0],
         lamports: account.lamports
       })),
       total: accounts.length,
       page,
       pageSize,
       metadata: {
         timestamp: Date.now()
       }
     };
   }
   ```
