@pezkuwi/rpc-provider 16.5.5
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 +68 -0
- package/build/bundle.d.ts +5 -0
- package/build/coder/error.d.ts +29 -0
- package/build/coder/index.d.ts +8 -0
- package/build/defaults.d.ts +5 -0
- package/build/http/index.d.ts +81 -0
- package/build/http/types.d.ts +7 -0
- package/build/index.d.ts +2 -0
- package/build/lru.d.ts +15 -0
- package/build/mock/index.d.ts +35 -0
- package/build/mock/mockHttp.d.ts +9 -0
- package/build/mock/mockWs.d.ts +26 -0
- package/build/mock/types.d.ts +23 -0
- package/build/packageDetect.d.ts +1 -0
- package/build/packageInfo.d.ts +6 -0
- package/build/substrate-connect/Health.d.ts +7 -0
- package/build/substrate-connect/index.d.ts +22 -0
- package/build/substrate-connect/types.d.ts +12 -0
- package/build/types.d.ts +85 -0
- package/build/ws/errors.d.ts +1 -0
- package/build/ws/index.d.ts +121 -0
- package/package.json +43 -0
- package/src/bundle.ts +8 -0
- package/src/coder/decodeResponse.spec.ts +70 -0
- package/src/coder/encodeJson.spec.ts +20 -0
- package/src/coder/encodeObject.spec.ts +25 -0
- package/src/coder/error.spec.ts +111 -0
- package/src/coder/error.ts +66 -0
- package/src/coder/index.ts +88 -0
- package/src/defaults.ts +10 -0
- package/src/http/index.spec.ts +72 -0
- package/src/http/index.ts +238 -0
- package/src/http/send.spec.ts +61 -0
- package/src/http/types.ts +11 -0
- package/src/index.ts +6 -0
- package/src/lru.spec.ts +74 -0
- package/src/lru.ts +197 -0
- package/src/mock/index.ts +259 -0
- package/src/mock/mockHttp.ts +35 -0
- package/src/mock/mockWs.ts +92 -0
- package/src/mock/on.spec.ts +43 -0
- package/src/mock/send.spec.ts +38 -0
- package/src/mock/subscribe.spec.ts +81 -0
- package/src/mock/types.ts +36 -0
- package/src/mock/unsubscribe.spec.ts +57 -0
- package/src/mod.ts +4 -0
- package/src/packageDetect.ts +12 -0
- package/src/packageInfo.ts +6 -0
- package/src/substrate-connect/Health.ts +325 -0
- package/src/substrate-connect/index.spec.ts +638 -0
- package/src/substrate-connect/index.ts +415 -0
- package/src/substrate-connect/types.ts +16 -0
- package/src/types.ts +101 -0
- package/src/ws/connect.spec.ts +167 -0
- package/src/ws/errors.ts +41 -0
- package/src/ws/index.spec.ts +97 -0
- package/src/ws/index.ts +652 -0
- package/src/ws/send.spec.ts +126 -0
- package/src/ws/state.spec.ts +20 -0
- package/src/ws/subscribe.spec.ts +68 -0
- package/src/ws/unsubscribe.spec.ts +100 -0
- package/tsconfig.build.json +17 -0
- package/tsconfig.build.tsbuildinfo +1 -0
- package/tsconfig.spec.json +18 -0
package/README.md
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# @pezkuwi/rpc-provider
|
|
2
|
+
|
|
3
|
+
Generic transport providers to handle the transport of method calls to and from Pezkuwi clients from applications interacting with it. It provides an interface to making RPC calls and is generally, unless you are operating at a low-level and taking care of encoding and decoding of parameters/results, it won't be directly used, rather only passed to a higher-level interface.
|
|
4
|
+
|
|
5
|
+
## Provider Selection
|
|
6
|
+
|
|
7
|
+
There are three flavours of the providers provided, one allowing for using HTTP as a transport mechanism, the other using WebSockets, and the third one uses substrate light-client through @substrate/connect. It is generally recommended to use the [[WsProvider]] since in addition to standard calls, it allows for subscriptions where all changes to state can be pushed from the node to the client.
|
|
8
|
+
|
|
9
|
+
All providers are usable (as is the API), in both browser-based and Node.js environments. Polyfills for unsupported functionality are automatically applied based on feature-detection.
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
Installation -
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
yarn add @pezkuwi/rpc-provider
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
WebSocket Initialization -
|
|
20
|
+
|
|
21
|
+
```javascript
|
|
22
|
+
import { WsProvider } from '@pezkuwi/rpc-provider';
|
|
23
|
+
|
|
24
|
+
// this is the actual default endpoint
|
|
25
|
+
const provider = new WsProvider('ws://127.0.0.1:9944');
|
|
26
|
+
const version = await provider.send('client_version', []);
|
|
27
|
+
|
|
28
|
+
console.log('client version', version);
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
HTTP Initialization -
|
|
32
|
+
|
|
33
|
+
```javascript
|
|
34
|
+
import { HttpProvider } from '@pezkuwi/rpc-provider';
|
|
35
|
+
|
|
36
|
+
// this is the actual default endpoint
|
|
37
|
+
const provider = new HttpProvider('http://127.0.0.1:9933');
|
|
38
|
+
const version = await provider.send('chain_getBlockHash', []);
|
|
39
|
+
|
|
40
|
+
console.log('latest block Hash', hash);
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
@substrate/connect Initialization -
|
|
44
|
+
|
|
45
|
+
Instantiating a Provider for the Pezkuwi Relay Chain:
|
|
46
|
+
```javascript
|
|
47
|
+
import { ScProvider } from '@pezkuwi/rpc-provider';
|
|
48
|
+
import * as Sc from '@substrate/connect';
|
|
49
|
+
|
|
50
|
+
const provider = new ScProvider(Sc, Sc.WellKnownChain.polkadot);
|
|
51
|
+
|
|
52
|
+
await provider.connect();
|
|
53
|
+
|
|
54
|
+
const version = await provider.send('chain_getBlockHash', []);
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Instantiating a Provider for a Pezkuwi parachain:
|
|
58
|
+
```javascript
|
|
59
|
+
import { ScProvider } from '@pezkuwi/rpc-provider';
|
|
60
|
+
import * as Sc from '@substrate/connect';
|
|
61
|
+
|
|
62
|
+
const polkadotProvider = new ScProvider(Sc, Sc.WellKnownChain.polkadot);
|
|
63
|
+
const parachainProvider = new ScProvider(Sc, parachainSpec, polkadotProvider);
|
|
64
|
+
|
|
65
|
+
await parachainProvider.connect();
|
|
66
|
+
|
|
67
|
+
const version = await parachainProvider.send('chain_getBlockHash', []);
|
|
68
|
+
```
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { RpcErrorInterface } from '../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* @name RpcError
|
|
4
|
+
* @summary Extension to the basic JS Error.
|
|
5
|
+
* @description
|
|
6
|
+
* The built-in JavaScript Error class is extended by adding a code to allow for Error categorization. In addition to the normal `stack`, `message`, the numeric `code` and `data` (any types) parameters are available on the object.
|
|
7
|
+
* @example
|
|
8
|
+
* <BR>
|
|
9
|
+
*
|
|
10
|
+
* ```javascript
|
|
11
|
+
* const { RpcError } from '@pezkuwi/util');
|
|
12
|
+
*
|
|
13
|
+
* throw new RpcError('some message', RpcError.CODES.METHOD_NOT_FOUND); // => error.code = -32601
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export default class RpcError<T = never> extends Error implements RpcErrorInterface<T> {
|
|
17
|
+
code: number;
|
|
18
|
+
data?: T;
|
|
19
|
+
message: string;
|
|
20
|
+
name: string;
|
|
21
|
+
stack: string;
|
|
22
|
+
constructor(message?: string, code?: number, data?: T);
|
|
23
|
+
static CODES: {
|
|
24
|
+
ASSERT: number;
|
|
25
|
+
INVALID_JSONRPC: number;
|
|
26
|
+
METHOD_NOT_FOUND: number;
|
|
27
|
+
UNKNOWN: number;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { JsonRpcRequest, JsonRpcResponse } from '../types.js';
|
|
2
|
+
/** @internal */
|
|
3
|
+
export declare class RpcCoder {
|
|
4
|
+
#private;
|
|
5
|
+
decodeResponse<T>(response?: JsonRpcResponse<T>): T;
|
|
6
|
+
encodeJson(method: string, params: unknown[]): [number, string];
|
|
7
|
+
encodeObject(method: string, params: unknown[]): [number, JsonRpcRequest];
|
|
8
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { ProviderInterface, ProviderInterfaceCallback, ProviderInterfaceEmitCb, ProviderInterfaceEmitted, ProviderStats } from '../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* # @polkadot/rpc-provider
|
|
4
|
+
*
|
|
5
|
+
* @name HttpProvider
|
|
6
|
+
*
|
|
7
|
+
* @description The HTTP Provider allows sending requests using HTTP to a HTTP RPC server TCP port. It does not support subscriptions so you won't be able to listen to events such as new blocks or balance changes. It is usually preferable using the [[WsProvider]].
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* <BR>
|
|
11
|
+
*
|
|
12
|
+
* ```javascript
|
|
13
|
+
* import Api from '@pezkuwi/api/promise';
|
|
14
|
+
* import { HttpProvider } from '@pezkuwi/rpc-provider';
|
|
15
|
+
*
|
|
16
|
+
* const provider = new HttpProvider('http://127.0.0.1:9933');
|
|
17
|
+
* const api = new Api(provider);
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @see [[WsProvider]]
|
|
21
|
+
*/
|
|
22
|
+
export declare class HttpProvider implements ProviderInterface {
|
|
23
|
+
#private;
|
|
24
|
+
/**
|
|
25
|
+
* @param {string} endpoint The endpoint url starting with http://
|
|
26
|
+
* @param {Record<string, string>} headers The headers provided to the underlying Http Endpoint
|
|
27
|
+
* @param {number} [cacheCapacity] Custom size of the HttpProvider LRUCache. Defaults to `DEFAULT_CAPACITY` (1024)
|
|
28
|
+
* @param {number} [cacheTtl] Custom TTL of the HttpProvider LRUCache. Determines how long an object can live in the cache. Defaults to `DEFAULT_TTL` (30000)
|
|
29
|
+
*/
|
|
30
|
+
constructor(endpoint?: string, headers?: Record<string, string>, cacheCapacity?: number, cacheTtl?: number | null);
|
|
31
|
+
/**
|
|
32
|
+
* @summary `true` when this provider supports subscriptions
|
|
33
|
+
*/
|
|
34
|
+
get hasSubscriptions(): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* @description Returns a clone of the object
|
|
37
|
+
*/
|
|
38
|
+
clone(): HttpProvider;
|
|
39
|
+
/**
|
|
40
|
+
* @description Manually connect from the connection
|
|
41
|
+
*/
|
|
42
|
+
connect(): Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* @description Manually disconnect from the connection
|
|
45
|
+
*/
|
|
46
|
+
disconnect(): Promise<void>;
|
|
47
|
+
/**
|
|
48
|
+
* @description Returns the connection stats
|
|
49
|
+
*/
|
|
50
|
+
get stats(): ProviderStats;
|
|
51
|
+
/**
|
|
52
|
+
* @description Returns the connection stats
|
|
53
|
+
*/
|
|
54
|
+
get ttl(): number | null | undefined;
|
|
55
|
+
/**
|
|
56
|
+
* @summary `true` when this provider supports clone()
|
|
57
|
+
*/
|
|
58
|
+
get isClonable(): boolean;
|
|
59
|
+
/**
|
|
60
|
+
* @summary Whether the node is connected or not.
|
|
61
|
+
* @return {boolean} true if connected
|
|
62
|
+
*/
|
|
63
|
+
get isConnected(): boolean;
|
|
64
|
+
/**
|
|
65
|
+
* @summary Events are not supported with the HttpProvider, see [[WsProvider]].
|
|
66
|
+
* @description HTTP Provider does not have 'on' emitters. WebSockets should be used instead.
|
|
67
|
+
*/
|
|
68
|
+
on(_type: ProviderInterfaceEmitted, _sub: ProviderInterfaceEmitCb): () => void;
|
|
69
|
+
/**
|
|
70
|
+
* @summary Send HTTP POST Request with Body to configured HTTP Endpoint.
|
|
71
|
+
*/
|
|
72
|
+
send<T>(method: string, params: unknown[], isCacheable?: boolean): Promise<T>;
|
|
73
|
+
/**
|
|
74
|
+
* @summary Subscriptions are not supported with the HttpProvider, see [[WsProvider]].
|
|
75
|
+
*/
|
|
76
|
+
subscribe(_types: string, _method: string, _params: unknown[], _cb: ProviderInterfaceCallback): Promise<number>;
|
|
77
|
+
/**
|
|
78
|
+
* @summary Subscriptions are not supported with the HttpProvider, see [[WsProvider]].
|
|
79
|
+
*/
|
|
80
|
+
unsubscribe(_type: string, _method: string, _id: number): Promise<boolean>;
|
|
81
|
+
}
|
package/build/index.d.ts
ADDED
package/build/lru.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare const DEFAULT_CAPACITY = 1024;
|
|
2
|
+
export declare const DEFAULT_TTL = 30000;
|
|
3
|
+
export declare class LRUCache {
|
|
4
|
+
#private;
|
|
5
|
+
readonly capacity: number;
|
|
6
|
+
constructor(capacity?: number, ttl?: number | null);
|
|
7
|
+
get ttl(): number | null;
|
|
8
|
+
get length(): number;
|
|
9
|
+
get lengthData(): number;
|
|
10
|
+
get lengthRefs(): number;
|
|
11
|
+
entries(): [string, unknown][];
|
|
12
|
+
keys(): string[];
|
|
13
|
+
get<T>(key: string): T | null;
|
|
14
|
+
set<T>(key: string, value: T): void;
|
|
15
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { Registry } from '@pezkuwi/types/types';
|
|
2
|
+
import type { ProviderInterface, ProviderInterfaceEmitCb, ProviderInterfaceEmitted } from '../types.js';
|
|
3
|
+
import type { MockStateSubscriptions } from './types.js';
|
|
4
|
+
/**
|
|
5
|
+
* A mock provider mainly used for testing.
|
|
6
|
+
* @return {ProviderInterface} The mock provider
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
export declare class MockProvider implements ProviderInterface {
|
|
10
|
+
private db;
|
|
11
|
+
private emitter;
|
|
12
|
+
private intervalId?;
|
|
13
|
+
isUpdating: boolean;
|
|
14
|
+
private registry;
|
|
15
|
+
private prevNumber;
|
|
16
|
+
private requests;
|
|
17
|
+
subscriptions: MockStateSubscriptions;
|
|
18
|
+
private subscriptionId;
|
|
19
|
+
private subscriptionMap;
|
|
20
|
+
constructor(registry: Registry);
|
|
21
|
+
get hasSubscriptions(): boolean;
|
|
22
|
+
clone(): MockProvider;
|
|
23
|
+
connect(): Promise<void>;
|
|
24
|
+
disconnect(): Promise<void>;
|
|
25
|
+
get isClonable(): boolean;
|
|
26
|
+
get isConnected(): boolean;
|
|
27
|
+
on(type: ProviderInterfaceEmitted, sub: ProviderInterfaceEmitCb): () => void;
|
|
28
|
+
send<T = any>(method: string, params: unknown[]): Promise<T>;
|
|
29
|
+
subscribe(_type: string, method: string, ...params: unknown[]): Promise<number>;
|
|
30
|
+
unsubscribe(_type: string, _method: string, id: number): Promise<boolean>;
|
|
31
|
+
private init;
|
|
32
|
+
private makeBlockHeader;
|
|
33
|
+
private setStateBn;
|
|
34
|
+
private updateSubs;
|
|
35
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Mock } from './types.js';
|
|
2
|
+
interface Request {
|
|
3
|
+
code?: number;
|
|
4
|
+
method: string;
|
|
5
|
+
reply?: Record<string, unknown>;
|
|
6
|
+
}
|
|
7
|
+
export declare const TEST_HTTP_URL = "http://localhost:9944";
|
|
8
|
+
export declare function mockHttp(requests: Request[]): Mock;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Server } from 'mock-socket';
|
|
2
|
+
interface Scope {
|
|
3
|
+
body: Record<string, Record<string, unknown>>;
|
|
4
|
+
requests: number;
|
|
5
|
+
server: Server;
|
|
6
|
+
done: any;
|
|
7
|
+
}
|
|
8
|
+
interface ErrorDef {
|
|
9
|
+
id: number;
|
|
10
|
+
error: {
|
|
11
|
+
code: number;
|
|
12
|
+
message: string;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
interface ReplyDef {
|
|
16
|
+
id: number;
|
|
17
|
+
reply: {
|
|
18
|
+
result: unknown;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export type Request = {
|
|
22
|
+
method: string;
|
|
23
|
+
} & (ErrorDef | ReplyDef);
|
|
24
|
+
export declare const TEST_WS_URL = "ws://localhost:9955";
|
|
25
|
+
export declare function mockWs(requests: Request[], wsUrl?: string): Scope;
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Server } from 'mock-socket';
|
|
2
|
+
export type Global = typeof globalThis & {
|
|
3
|
+
WebSocket: typeof WebSocket;
|
|
4
|
+
fetch: any;
|
|
5
|
+
};
|
|
6
|
+
export interface Mock {
|
|
7
|
+
body: Record<string, Record<string, unknown>>;
|
|
8
|
+
requests: number;
|
|
9
|
+
server: Server;
|
|
10
|
+
done: () => Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
export type MockStateSubscriptionCallback = (error: Error | null, value: any) => void;
|
|
13
|
+
export interface MockStateSubscription {
|
|
14
|
+
callbacks: Record<number, MockStateSubscriptionCallback>;
|
|
15
|
+
lastValue: any;
|
|
16
|
+
}
|
|
17
|
+
export interface MockStateSubscriptions {
|
|
18
|
+
chain_subscribeNewHead: MockStateSubscription;
|
|
19
|
+
state_subscribeStorage: MockStateSubscription;
|
|
20
|
+
[key: string]: MockStateSubscription;
|
|
21
|
+
}
|
|
22
|
+
export type MockStateDb = Record<string, Uint8Array>;
|
|
23
|
+
export type MockStateRequests = Record<string, (db: MockStateDb, params: any[]) => string>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type * as ScType from '@substrate/connect';
|
|
2
|
+
import type { ProviderInterface, ProviderInterfaceCallback, ProviderInterfaceEmitCb, ProviderInterfaceEmitted } from '../types.js';
|
|
3
|
+
import { healthChecker } from './Health.js';
|
|
4
|
+
interface SubstrateConnect {
|
|
5
|
+
WellKnownChain: typeof ScType['WellKnownChain'];
|
|
6
|
+
createScClient: typeof ScType['createScClient'];
|
|
7
|
+
}
|
|
8
|
+
export declare class ScProvider implements ProviderInterface {
|
|
9
|
+
#private;
|
|
10
|
+
constructor(Sc: SubstrateConnect, spec: string | ScType.WellKnownChain, sharedSandbox?: ScProvider);
|
|
11
|
+
get hasSubscriptions(): boolean;
|
|
12
|
+
get isClonable(): boolean;
|
|
13
|
+
get isConnected(): boolean;
|
|
14
|
+
clone(): ProviderInterface;
|
|
15
|
+
connect(config?: ScType.Config, checkerFactory?: typeof healthChecker): Promise<void>;
|
|
16
|
+
disconnect(): Promise<void>;
|
|
17
|
+
on(type: ProviderInterfaceEmitted, sub: ProviderInterfaceEmitCb): () => void;
|
|
18
|
+
send<T = any>(method: string, params: unknown[]): Promise<T>;
|
|
19
|
+
subscribe(type: string, method: string, params: any[], callback: ProviderInterfaceCallback): Promise<number | string>;
|
|
20
|
+
unsubscribe(type: string, method: string, id: number | string): Promise<boolean>;
|
|
21
|
+
}
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface SmoldotHealth {
|
|
2
|
+
isSyncing: boolean;
|
|
3
|
+
peers: number;
|
|
4
|
+
shouldHavePeers: boolean;
|
|
5
|
+
}
|
|
6
|
+
export interface HealthChecker {
|
|
7
|
+
setSendJsonRpc(sendRequest: (request: string) => void): void;
|
|
8
|
+
start(healthCallback: (health: SmoldotHealth) => void): void;
|
|
9
|
+
stop(): void;
|
|
10
|
+
sendJsonRpc(request: string): void;
|
|
11
|
+
responsePassThrough(response: string): string | null;
|
|
12
|
+
}
|
package/build/types.d.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
export interface JsonRpcObject {
|
|
2
|
+
id: number;
|
|
3
|
+
jsonrpc: '2.0';
|
|
4
|
+
}
|
|
5
|
+
export interface JsonRpcRequest extends JsonRpcObject {
|
|
6
|
+
method: string;
|
|
7
|
+
params: unknown[];
|
|
8
|
+
}
|
|
9
|
+
export interface JsonRpcResponseBaseError {
|
|
10
|
+
code: number;
|
|
11
|
+
data?: number | string;
|
|
12
|
+
message: string;
|
|
13
|
+
}
|
|
14
|
+
export interface RpcErrorInterface<T> {
|
|
15
|
+
code: number;
|
|
16
|
+
data?: T;
|
|
17
|
+
message: string;
|
|
18
|
+
stack: string;
|
|
19
|
+
}
|
|
20
|
+
interface JsonRpcResponseSingle<T> {
|
|
21
|
+
error?: JsonRpcResponseBaseError;
|
|
22
|
+
result: T;
|
|
23
|
+
}
|
|
24
|
+
interface JsonRpcResponseSubscription<T> {
|
|
25
|
+
method?: string;
|
|
26
|
+
params: {
|
|
27
|
+
error?: JsonRpcResponseBaseError;
|
|
28
|
+
result: T;
|
|
29
|
+
subscription: number | string;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
export type JsonRpcResponseBase<T> = JsonRpcResponseSingle<T> & JsonRpcResponseSubscription<T>;
|
|
33
|
+
export type JsonRpcResponse<T> = JsonRpcObject & JsonRpcResponseBase<T>;
|
|
34
|
+
export type ProviderInterfaceCallback = (error: Error | null, result: any) => void;
|
|
35
|
+
export type ProviderInterfaceEmitted = 'connected' | 'disconnected' | 'error';
|
|
36
|
+
export type ProviderInterfaceEmitCb = (value?: any) => any;
|
|
37
|
+
export interface ProviderInterface {
|
|
38
|
+
/** true if the provider supports subscriptions (not available for HTTP) */
|
|
39
|
+
readonly hasSubscriptions: boolean;
|
|
40
|
+
/** true if the clone() functionality is available on the provider */
|
|
41
|
+
readonly isClonable: boolean;
|
|
42
|
+
/** true if the provider is currently connected (ws/sc has connection logic) */
|
|
43
|
+
readonly isConnected: boolean;
|
|
44
|
+
/** (optional) stats for the provider with connections/bytes */
|
|
45
|
+
readonly stats?: ProviderStats;
|
|
46
|
+
/** (optional) stats for the provider with connections/bytes */
|
|
47
|
+
readonly ttl?: number | null;
|
|
48
|
+
clone(): ProviderInterface;
|
|
49
|
+
connect(): Promise<void>;
|
|
50
|
+
disconnect(): Promise<void>;
|
|
51
|
+
on(type: ProviderInterfaceEmitted, sub: ProviderInterfaceEmitCb): () => void;
|
|
52
|
+
send<T = any>(method: string, params: unknown[], isCacheable?: boolean): Promise<T>;
|
|
53
|
+
subscribe(type: string, method: string, params: unknown[], cb: ProviderInterfaceCallback): Promise<number | string>;
|
|
54
|
+
unsubscribe(type: string, method: string, id: number | string): Promise<boolean>;
|
|
55
|
+
}
|
|
56
|
+
/** Stats for a specific endpoint */
|
|
57
|
+
export interface EndpointStats {
|
|
58
|
+
/** The total number of bytes sent */
|
|
59
|
+
bytesRecv: number;
|
|
60
|
+
/** The total number of bytes received */
|
|
61
|
+
bytesSent: number;
|
|
62
|
+
/** The number of cached/in-progress requests made */
|
|
63
|
+
cached: number;
|
|
64
|
+
/** The number of errors found */
|
|
65
|
+
errors: number;
|
|
66
|
+
/** The number of requests */
|
|
67
|
+
requests: number;
|
|
68
|
+
/** The number of subscriptions */
|
|
69
|
+
subscriptions: number;
|
|
70
|
+
/** The number of request timeouts */
|
|
71
|
+
timeout: number;
|
|
72
|
+
}
|
|
73
|
+
/** Overall stats for the provider */
|
|
74
|
+
export interface ProviderStats {
|
|
75
|
+
/** Details for the active/open requests */
|
|
76
|
+
active: {
|
|
77
|
+
/** Number of active requests */
|
|
78
|
+
requests: number;
|
|
79
|
+
/** Number of active subscriptions */
|
|
80
|
+
subscriptions: number;
|
|
81
|
+
};
|
|
82
|
+
/** The total requests that have been made */
|
|
83
|
+
total: EndpointStats;
|
|
84
|
+
}
|
|
85
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getWSErrorString(code: number): string;
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import type { EndpointStats, ProviderInterface, ProviderInterfaceCallback, ProviderInterfaceEmitCb, ProviderInterfaceEmitted, ProviderStats } from '../types.js';
|
|
2
|
+
interface SubscriptionHandler {
|
|
3
|
+
callback: ProviderInterfaceCallback;
|
|
4
|
+
type: string;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* # @polkadot/rpc-provider/ws
|
|
8
|
+
*
|
|
9
|
+
* @name WsProvider
|
|
10
|
+
*
|
|
11
|
+
* @description The WebSocket Provider allows sending requests using WebSocket to a WebSocket RPC server TCP port. Unlike the [[HttpProvider]], it does support subscriptions and allows listening to events such as new blocks or balance changes.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* <BR>
|
|
15
|
+
*
|
|
16
|
+
* ```javascript
|
|
17
|
+
* import Api from '@pezkuwi/api/promise';
|
|
18
|
+
* import { WsProvider } from '@pezkuwi/rpc-provider/ws';
|
|
19
|
+
*
|
|
20
|
+
* const provider = new WsProvider('ws://127.0.0.1:9944');
|
|
21
|
+
* const api = new Api(provider);
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @see [[HttpProvider]]
|
|
25
|
+
*/
|
|
26
|
+
export declare class WsProvider implements ProviderInterface {
|
|
27
|
+
#private;
|
|
28
|
+
/**
|
|
29
|
+
* @param {string | string[]} endpoint The endpoint url. Usually `ws://ip:9944` or `wss://ip:9944`, may provide an array of endpoint strings.
|
|
30
|
+
* @param {number | false} autoConnectMs Whether to connect automatically or not (default). Provided value is used as a delay between retries.
|
|
31
|
+
* @param {Record<string, string>} headers The headers provided to the underlying WebSocket
|
|
32
|
+
* @param {number} [timeout] Custom timeout value used per request . Defaults to `DEFAULT_TIMEOUT_MS`
|
|
33
|
+
* @param {number} [cacheCapacity] Custom size of the WsProvider LRUCache. Defaults to `DEFAULT_CAPACITY` (1024)
|
|
34
|
+
* @param {number} [cacheTtl] Custom TTL of the WsProvider LRUCache. Determines how long an object can live in the cache. Defaults to DEFAULT_TTL` (30000)
|
|
35
|
+
*/
|
|
36
|
+
constructor(endpoint?: string | string[], autoConnectMs?: number | false, headers?: Record<string, string>, timeout?: number, cacheCapacity?: number, cacheTtl?: number | null);
|
|
37
|
+
/**
|
|
38
|
+
* @summary `true` when this provider supports subscriptions
|
|
39
|
+
*/
|
|
40
|
+
get hasSubscriptions(): boolean;
|
|
41
|
+
/**
|
|
42
|
+
* @summary `true` when this provider supports clone()
|
|
43
|
+
*/
|
|
44
|
+
get isClonable(): boolean;
|
|
45
|
+
/**
|
|
46
|
+
* @summary Whether the node is connected or not.
|
|
47
|
+
* @return {boolean} true if connected
|
|
48
|
+
*/
|
|
49
|
+
get isConnected(): boolean;
|
|
50
|
+
/**
|
|
51
|
+
* @description Promise that resolves the first time we are connected and loaded
|
|
52
|
+
*/
|
|
53
|
+
get isReady(): Promise<WsProvider>;
|
|
54
|
+
get endpoint(): string;
|
|
55
|
+
/**
|
|
56
|
+
* @description Returns a clone of the object
|
|
57
|
+
*/
|
|
58
|
+
clone(): WsProvider;
|
|
59
|
+
protected selectEndpointIndex(endpoints: string[]): number;
|
|
60
|
+
/**
|
|
61
|
+
* @summary Manually connect
|
|
62
|
+
* @description The [[WsProvider]] connects automatically by default, however if you decided otherwise, you may
|
|
63
|
+
* connect manually using this method.
|
|
64
|
+
*/
|
|
65
|
+
connect(): Promise<void>;
|
|
66
|
+
/**
|
|
67
|
+
* @description Connect, never throwing an error, but rather forcing a retry
|
|
68
|
+
*/
|
|
69
|
+
connectWithRetry(): Promise<void>;
|
|
70
|
+
/**
|
|
71
|
+
* @description Manually disconnect from the connection, clearing auto-connect logic
|
|
72
|
+
*/
|
|
73
|
+
disconnect(): Promise<void>;
|
|
74
|
+
/**
|
|
75
|
+
* @description Returns the connection stats
|
|
76
|
+
*/
|
|
77
|
+
get stats(): ProviderStats;
|
|
78
|
+
/**
|
|
79
|
+
* @description Returns the connection stats
|
|
80
|
+
*/
|
|
81
|
+
get ttl(): number | null | undefined;
|
|
82
|
+
get endpointStats(): EndpointStats;
|
|
83
|
+
/**
|
|
84
|
+
* @summary Listens on events after having subscribed using the [[subscribe]] function.
|
|
85
|
+
* @param {ProviderInterfaceEmitted} type Event
|
|
86
|
+
* @param {ProviderInterfaceEmitCb} sub Callback
|
|
87
|
+
* @return unsubscribe function
|
|
88
|
+
*/
|
|
89
|
+
on(type: ProviderInterfaceEmitted, sub: ProviderInterfaceEmitCb): () => void;
|
|
90
|
+
/**
|
|
91
|
+
* @summary Send JSON data using WebSockets to configured HTTP Endpoint or queue.
|
|
92
|
+
* @param method The RPC methods to execute
|
|
93
|
+
* @param params Encoded parameters as applicable for the method
|
|
94
|
+
* @param subscription Subscription details (internally used)
|
|
95
|
+
*/
|
|
96
|
+
send<T = any>(method: string, params: unknown[], isCacheable?: boolean, subscription?: SubscriptionHandler): Promise<T>;
|
|
97
|
+
/**
|
|
98
|
+
* @name subscribe
|
|
99
|
+
* @summary Allows subscribing to a specific event.
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* <BR>
|
|
103
|
+
*
|
|
104
|
+
* ```javascript
|
|
105
|
+
* const provider = new WsProvider('ws://127.0.0.1:9944');
|
|
106
|
+
* const rpc = new Rpc(provider);
|
|
107
|
+
*
|
|
108
|
+
* rpc.state.subscribeStorage([[storage.system.account, <Address>]], (_, values) => {
|
|
109
|
+
* console.log(values)
|
|
110
|
+
* }).then((subscriptionId) => {
|
|
111
|
+
* console.log('balance changes subscription id: ', subscriptionId)
|
|
112
|
+
* })
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
subscribe(type: string, method: string, params: unknown[], callback: ProviderInterfaceCallback): Promise<number | string>;
|
|
116
|
+
/**
|
|
117
|
+
* @summary Allows unsubscribing to subscriptions made with [[subscribe]].
|
|
118
|
+
*/
|
|
119
|
+
unsubscribe(type: string, method: string, id: number | string): Promise<boolean>;
|
|
120
|
+
}
|
|
121
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"author": "Jaco Greeff <jacogr@gmail.com>",
|
|
3
|
+
"bugs": "https://github.com/pezkuwichain/pezkuwi-api/issues",
|
|
4
|
+
"description": "Transport providers for the API",
|
|
5
|
+
"engines": {
|
|
6
|
+
"node": ">=18"
|
|
7
|
+
},
|
|
8
|
+
"homepage": "https://github.com/pezkuwichain/pezkuwi-api/tree/master/packages/rpc-provider#readme",
|
|
9
|
+
"license": "Apache-2.0",
|
|
10
|
+
"name": "@pezkuwi/rpc-provider",
|
|
11
|
+
"repository": {
|
|
12
|
+
"directory": "packages/rpc-provider",
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://github.com/pezkuwichain/pezkuwi-api.git"
|
|
15
|
+
},
|
|
16
|
+
"sideEffects": [
|
|
17
|
+
"./packageDetect.js",
|
|
18
|
+
"./packageDetect.cjs"
|
|
19
|
+
],
|
|
20
|
+
"type": "module",
|
|
21
|
+
"version": "16.5.5",
|
|
22
|
+
"main": "index.js",
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@pezkuwi/keyring": "14.0.5",
|
|
25
|
+
"@pezkuwi/types": "16.5.5",
|
|
26
|
+
"@pezkuwi/types-support": "16.5.5",
|
|
27
|
+
"@pezkuwi/util": "14.0.5",
|
|
28
|
+
"@pezkuwi/util-crypto": "14.0.5",
|
|
29
|
+
"@pezkuwi/x-fetch": "14.0.5",
|
|
30
|
+
"@pezkuwi/x-global": "14.0.5",
|
|
31
|
+
"@pezkuwi/x-ws": "14.0.5",
|
|
32
|
+
"eventemitter3": "^5.0.1",
|
|
33
|
+
"mock-socket": "^9.3.1",
|
|
34
|
+
"nock": "^13.5.5",
|
|
35
|
+
"tslib": "^2.8.1"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@substrate/connect": "0.8.11"
|
|
39
|
+
},
|
|
40
|
+
"optionalDependencies": {
|
|
41
|
+
"@substrate/connect": "0.8.11"
|
|
42
|
+
}
|
|
43
|
+
}
|
package/src/bundle.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/rpc-provider authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
export { HttpProvider } from './http/index.js';
|
|
5
|
+
export { DEFAULT_CAPACITY, LRUCache } from './lru.js';
|
|
6
|
+
export { packageInfo } from './packageInfo.js';
|
|
7
|
+
export { ScProvider } from './substrate-connect/index.js';
|
|
8
|
+
export { WsProvider } from './ws/index.js';
|