-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
web3_base_provider.ts
252 lines (227 loc) · 8.11 KB
/
web3_base_provider.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
import { Socket } from 'net';
import { Web3Error } from './error_types.js';
import { EthExecutionAPI } from './apis/eth_execution_api.js';
import {
JsonRpcNotification,
JsonRpcPayload,
JsonRpcResponse,
JsonRpcResponseWithError,
JsonRpcResponseWithResult,
JsonRpcResult,
JsonRpcSubscriptionResult,
} from './json_rpc_types';
import {
Web3APISpec,
Web3APIMethod,
Web3APIReturnType,
Web3APIPayload,
ProviderConnectInfo,
ProviderRpcError,
ProviderMessage,
} from './web3_api_types';
import { Web3EthExecutionAPI } from './apis/web3_eth_execution_api';
import { Web3DeferredPromiseInterface } from './web3_deferred_promise_type';
const symbol = Symbol.for('web3/base-provider');
export interface SocketRequestItem<
API extends Web3APISpec,
Method extends Web3APIMethod<API>,
ResponseType,
> {
payload: Web3APIPayload<API, Method>;
deferredPromise: Web3DeferredPromiseInterface<ResponseType>;
}
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1193.md#connectivity
export type Web3ProviderStatus = 'connecting' | 'connected' | 'disconnected';
export type Web3ProviderEventCallback<T = JsonRpcResult> = (
error: Error | ProviderRpcError | undefined,
result?: JsonRpcSubscriptionResult | JsonRpcNotification<T>,
) => void;
export type Web3ProviderMessageEventCallback<T = JsonRpcResult> = (
result?: JsonRpcSubscriptionResult | JsonRpcNotification<T>,
) => void;
export type Web3Eip1193ProviderEventCallback<T> = (data: T) => void;
export type Web3ProviderRequestCallback<ResultType = unknown> = (
// Used "null" value to match the legacy version
// eslint-disable-next-line @typescript-eslint/ban-types
err?: Error | Web3Error | null | JsonRpcResponseWithError<Error>,
response?: JsonRpcResponseWithResult<ResultType>,
) => void;
export interface LegacySendProvider {
send<R = JsonRpcResult, P = unknown>(
payload: JsonRpcPayload<P>,
// Used "null" value to match the legacy version
// eslint-disable-next-line @typescript-eslint/ban-types
callback: (err: Error | null, response?: JsonRpcResponse<R>) => void,
): void;
}
export interface LegacySendAsyncProvider {
sendAsync<R = JsonRpcResult, P = unknown>(
payload: JsonRpcPayload<P>,
): Promise<JsonRpcResponse<R>>;
}
export interface LegacyRequestProvider {
request<R = JsonRpcResult, P = unknown>(
payload: JsonRpcPayload<P>,
// eslint-disable-next-line @typescript-eslint/ban-types
callback: (err: Error | null, response: JsonRpcResponse<R>) => void,
): void;
}
export interface EIP1193Provider<API extends Web3APISpec> {
request<Method extends Web3APIMethod<API>, ResponseType = Web3APIReturnType<API, Method>>(
args: Web3APIPayload<API, Method>,
): Promise<JsonRpcResponseWithResult<ResponseType> | unknown>;
}
// Provider interface compatible with EIP-1193
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1193.md
export abstract class Web3BaseProvider<API extends Web3APISpec = EthExecutionAPI>
implements LegacySendProvider, LegacySendAsyncProvider, EIP1193Provider<API>
{
public static isWeb3Provider(provider: unknown) {
return (
provider instanceof Web3BaseProvider ||
Boolean(provider && (provider as { [symbol]: boolean })[symbol])
);
}
// To match an object "instanceof" does not work if
// matcher class and object is using different package versions
// to overcome this bottleneck used this approach.
// The symbol value for one string will always remain same regardless of package versions
// eslint-disable-next-line class-methods-use-this
public get [symbol]() {
return true;
}
public abstract getStatus(): Web3ProviderStatus;
public abstract supportsSubscriptions(): boolean;
/**
* @deprecated Please use `.request` instead.
* @param payload - Request Payload
* @param callback - Callback
*/
public send<ResultType = JsonRpcResult, P = unknown>(
payload: JsonRpcPayload<P>,
// eslint-disable-next-line @typescript-eslint/ban-types
callback: (err: Error | null, response?: JsonRpcResponse<ResultType>) => void,
) {
this.request<Web3APIMethod<API>, ResultType>(
payload as Web3APIPayload<API, Web3APIMethod<API>>,
)
.then(response => {
// eslint-disable-next-line no-null/no-null
callback(null, response);
})
.catch((err: Error | Web3Error) => {
callback(err);
});
}
/**
* @deprecated Please use `.request` instead.
* @param payload - Request Payload
*/
public async sendAsync<R = JsonRpcResult, P = unknown>(payload: JsonRpcPayload<P>) {
return this.request(payload as Web3APIPayload<API, Web3APIMethod<API>>) as Promise<
JsonRpcResponse<R>
>;
}
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1193.md#request
public abstract request<
Method extends Web3APIMethod<API>,
ResultType = Web3APIReturnType<API, Method> | unknown,
>(args: Web3APIPayload<API, Method>): Promise<JsonRpcResponseWithResult<ResultType>>;
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1193.md#events
public abstract on(
type: 'disconnect',
listener: Web3Eip1193ProviderEventCallback<ProviderRpcError>,
): void;
public abstract on<T = JsonRpcResult>(
type: 'message' | string,
listener:
| Web3Eip1193ProviderEventCallback<ProviderMessage>
| Web3ProviderMessageEventCallback<T>,
): void;
// for old providers
public abstract on<T = JsonRpcResult>(
type: 'data' | string,
listener:
| Web3Eip1193ProviderEventCallback<ProviderMessage>
| Web3ProviderMessageEventCallback<T>,
): void;
public abstract on(
type: 'connect',
listener: Web3Eip1193ProviderEventCallback<ProviderConnectInfo>,
): void;
public abstract on(
type: 'chainChanged',
listener: Web3Eip1193ProviderEventCallback<string>,
): void;
public abstract on(
type: 'accountsChanged',
listener: Web3Eip1193ProviderEventCallback<string[]>,
): void;
public abstract removeListener(
type: 'disconnect',
listener: Web3Eip1193ProviderEventCallback<ProviderRpcError>,
): void;
public abstract removeListener<T = JsonRpcResult>(
type: 'message' | string,
listener: Web3Eip1193ProviderEventCallback<ProviderMessage> | Web3ProviderEventCallback<T>,
): void;
public abstract removeListener(
type: 'connect',
listener: Web3Eip1193ProviderEventCallback<ProviderConnectInfo>,
): void;
public abstract removeListener(
type: 'chainChanged',
listener: Web3Eip1193ProviderEventCallback<string>,
): void;
public abstract removeListener(
type: 'accountsChanged',
listener: Web3Eip1193ProviderEventCallback<string[]>,
): void;
public abstract once(
type: 'disconnect',
listener: Web3Eip1193ProviderEventCallback<ProviderRpcError>,
): void;
public abstract once<T = JsonRpcResult>(
type: 'message' | string,
listener: Web3Eip1193ProviderEventCallback<ProviderMessage> | Web3ProviderEventCallback<T>,
): void;
public abstract once(
type: 'connect',
listener: Web3Eip1193ProviderEventCallback<ProviderConnectInfo>,
): void;
public abstract once(
type: 'chainChanged',
listener: Web3Eip1193ProviderEventCallback<string>,
): void;
public abstract once(
type: 'accountsChanged',
listener: Web3Eip1193ProviderEventCallback<string[]>,
): void;
public abstract removeAllListeners?(type: string): void;
public abstract connect(): void;
public abstract disconnect(code?: number, data?: string): void;
public abstract reset(): void;
}
export type SupportedProviders<API extends Web3APISpec = Web3EthExecutionAPI> =
| EIP1193Provider<API>
| Web3BaseProvider<API>
| LegacyRequestProvider
| LegacySendProvider
| LegacySendAsyncProvider;
export type Web3BaseProviderConstructor = new <API extends Web3APISpec>(
url: string,
net?: Socket,
) => Web3BaseProvider<API>;