Skip to content

Commit 1285419

Browse files
committed
Update the README for @solana/rpc-spec
1 parent d81069a commit 1285419

File tree

1 file changed

+102
-1
lines changed

1 file changed

+102
-1
lines changed

packages/rpc-spec/README.md

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,105 @@
1414

1515
# @solana/rpc-spec
1616

17-
TODO
17+
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@experimental`](https://github.com/solana-labs/solana-web3.js/tree/master/packages/library).
18+
19+
This API is designed to be used as follows:
20+
21+
```ts
22+
const rpc =
23+
// Step 1 - Create an `Rpc` instance. This may be stateful.
24+
createSolanaRpc(mainnet('https://api.mainnet-beta.solana.com'));
25+
const response = await rpc
26+
// Step 2 - Call supported methods on it to produce `PendingRpcRequest` objects.
27+
.getLatestBlockhash({ commitment: 'confirmed' })
28+
// Step 3 - Call the `send()` method on those pending requests to trigger them.
29+
.send({ abortSignal: AbortSignal.timeout(10_000) });
30+
```
31+
32+
## Types
33+
34+
### `PendingRpcRequest<TResponse>`
35+
36+
Pending requests are the result of calling a supported method on on `Rpc` object. They encapsulate all of the information necessary to make the request without actually making it.
37+
38+
Calling the `send(options)` method on a `PendingRpcRequest` will trigger the request and return a promise for `TResponse`.
39+
40+
### `Rpc<TRpcMethods, TRpcTransport>`
41+
42+
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.
43+
44+
### `RpcApi<TRpcMethods>`
45+
46+
For each of `TRpcMethods` this object exposes a method with the same name that maps between its input arguments and a `RpcRequest<TResponse>` that describes how to prepare a JSON RPC request to fetch `TResponse`.
47+
48+
### `RpcApiMethods`
49+
50+
This is a marker interface that all RPC method definitions must extend to be accepted for use with the `RpcApi` creator.
51+
52+
### `RpcRequest`
53+
54+
This type describes 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:
55+
56+
- customize the JSON RPC method name in the case that it's different than the name of that function
57+
- define the shape of the JSON RPC params in case they are different than the arguments provided to that function
58+
- 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.
59+
60+
### `RpcSendOptions`
61+
62+
A configuration object consisting of the following properties:
63+
64+
- `abortSignal`: An optional signal that you can supply when triggering a `PendingRpcRequest` that you might later need to abort.
65+
66+
### `RpcTransport`
67+
68+
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:
69+
70+
- `payload`: A value of arbitrary type to be sent.
71+
- `signal`: An optional `AbortSignal` on which the `'abort'` event will be fired if the request should be cancelled.
72+
73+
## Functions
74+
75+
### `createRpc(config)`
76+
77+
Creates an RPC instance given an `RpcApi<TRpcMethods>` and a `RpcTransport` capable of fulfilling them.
78+
79+
#### Arguments
80+
81+
A config object with the following properties:
82+
83+
- `api`: An instance of `RpcApi`
84+
- `transport`: A function that implements the `RpcTransport` interface
85+
86+
### `createRpcApi(config)`
87+
88+
Creates a JavaScript proxy that converts _any_ function call called on it to a `RpcRequest` by:
89+
90+
- setting `methodName` to the name of the function called
91+
- setting `params` to the arguments supplied to that function, optionally transformed by `config.parametersTransformer`
92+
- setting `responseTransformer` to `config.responseTransformer` or the identity function if no such config exists
93+
94+
```ts
95+
// For example, given this `RpcApi`:
96+
const rpcApi = createRpcApi({
97+
paramsTransformer: (...rawParams) => rawParams.reverse(),
98+
responseTransformer: response => response.result,
99+
});
100+
101+
// ...the following function call:
102+
rpcApi.foo('bar', { baz: 'bat' });
103+
104+
// ...will produce the following `RpcRequest` object:
105+
//
106+
// {
107+
// methodName: 'foo',
108+
// params: [{ baz: 'bat' }, 'bar'],
109+
// responseTransformer: (response) => response.result,
110+
// }
111+
```
112+
113+
#### Arguments
114+
115+
A config object with the following properties:
116+
117+
- `parametersTransformer<T>(params: T, methodName): unknown`: An optional function that maps between the shape of the arguments an RPC method was called with and the shape of the params expected by the JSON RPC server.
118+
- `responseTransformer<T>(response, methodName): T`: An optional function that maps between the shape of the JSON RPC server response for a given method and the shape of the response expected by the `RpcApi`.

0 commit comments

Comments
 (0)