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

# RPC

> HTTP and WebSocket JSON RPC Clients.

<Card title="GitHub Repository" icon="github" horizontal={true} href="https://github.com/sava-software/sava" />

## [Dependencies](https://github.com/sava-software/sava/blob/main/sava-rpc/src/main/java/module-info.java)

* java.net.http
* software.sava.json.iterator
* software.sava.core

## HTTP

<Info>
  See the official Solana docs for context on each [HTTP RPC method.](https://solana.com/docs/rpc/http)
</Info>

### Create Client

```java theme={null}
try (var httpClient = HttpClient.newHttpClient()) {
    var rpcClient = SolanaRpcClient.createClient(
        SolanaNetwork.MAIN_NET.getEndpoint(),
        httpClient
    );
}
```

### Fetch & Parse Accounts

```java theme={null}
var accountInfo = rpcClient.getAccountInfo(
    PublicKey.fromBase58Encoded("6T6vqb3VykNToz4sqY5C29Psb8iNUuVZPAqJXbAgorVF"),
).join();

byte[] tableData = accountInfo.data();
var table = AddressLookupTable.read(accountInfo.pubKey(), tableData);
System.out.println(table);
```

```java theme={null}
var accountInfo = rpcClient.getAccountInfo(
    PublicKey.fromBase58Encoded("6T6vqb3VykNToz4sqY5C29Psb8iNUuVZPAqJXbAgorVF"),
    AddressLookupTable.FACTORY
).join();

AddressLookupTable table = accountInfo.data();
System.out.println(table);
```

### Query Program Accounts with [Filters](https://solana.com/docs/rpc#filter-criteria)

<Info>
  **Note**: You will need access to an RPC node which has getProgramAccounts enabled, such as [Helius](https://www.helius.dev/).
</Info>

Retrieve all lookup tables which are active and frozen.

```java theme={null}
byte[] stillActive = new byte[Long.BYTES];
ByteUtil.putInt64LE(stillActive, 0, Clock.MAX_SLOT);
var activeFilter = Filter.createMemCompFilter(DEACTIVATION_SLOT_OFFSET, stillActive);
var noAuthorityFilter = Filter.createMemCompFilter(AUTHORITY_OPTION_OFFSET, new byte[]{0});

var accountInfoFuture = rpcClient.getProgramAccounts(
    SolanaAccounts.MAIN_NET.addressLookupTableProgram(),
    List.of(
        activeFilter,
        noAuthorityFilter
    ),
    AddressLookupTable.FACTORY
);

var accountInfoList = accountInfoFuture.join();
accountInfoList.stream().map(AccountInfo::data).forEach(System.out::println);
System.out.format("Retrieved %d tables which are active and frozen.%n", accountInfoList.size());
```

## WebSocket

<Info>
  See the official Solana docs for context on each [WebSocket RPC method.](https://solana.com/docs/rpc/websocket)
</Info>

### Create Client

```java theme={null}
try (var httpClient = HttpClient.newHttpClient()) {
    var webSocket = SolanaRpcWebsocket.build()
        .webSocketBuilder(httpClient.newWebSocketBuilder())
        .uri(SolanaNetwork.MAIN_NET.getWebSocketEndpoint())
        .solanaAccounts(SolanaAccounts.MAIN_NET)
        .commitment(Commitment.CONFIRMED)
        .onOpen(ws -> System.out.println("Websocket connected to " + ws.endpoint()))
        .onClose((_, statusCode, reason) -> System.out.format("%d: %s%n", statusCode, reason))
        .onError((_, throwable) -> throwable.printStackTrace())
        .create();

    webSocket.connect();
}
```

### Stream Program Accounts

#### Token Accounts

```java theme={null}
var solanaAccounts = SolanaAccounts.MAIN_NET;
var tokenProgram = solanaAccounts.tokenProgram();
var tokenOwner = PublicKey.fromBase58Encoded("");

webSocket.programSubscribe(
    tokenProgram,
    List.of(
        Filter.createDataSizeFilter(TokenAccount.BYTES),
        Filter.createMemCompFilter(TokenAccount.OWNER_OFFSET, tokenOwner)
    ),
    accountInfo -> {
        var tokenAccount = TokenAccount.read(accountInfo.pubKey(), accountInfo.data());
        System.out.println(tokenAccount);
    }
);
```

#### Address Lookup Tables

```java theme={null}
var solanaAccounts = SolanaAccounts.MAIN_NET;
var addressLookupTableProgram = solanaAccounts.addressLookupTableProgram();

webSocket.programSubscribe(addressLookupTableProgram, accountInfo -> {
    var table = AddressLookupTable.read(accountInfo.pubKey(), accountInfo.data());
    System.out.println(table);
});
```
