Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@solana/rpc-spec

Package Overview
Dependencies
Maintainers
14
Versions
896
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@solana/rpc-spec

A generic implementation of JSON RPCs using proxies

  • 2.0.0-canary-20241016092828
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.6K
decreased by-62.37%
Maintainers
14
Weekly downloads
 
Created
Source

npm npm-downloads semantic-release
code-style-prettier

@solana/rpc-spec

This package contains types that describe the implementation of the JSON RPC API, as well as methods to create one. It can be used standalone, but it is also exported as part of the Solana JavaScript SDK @solana/web3.js@rc.

This API is designed to be used as follows:

const rpc =
    // Step 1 - Create an `Rpc` instance. This may be stateful.
    createSolanaRpc(mainnet('https://api.mainnet-beta.solana.com'));
const response = await rpc
    // Step 2 - Call supported methods on it to produce `PendingRpcRequest` objects.
    .getLatestBlockhash({ commitment: 'confirmed' })
    // Step 3 - Call the `send()` method on those pending requests to trigger them.
    .send({ abortSignal: AbortSignal.timeout(10_000) });

Types

PendingRpcRequest<TResponse>

Pending requests are the result of calling a supported method on an Rpc object. They encapsulate all of the information necessary to make the request without actually making it.

Calling the send(options) method on a PendingRpcRequest will trigger the request and return a promise for TResponse.

Rpc<TRpcMethods, TRpcTransport>

An object that exposes all of the functions described by TRpcMethods, and fulfils them using TRpcTransport. Calling each method returns a PendingRpcRequest<TResponse> where TResponse is that method's response type.

RpcRequest

An object that describes the elements of a JSON RPC request. It consists of the following properties:

  • methodName: The name of the JSON RPC method to be called.
  • params: The parameters to be passed to the JSON RPC method.

RpcRequestTransformer

A function that accepts an RpcRequest and returns another RpcRequest. This allows the RpcApi to transform the request before it is sent to the JSON RPC server.

RpcResponse

A type that represents the response from a JSON RPC server. This could be any sort of data which is why RpcResponse defaults to unknown. You may use a type parameter to specify the shape of the response — e.g. RpcResponse<{ result: number }>.

RpcResponseTransformer

A function that accepts an RpcResponse and returns another RpcResponse. This allows the RpcApi to transform the response before it is returned to the caller.

RpcApi<TRpcMethods>

For each of TRpcMethods this object exposes a method with the same name that maps between its input arguments and a RpcApiRequestPlan<TResponse> that describes how to prepare a JSON RPC request to fetch TResponse.

RpcApiMethods

This is a marker interface that all RPC method definitions must extend to be accepted for use with the RpcApi creator.

RpcApiRequestPlan

This type allows an RpcApi to describe how a particular request should be issued to the JSON RPC server. Given a function that was called on a Rpc, this object gives you the opportunity to:

  • provide a payload from the requested method name and parameters.
  • provide a function to transform the JSON RPC server's response, in case it does not match the TResponse specified by the PendingRpcRequest<TResponse> returned from that function.

RpcSendOptions

A configuration object consisting of the following properties:

  • abortSignal: An optional signal that you can supply when triggering a PendingRpcRequest that you might later need to abort.

RpcTransport

Any function that implements this interface can act as a transport for an Rpc. It need only return a promise for a response given the following config:

  • payload: A value of arbitrary type to be sent.
  • signal: An optional AbortSignal on which the 'abort' event will be fired if the request should be cancelled.

Functions

createRpc(config)

Creates an RPC instance given an RpcApi<TRpcMethods> and a RpcTransport capable of fulfilling them.

Arguments

A config object with the following properties:

  • api: An instance of RpcApi
  • transport: A function that implements the RpcTransport interface

createJsonRpcApi(config)

Creates a JavaScript proxy that converts any function call called on it to a RpcApiRequestPlan by:

  • setting payload to a JSON RPC v2 payload object with the requested methodName and params properties, optionally transformed by config.requestTransformer.
  • setting responseTransformer to config.responseTransformer, if provided.
// For example, given this `RpcApi`:
const rpcApi = createJsonRpcApi({
    requestTransformer: (...rawParams) => rawParams.reverse(),
    responseTransformer: response => response.result,
});

// ...the following function call:
rpcApi.foo('bar', { baz: 'bat' });

// ...will produce the following `RpcApiRequestPlan` object:
//
//     {
//         payload: { id: 1, jsonrpc: '2.0', method: 'foo', params: ['bar', { baz: 'bat' }] },
//         responseTransformer: (response) => response.result,
//     }
Arguments

A config object with the following properties:

  • requestTransformer<T>(request: RpcRequest<T>): RpcRequest: An optional function that transforms the RpcRequest before it is sent to the JSON RPC server.
  • responseTransformer<T>(response: RpcResponse, request: RpcRequest): RpcResponse<T>: An optional function that transforms the RpcResponse before it is returned to the caller.

isJsonRpcPayload(payload)

A helper function that returns true if the given payload is a JSON RPC v2 payload. This means, the payload is an object such that:

  • It has a jsonrpc property with a value of '2.0'.
  • It has a method property that is a string.
  • It has a params property of any type.
import { isJsonRpcPayload } from '@solana/rpc-spec';

if (isJsonRpcPayload(payload)) {
    const payloadMethod: string = payload.method;
    const payloadParams: unknown = payload.params;
}

Keywords

FAQs

Package last updated on 16 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc