-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
json-rpc-provider.js
508 lines (508 loc) · 23 KB
/
json-rpc-provider.js
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
var abstract_signer_1 = require("@ethersproject/abstract-signer");
var bignumber_1 = require("@ethersproject/bignumber");
var bytes_1 = require("@ethersproject/bytes");
var properties_1 = require("@ethersproject/properties");
var strings_1 = require("@ethersproject/strings");
var web_1 = require("@ethersproject/web");
var logger_1 = require("@ethersproject/logger");
var _version_1 = require("./_version");
var logger = new logger_1.Logger(_version_1.version);
var base_provider_1 = require("./base-provider");
function timer(timeout) {
return new Promise(function (resolve) {
setTimeout(function () {
resolve();
}, timeout);
});
}
function getResult(payload) {
if (payload.error) {
// @TODO: not any
var error = new Error(payload.error.message);
error.code = payload.error.code;
error.data = payload.error.data;
throw error;
}
return payload.result;
}
function getLowerCase(value) {
if (value) {
return value.toLowerCase();
}
return value;
}
var _constructorGuard = {};
var JsonRpcSigner = /** @class */ (function (_super) {
__extends(JsonRpcSigner, _super);
function JsonRpcSigner(constructorGuard, provider, addressOrIndex) {
var _newTarget = this.constructor;
var _this = this;
logger.checkNew(_newTarget, JsonRpcSigner);
_this = _super.call(this) || this;
if (constructorGuard !== _constructorGuard) {
throw new Error("do not call the JsonRpcSigner constructor directly; use provider.getSigner");
}
properties_1.defineReadOnly(_this, "provider", provider);
if (addressOrIndex == null) {
addressOrIndex = 0;
}
if (typeof (addressOrIndex) === "string") {
properties_1.defineReadOnly(_this, "_address", _this.provider.formatter.address(addressOrIndex));
properties_1.defineReadOnly(_this, "_index", null);
}
else if (typeof (addressOrIndex) === "number") {
properties_1.defineReadOnly(_this, "_index", addressOrIndex);
properties_1.defineReadOnly(_this, "_address", null);
}
else {
logger.throwArgumentError("invalid address or index", "addressOrIndex", addressOrIndex);
}
return _this;
}
JsonRpcSigner.prototype.connect = function (provider) {
return logger.throwError("cannot alter JSON-RPC Signer connection", logger_1.Logger.errors.UNSUPPORTED_OPERATION, {
operation: "connect"
});
};
JsonRpcSigner.prototype.connectUnchecked = function () {
return new UncheckedJsonRpcSigner(_constructorGuard, this.provider, this._address || this._index);
};
JsonRpcSigner.prototype.getAddress = function () {
var _this = this;
if (this._address) {
return Promise.resolve(this._address);
}
return this.provider.send("eth_accounts", []).then(function (accounts) {
if (accounts.length <= _this._index) {
logger.throwError("unknown account #" + _this._index, logger_1.Logger.errors.UNSUPPORTED_OPERATION, {
operation: "getAddress"
});
}
return _this.provider.formatter.address(accounts[_this._index]);
});
};
JsonRpcSigner.prototype.sendUncheckedTransaction = function (transaction) {
var _this = this;
transaction = properties_1.shallowCopy(transaction);
var fromAddress = this.getAddress().then(function (address) {
if (address) {
address = address.toLowerCase();
}
return address;
});
// The JSON-RPC for eth_sendTransaction uses 90000 gas; if the user
// wishes to use this, it is easy to specify explicitly, otherwise
// we look it up for them.
if (transaction.gasLimit == null) {
var estimate = properties_1.shallowCopy(transaction);
estimate.from = fromAddress;
transaction.gasLimit = this.provider.estimateGas(estimate);
}
return Promise.all([
properties_1.resolveProperties(transaction),
fromAddress
]).then(function (results) {
var tx = results[0];
var hexTx = _this.provider.constructor.hexlifyTransaction(tx);
hexTx.from = results[1];
return _this.provider.send("eth_sendTransaction", [hexTx]).then(function (hash) {
return hash;
}, function (error) {
if (error.responseText) {
// See: JsonRpcProvider.sendTransaction (@TODO: Expose a ._throwError??)
if (error.responseText.indexOf("insufficient funds") >= 0) {
logger.throwError("insufficient funds", logger_1.Logger.errors.INSUFFICIENT_FUNDS, {
transaction: tx
});
}
if (error.responseText.indexOf("nonce too low") >= 0) {
logger.throwError("nonce has already been used", logger_1.Logger.errors.NONCE_EXPIRED, {
transaction: tx
});
}
if (error.responseText.indexOf("replacement transaction underpriced") >= 0) {
logger.throwError("replacement fee too low", logger_1.Logger.errors.REPLACEMENT_UNDERPRICED, {
transaction: tx
});
}
}
throw error;
});
});
};
JsonRpcSigner.prototype.signTransaction = function (transaction) {
return logger.throwError("signing transactions is unsupported", logger_1.Logger.errors.UNSUPPORTED_OPERATION, {
operation: "signTransaction"
});
};
JsonRpcSigner.prototype.sendTransaction = function (transaction) {
var _this = this;
return this.sendUncheckedTransaction(transaction).then(function (hash) {
return web_1.poll(function () {
return _this.provider.getTransaction(hash).then(function (tx) {
if (tx === null) {
return undefined;
}
return _this.provider._wrapTransaction(tx, hash);
});
}, { onceBlock: _this.provider }).catch(function (error) {
error.transactionHash = hash;
throw error;
});
});
};
JsonRpcSigner.prototype.signMessage = function (message) {
var _this = this;
var data = ((typeof (message) === "string") ? strings_1.toUtf8Bytes(message) : message);
return this.getAddress().then(function (address) {
// https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_sign
return _this.provider.send("eth_sign", [address.toLowerCase(), bytes_1.hexlify(data)]);
});
};
JsonRpcSigner.prototype.unlock = function (password) {
var provider = this.provider;
return this.getAddress().then(function (address) {
return provider.send("personal_unlockAccount", [address.toLowerCase(), password, null]);
});
};
return JsonRpcSigner;
}(abstract_signer_1.Signer));
exports.JsonRpcSigner = JsonRpcSigner;
var UncheckedJsonRpcSigner = /** @class */ (function (_super) {
__extends(UncheckedJsonRpcSigner, _super);
function UncheckedJsonRpcSigner() {
return _super !== null && _super.apply(this, arguments) || this;
}
UncheckedJsonRpcSigner.prototype.sendTransaction = function (transaction) {
var _this = this;
return this.sendUncheckedTransaction(transaction).then(function (hash) {
return {
hash: hash,
nonce: null,
gasLimit: null,
gasPrice: null,
data: null,
value: null,
chainId: null,
confirmations: 0,
from: null,
wait: function (confirmations) { return _this.provider.waitForTransaction(hash, confirmations); }
};
});
};
return UncheckedJsonRpcSigner;
}(JsonRpcSigner));
var allowedTransactionKeys = {
chainId: true, data: true, gasLimit: true, gasPrice: true, nonce: true, to: true, value: true
};
var JsonRpcProvider = /** @class */ (function (_super) {
__extends(JsonRpcProvider, _super);
function JsonRpcProvider(url, network) {
var _newTarget = this.constructor;
var _this = this;
logger.checkNew(_newTarget, JsonRpcProvider);
var getNetwork = properties_1.getStatic((_newTarget), "getNetwork");
// One parameter, but it is a network name, so swap it with the URL
if (typeof (url) === "string") {
if (network === null) {
var checkNetwork = getNetwork(url);
network = checkNetwork;
url = null;
}
}
if (network) {
// The network has been specified explicitly, we can use it
_this = _super.call(this, network) || this;
}
else {
// The network is unknown, query the JSON-RPC for it
var ready = new Promise(function (resolve, reject) {
setTimeout(function () { return __awaiter(_this, void 0, void 0, function () {
var chainId, error_1, error_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
chainId = null;
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 8]);
return [4 /*yield*/, this.send("eth_chainId", [])];
case 2:
chainId = _a.sent();
return [3 /*break*/, 8];
case 3:
error_1 = _a.sent();
_a.label = 4;
case 4:
_a.trys.push([4, 6, , 7]);
return [4 /*yield*/, this.send("net_version", [])];
case 5:
chainId = _a.sent();
return [3 /*break*/, 7];
case 6:
error_2 = _a.sent();
return [3 /*break*/, 7];
case 7: return [3 /*break*/, 8];
case 8:
if (chainId != null) {
try {
return [2 /*return*/, resolve(getNetwork(bignumber_1.BigNumber.from(chainId).toNumber()))];
}
catch (error) {
console.log("e3", error);
}
}
reject(logger.makeError("could not detect network", logger_1.Logger.errors.NETWORK_ERROR));
return [2 /*return*/];
}
});
}); }, 0);
});
_this = _super.call(this, ready) || this;
}
// Default URL
if (!url) {
url = "http:/" + "/localhost:8545";
}
if (typeof (url) === "string") {
_this.connection = Object.freeze({
url: url
});
}
else {
_this.connection = Object.freeze(properties_1.shallowCopy(url));
}
_this._nextId = 42;
return _this;
}
JsonRpcProvider.prototype.getSigner = function (addressOrIndex) {
return new JsonRpcSigner(_constructorGuard, this, addressOrIndex);
};
JsonRpcProvider.prototype.getUncheckedSigner = function (addressOrIndex) {
return this.getSigner(addressOrIndex).connectUnchecked();
};
JsonRpcProvider.prototype.listAccounts = function () {
var _this = this;
return this.send("eth_accounts", []).then(function (accounts) {
return accounts.map(function (a) { return _this.formatter.address(a); });
});
};
JsonRpcProvider.prototype.send = function (method, params) {
var _this = this;
var request = {
method: method,
params: params,
id: (this._nextId++),
jsonrpc: "2.0"
};
this.emit("debug", {
action: "request",
request: properties_1.deepCopy(request),
provider: this
});
return web_1.fetchJson(this.connection, JSON.stringify(request), getResult).then(function (result) {
_this.emit("debug", {
action: "response",
request: request,
response: result,
provider: _this
});
return result;
});
};
JsonRpcProvider.prototype.perform = function (method, params) {
switch (method) {
case "getBlockNumber":
return this.send("eth_blockNumber", []);
case "getGasPrice":
return this.send("eth_gasPrice", []);
case "getBalance":
return this.send("eth_getBalance", [getLowerCase(params.address), params.blockTag]);
case "getTransactionCount":
return this.send("eth_getTransactionCount", [getLowerCase(params.address), params.blockTag]);
case "getCode":
return this.send("eth_getCode", [getLowerCase(params.address), params.blockTag]);
case "getStorageAt":
return this.send("eth_getStorageAt", [getLowerCase(params.address), params.position, params.blockTag]);
case "sendTransaction":
return this.send("eth_sendRawTransaction", [params.signedTransaction]).catch(function (error) {
if (error.responseText) {
// "insufficient funds for gas * price + value"
if (error.responseText.indexOf("insufficient funds") > 0) {
logger.throwError("insufficient funds", logger_1.Logger.errors.INSUFFICIENT_FUNDS, {});
}
// "nonce too low"
if (error.responseText.indexOf("nonce too low") > 0) {
logger.throwError("nonce has already been used", logger_1.Logger.errors.NONCE_EXPIRED, {});
}
// "replacement transaction underpriced"
if (error.responseText.indexOf("replacement transaction underpriced") > 0) {
logger.throwError("replacement fee too low", logger_1.Logger.errors.REPLACEMENT_UNDERPRICED, {});
}
}
throw error;
});
case "getBlock":
if (params.blockTag) {
return this.send("eth_getBlockByNumber", [params.blockTag, !!params.includeTransactions]);
}
else if (params.blockHash) {
return this.send("eth_getBlockByHash", [params.blockHash, !!params.includeTransactions]);
}
return logger.throwArgumentError("invalid block tag or block hash", "params", params);
case "getTransaction":
return this.send("eth_getTransactionByHash", [params.transactionHash]);
case "getTransactionReceipt":
return this.send("eth_getTransactionReceipt", [params.transactionHash]);
case "call": {
var hexlifyTransaction = properties_1.getStatic(this.constructor, "hexlifyTransaction");
return this.send("eth_call", [hexlifyTransaction(params.transaction, { from: true }), params.blockTag]);
}
case "estimateGas": {
var hexlifyTransaction = properties_1.getStatic(this.constructor, "hexlifyTransaction");
return this.send("eth_estimateGas", [hexlifyTransaction(params.transaction, { from: true })]);
}
case "getLogs":
if (params.filter && params.filter.address != null) {
params.filter.address = getLowerCase(params.filter.address);
}
return this.send("eth_getLogs", [params.filter]);
default:
break;
}
return logger.throwError(method + " not implemented", logger_1.Logger.errors.NOT_IMPLEMENTED, { operation: method });
};
JsonRpcProvider.prototype._startPending = function () {
if (this._pendingFilter != null) {
return;
}
var self = this;
var pendingFilter = this.send("eth_newPendingTransactionFilter", []);
this._pendingFilter = pendingFilter;
pendingFilter.then(function (filterId) {
function poll() {
self.send("eth_getFilterChanges", [filterId]).then(function (hashes) {
if (self._pendingFilter != pendingFilter) {
return null;
}
var seq = Promise.resolve();
hashes.forEach(function (hash) {
// @TODO: This should be garbage collected at some point... How? When?
self._emitted["t:" + hash.toLowerCase()] = "pending";
seq = seq.then(function () {
return self.getTransaction(hash).then(function (tx) {
self.emit("pending", tx);
return null;
});
});
});
return seq.then(function () {
return timer(1000);
});
}).then(function () {
if (self._pendingFilter != pendingFilter) {
self.send("eth_uninstallFilter", [filterId]);
return;
}
setTimeout(function () { poll(); }, 0);
return null;
}).catch(function (error) { });
}
poll();
return filterId;
}).catch(function (error) { });
};
JsonRpcProvider.prototype._stopPending = function () {
this._pendingFilter = null;
};
// Convert an ethers.js transaction into a JSON-RPC transaction
// - gasLimit => gas
// - All values hexlified
// - All numeric values zero-striped
// NOTE: This allows a TransactionRequest, but all values should be resolved
// before this is called
JsonRpcProvider.hexlifyTransaction = function (transaction, allowExtra) {
// Check only allowed properties are given
var allowed = properties_1.shallowCopy(allowedTransactionKeys);
if (allowExtra) {
for (var key in allowExtra) {
if (allowExtra[key]) {
allowed[key] = true;
}
}
}
properties_1.checkProperties(transaction, allowed);
var result = {};
// Some nodes (INFURA ropsten; INFURA mainnet is fine) do not like leading zeros.
["gasLimit", "gasPrice", "nonce", "value"].forEach(function (key) {
if (transaction[key] == null) {
return;
}
var value = bytes_1.hexValue(transaction[key]);
if (key === "gasLimit") {
key = "gas";
}
result[key] = value;
});
["from", "to", "data"].forEach(function (key) {
if (transaction[key] == null) {
return;
}
result[key] = bytes_1.hexlify(transaction[key]);
});
return result;
};
return JsonRpcProvider;
}(base_provider_1.BaseProvider));
exports.JsonRpcProvider = JsonRpcProvider;