Leverage AccountsDB Infrastructure for Solana RPC Requests

Leverage Alchemy’s AccountsDB Infrastructure for Solana RPC Requests!

The most expensive Solana RPC requests involve account scans, such as getProgramAccounts and getLargestTokenAccounts. These methods are incredibly useful but non-performant methods since they induce a heavy RPC load on Solana validator nodes, often resulting in a 5XX response due to timeout or a response with high latency.

Alchemy has built out core infrastructure that sits atop our Solana validator nodes to support these methods at scale, through what we call the AccountsDB Infrastructure. This infrastructure allows for faster, scalable, and more reliable responses to these methods by paginating the response with a pageKey. You could then loop through your that same request with at scan and aggregate the full response from our validator nodes.

You can see pageKey is now an optional parameter in each account-scanning method in our Solana docs, and you may also include an order optional parameter that would sort the accounts in the response by their pubkey field.

Here’s an example with getProgramAccounts:

typescript
1const axios = require("axios");
2
3async function getProgramAccountsExample() {
4 let gPAExampleRequest = {
5 method: "getProgramAccounts",
6 params: [
7 "ZETAxsqBRek56DhiGXrn75yj2NHU3aYUnxvHXpkf3aD",
8 {
9 encoding: "base64",
10 withContext: true,
11 order: "desc",
12 dataSlice: {
13 offset: 0,
14 length: 10,
15 },
16 },
17 ],
18 id: 0,
19 jsonrpc: "2.0",
20 };
21 let programAccounts = [];
22
23 const alchemyRPCUrl =
24 "https://solana-mainnet.g.alchemy.com/v2/<YOUR-API-KEY>";
25 try {
26 let response = await axios.post(alchemyRPCUrl, gPAExampleRequest);
27 let responseData = response.data["result"];
28
29 // continue aggregating if there's a new pageKey present in the latest response
30 while (responseData["pageKey"]) {
31 programAccounts = programAccounts.concat(responseData["value"]);
32
33 // place the pagekey within the optional config object
34 // (you may need to create that config object if you didn't have it originally)
35 gPAExampleRequest["params"][1]["pageKey"] = responseData["pageKey"];
36
37 // make another call to getProgramAccounts with the pageKey
38 response = await axios.post(alchemyRPCUrl, gPAExampleRequest);
39 responseData = response.data["result"];
40 }
41
42 programAccounts = programAccounts.concat(responseData["value"]);
43 console.log(programAccounts);
44 return programAccounts;
45 } catch (err) {
46 console.log(err.message);
47 return [];
48 }
49}