Solana RPC client
The Solana RPC client gives Go programs one typed interface to query a Solana cluster, inspect ledger and account state, and send or simulate transactions. It turns endpoint-specific JSON-RPC calls into domain values while keeping commitment, encoding, error, and request pacing choices visible to the caller.
Client model
The client has two related roles. The high-level rpc.Client offers typed Solana methods, while the lower-level JSON-RPC client sends general requests for methods that do not have a typed wrapper. The lower layer is described in JSON-RPC transport. Both roles use an HTTP RPC endpoint. WebSocket events use a separate client described in WebSocket subscriptions.
RPC endpoints
An RPC endpoint is the HTTP URL for a cluster node or RPC provider. The package defines HTTPS endpoints for mainnet-beta, testnet, and devnet, plus local HTTP and WebSocket endpoints for a local validator. A client stores one endpoint and sends its requests to that endpoint.
The endpoint does not define the read view by itself. Each method can request a commitment level, and some methods can require a minimum context slot so that a node does not answer from an older view.
References: rpc/endpoints.go• L17-70 rpc/types.go• L31-38 rpc/types.go• L519-559
Request options
Typed methods expose optional request fields through method-specific option values. These options select the read commitment, response encoding, transaction detail, result filters, pagination, or minimum slot without making callers build JSON by hand.
The same pattern covers transaction submission and simulation. Submission options control encoding, preflight checks, retry limits, and the minimum slot. Simulation options control signature checks, blockhash replacement, logs, returned accounts, and related execution data. See Transaction submission for that lifecycle.
References: rpc/getAccountInfo.go• L66-95 rpc/getTransactionsForAddress.go• L36-70 rpc/types.go• L653-685 rpc/simulateTransaction.go• L95-131
Request and response model
The client follows JSON-RPC 2.0. A request names a method and carries positional parameters. A response contains either a result or an error. The high-level client adds typed method arguments and typed result values around this common envelope.
Typed values
Each method family returns a type that matches the Solana data it represents. Responses that need a node view use RPCContext, which carries the slot and optional API version beside the value. Optional chain data uses pointers or nullable fields so the caller can distinguish missing data from a zero value.
Encoding affects the shape of some typed values. Account and transaction data can be binary or JSON. DataBytesOrJSON keeps the selected form and provides access to decoded bytes or raw JSON. Block transaction results can also be requested as account keys without the full message. Use the accessor that matches the requested transaction detail.
Parsed transaction reads expose parsed instructions and transaction metadata as typed values. Instruction data can be a parsed object or a string, so consumers must handle the response form selected by the node.
References: rpc/types.go• L31-43 rpc/types.go• L86-188 rpc/types.go• L340-467 rpc/types.go• L562-651 rpc/getTransaction.go• L79-159 rpc/getParsedTransaction.go• L21-53 rpc/client_test.go• L576-679
Commitments and encodings
A commitment is the chain view used by a read or a transaction preflight check. The client supports processed, confirmed, and finalized. Stronger commitments reduce the chance of reading a view that the cluster later drops, but they can be less recent.
CompareCommitment orders these levels from weakest to strongest. Unknown values rank below the known levels, so an unknown status does not satisfy a required commitment.
Account, block, and transaction methods support binary, JSON, or parsed JSON forms where the Solana RPC method allows them. The typed layer selects safe defaults for several methods, but callers must select an encoding that matches how they plan to read the returned data.
Versioned block and transaction reads also declare the newest transaction version that the caller can decode. Set MaxSupportedTransactionVersion to include v1 when the response may contain v1 transactions. Otherwise the node can reject the request even when the endpoint supports it.
References: rpc/types.go• L519-559 rpc/types.go• L598-606 rpc/getAccountInfo.go• L66-94 rpc/getBlock.go• L33-65 rpc/getTransaction.go• L25-34 rpc/getParsedTransaction.go• L12-19 rpc/commitment_test.go• L9-33
Batch requests
Batch requests group several JSON-RPC calls into one HTTP exchange. The normal batch path assigns request IDs and JSON-RPC version fields so the caller can match responses by ID. Response order is not a reliable match for request order. Raw batch calls preserve the request objects and therefore place ID and protocol correctness on the caller.
References: rpc/…/jsonrpc.go• L86-130 rpc/…/jsonrpc.go• L311-357 rpc/…/jsonrpc.go• L482-500
Method families
The typed methods organize Solana's RPC surface by the kind of chain data or action they serve. This grouping helps an engineer choose the smallest query that supplies the needed state and then follow the result into a later transaction or subscription flow.
Account and program state
Account methods read lamport balances, account owners, executable status, and account data. Program-account methods find accounts owned by a program and can narrow the result with filters, data slices, or a minimum slot. Token methods provide token balances, supply, and accounts. Direct account helpers can decode binary or Borsh data into a caller-provided value.
Account lookup has two absence modes. The usual typed account methods turn a missing account into ErrNotFound, while GetAccountInfoMaybe preserves the successful RPC result with a nil value and no error. This choice lets callers either use a simple error contract or inspect the returned context.
References: rpc/getAccountInfo.go• L27-64 rpc/getAccountInfo.go• L97-124 rpc/getBalance.go• L25-80 rpc/getProgramAccounts.go• L25-98 rpc/types.go• L340-386 rpc/types.go• L469-516 rpc/client_test.go• L145-198
Ledger and cluster state
Ledger methods describe slots, blocks, block times, block height, block hashes, epoch data, rewards, and validator information. Health, identity, version, and supply methods describe the node or cluster rather than one account. Many results include a slot context so downstream code can relate the answer to a chain position.
References: rpc/getBlock.go• L72-168 rpc/getLatestBlockhash.go• L25-81 rpc/getEpochInfo.go• L20-55 rpc/getHealth.go• L21-36 rpc/getVersion.go• L21-33
Transaction history and status
History methods find transactions by signature or address and return signatures, confirmation state, errors, metadata, or full transaction data. The parsed transaction method requests JSON-parsed instruction data for one signature, while the regular transaction method supports other response encodings. Status methods report whether a submitted signature is processed, confirmed, finalized, or failed.
Address history can be paged and filtered, but getTransactionsForAddress is a provider extension and is not available on every RPC endpoint.
References: rpc/getTransaction.go• L36-90 rpc/getParsedTransaction.go• L12-53 rpc/getSignatureStatuses.go• L23-83 rpc/getTransactionsForAddress.go• L171-245 rpc/types.go• L317-337 rpc/client_test.go• L2622-2687
Transaction actions
Transaction actions simulate a transaction or submit it to the cluster. Simulation returns execution errors, logs, compute use, account changes, and return data without submitting the transaction. Submission accepts a signed transaction object, raw wire bytes, or an already encoded value.
A successful submission only means that the RPC service accepted the request. It does not mean the cluster confirmed the transaction. Use status methods or the flow in Transaction submission for confirmation.
The same submission boundary applies to v1 transactions. The caller must build and sign the v1 message, use a cluster where the v1 feature is active, and then submit it through the normal transaction method.
References: rpc/simulateTransaction.go• L27-60 rpc/simulateTransaction.go• L83-195 rpc/sendTransaction.go• L27-84 rpc/sendEncodedTransaction.go• L25-56 rpc/getSignatureStatuses.go• L28-75
Raw RPC access
Raw access is the escape hatch for a Solana method that has no typed wrapper or for a provider-specific method. RPCCallForInto decodes a successful result into a caller-provided value. Callback and batch forms expose the lower-level exchange. This path keeps the JSON-RPC contract visible and does not add typed validation for the custom method.
References: rpc/client.go• L43-47 rpc/client.go• L189-208 rpc/…/jsonrpc.go• L34-135
Error model
Errors can occur before a node returns a JSON-RPC result, in the JSON-RPC envelope, or after a valid result is decoded. Callers should use the error first. They should then inspect the typed result and its chain-level fields when a method defines them.
Transport and protocol errors
Network failures, request creation failures, timeouts, malformed response bodies, and missing response bodies return Go errors. An HTTP status without a usable JSON-RPC error response returns an HTTPError with the status code. A valid JSON-RPC error returns an RPCError with a code, message, and optional data.
The request context controls cancellation for the HTTP exchange. Closing a client releases idle HTTP connections. It does not turn a failed or accepted transaction into a confirmed one.
References: rpc/…/jsonrpc.go• L196-268 rpc/…/jsonrpc.go• L409-480 rpc/…/jsonrpc.go• L533-623
Semantic result errors
Some typed methods turn an empty successful result into a package error because the absence has a clear meaning. ErrNotFound represents missing account, transaction, or status data. ErrNotConfirmed represents a block that the node did not return as confirmed.
Other methods can return a non-nil result with an embedded execution error. Simulation and transaction metadata use this form because the node successfully returned execution details even when the transaction failed.
References: rpc/client.go• L32-35 rpc/getAccountInfo.go• L97-124 rpc/getBlock.go• L123-133 rpc/getTransaction.go• L69-76 rpc/getParsedTransaction.go• L46-52 rpc/getSignatureStatuses.go• L42-50 rpc/simulateTransaction.go• L27-39
Rate limiting
Rate limiting places a request gate in front of the JSON-RPC transport. The package provides a requests-per-second limiter and a context-aware token-bucket limiter. Both limit each outgoing RPC call before it reaches the endpoint.
Limiter choices
NewWithRateLimit uses a fixed requests-per-second rate and waits at the limiter without a request context. NewWithLimiter accepts a rate and burst size and waits with the caller's context. Cancellation or a context deadline can stop a request before it is sent.
These constructors return the lower-level JSONRPCClient interface. To keep the high-level typed methods, pass the result to NewWithCustomRPCClient. A batch is one limited call even when it contains several JSON-RPC requests.
References: rpc/client-with-ratelimit.go• L12-64 rpc/client-with-ratelimit2.go• L12-76 rpc/client.go• L43-47 rpc/client.go• L127-133
Client construction
Client construction chooses the endpoint and the transport behavior that all later calls share. The default constructor creates a concurrent-safe client with a five-minute HTTP timeout. Alternate constructors add headers, a timeout, or a default commitment without changing the typed method surface.
Constructor variants
Use the constructor that matches the scope of the setting:
| Constructor | Purpose |
|---|---|
New | Use an endpoint with the standard HTTP client settings. |
NewWithHeaders | Add provider or authentication headers to every RPC request. |
NewWithTimeout | Replace the standard timeout for request, dial, and idle connection work. |
NewWithCommitment | Store a default commitment for callers to use as a fallback. Explicit method arguments still win. |
NewWithTimeoutAndCommitment | Combine the timeout and stored commitment choices. |
NewWithCustomRPCClient | Wrap a compatible JSON-RPC transport, such as a rate-limited one, with the typed Solana client. |
The stored default commitment is readable through DefaultCommitment. It is a client-level preference. Method-specific commitment fields remain the request contract.
References: rpc/client.go• L49-133 rpc/client.go• L135-187 rpc/client_constructors_test.go• L18-63
Connection lifecycle
The HTTP client is safe for concurrent use by multiple goroutines and reuses connections for calls to the endpoint. Call Close when the client will no longer be used so idle connections can be released. For live event streams, create a WebSocket client and follow WebSocket subscriptions. Its lifecycle and failure rules are separate from this HTTP client.
References: rpc/client.go• L49-58 rpc/client.go• L116-125 rpc/client.go• L141-186 rpc/…/client.go• L61-146
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the typed option fields and the JSON-RPC parameter names aligned for every method family.
- When a result encoding or transaction detail mode changes, update the matching response accessors and the caller-visible conversion rules together.
- Keep parsed transaction instruction envelopes able to represent both parsed JSON objects and string instruction data.
- When transaction message versions change, keep the supported-version option, transaction conversion path, and version-specific response fields aligned.
- Preserve the distinction between an empty successful result,
ErrNotFound,ErrNotConfirmed, and a node-returnedRPCError. - Keep rate-limit wrappers compatible with
JSONRPCClientso they can still be inserted beneathNewWithCustomRPCClient. - Keep HTTP and WebSocket endpoint constants paired for each supported cluster.
- Keep commitment comparison behavior aligned with the supported commitment values and unknown-status handling.
- Preserve both account lookup contracts: typed absence as
ErrNotFoundand raw null preservation throughGetAccountInfoMaybe.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A method requests an encoding or transaction version the caller cannot decode. | The node can reject the request or the typed result cannot produce a transaction. | Set the encoding and MaxSupportedTransactionVersion to match the consumer. |
A parsed transaction request uses processed commitment. | The RPC method rejects the unsupported commitment. | Use confirmed or finalized for parsed transaction reads. |
| A v1 transaction is submitted before SIMD-0385 is active on the target cluster. | The cluster cannot accept the v1 message. | Use a cluster with SIMD-0385 active before building and sending a v1 transaction. |
| A block, transaction, or account is absent at the requested view. | The typed method returns ErrNotConfirmed or ErrNotFound instead of a usable value. | Handle these errors as chain state and retry or query with an appropriate commitment. |
GetAccountInfoMaybe is used when callers expect a non-nil account. | A missing account returns a successful result with Value == nil. | Use GetAccountInfoWithOpts when absence must be an error. |
| A commitment value is unknown. | Commitment comparison treats it as weaker than every known level. | Validate commitment values before using them as status or requirement data. |
SendTransaction returns success before cluster confirmation. | The caller may report a transaction as complete even though it later fails or expires. | Query signature status after submission. |
getTransactionsForAddress is sent to a provider without the extension. | The endpoint returns a method-not-found RPC error. | Confirm provider support before using the method. |
| A context expires while a rate-limited call waits. | NewWithLimiter returns the limiter's context error and sends no RPC request. | Set a deadline that includes expected limiter wait time. |
| A batch response is consumed by position. | Results can be matched to the wrong request because response order is not guaranteed. | Match each response by its JSON-RPC ID. |
Key files
| File | Why to read it |
|---|---|
| rpc/client.go | Defines the high-level client, endpoint construction, shared transport settings, raw access, and client lifecycle. |
| rpc/endpoints.go | Shows the supported cluster endpoint pairs and the separation between HTTP RPC and WebSocket URLs. |
| rpc/…/jsonrpc.go | Defines the JSON-RPC request, response, batch, option, and error contract beneath the typed client. |
| rpc/types.go | Defines shared context, commitment, encoding-related data, account and transaction response shapes, and transaction options. |
| rpc/getAccountInfo.go | Illustrates typed account reads, optional query settings, and the two missing-account contracts. |
| rpc/getBlock.go | Illustrates ledger results, transaction detail choices, encodings, and confirmation handling. |
| rpc/getTransaction.go | Illustrates typed transaction reads, response encodings, and transaction conversion. |
| rpc/getParsedTransaction.go | Shows parsed transaction reads and the mixed parsed or string instruction data returned by JSON-parsed responses. |
| rpc/sendTransaction.go | Shows the typed transaction submission entry point and its acceptance-versus-confirmation boundary. |
| rpc/simulateTransaction.go | Shows the transaction execution preview and its diagnostic result shape. |
| rpc/client-with-ratelimit.go | Shows fixed-rate transport gating. |
| rpc/client-with-ratelimit2.go | Shows context-aware rate and burst control. |
| rpc/…/client.go | Provides the separate WebSocket connection and subscription lifecycle model. |