-
Notifications
You must be signed in to change notification settings - Fork 19
/
substrateSocketConnect.ts
61 lines (55 loc) · 2.04 KB
/
substrateSocketConnect.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { DefinitionRpcExt } from '@polkadot/types/types';
import { ApiPromise, WsProvider } from '@polkadot/api';
export type SubstrateSocketConnectionCallbacksType = {
/**
* Callback called before whole connection logic started
*/
onConnectInit?: () => void;
/**
* Callback for when the connection is established.
*/
onConnect?: (api: ApiPromise) => void;
/**
* Callback for when the connection is successful.
*/
onConnectSucccess?: (api: ApiPromise) => void;
/**
* Callback for when an error occurs with the connection.
*/
onConnectError?: (error: Event) => void;
};
/**
* Connects to a Substrate node using WebSockets API by creating a new WsProvider instance with the given socket address.
*
* @category Helpers
* @param {Object} state - An object that contains the state of the API, including the connection status, socket address, and JSON-RPC interface.
* @param {SubstrateSocketConnectionCallbacksType} callbacks - Optional callbacks
* @returns {ApiPromise} - An instance of the ApiPromise class.
*/
export const substrateSocketConnect = (
state: {
apiState: string | null | undefined;
socket: string;
jsonrpc: {
[x: string]: Record<string, DefinitionRpcExt>;
};
},
callbacks?: SubstrateSocketConnectionCallbacksType,
): ApiPromise | undefined => {
const { apiState, socket, jsonrpc } = state;
// We only want this function to be performed once
if (apiState) return;
callbacks?.onConnectInit?.();
// console.log(`Connected socket: ${socket}`);
const provider = new WsProvider(socket);
const _api = new ApiPromise({ provider, rpc: jsonrpc });
// Set listeners for disconnection and reconnection event.
_api.on('connected', () => {
callbacks?.onConnect?.(_api);
// `ready` event is not emitted upon reconnection and is checked explicitly here.
void _api.isReady.then(() => callbacks?.onConnectSucccess?.(_api));
});
_api.on('ready', () => callbacks?.onConnectSucccess?.(_api));
_api.on('error', (err: Event) => callbacks?.onConnectError?.(err));
return _api;
};