@percolatorct/sdk 1.0.0-beta.14 → 1.0.0-beta.16

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.
@@ -0,0 +1,347 @@
1
+ import { Connection, type Commitment, type ConnectionConfig } from "@solana/web3.js";
2
+ /**
3
+ * Configuration for exponential-backoff retry on RPC calls.
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * const retryConfig: RetryConfig = {
8
+ * maxRetries: 3,
9
+ * baseDelayMs: 500,
10
+ * maxDelayMs: 10_000,
11
+ * retryableStatusCodes: [429, 502, 503],
12
+ * };
13
+ * ```
14
+ */
15
+ export interface RetryConfig {
16
+ /**
17
+ * Maximum number of retry attempts after the initial request fails.
18
+ * @default 3
19
+ */
20
+ maxRetries?: number;
21
+ /**
22
+ * Base delay in ms for exponential backoff.
23
+ * Delay for attempt N is: `min(baseDelayMs * 2^N, maxDelayMs) + jitter`.
24
+ * @default 500
25
+ */
26
+ baseDelayMs?: number;
27
+ /**
28
+ * Maximum delay in ms (backoff cap).
29
+ * @default 10_000
30
+ */
31
+ maxDelayMs?: number;
32
+ /**
33
+ * Jitter factor (0–1). Applied as random `[0, jitterFactor * delay]` addition.
34
+ * @default 0.25
35
+ */
36
+ jitterFactor?: number;
37
+ /**
38
+ * HTTP status codes considered retryable.
39
+ * Errors matching these codes (or containing their string representation)
40
+ * will be retried.
41
+ * @default [429, 502, 503, 504]
42
+ */
43
+ retryableStatusCodes?: number[];
44
+ }
45
+ /**
46
+ * Configuration for a single RPC endpoint in the pool.
47
+ *
48
+ * @example
49
+ * ```ts
50
+ * const endpoint: RpcEndpointConfig = {
51
+ * url: "https://mainnet.helius-rpc.com/?api-key=YOUR_KEY",
52
+ * weight: 10,
53
+ * label: "helius-primary",
54
+ * };
55
+ * ```
56
+ */
57
+ export interface RpcEndpointConfig {
58
+ /** RPC endpoint URL. */
59
+ url: string;
60
+ /**
61
+ * Relative weight for round-robin selection.
62
+ * Higher weight = more requests routed here.
63
+ * @default 1
64
+ */
65
+ weight?: number;
66
+ /**
67
+ * Human-readable label for logging / diagnostics.
68
+ * @default url hostname
69
+ */
70
+ label?: string;
71
+ /**
72
+ * Extra `ConnectionConfig` options (commitment, confirmTransactionInitialTimeout, etc.)
73
+ * merged into the Solana `Connection` constructor for this endpoint.
74
+ */
75
+ connectionConfig?: ConnectionConfig;
76
+ }
77
+ /**
78
+ * Strategy for selecting the next RPC endpoint from the pool.
79
+ *
80
+ * - `"round-robin"` — weighted round-robin across healthy endpoints.
81
+ * - `"failover"` — use the first healthy endpoint; only advance on failure.
82
+ */
83
+ export type SelectionStrategy = "round-robin" | "failover";
84
+ /**
85
+ * Full configuration for the RPC connection pool.
86
+ *
87
+ * @example
88
+ * ```ts
89
+ * import { RpcPool } from "@percolator/sdk";
90
+ *
91
+ * const pool = new RpcPool({
92
+ * endpoints: [
93
+ * { url: "https://mainnet.helius-rpc.com/?api-key=KEY", weight: 10, label: "helius" },
94
+ * { url: "https://api.mainnet-beta.solana.com", weight: 1, label: "public" },
95
+ * ],
96
+ * strategy: "failover",
97
+ * retry: { maxRetries: 3, baseDelayMs: 500 },
98
+ * requestTimeoutMs: 30_000,
99
+ * });
100
+ *
101
+ * // Use like a Connection — same surface
102
+ * const slot = await pool.call(conn => conn.getSlot());
103
+ * ```
104
+ */
105
+ export interface RpcPoolConfig {
106
+ /**
107
+ * One or more RPC endpoints. At least one is required.
108
+ * If a bare `string[]` is passed, each string is treated as `{ url: string }`.
109
+ */
110
+ endpoints: (RpcEndpointConfig | string)[];
111
+ /**
112
+ * How to pick the next endpoint.
113
+ * @default "failover"
114
+ */
115
+ strategy?: SelectionStrategy;
116
+ /**
117
+ * Retry config applied to every `call()`.
118
+ * Set to `false` to disable retries entirely.
119
+ * @default { maxRetries: 3, baseDelayMs: 500 }
120
+ */
121
+ retry?: RetryConfig | false;
122
+ /**
123
+ * Per-request timeout in ms. Applies an `AbortSignal` timeout to `Connection`
124
+ * calls where supported, and is used as a deadline for the health probe.
125
+ * @default 30_000
126
+ */
127
+ requestTimeoutMs?: number;
128
+ /**
129
+ * Default Solana commitment level for connections.
130
+ * @default "confirmed"
131
+ */
132
+ commitment?: Commitment;
133
+ /**
134
+ * If true, `console.warn` diagnostic messages on retries, failovers, etc.
135
+ * @default true
136
+ */
137
+ verbose?: boolean;
138
+ }
139
+ /**
140
+ * Result of an RPC health probe.
141
+ *
142
+ * @example
143
+ * ```ts
144
+ * import { checkRpcHealth } from "@percolator/sdk";
145
+ *
146
+ * const health = await checkRpcHealth("https://api.mainnet-beta.solana.com");
147
+ * console.log(`Slot: ${health.slot}, Latency: ${health.latencyMs}ms`);
148
+ * if (!health.healthy) console.warn(`Unhealthy: ${health.error}`);
149
+ * ```
150
+ */
151
+ export interface RpcHealthResult {
152
+ /** The endpoint that was probed. */
153
+ endpoint: string;
154
+ /** Whether the probe succeeded (getSlot returned without error). */
155
+ healthy: boolean;
156
+ /** Round-trip latency in milliseconds (0 if unhealthy). */
157
+ latencyMs: number;
158
+ /** Current slot height (0 if unhealthy). */
159
+ slot: number;
160
+ /** Error message if the probe failed. */
161
+ error?: string;
162
+ }
163
+ /**
164
+ * Probe an RPC endpoint's health by calling `getSlot()` and measuring latency.
165
+ *
166
+ * @param endpoint - RPC URL to probe
167
+ * @param timeoutMs - Timeout in ms for the probe request (default: 5000)
168
+ * @returns Health result with latency and slot height
169
+ *
170
+ * @example
171
+ * ```ts
172
+ * import { checkRpcHealth } from "@percolator/sdk";
173
+ *
174
+ * const result = await checkRpcHealth("https://api.mainnet-beta.solana.com", 3000);
175
+ * if (result.healthy) {
176
+ * console.log(`Slot ${result.slot} — ${result.latencyMs}ms`);
177
+ * } else {
178
+ * console.error(`RPC down: ${result.error}`);
179
+ * }
180
+ * ```
181
+ */
182
+ export declare function checkRpcHealth(endpoint: string, timeoutMs?: number): Promise<RpcHealthResult>;
183
+ /** Resolved defaults for RetryConfig. */
184
+ interface ResolvedRetryConfig {
185
+ maxRetries: number;
186
+ baseDelayMs: number;
187
+ maxDelayMs: number;
188
+ jitterFactor: number;
189
+ retryableStatusCodes: number[];
190
+ }
191
+ declare function resolveRetryConfig(cfg?: RetryConfig | false): ResolvedRetryConfig | null;
192
+ declare function normalizeEndpoint(ep: RpcEndpointConfig | string): RpcEndpointConfig;
193
+ declare function endpointLabel(ep: RpcEndpointConfig): string;
194
+ declare function isRetryable(err: unknown, codes: number[]): boolean;
195
+ declare function computeDelay(attempt: number, config: ResolvedRetryConfig): number;
196
+ /**
197
+ * RPC connection pool with retry, failover, and round-robin support.
198
+ *
199
+ * Wraps one or more Solana RPC endpoints behind a single `call()` interface
200
+ * that automatically retries transient errors and fails over to alternate
201
+ * endpoints when one goes down.
202
+ *
203
+ * @example
204
+ * ```ts
205
+ * import { RpcPool } from "@percolator/sdk";
206
+ *
207
+ * const pool = new RpcPool({
208
+ * endpoints: [
209
+ * { url: "https://mainnet.helius-rpc.com/?api-key=KEY", weight: 10, label: "helius" },
210
+ * { url: "https://api.mainnet-beta.solana.com", weight: 1, label: "public" },
211
+ * ],
212
+ * strategy: "failover",
213
+ * retry: { maxRetries: 3 },
214
+ * requestTimeoutMs: 30_000,
215
+ * });
216
+ *
217
+ * // Execute any Connection method through the pool
218
+ * const slot = await pool.call(conn => conn.getSlot());
219
+ *
220
+ * // Or get a raw connection for one-off use
221
+ * const conn = pool.getConnection();
222
+ *
223
+ * // Health check all endpoints
224
+ * const results = await pool.healthCheck();
225
+ * ```
226
+ */
227
+ export declare class RpcPool {
228
+ private readonly endpoints;
229
+ private readonly strategy;
230
+ private readonly retryConfig;
231
+ private readonly requestTimeoutMs;
232
+ private readonly verbose;
233
+ /** Round-robin index tracker. */
234
+ private rrIndex;
235
+ /** Consecutive failure threshold before marking an endpoint unhealthy. */
236
+ private static readonly UNHEALTHY_THRESHOLD;
237
+ /** Minimum endpoints before auto-recovery is attempted. */
238
+ private static readonly MIN_HEALTHY;
239
+ constructor(config: RpcPoolConfig);
240
+ /**
241
+ * Execute a function against a pooled connection with automatic retry
242
+ * and failover.
243
+ *
244
+ * @param fn - Async function that receives a `Connection` and returns a result.
245
+ * @returns The result of `fn`.
246
+ * @throws The last error if all retries and failovers are exhausted.
247
+ *
248
+ * @example
249
+ * ```ts
250
+ * const balance = await pool.call(c => c.getBalance(pubkey));
251
+ * const markets = await pool.call(c => discoverMarkets(c, programId, opts));
252
+ * ```
253
+ */
254
+ call<T>(fn: (connection: Connection) => Promise<T>): Promise<T>;
255
+ /**
256
+ * Get a raw `Connection` from the current preferred endpoint.
257
+ * Useful when you need to pass a Connection to external code.
258
+ *
259
+ * NOTE: This bypasses retry and failover logic. Prefer `call()`.
260
+ *
261
+ * @returns Solana Connection from the current preferred endpoint.
262
+ *
263
+ * @example
264
+ * ```ts
265
+ * const conn = pool.getConnection();
266
+ * const balance = await conn.getBalance(pubkey);
267
+ * ```
268
+ */
269
+ getConnection(): Connection;
270
+ /**
271
+ * Run a health check against all endpoints in the pool.
272
+ *
273
+ * @param timeoutMs - Per-endpoint probe timeout (default: 5000)
274
+ * @returns Array of health results, one per endpoint.
275
+ *
276
+ * @example
277
+ * ```ts
278
+ * const results = await pool.healthCheck();
279
+ * for (const r of results) {
280
+ * console.log(`${r.endpoint}: ${r.healthy ? 'UP' : 'DOWN'} (${r.latencyMs}ms, slot ${r.slot})`);
281
+ * }
282
+ * ```
283
+ */
284
+ healthCheck(timeoutMs?: number): Promise<RpcHealthResult[]>;
285
+ /**
286
+ * Get the number of endpoints in the pool.
287
+ */
288
+ get size(): number;
289
+ /**
290
+ * Get the number of currently healthy endpoints.
291
+ */
292
+ get healthyCount(): number;
293
+ /**
294
+ * Get endpoint labels and their current status.
295
+ *
296
+ * @returns Array of `{ label, url, healthy, failures, lastLatencyMs }`.
297
+ */
298
+ status(): Array<{
299
+ label: string;
300
+ url: string;
301
+ healthy: boolean;
302
+ failures: number;
303
+ lastLatencyMs: number;
304
+ }>;
305
+ /**
306
+ * Select the next endpoint based on strategy.
307
+ * Returns -1 if no endpoint is available.
308
+ */
309
+ private selectEndpoint;
310
+ /**
311
+ * If all endpoints are unhealthy, reset them so we at least try again.
312
+ */
313
+ private maybeRecoverEndpoints;
314
+ }
315
+ /**
316
+ * Execute an async function with exponential-backoff retry.
317
+ *
318
+ * Use this when you already have a `Connection` and just want retry logic
319
+ * without a full pool.
320
+ *
321
+ * @param fn - Async function to execute
322
+ * @param config - Retry configuration (default: 3 retries, 500ms base delay)
323
+ * @returns Result of `fn`
324
+ * @throws The last error if all retries are exhausted
325
+ *
326
+ * @example
327
+ * ```ts
328
+ * import { withRetry } from "@percolator/sdk";
329
+ * import { Connection } from "@solana/web3.js";
330
+ *
331
+ * const conn = new Connection("https://api.mainnet-beta.solana.com");
332
+ * const slot = await withRetry(
333
+ * () => conn.getSlot(),
334
+ * { maxRetries: 3, baseDelayMs: 1000 },
335
+ * );
336
+ * ```
337
+ */
338
+ export declare function withRetry<T>(fn: () => Promise<T>, config?: RetryConfig): Promise<T>;
339
+ /** @internal — exposed for unit tests only */
340
+ export declare const _internal: {
341
+ readonly isRetryable: typeof isRetryable;
342
+ readonly computeDelay: typeof computeDelay;
343
+ readonly resolveRetryConfig: typeof resolveRetryConfig;
344
+ readonly normalizeEndpoint: typeof normalizeEndpoint;
345
+ readonly endpointLabel: typeof endpointLabel;
346
+ };
347
+ export {};