@solana/rpc-transport-http 2.0.0-experimental.a2fc5a3 → 2.0.0-experimental.a456a18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +228 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -14,4 +14,231 @@
|
|
|
14
14
|
|
|
15
15
|
# @solana/rpc-transport-http
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
This package allows developers to create custom RPC transports. With this library, one can implement highly specialized functionality for leveraging multiple transports, attempting/handling retries, and more.
|
|
18
|
+
|
|
19
|
+
## Functions
|
|
20
|
+
|
|
21
|
+
### `createHttpTransport()`
|
|
22
|
+
|
|
23
|
+
Call this to create a function that conforms to the `RpcTransport` interface (see `@solana/rpc-spec`). You can use that function in your programs to make `POST` requests with headers suitable for sending JSON data to a server.
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { createHttpTransport } from '@solana/rpc-transport-http';
|
|
27
|
+
|
|
28
|
+
const transport = createHttpTransport({ url: 'https://api.mainnet-beta.solana.com' });
|
|
29
|
+
const response = await transport({
|
|
30
|
+
payload: { id: 1, jsonrpc: '2.0', method: 'getSlot' },
|
|
31
|
+
});
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
#### Config
|
|
35
|
+
|
|
36
|
+
##### `dispatcher_NODE_ONLY`
|
|
37
|
+
|
|
38
|
+
In Node environments you can tune how requests are dispatched to the network. Use this config parameter to install a [`undici.Dispatcher`](https://undici.nodejs.org/#/docs/api/Agent) in your transport.
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import { createHttpTransport } from '@solana/rpc-transport-http';
|
|
42
|
+
import { Agent, BalancedPool } from 'undici';
|
|
43
|
+
|
|
44
|
+
// Create a dispatcher that, when called with a special URL, creates a round-robin pool of RPCs.
|
|
45
|
+
const dispatcher = new Agent({
|
|
46
|
+
factory(origin, opts) {
|
|
47
|
+
if (origin === 'https://mypool') {
|
|
48
|
+
const upstreams = [
|
|
49
|
+
'https://api.mainnet-beta.solana.com',
|
|
50
|
+
'https://mainnet.helius-rpc.com',
|
|
51
|
+
'https://several-neat-iguana.quiknode.pro',
|
|
52
|
+
];
|
|
53
|
+
return new BalancedPool(upstreams, {
|
|
54
|
+
...opts,
|
|
55
|
+
bodyTimeout: 60e3,
|
|
56
|
+
headersTimeout: 5e3,
|
|
57
|
+
keepAliveTimeout: 19e3,
|
|
58
|
+
});
|
|
59
|
+
} else {
|
|
60
|
+
return new Pool(origin, opts);
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
const transport = createHttpTransport({
|
|
65
|
+
dispatcher_NODE_ONLY: dispatcher,
|
|
66
|
+
url: 'https://mypool',
|
|
67
|
+
});
|
|
68
|
+
let id = 0;
|
|
69
|
+
const balances = await Promise.allSettled(
|
|
70
|
+
accounts.map(account =>
|
|
71
|
+
transport({
|
|
72
|
+
payload: {
|
|
73
|
+
id: ++id,
|
|
74
|
+
jsonrpc: '2.0',
|
|
75
|
+
method: 'getBalance',
|
|
76
|
+
params: [account],
|
|
77
|
+
},
|
|
78
|
+
}),
|
|
79
|
+
),
|
|
80
|
+
);
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
##### `headers`
|
|
84
|
+
|
|
85
|
+
An object of headers to set on the request. Avoid [forbidden headers](https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name). Additionally, the headers `Accept`, `Content-Length`, and `Content-Type` are disallowed.
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
import { createHttpTransport } from '@solana/rpc-transport-http';
|
|
89
|
+
|
|
90
|
+
const transport = createHttpTransport({
|
|
91
|
+
headers: {
|
|
92
|
+
// Authorize with the RPC using a bearer token
|
|
93
|
+
Authorization: `Bearer ${process.env.RPC_AUTH_TOKEN}`,
|
|
94
|
+
},
|
|
95
|
+
url: 'https://several-neat-iguana.quiknode.pro',
|
|
96
|
+
});
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
##### `url`
|
|
100
|
+
|
|
101
|
+
A string representing the target endpoint. In Node, it must be an absolute URL using the `http` or `https` protocol.
|
|
102
|
+
|
|
103
|
+
## Augmenting Transports
|
|
104
|
+
|
|
105
|
+
Using this core transport, you can implement specialized functionality for leveraging multiple transports, attempting/handling retries, and more.
|
|
106
|
+
|
|
107
|
+
### Round Robin
|
|
108
|
+
|
|
109
|
+
Here’s an example of how someone might implement a “round robin” approach to distribute requests to multiple transports:
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
import { RpcTransport } from '@solana/rpc-spec';
|
|
113
|
+
import { createHttpTransport } from '@solana/rpc-transport-http';
|
|
114
|
+
|
|
115
|
+
// Create a transport for each RPC server
|
|
116
|
+
const transports = [
|
|
117
|
+
createHttpTransport({ url: 'https://mainnet-beta.my-server-1.com' }),
|
|
118
|
+
createHttpTransport({ url: 'https://mainnet-beta.my-server-2.com' }),
|
|
119
|
+
createHttpTransport({ url: 'https://mainnet-beta.my-server-3.com' }),
|
|
120
|
+
];
|
|
121
|
+
|
|
122
|
+
// Create a wrapper transport that distributes requests to them
|
|
123
|
+
let nextTransport = 0;
|
|
124
|
+
async function roundRobinTransport<TResponse>(...args: Parameters<RpcTransport>): Promise<TResponse> {
|
|
125
|
+
const transport = transports[nextTransport];
|
|
126
|
+
nextTransport = (nextTransport + 1) % transports.length;
|
|
127
|
+
return await transport(...args);
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Sharding
|
|
132
|
+
|
|
133
|
+
Another example of a possible customization for a transport is to shard requests deterministically among a set of servers. Here’s an example:
|
|
134
|
+
|
|
135
|
+
Perhaps your application needs to make a large number of requests, or needs to fan request for different methods out to different servers. Here’s an example of an implementation that does the latter:
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
import { RpcTransport } from '@solana/rpc-spec';
|
|
139
|
+
import { createHttpTransport } from '@solana/rpc-transport-http';
|
|
140
|
+
|
|
141
|
+
// Create multiple transports
|
|
142
|
+
const transportA = createHttpTransport({ url: 'https://mainnet-beta.my-server-1.com' });
|
|
143
|
+
const transportB = createHttpTransport({ url: 'https://mainnet-beta.my-server-2.com' });
|
|
144
|
+
const transportC = createHttpTransport({ url: 'https://mainnet-beta.my-server-3.com' });
|
|
145
|
+
const transportD = createHttpTransport({ url: 'https://mainnet-beta.my-server-4.com' });
|
|
146
|
+
|
|
147
|
+
// Function to determine which shard to use based on the request method
|
|
148
|
+
function selectShard(method: string): RpcTransport {
|
|
149
|
+
switch (method) {
|
|
150
|
+
case 'getAccountInfo':
|
|
151
|
+
case 'getBalance':
|
|
152
|
+
return transportA;
|
|
153
|
+
case 'getTransaction':
|
|
154
|
+
case 'getRecentBlockhash':
|
|
155
|
+
return transportB;
|
|
156
|
+
case 'sendTransaction':
|
|
157
|
+
return transportC;
|
|
158
|
+
default:
|
|
159
|
+
return transportD;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
async function shardingTransport<TResponse>(...args: Parameters<RpcTransport>): Promise<TResponse> {
|
|
164
|
+
const payload = args[0].payload as { method: string };
|
|
165
|
+
const selectedTransport = selectShard(payload.method);
|
|
166
|
+
return await selectedTransport(...args);
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### Retry Logic
|
|
171
|
+
|
|
172
|
+
The transport library can also be used to implement custom retry logic on any request:
|
|
173
|
+
|
|
174
|
+
```ts
|
|
175
|
+
import { RpcTransport } from '@solana/rpc-spec';
|
|
176
|
+
import { createHttpTransport } from '@solana/rpc-transport-http';
|
|
177
|
+
|
|
178
|
+
// Set the maximum number of attempts to retry a request
|
|
179
|
+
const MAX_ATTEMPTS = 4;
|
|
180
|
+
|
|
181
|
+
// Create the default transport
|
|
182
|
+
const defaultTransport = createHttpTransport({ url: 'https://mainnet-beta.my-server-1.com' });
|
|
183
|
+
|
|
184
|
+
// Sleep function to wait for a given number of milliseconds
|
|
185
|
+
function sleep(ms: number): Promise<void> {
|
|
186
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Calculate the delay for a given attempt
|
|
190
|
+
function calculateRetryDelay(attempt: number): number {
|
|
191
|
+
// Exponential backoff with a maximum of 1.5 seconds
|
|
192
|
+
return Math.min(100 * Math.pow(2, attempt), 1500);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// A retrying transport that will retry up to `MAX_ATTEMPTS` times before failing
|
|
196
|
+
async function retryingTransport<TResponse>(...args: Parameters<RpcTransport>): Promise<TResponse> {
|
|
197
|
+
let requestError;
|
|
198
|
+
for (let attempts = 0; attempts < MAX_ATTEMPTS; attempts++) {
|
|
199
|
+
try {
|
|
200
|
+
return await defaultTransport(...args);
|
|
201
|
+
} catch (err) {
|
|
202
|
+
requestError = err;
|
|
203
|
+
// Only sleep if we have more attempts remaining
|
|
204
|
+
if (attempts < MAX_ATTEMPTS - 1) {
|
|
205
|
+
const retryDelay = calculateRetryDelay(attempts);
|
|
206
|
+
await sleep(retryDelay);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
throw requestError;
|
|
211
|
+
}
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
### Failover
|
|
215
|
+
|
|
216
|
+
Here’s an example of some failover logic integrated into a transport:
|
|
217
|
+
|
|
218
|
+
```ts
|
|
219
|
+
import { RpcTransport } from '@solana/rpc-spec';
|
|
220
|
+
import { createHttpTransport } from '@solana/rpc-transport-http';
|
|
221
|
+
|
|
222
|
+
// Create a transport for each RPC server
|
|
223
|
+
const transports = [
|
|
224
|
+
createHttpTransport({ url: 'https://mainnet-beta.my-server-1.com' }),
|
|
225
|
+
createHttpTransport({ url: 'https://mainnet-beta.my-server-2.com' }),
|
|
226
|
+
createHttpTransport({ url: 'https://mainnet-beta.my-server-2.com' }),
|
|
227
|
+
];
|
|
228
|
+
|
|
229
|
+
// A failover transport that will try each transport in order until one succeeds before failing
|
|
230
|
+
async function failoverTransport<TResponse>(...args: Parameters<RpcTransport>): Promise<TResponse> {
|
|
231
|
+
let requestError;
|
|
232
|
+
|
|
233
|
+
for (const transport of transports) {
|
|
234
|
+
try {
|
|
235
|
+
return await transport(...args);
|
|
236
|
+
} catch (err) {
|
|
237
|
+
requestError = err;
|
|
238
|
+
console.error(err);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
throw requestError;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@solana/rpc-transport-http",
|
|
3
|
-
"version": "2.0.0-experimental.
|
|
3
|
+
"version": "2.0.0-experimental.a456a18",
|
|
4
4
|
"description": "An RPC transport that uses HTTP requests",
|
|
5
5
|
"exports": {
|
|
6
6
|
"browser": {
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
],
|
|
48
48
|
"dependencies": {
|
|
49
49
|
"undici": "^6.6.2",
|
|
50
|
-
"@solana/rpc-spec": "2.0.0-experimental.
|
|
50
|
+
"@solana/rpc-spec": "2.0.0-experimental.a456a18"
|
|
51
51
|
},
|
|
52
52
|
"bundlewatch": {
|
|
53
53
|
"defaultCompression": "gzip",
|