Introducing a new way to review your PRs.

Read the announcement

solana-foundation / solana-web3.js

Solana JavaScript SDK

View on GitHub

23 documents · Updated Nov 16, 2026

docs / cluster-and-encoding-helpers

Cluster and encoding helpers

Cluster and encoding helpers provide shared rules for network targets, byte values, large numbers, cryptographic checks, timing, validation, and errors. They form the small utility layer that lets connection, transaction, message, account, and program flows use the same data and failure rules.

Cluster endpoint selection

Cluster endpoint selection maps a supported Solana cluster to an RPC URL. A cluster is a network target such as devnet, testnet, or mainnet-beta; HTTPS is the default and HTTP is available only when the caller disables TLS. An omitted cluster selects Devnet (packages/…/cluster.ts• L1-34).

The selected URL gives the connection layer its starting point for JSON-RPC requests. Keeping this choice in one helper gives callers a shared endpoint policy instead of separate cluster rules (packages/…/index.ts• L1-3).

References: packages/…/cluster.ts• L1-34 packages/…/index.ts• L1-3

WebSocket URL derivation

WebSocket URL derivation converts an HTTP RPC endpoint into the endpoint used for subscriptions. The connection can use an explicit WebSocket URL when one is supplied. Otherwise it derives the URL from the RPC endpoint (packages/…/makeWebsocketUrl.ts• L1-21).

The scheme changes from http to ws or from https to wss. An explicit port moves up by one for a direct validator WebSocket port. An endpoint without a port keeps the host and path so a reverse proxy can manage the upgrade.

The endpoint choice moves through the connection flow as shown below.

flowchart LR
    A[RPC endpoint] --> B{WebSocket URL supplied?}
    B -->|Yes| C[Use supplied URL]
    B -->|No| D[Derive WebSocket scheme and port]
    C --> E[Open subscription channel]
    D --> E

References: packages/…/makeWebsocketUrl.ts• L1-21 packages/…/connection.ts• L3204-3248

Byte and numeric conversion

Byte and numeric conversion keeps protocol values safe across runtime types. Typed-array helpers can keep a byte view when its backing range is already suitable, or copy the exact bytes when a packed value is required. This protects hashing and signing from unrelated bytes in a larger backing buffer (packages/…/typed-array.ts• L1-57).

Numeric conversion accepts a JavaScript number only when it is a safe integer. It then converts the value to bigint, or preserves an existing bigint; optional and nullable forms keep their missing-value meaning (packages/…/bigint.ts• L1-30). This prevents large slots, amounts, and other protocol values from losing precision at an API boundary.

References: packages/…/typed-array.ts• L1-57 packages/…/bigint.ts• L1-30

Assertion checks

Assertion checks turn invalid state into a clear error before a value enters signing, serialization, or network work. The assertion also narrows the value for TypeScript after the condition succeeds (packages/…/assert.ts• L1-8).

The check is used for rules that types alone cannot express. These rules include required signer counts, signature sizes, and valid decoded structures (packages/…/versioned.ts• L69-95, packages/…/validator-info.ts• L59-74).

References: packages/…/assert.ts• L1-8 packages/…/versioned.ts• L69-95 packages/…/validator-info.ts• L59-74

Cryptographic utilities

Cryptographic utilities provide the hash and key operations needed to prepare and check signed data. SHA-256 first checks that the runtime supports digest operations, then hashes the exact packed bytes through Web Crypto. Ed25519 utilities check public-key encoding, sign packed message bytes, and verify copied signature, message, and public-key bytes. Secp256k1 utilities create public keys, validate private keys, and return compact ECDSA signatures with their recovery value (packages/…/sha256.ts• L1-20, packages/…/ed25519.ts• L1-49, packages/…/secp256k1.ts• L1-11).

These helpers create a common byte boundary for transaction signing, address checks, and other protocol operations that must agree on the bytes being hashed or signed. They also keep input snapshots separate from later changes to caller-owned arrays during signature verification (packages/…/typed-array.ts• L20-39, packages/…/ed25519.ts• L30-49).

References: packages/…/sha256.ts• L1-20 packages/…/ed25519.ts• L1-49 packages/…/secp256k1.ts• L1-11 packages/…/typed-array.ts• L20-39

Timing helpers

Timing helpers give asynchronous flows a shared chain-time estimate and simple delay behavior. The slot estimate comes from the configured tick and slot rates. sleep pauses a task for a duration, while connection and loader flows use delays to avoid immediate repeated work (packages/…/timing.ts• L1-23, packages/…/sleep.ts• L1-4, packages/…/connection.ts• L5695-5725, packages/…/loader.ts• L164-200).

These values guide polling and pacing. They do not define confirmed chain state, so callers must still use RPC responses for state decisions.

References: packages/…/timing.ts• L1-23 packages/…/sleep.ts• L1-4 packages/…/connection.ts• L5695-5725 packages/…/loader.ts• L164-200

Shared error utilities

Shared error utilities preserve context when an RPC request, simulation, or transaction send fails. SolanaJSONRPCError carries the server code and optional data. A caller can add the failed operation to the message without losing the original server text (packages/…/errors.ts• L106-145).

SendTransactionError represents send and simulation failures with the transaction message and any logs already available. The send-and-confirm helpers also create it when a confirmation status contains an error, preserving the signature and serialized status. If logs are not ready, getLogs can ask a Connection for the transaction and retry until the log messages appear (packages/…/errors.ts• L7-103, packages/…/send-and-confirm-raw-transaction.ts• L82-100, packages/…/send-and-confirm-transaction.ts• L104-112). This gives callers one error path that supports both immediate diagnosis and later detail.

The failure context moves through the client as shown below.

flowchart LR
    A[RPC or transaction action] --> B{Failure type}
    B -->|RPC method| C[SolanaJSONRPCError]
    B -->|Send or simulation| D[SendTransactionError]
    D --> E[Read message and cached logs]
    E --> F[Fetch full logs from Connection]

References: packages/…/errors.ts• L7-103 packages/…/errors.ts• L106-145 packages/…/send-and-confirm-raw-transaction.ts• L82-100 packages/…/send-and-confirm-transaction.ts• L104-112 packages/…/connection.ts• L3309-3317 packages/…/connection.ts• L5898-5919 packages/…/connection.ts• L6044-6060

  • Network and protocol utilities places these helpers in the wider network and protocol layer.
  • Connection and RPC explains how endpoint, timing, and error helpers support RPC work.
  • Keys and signatures explains the key and signature flows that use the cryptographic helpers.
  • Transaction send and confirm explains the send, confirmation, and failure flow that uses the shared errors.
  • Platform runtime forks explains runtime-specific network behavior that works beside these shared helpers.

Change checklist

When these files or sections are changed, remember to consider these:

  • A cluster or endpoint-scheme change must stay aligned with both the public cluster helper and WebSocket URL derivation.
  • A change to typed-array packing must be checked at every hash and signature boundary so a backing buffer does not add unrelated bytes.
  • A change from number to bigint or back must preserve precision at RPC, program, and account-state boundaries.
  • A change to assertion rules must preserve the protocol invariants expected by transaction and decoded-data consumers.
  • A change to digest or signature support must preserve runtime capability checks and exact byte inputs.
  • A change to slot timing or sleep behavior must be checked against polling and pacing flows that use the shared estimate.
  • A change to transaction confirmation or error data must preserve immediate status context and later log retrieval.

Important failure modes

TriggerConsequenceGuard
An unsupported cluster reaches endpoint selectionThe connection has no valid RPC targetThe cluster type and runtime lookup restrict the supported names (packages/…/cluster.ts• L14-34)
A malformed endpoint reaches WebSocket derivationThe subscription channel cannot be createdURL matching rejects an endpoint without a valid scheme and host (packages/…/makeWebsocketUrl.ts• L1-7)
A proxy endpoint receives an automatic port shiftWebSocket attempts reach the wrong portPort shifting occurs only for an explicitly supplied RPC port (packages/…/makeWebsocketUrl.ts• L10-20)
A sliced byte view enters a hash or signature operation without packingUnrelated bytes can change the digest or signaturePacked conversion copies views that do not cover their full backing buffer (packages/…/typed-array.ts• L20-39)
An unsafe number represents a large protocol valueAmount or slot precision is lostNumeric coercion accepts numbers only when they are safe integers (packages/…/bigint.ts• L3-15)
The runtime lacks SHA-256 digest supportHashing cannot produce a digestThe helper checks digest capability before calling Web Crypto (packages/…/sha256.ts• L12-19)
A confirmed transaction status contains an errorThe caller receives no successful signature resultSend-and-confirm helpers raise SendTransactionError with the signature and serialized status (packages/…/send-and-confirm-raw-transaction.ts• L87-100, packages/…/send-and-confirm-transaction.ts• L101-112)
A transaction fails before logs are availableThe caller lacks execution detailSendTransactionError.getLogs retries transaction lookup and stores the result (packages/…/errors.ts• L76-103)

Key files

FileWhy to read it
packages/…/cluster.tsDefines supported cluster targets and the HTTP or HTTPS endpoint policy.
packages/…/makeWebsocketUrl.tsDefines how an RPC URL becomes a subscription URL.
packages/…/typed-array.tsDefines safe views, packed byte input, and byte concatenation.
packages/…/bigint.tsDefines safe conversion of numeric API values to bigint.
packages/…/assert.tsDefines the shared invariant check used before protocol work.
packages/…/sha256.ts, ed25519.ts, and secp256k1.tsDefine shared hashing, signing, verification, and key checks.
packages/…/sleep.ts and packages/…/timing.tsDefine async delay behavior and chain-time estimates.
packages/…/errors.tsDefines shared RPC and transaction failure context.
packages/…/send-and-confirm-raw-transaction.ts and send-and-confirm-transaction.tsShow how transaction status failures become shared send errors after confirmation.
packages/…/connection.tsShows how endpoint selection, timing, and errors join the RPC flow.
packages/…/versioned.tsShows assertion and signature rules at the transaction boundary.