GitHub Repository

Dependencies

HTTP

See the official Solana docs for context on each HTTP RPC method.

Create Client

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

Fetch & Parse Accounts

var accountInfo = rpcClient.getAccountInfo(
    PublicKey.fromBase58Encoded("6T6vqb3VykNToz4sqY5C29Psb8iNUuVZPAqJXbAgorVF"),
).join();

byte[] tableData = accountInfo.data();
var table = AddressLookupTable.read(accountInfo.pubKey(), tableData);
System.out.println(table);
var accountInfo = rpcClient.getAccountInfo(
    PublicKey.fromBase58Encoded("6T6vqb3VykNToz4sqY5C29Psb8iNUuVZPAqJXbAgorVF"),
    AddressLookupTable.FACTORY
).join();

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

Query Program Accounts with Filters

Note: You will need access to an RPC node which has getProgramAccounts enabled, such as Helius.

Retrieve all lookup tables which are active and frozen.

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

See the official Solana docs for context on each WebSocket RPC method.

Create Client

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

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

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);
});