UNPKG

1.09 kBJavaScriptView Raw
1class RPCError extends Error {
2 constructor(object) {
3 super(object);
4 Object.assign(this, object);
5 }
6}
7
8class BaseProvider {
9 /**
10 * @param url {string} - Full json rpc http url
11 * @param [options] {object}
12 * @param [options.timeout=60*1000] {number} - Request time out in ms
13 * @param [options.logger] {object} - Logger with `info` and `error`
14 * @return {BaseProvider}
15 */
16 constructor(url, {
17 timeout = 5 * 60 * 1000,
18 logger = { info: () => undefined, error: () => undefined },
19 } = {}) {
20 this.url = url;
21 this.timeout = timeout;
22 this.logger = logger;
23 }
24
25 /**
26 * Gen a random json rpc id.
27 * It is used in `call` method, overwrite it to gen your own id.
28 *
29 * @return {string}
30 */
31 requestId() {
32 return `${Date.now()}${Math.random().toFixed(7).substring(2)}`; // 13+7=20 int string
33 }
34
35 async call() {
36 throw new Error(`NotImplementError: ${this.constructor.name}.call not implement.`);
37 }
38
39 close() {}
40}
41
42module.exports = BaseProvider;
43module.exports.RPCError = RPCError;