Skip to content

RPC Client

The RPC layer is a composable function type — no class hierarchy. Wrap a base client with middleware to add timeouts, retries, rate-limiting, and concurrency caps.

Create a client

ts
import { rpcHttpFromUrl } from "solana-kiss";

const rpc = rpcHttpFromUrl(new URL("https://api.mainnet-beta.solana.com"), {
  commitmentLevel: "confirmed",        // default
  extraRequestHeaders: { "Authorization": "Bearer mytoken" },
});

Middleware

ts
import {
  rpcHttpWithTimeout,
  rpcHttpWithConcurrentRequestsLimit,
  rpcHttpWithRequestsPerSecondLimit,
  rpcHttpWithRetryOnError,
} from "solana-kiss";

const rpc = rpcHttpWithRetryOnError(
  rpcHttpWithTimeout(
    rpcHttpWithConcurrentRequestsLimit(
      rpcHttpWithRequestsPerSecondLimit(
        rpcHttpFromUrl(new URL("https://my-rpc.example.com")),
        40,     // 40 RPS
      ),
      10,       // 10 concurrent requests
    ),
    5_000,      // 5 s timeout per call
  ),
  async ({ retriedCounter, totalDurationMs }) => {
    return totalDurationMs < 30_000 && retriedCounter < 5;
  },
);

Error handling

ts
import { RpcHttpError } from "solana-kiss";

try {
  await rpcHttpSendTransaction(rpc, packet);
} catch (err) {
  if (err instanceof RpcHttpError) {
    console.error(err.code, err.desc, err.data);
  }
}

RPC helper reference

Every helper accepts RpcHttp as its first argument:

FunctionJSON-RPC method
rpcHttpGetAccountWithDatagetAccountInfo
rpcHttpGetAccountMetadatagetAccountInfo (no data)
rpcHttpGetAccountLamportsgetAccountInfo (lamports only)
rpcHttpFindProgramOwnedAccountsgetProgramAccounts
rpcHttpFindAccountTransactionsgetSignaturesForAddress
rpcHttpFindBlocksgetBlocksWithLimit
rpcHttpGetBlockMetadatagetBlock (metadata only)
rpcHttpGetBlockWithTransactionsgetBlock (full)
rpcHttpGetBlockTimeOnlygetBlockTime
rpcHttpGetLatestBlockHashgetLatestBlockhash
rpcHttpIsBlockHashValidisBlockhashValid
rpcHttpGetTransactiongetTransaction
rpcHttpSendTransactionsendTransaction
rpcHttpSimulateTransactionsimulateTransaction
rpcHttpWaitForTransactionpolling via getTransaction

Solana: Keep It Simple, Stupid. (KISS)