Skip to content

Commit f71c133

Browse files
committed
Update the README for @solana/rpc
1 parent b7e6465 commit f71c133

File tree

6 files changed

+116
-6
lines changed

6 files changed

+116
-6
lines changed

packages/rpc/README.md

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

1515
# @solana/rpc
1616

17-
TODO
17+
This package contains utilities for creating objects that you can use to communicate with a Solana JSON RPC server. 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+
Unless you plan to create a custom RPC interface, you can use the [`createSolanaRpc(clusterUrl)`](#createsolanarpcclusterurl-config) function to obtain a default implementation of the [Solana JSON RPC API](https://solana.com/docs/rpc/http).
20+
21+
## Types
22+
23+
### `RpcTransport{Devnet|Testnet|Mainnet}`
24+
25+
These types refine the base `RpcTransport` type. Each describes a transport that is specific in some way to a particular Solana cluster.
26+
27+
For instance, a `RpcTransportDevnet` is understood to communicate with a RPC server related to devnet, and as such might only be accepted for use as the transport of a `RpcDevnet`.
28+
29+
This is useful in cases where you need to make assertions about what capabilities a RPC offers. For example, RPC methods like `requestAirdrop` are not available on mainnet. You can use the ability to assert on the type of RPC transport at compile time to prevent calling unimplemented methods or presuming the existence of unavailable capabilities.
30+
31+
### `RpcTransportFromClusterUrl<TClusterUrl extends ClusterUrl>`
32+
33+
Given a `ClusterUrl`, this utility type will resolve to as specific a `RpcTransport` as possible.
34+
35+
```ts
36+
function createCustomTransport<TClusterUrl extends ClusterUrl>(
37+
clusterUrl: TClusterUrl,
38+
): RpcTransportFromClusterUrl<TClusterUrl> {
39+
/* ... */
40+
}
41+
const transport = createCustomTransport(testnet('http://api.testnet.solana.com'));
42+
transport satisfies RpcTransportTestnet; // OK
43+
```
44+
45+
### `Rpc{Devnet|Testnet|Mainnet}<TRpcMethods>`
46+
47+
These types refine the base `Rpc` type. Each describes a RPC that is specific in some way to a particular Solana cluster and a corpus of RPC methods.
48+
49+
This is useful in cases where you need to make assertions about the suitablilty of a RPC for a given purpose. For example, you might like to make it a type error to combine certain types with RPC belonging to certain clusters, at compile time.
50+
51+
```ts
52+
async function getSpecialAccountInfo(
53+
address: Address<'ReAL1111111111111111111111111111'>,
54+
rpc: RpcMainnet,
55+
): Promise<SpecialAccountInfo>;
56+
async function getSpecialAccountInfo(
57+
address: Address<'TeST1111111111111111111111111111'>,
58+
rpc: RpcDevnet | RpcTestnet,
59+
): Promise<SpecialAccountInfo>;
60+
async function getSpecialAccountInfo(address: Address, rpc: Rpc): Promise<SpecialAccountInfo> {
61+
/* ... */
62+
}
63+
const rpc = createSolanaRpc(devnet('https://api.devnet.solana.com'));
64+
await getSpecialAccountInfo(address('ReAL1111111111111111111111111111'), rpc); // ERROR
65+
```
66+
67+
### `RpcFromTransport<TRpcMethods, TRpcTransport extends RpcTransport>`
68+
69+
Given a `RpcTransport`, this utility type will resolve to as specific a `Rpc` as possible.
70+
71+
```ts
72+
function createCustomRpc<TRpcTransport extends RpcTransport>(
73+
transport: TRpcTransport,
74+
): RpcFromTransport<MyCustomRpcMethods, TRpcTransport> {
75+
/* ... */
76+
}
77+
const transport = createDefaultRpcTransport({ url: mainnet('http://rpc.company') });
78+
transport satisfies RpcTransportMainnet; // OK
79+
const rpc = createCustomRpc(transport);
80+
rpc satisfies RpcMainnet<MyCustomRpcMethods>; // OK
81+
```
82+
83+
### SolanaRpcApiFromTransport<TTransport extends RpcTransport>
84+
85+
## Constants
86+
87+
### `DEFAULT_RPC_CONFIG`
88+
89+
When you create `Rpc` instances with custom transports but otherwise the default RPC API behaviours, use this.
90+
91+
```ts
92+
const myCustomRpc = createRpc({
93+
api: createSolanaRpcApi(DEFAULT_RPC_CONFIG),
94+
transport: myCustomTransport,
95+
});
96+
```
97+
98+
## Functions
99+
100+
### `createDefaultRpcTransport(config)`
101+
102+
Creates a `RpcTransport` with some default behaviours.
103+
104+
The default behaviours include:
105+
106+
- An automatically-set `Solana-Client` request header, containing the version of `@solana/web3.js`
107+
- Logic that coalesces multiple calls in the same runloop, for the same methods with the same arguments, into a single network request.
108+
109+
#### Arguments
110+
111+
A config object with the following properties:
112+
113+
- `dispatcher_NODE_ONLY`: An optional `Undici.Dispatcher` instance that governs how the networking stack should behave. This option is only relevant in Node applications. Consult the documentation for the various subclasses of `Undici.Dispatcher`, such as `Agent`, `Client`, and `Pool`, at https://undici.nodejs.org/#/docs/api/Client.
114+
- `headers`: An optional object where the keys are HTTP header names and the values are HTTP header values. This parameter is typed to disallow certain headers from being overwritten.
115+
- `url`: A `ClusterUrl` at which the RPC server can be contacted.
116+
117+
### `createSolanaRpc(clusterUrl, config)`
118+
119+
Creates a `Rpc` instance that exposes the Solana JSON RPC API given a cluster URL and some optional transport config. See `createDefaultRpcTransport` for the shape of the transport config.
120+
121+
### `createSolanaRpcFromTransport(transport)`
122+
123+
Creates a `Rpc` instance that exposes the Solana JSON RPC API given the supplied `RpcTransport`.

packages/rpc/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ export * from '@solana/rpc-api';
22
export * from '@solana/rpc-spec';
33

44
export * from './rpc';
5+
export * from './rpc-default-config';
56
export * from './rpc-clusters';
67
export * from './rpc-transport';

packages/rpc/src/rpc-clusters.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import type { ClusterUrl, DevnetUrl, MainnetUrl, TestnetUrl } from '@solana/rpc-
55
export type RpcTransportDevnet = RpcTransport & { '~cluster': 'devnet' };
66
export type RpcTransportTestnet = RpcTransport & { '~cluster': 'testnet' };
77
export type RpcTransportMainnet = RpcTransport & { '~cluster': 'mainnet' };
8-
export type RpcTransportWithCluster = RpcTransportDevnet | RpcTransportMainnet | RpcTransportTestnet;
98
export type RpcTransportFromClusterUrl<TClusterUrl extends ClusterUrl> = TClusterUrl extends DevnetUrl
109
? RpcTransportDevnet
1110
: TClusterUrl extends TestnetUrl

packages/rpc/src/rpc-default-config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import type { createSolanaRpcApi } from '@solana/rpc-api';
22

33
import { createSolanaJsonRpcIntegerOverflowError } from './rpc-integer-overflow-error';
44

5-
export const DEFAULT_RPC_CONFIG: Partial<Parameters<typeof createSolanaRpcApi>[0]> = {
5+
export const DEFAULT_RPC_CONFIG: Partial<NonNullable<Parameters<typeof createSolanaRpcApi>[0]>> = {
66
defaultCommitment: 'confirmed',
77
onIntegerOverflow(methodName, keyPath, value) {
88
throw createSolanaJsonRpcIntegerOverflowError(methodName, keyPath, value);
99
},
10-
};
10+
} as const;

packages/rpc/src/rpc-transport.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { getRpcTransportWithRequestCoalescing } from './rpc-request-coalescer';
77
import { getSolanaRpcPayloadDeduplicationKey } from './rpc-request-deduplication';
88

99
type RpcTransportConfig = Parameters<typeof createHttpTransport>[0];
10-
export interface DefaultRpcTransportConfig<TClusterUrl extends ClusterUrl> extends RpcTransportConfig {
10+
interface DefaultRpcTransportConfig<TClusterUrl extends ClusterUrl> extends RpcTransportConfig {
1111
url: TClusterUrl;
1212
}
1313

packages/rpc/src/rpc.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import { ClusterUrl } from '@solana/rpc-types';
44

55
import type { RpcFromTransport, SolanaRpcApiFromTransport } from './rpc-clusters';
66
import { DEFAULT_RPC_CONFIG } from './rpc-default-config';
7-
import { createDefaultRpcTransport, DefaultRpcTransportConfig } from './rpc-transport';
7+
import { createDefaultRpcTransport } from './rpc-transport';
8+
9+
type DefaultRpcTransportConfig<TClusterUrl extends ClusterUrl> = Parameters<
10+
typeof createDefaultRpcTransport<TClusterUrl>
11+
>[0];
812

913
/** Creates a new Solana RPC using the default decorated HTTP transport. */
1014
export function createSolanaRpc<TClusterUrl extends ClusterUrl>(

0 commit comments

Comments
 (0)