@eip-1193/jsonrpc-provider 0.0.0 → 0.6.0
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/LICENSE +21 -0
- package/README.md +7 -1
- package/dist/index.cjs +140 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.mjs +136 -0
- package/package.json +65 -9
- package/src/JSONRPCHTTPProvider.ts +140 -0
- package/src/index.ts +1 -0
- package/.npm-publish.log +0 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-present Ronan Sandford
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
1
|
# @eip-1193/jsonrpc-provider
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
An [EIP-1193](https://eips.ethereum.org/EIPS/eip-1193) provider backed by a JSON-RPC node over HTTP, with optional request throttling and `eth_batch` support.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install @eip-1193/jsonrpc-provider
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
Formerly published as `eip-1193-jsonrpc-provider`.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var PromiseThrottle = require('promise-throttle');
|
|
4
|
+
|
|
5
|
+
var __async = (__this, __arguments, generator) => {
|
|
6
|
+
return new Promise((resolve, reject) => {
|
|
7
|
+
var fulfilled = (value) => {
|
|
8
|
+
try {
|
|
9
|
+
step(generator.next(value));
|
|
10
|
+
} catch (e) {
|
|
11
|
+
reject(e);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
var rejected = (value) => {
|
|
15
|
+
try {
|
|
16
|
+
step(generator.throw(value));
|
|
17
|
+
} catch (e) {
|
|
18
|
+
reject(e);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
22
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
class JSONRPCError extends Error {
|
|
26
|
+
constructor(message, cause) {
|
|
27
|
+
super(message);
|
|
28
|
+
this.cause = cause;
|
|
29
|
+
this.isInvalidError = true;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
let counter = 0;
|
|
33
|
+
function ethereum_request(endpoint, req) {
|
|
34
|
+
return __async(this, null, function* () {
|
|
35
|
+
const { method, params } = req;
|
|
36
|
+
if (method === "eth_batch") {
|
|
37
|
+
if (params && params.length === 0) {
|
|
38
|
+
return [];
|
|
39
|
+
}
|
|
40
|
+
const requests = [];
|
|
41
|
+
for (const param of params) {
|
|
42
|
+
requests.push({
|
|
43
|
+
id: ++counter,
|
|
44
|
+
jsonrpc: "2.0",
|
|
45
|
+
method: param.method,
|
|
46
|
+
params: param.params || []
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
let response2;
|
|
50
|
+
try {
|
|
51
|
+
response2 = yield fetch(endpoint, {
|
|
52
|
+
method: "POST",
|
|
53
|
+
headers: { "Content-Type": "application/json" },
|
|
54
|
+
body: JSON.stringify(requests)
|
|
55
|
+
});
|
|
56
|
+
} catch (fetchError) {
|
|
57
|
+
throw new JSONRPCError(`Failed To Batch Fetch at ${endpoint}`, fetchError);
|
|
58
|
+
}
|
|
59
|
+
if (response2.status != 200) {
|
|
60
|
+
throw new JSONRPCError(
|
|
61
|
+
`Failed To Batch Fetch (Status = ${response2.status}) at ${endpoint}`,
|
|
62
|
+
new Error(`status: ${response2.status}`)
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
let jsonArray;
|
|
66
|
+
try {
|
|
67
|
+
jsonArray = yield response2.json();
|
|
68
|
+
} catch (parsingError) {
|
|
69
|
+
throw new JSONRPCError("Failed To Batch parse response as json", parsingError);
|
|
70
|
+
}
|
|
71
|
+
let hasError = false;
|
|
72
|
+
for (const response3 of jsonArray) {
|
|
73
|
+
if (response3.error || !("result" in response3)) {
|
|
74
|
+
hasError = true;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (hasError) {
|
|
78
|
+
throw jsonArray;
|
|
79
|
+
}
|
|
80
|
+
return jsonArray.map((v) => v.result);
|
|
81
|
+
}
|
|
82
|
+
let response;
|
|
83
|
+
try {
|
|
84
|
+
response = yield fetch(endpoint, {
|
|
85
|
+
method: "POST",
|
|
86
|
+
headers: { "Content-Type": "application/json" },
|
|
87
|
+
body: JSON.stringify({
|
|
88
|
+
id: ++counter,
|
|
89
|
+
jsonrpc: "2.0",
|
|
90
|
+
method,
|
|
91
|
+
params: params || []
|
|
92
|
+
})
|
|
93
|
+
});
|
|
94
|
+
} catch (fetchError) {
|
|
95
|
+
throw new JSONRPCError(`Failed To Fetch at ${endpoint} (method: ${method})`, fetchError);
|
|
96
|
+
}
|
|
97
|
+
if (response.status != 200) {
|
|
98
|
+
throw new JSONRPCError(
|
|
99
|
+
`Failed To Fetch (status = ${response.status}) at ${endpoint} (method: ${method})`,
|
|
100
|
+
new Error(`status: ${response.status}`)
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
let json;
|
|
104
|
+
try {
|
|
105
|
+
json = yield response.json();
|
|
106
|
+
} catch (parsingError) {
|
|
107
|
+
throw new JSONRPCError("Failed To parse response json", parsingError);
|
|
108
|
+
}
|
|
109
|
+
if (json.error) {
|
|
110
|
+
throw json.error;
|
|
111
|
+
}
|
|
112
|
+
if (!("result" in json)) {
|
|
113
|
+
throw { code: 5e3, message: "No Result" };
|
|
114
|
+
}
|
|
115
|
+
return json.result;
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
class JSONRPCHTTPProvider {
|
|
119
|
+
constructor(endpoint, options) {
|
|
120
|
+
this.endpoint = endpoint;
|
|
121
|
+
this.supportsETHBatch = true;
|
|
122
|
+
if (options == null ? void 0 : options.requestsPerSecond) {
|
|
123
|
+
this.promiseThrottle = new PromiseThrottle({
|
|
124
|
+
requestsPerSecond: options.requestsPerSecond,
|
|
125
|
+
promiseImplementation: Promise
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
request(args) {
|
|
130
|
+
if (this.promiseThrottle) {
|
|
131
|
+
return this.promiseThrottle.add(ethereum_request.bind(null, this.endpoint, args));
|
|
132
|
+
} else {
|
|
133
|
+
return ethereum_request(this.endpoint, args);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
exports.JSONRPCError = JSONRPCError;
|
|
139
|
+
exports.JSONRPCHTTPProvider = JSONRPCHTTPProvider;
|
|
140
|
+
exports.ethereum_request = ethereum_request;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { EIP1193HttpNodeProvider } from 'eip-1193';
|
|
2
|
+
|
|
3
|
+
declare class JSONRPCError extends Error {
|
|
4
|
+
cause: Error;
|
|
5
|
+
readonly isInvalidError = true;
|
|
6
|
+
constructor(message: string, cause: Error);
|
|
7
|
+
}
|
|
8
|
+
declare function ethereum_request<U extends any, T>(endpoint: string, req: {
|
|
9
|
+
method: string;
|
|
10
|
+
params?: U;
|
|
11
|
+
}): Promise<T>;
|
|
12
|
+
declare class JSONRPCHTTPProvider implements EIP1193HttpNodeProvider {
|
|
13
|
+
protected endpoint: string;
|
|
14
|
+
supportsETHBatch: boolean;
|
|
15
|
+
private promiseThrottle;
|
|
16
|
+
constructor(endpoint: string, options?: {
|
|
17
|
+
requestsPerSecond?: number;
|
|
18
|
+
});
|
|
19
|
+
request(args: {
|
|
20
|
+
method: string;
|
|
21
|
+
params?: any;
|
|
22
|
+
}): Promise<any>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export { JSONRPCError, JSONRPCHTTPProvider, ethereum_request };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import PromiseThrottle from 'promise-throttle';
|
|
2
|
+
|
|
3
|
+
var __async = (__this, __arguments, generator) => {
|
|
4
|
+
return new Promise((resolve, reject) => {
|
|
5
|
+
var fulfilled = (value) => {
|
|
6
|
+
try {
|
|
7
|
+
step(generator.next(value));
|
|
8
|
+
} catch (e) {
|
|
9
|
+
reject(e);
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var rejected = (value) => {
|
|
13
|
+
try {
|
|
14
|
+
step(generator.throw(value));
|
|
15
|
+
} catch (e) {
|
|
16
|
+
reject(e);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
20
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
class JSONRPCError extends Error {
|
|
24
|
+
constructor(message, cause) {
|
|
25
|
+
super(message);
|
|
26
|
+
this.cause = cause;
|
|
27
|
+
this.isInvalidError = true;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
let counter = 0;
|
|
31
|
+
function ethereum_request(endpoint, req) {
|
|
32
|
+
return __async(this, null, function* () {
|
|
33
|
+
const { method, params } = req;
|
|
34
|
+
if (method === "eth_batch") {
|
|
35
|
+
if (params && params.length === 0) {
|
|
36
|
+
return [];
|
|
37
|
+
}
|
|
38
|
+
const requests = [];
|
|
39
|
+
for (const param of params) {
|
|
40
|
+
requests.push({
|
|
41
|
+
id: ++counter,
|
|
42
|
+
jsonrpc: "2.0",
|
|
43
|
+
method: param.method,
|
|
44
|
+
params: param.params || []
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
let response2;
|
|
48
|
+
try {
|
|
49
|
+
response2 = yield fetch(endpoint, {
|
|
50
|
+
method: "POST",
|
|
51
|
+
headers: { "Content-Type": "application/json" },
|
|
52
|
+
body: JSON.stringify(requests)
|
|
53
|
+
});
|
|
54
|
+
} catch (fetchError) {
|
|
55
|
+
throw new JSONRPCError(`Failed To Batch Fetch at ${endpoint}`, fetchError);
|
|
56
|
+
}
|
|
57
|
+
if (response2.status != 200) {
|
|
58
|
+
throw new JSONRPCError(
|
|
59
|
+
`Failed To Batch Fetch (Status = ${response2.status}) at ${endpoint}`,
|
|
60
|
+
new Error(`status: ${response2.status}`)
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
let jsonArray;
|
|
64
|
+
try {
|
|
65
|
+
jsonArray = yield response2.json();
|
|
66
|
+
} catch (parsingError) {
|
|
67
|
+
throw new JSONRPCError("Failed To Batch parse response as json", parsingError);
|
|
68
|
+
}
|
|
69
|
+
let hasError = false;
|
|
70
|
+
for (const response3 of jsonArray) {
|
|
71
|
+
if (response3.error || !("result" in response3)) {
|
|
72
|
+
hasError = true;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (hasError) {
|
|
76
|
+
throw jsonArray;
|
|
77
|
+
}
|
|
78
|
+
return jsonArray.map((v) => v.result);
|
|
79
|
+
}
|
|
80
|
+
let response;
|
|
81
|
+
try {
|
|
82
|
+
response = yield fetch(endpoint, {
|
|
83
|
+
method: "POST",
|
|
84
|
+
headers: { "Content-Type": "application/json" },
|
|
85
|
+
body: JSON.stringify({
|
|
86
|
+
id: ++counter,
|
|
87
|
+
jsonrpc: "2.0",
|
|
88
|
+
method,
|
|
89
|
+
params: params || []
|
|
90
|
+
})
|
|
91
|
+
});
|
|
92
|
+
} catch (fetchError) {
|
|
93
|
+
throw new JSONRPCError(`Failed To Fetch at ${endpoint} (method: ${method})`, fetchError);
|
|
94
|
+
}
|
|
95
|
+
if (response.status != 200) {
|
|
96
|
+
throw new JSONRPCError(
|
|
97
|
+
`Failed To Fetch (status = ${response.status}) at ${endpoint} (method: ${method})`,
|
|
98
|
+
new Error(`status: ${response.status}`)
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
let json;
|
|
102
|
+
try {
|
|
103
|
+
json = yield response.json();
|
|
104
|
+
} catch (parsingError) {
|
|
105
|
+
throw new JSONRPCError("Failed To parse response json", parsingError);
|
|
106
|
+
}
|
|
107
|
+
if (json.error) {
|
|
108
|
+
throw json.error;
|
|
109
|
+
}
|
|
110
|
+
if (!("result" in json)) {
|
|
111
|
+
throw { code: 5e3, message: "No Result" };
|
|
112
|
+
}
|
|
113
|
+
return json.result;
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
class JSONRPCHTTPProvider {
|
|
117
|
+
constructor(endpoint, options) {
|
|
118
|
+
this.endpoint = endpoint;
|
|
119
|
+
this.supportsETHBatch = true;
|
|
120
|
+
if (options == null ? void 0 : options.requestsPerSecond) {
|
|
121
|
+
this.promiseThrottle = new PromiseThrottle({
|
|
122
|
+
requestsPerSecond: options.requestsPerSecond,
|
|
123
|
+
promiseImplementation: Promise
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
request(args) {
|
|
128
|
+
if (this.promiseThrottle) {
|
|
129
|
+
return this.promiseThrottle.add(ethereum_request.bind(null, this.endpoint, args));
|
|
130
|
+
} else {
|
|
131
|
+
return ethereum_request(this.endpoint, args);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export { JSONRPCError, JSONRPCHTTPProvider, ethereum_request };
|
package/package.json
CHANGED
|
@@ -1,10 +1,66 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
2
|
+
"name": "@eip-1193/jsonrpc-provider",
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"keywords": [
|
|
5
|
+
"eip-1193",
|
|
6
|
+
"ethereum",
|
|
7
|
+
"provider",
|
|
8
|
+
"json-rpc",
|
|
9
|
+
"http",
|
|
10
|
+
"batch",
|
|
11
|
+
"throttle"
|
|
12
|
+
],
|
|
13
|
+
"author": "wighawag <wighawag@gmail.com>",
|
|
14
|
+
"homepage": "https://github.com/wighawag/eip-1193-jsonrpc-provider#readme",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/wighawag/eip-1193-jsonrpc-provider.git"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/wighawag/eip-1193-jsonrpc-provider/issues"
|
|
21
|
+
},
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"type": "module",
|
|
27
|
+
"main": "./dist/index.cjs",
|
|
28
|
+
"module": "./dist/index.mjs",
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"require": {
|
|
33
|
+
"types": "./dist/index.d.ts",
|
|
34
|
+
"default": "./dist/index.cjs"
|
|
35
|
+
},
|
|
36
|
+
"import": {
|
|
37
|
+
"types": "./dist/index.d.ts",
|
|
38
|
+
"default": "./dist/index.mjs"
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"files": [
|
|
43
|
+
"dist",
|
|
44
|
+
"src"
|
|
45
|
+
],
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@changesets/cli": "^2.29.8",
|
|
48
|
+
"pkgroll": "^2.0.2",
|
|
49
|
+
"prettier": "^3.2.5",
|
|
50
|
+
"set-defaults": "^0.0.5",
|
|
51
|
+
"typescript": "^5.4.5"
|
|
52
|
+
},
|
|
53
|
+
"dependencies": {
|
|
54
|
+
"eip-1193": "^0.7.0",
|
|
55
|
+
"named-logs": "^0.3.2",
|
|
56
|
+
"promise-throttle": "^1.1.2"
|
|
57
|
+
},
|
|
58
|
+
"scripts": {
|
|
59
|
+
"build": "pkgroll",
|
|
60
|
+
"dev": "pkgroll --watch",
|
|
61
|
+
"format:check": "prettier --check .",
|
|
62
|
+
"test": "pnpm build && node --test test/*.test.mjs",
|
|
63
|
+
"format": "prettier --write .",
|
|
64
|
+
"release:ci": "pnpm format:check && pnpm test && pnpm changeset publish"
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import type {EIP1193HttpNodeProvider} from 'eip-1193';
|
|
2
|
+
import PromiseThrottle from 'promise-throttle';
|
|
3
|
+
|
|
4
|
+
export class JSONRPCError extends Error {
|
|
5
|
+
public readonly isInvalidError = true;
|
|
6
|
+
constructor(
|
|
7
|
+
message: string,
|
|
8
|
+
public cause: Error,
|
|
9
|
+
) {
|
|
10
|
+
super(message);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
let counter = 0;
|
|
15
|
+
export async function ethereum_request<U extends any, T>(
|
|
16
|
+
endpoint: string,
|
|
17
|
+
req: {method: string; params?: U},
|
|
18
|
+
): Promise<T> {
|
|
19
|
+
const {method, params} = req;
|
|
20
|
+
// NOTE: special case to allow batch request via EIP-1193
|
|
21
|
+
if (method === 'eth_batch') {
|
|
22
|
+
if (params && (params as any[]).length === 0) {
|
|
23
|
+
return [] as unknown as T;
|
|
24
|
+
}
|
|
25
|
+
const requests = [];
|
|
26
|
+
for (const param of params as {method: string; params?: any[]}[]) {
|
|
27
|
+
requests.push({
|
|
28
|
+
id: ++counter,
|
|
29
|
+
jsonrpc: '2.0',
|
|
30
|
+
method: param.method,
|
|
31
|
+
params: param.params || [],
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
let response: Response;
|
|
36
|
+
try {
|
|
37
|
+
response = await fetch(endpoint, {
|
|
38
|
+
method: 'POST',
|
|
39
|
+
headers: {'Content-Type': 'application/json'},
|
|
40
|
+
body: JSON.stringify(requests),
|
|
41
|
+
});
|
|
42
|
+
} catch (fetchError) {
|
|
43
|
+
throw new JSONRPCError(`Failed To Batch Fetch at ${endpoint}`, fetchError);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (response.status != 200) {
|
|
47
|
+
throw new JSONRPCError(
|
|
48
|
+
`Failed To Batch Fetch (Status = ${response.status}) at ${endpoint}`,
|
|
49
|
+
new Error(`status: ${response.status}`),
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
let jsonArray: {result?: T; error?: any}[];
|
|
54
|
+
try {
|
|
55
|
+
jsonArray = await response.json();
|
|
56
|
+
} catch (parsingError) {
|
|
57
|
+
throw new JSONRPCError('Failed To Batch parse response as json', parsingError);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
let hasError = false;
|
|
61
|
+
for (const response of jsonArray) {
|
|
62
|
+
// `null`, `false` and `0` are valid results (a pending receipt, `eth_syncing`,
|
|
63
|
+
// `hardhat_impersonateAccount`): only an error, or a missing `result` member, is a failure.
|
|
64
|
+
if (response.error || !('result' in response)) {
|
|
65
|
+
hasError = true;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (hasError) {
|
|
70
|
+
throw jsonArray;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return jsonArray.map((v) => v.result) as unknown as T;
|
|
74
|
+
}
|
|
75
|
+
let response: Response;
|
|
76
|
+
try {
|
|
77
|
+
response = await fetch(endpoint, {
|
|
78
|
+
method: 'POST',
|
|
79
|
+
headers: {'Content-Type': 'application/json'},
|
|
80
|
+
body: JSON.stringify({
|
|
81
|
+
id: ++counter,
|
|
82
|
+
jsonrpc: '2.0',
|
|
83
|
+
method,
|
|
84
|
+
params: params || [],
|
|
85
|
+
}),
|
|
86
|
+
});
|
|
87
|
+
} catch (fetchError) {
|
|
88
|
+
throw new JSONRPCError(`Failed To Fetch at ${endpoint} (method: ${method})`, fetchError);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (response.status != 200) {
|
|
92
|
+
throw new JSONRPCError(
|
|
93
|
+
`Failed To Fetch (status = ${response.status}) at ${endpoint} (method: ${method})`,
|
|
94
|
+
new Error(`status: ${response.status}`),
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
let json: {result?: T; error?: any};
|
|
98
|
+
try {
|
|
99
|
+
json = await response.json();
|
|
100
|
+
} catch (parsingError) {
|
|
101
|
+
throw new JSONRPCError('Failed To parse response json', parsingError);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (json.error) {
|
|
105
|
+
throw json.error;
|
|
106
|
+
}
|
|
107
|
+
// A JSON-RPC response carries EITHER `error` OR `result`, and `result` may legitimately be
|
|
108
|
+
// `null` or `false` (a pending `eth_getTransactionReceipt`, `eth_syncing`, and
|
|
109
|
+
// `hardhat_impersonateAccount`, which anvil and hardhat answer with `null`). Only a response
|
|
110
|
+
// with neither member is malformed.
|
|
111
|
+
if (!('result' in json)) {
|
|
112
|
+
throw {code: 5000, message: 'No Result'};
|
|
113
|
+
}
|
|
114
|
+
return json.result as T;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export class JSONRPCHTTPProvider implements EIP1193HttpNodeProvider {
|
|
118
|
+
supportsETHBatch = true;
|
|
119
|
+
private promiseThrottle: PromiseThrottle | undefined;
|
|
120
|
+
constructor(
|
|
121
|
+
protected endpoint: string,
|
|
122
|
+
options?: {requestsPerSecond?: number},
|
|
123
|
+
) {
|
|
124
|
+
if (options?.requestsPerSecond) {
|
|
125
|
+
this.promiseThrottle = new PromiseThrottle({
|
|
126
|
+
requestsPerSecond: options.requestsPerSecond,
|
|
127
|
+
promiseImplementation: Promise,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
request(args: {method: string; params?: any}): Promise<any>;
|
|
133
|
+
request<T, U extends any = any>(args: {method: string; params: U}): Promise<T> {
|
|
134
|
+
if (this.promiseThrottle) {
|
|
135
|
+
return this.promiseThrottle.add(ethereum_request.bind(null, this.endpoint, args));
|
|
136
|
+
} else {
|
|
137
|
+
return ethereum_request<U, T>(this.endpoint, args);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './JSONRPCHTTPProvider';
|
package/.npm-publish.log
DELETED
|
File without changes
|