-
Notifications
You must be signed in to change notification settings - Fork 10
/
jsonrpc-provider.ts
627 lines (560 loc) · 18.1 KB
/
jsonrpc-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
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
// eslint-disable-next-line max-classes-per-file
import { BigNumber } from '@ethersproject/bignumber';
import { Logger } from '@ethersproject/logger';
import {
deepCopy, Deferrable,
defineReadOnly,
getStatic, resolveProperties,
shallowCopy
} from '@ethersproject/properties';
import { ConnectionInfo, fetchJson } from '@ethersproject/web';
import { Bytes } from '@ethersproject/bytes';
import { getNetwork, Network, Networkish } from '../networks';
import { version } from '../version';
import { BaseProvider, CONSTANTS, Event, RPC_ACTION } from './base-provider';
// eslint-disable-next-line import/order
import { AccountAddress, ChainId, TransactionRequest } from '../types';
import { Signer } from '../abstract-signer';
import { Provider } from '../abstract-provider';
const logger = new Logger(version);
const errorGas = new Set(['call', 'estimateGas']);
// FIXME: recheck the error.
function checkError(method: string, error: any, params: any): never {
let {message} = error;
if (
error.code === Logger.errors.SERVER_ERROR &&
error.error &&
typeof error.error.message === 'string'
) {
message = error.error.message;
} else if (typeof error.body === 'string') {
message = error.body;
} else if (typeof error.responseText === 'string') {
message = error.responseText;
}
message = (message || '').toLowerCase();
const transaction = params.transaction || params.signedTransaction;
// "insufficient funds for gas * price + value + cost(data)"
if (message.match(/insufficient funds/)) {
logger.throwError(
'insufficient funds for intrinsic transaction cost',
Logger.errors.INSUFFICIENT_FUNDS,
{
error,
method,
transaction
}
);
}
// "nonce too low"
if (message.match(/nonce too low/)) {
logger.throwError(
'nonce has already been used',
Logger.errors.NONCE_EXPIRED,
{
error,
method,
transaction
}
);
}
// "replacement transaction underpriced"
if (message.match(/replacement transaction underpriced/)) {
logger.throwError(
'replacement fee too low',
Logger.errors.REPLACEMENT_UNDERPRICED,
{
error,
method,
transaction
}
);
}
if (
errorGas.has(method) &&
message.match(
/gas required exceeds allowance|always failing transaction|execution reverted/
)
) {
logger.throwError(
'cannot estimate gas; transaction may fail or may require manual gas limit',
Logger.errors.UNPREDICTABLE_GAS_LIMIT,
{
error,
method,
transaction
}
);
}
throw error;
}
function timer(timeout: number): Promise<any> {
return new Promise(function(resolve) {
setTimeout(resolve, timeout);
});
}
function getResult(payload: {
error?: { code?: number; data?: any; message?: string };
result?: any;
}): any {
if (payload.error) {
// @TODO: not any
const error: any = new Error(payload.error.message);
error.code = payload.error.code;
error.data = payload.error.data;
throw error;
}
return payload.result;
}
const _constructorGuard = {};
export class JsonRpcSigner extends Signer {
// eslint-disable-next-line no-use-before-define
readonly provider: JsonrpcProvider;
_index?: number;
_address?: string;
// eslint-disable-next-line no-use-before-define
constructor(constructorGuard: any, provider: JsonrpcProvider, addressOrIndex?: string | number) {
logger.checkNew(new.target, JsonRpcSigner);
super();
if (constructorGuard !== _constructorGuard) {
throw new Error('do not call the JsonRpcSigner constructor directly; use provider.getSigner');
}
defineReadOnly(this, 'provider', provider);
// eslint-disable-next-line no-param-reassign
if (addressOrIndex === undefined) {
addressOrIndex = 0;
}
if (typeof (addressOrIndex) === 'string') {
defineReadOnly(this, '_address', this.provider.formatter.address(addressOrIndex));
} else if (typeof (addressOrIndex) === 'number') {
defineReadOnly(this, '_index', addressOrIndex);
} else {
logger.throwArgumentError('invalid address or index', 'addressOrIndex', addressOrIndex);
}
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars,class-methods-use-this
connect(provider: Provider): JsonRpcSigner {
return logger.throwError('cannot alter JSON-RPC Signer connection', Logger.errors.UNSUPPORTED_OPERATION, {
operation: 'connect'
});
}
// connectUnchecked(): JsonRpcSigner {
// return new UncheckedJsonRpcSigner(_constructorGuard, this.provider, this._address || this._index);
// }
async getAddress(): Promise<string> {
// eslint-disable-next-line no-underscore-dangle
if (this._address) {
// eslint-disable-next-line no-underscore-dangle
return Promise.resolve(this._address);
}
return this.provider.listAccounts().then((accounts) => {
// eslint-disable-next-line no-underscore-dangle
if (accounts.length <= this._index) {
// eslint-disable-next-line no-underscore-dangle
logger.throwError(`unknown account #${this._index}`, Logger.errors.UNSUPPORTED_OPERATION, {
operation: 'getAddress'
});
}
// eslint-disable-next-line no-underscore-dangle
return accounts[this._index];
});
}
async signTransaction(transaction: Deferrable<TransactionRequest>): Promise<string> {
// eslint-disable-next-line no-param-reassign
const request = await resolveProperties(transaction);
const sender = await this.getAddress();
if (request.sender !== undefined) {
if (request.sender !== sender) {
logger.throwArgumentError('from address mismatch', 'transaction', transaction);
}
} else {
request.sender = sender;
}
return this.provider.send('account.sign_txn_request', [request]).then((hexTxnData) => {
return hexTxnData;
},
(error) => {
return checkError('signTransaction', error, request);
});
}
// eslint-disable-next-line class-methods-use-this,@typescript-eslint/no-unused-vars
async signMessage(message: Bytes | string): Promise<string> {
return logger.throwError('signing message is unsupported', Logger.errors.UNSUPPORTED_OPERATION, {
operation: 'signMessage'
});
// const data = ((typeof(message) === "string") ? toUtf8Bytes(message): message);
// const address = await this.getAddress();
//
// // https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_sign
// return this.provider.send("eth_sign", [ address.toLowerCase(), hexlify(data) ]);
}
async unlock(password: string): Promise<void> {
const { provider } = this;
const address = await this.getAddress();
return provider.send('account.unlock', [address.toLowerCase(), password, undefined]);
}
}
// class UncheckedJsonRpcSigner extends JsonRpcSigner {
// sendTransaction(transaction: Deferrable<TransactionRequest>): Promise<TransactionResponse> {
// return this.sendUncheckedTransaction(transaction).then((hash) => {
// return <TransactionResponse>{
// hash: hash,
// nonce: null,
// gasLimit: null,
// gasPrice: null,
// data: null,
// value: null,
// chainId: null,
// confirmations: 0,
// from: null,
// wait: (confirmations?: number) => { return this.provider.waitForTransaction(hash, confirmations); }
// };
// });
// }
// }
export class JsonrpcProvider extends BaseProvider {
readonly connection: ConnectionInfo;
_pendingFilter: Promise<number>;
_nextId: number;
constructor(url?: ConnectionInfo | string, network?: Networkish) {
logger.checkNew(new.target, JsonrpcProvider);
let networkOrReady: Networkish | Promise<Network> = network;
// The network is unknown, query the JSON-RPC for it
if (networkOrReady == null) {
networkOrReady = new Promise((resolve, reject) => {
setTimeout(() => {
this.detectNetwork().then(
(network) => {
resolve(network);
},
(error) => {
reject(error);
}
);
}, 0);
});
}
super(networkOrReady);
// Default URL
if (!url) {
url = getStatic<() => string>(this.constructor, 'defaultUrl')();
}
if (typeof url === 'string') {
defineReadOnly(
this,
'connection',
Object.freeze({
url: url
})
);
} else {
defineReadOnly(this, 'connection', Object.freeze(shallowCopy(url)));
}
this._nextId = 42;
}
static defaultUrl(): string {
return 'http://localhost:9850';
}
async detectNetwork(): Promise<Network> {
await timer(0);
let chainId = null;
try {
const resp = (await this.send('chain.id', [])) as ChainId;
chainId = resp.id;
} catch (error) {
try {
const chainInfo = await this.perform(RPC_ACTION.getChainInfo, null);
chainId = chainInfo.chain_id;
// eslint-disable-next-line no-empty
} catch (error) {
}
}
if (chainId != null) {
try {
return getNetwork(BigNumber.from(chainId).toNumber());
} catch (error) {
return logger.throwError(
'could not detect network',
Logger.errors.NETWORK_ERROR,
{
chainId: chainId,
event: 'invalidNetwork',
serverError: error
}
);
}
}
return logger.throwError(
'could not detect network',
Logger.errors.NETWORK_ERROR,
{
event: 'noNetwork'
}
);
}
getSigner(addressOrIndex?: string | number): JsonRpcSigner {
return new JsonRpcSigner(_constructorGuard, this, addressOrIndex);
}
// getUncheckedSigner(addressOrIndex?: string | number): UncheckedJsonRpcSigner {
// return this.getSigner(addressOrIndex).connectUnchecked();
// }
listAccounts(): Promise<Array<string>> {
// eslint-disable-next-line @typescript-eslint/ban-types
return this.send('account.list', []).then((accounts: Array<{ address: AccountAddress }>) => {
return accounts.map(({ address }) => this.formatter.address(address));
});
}
send(method: string, params: Array<any>): Promise<any> {
const request = {
method: method,
params: params,
id: this._nextId++,
jsonrpc: '2.0'
};
this.emit('debug', {
action: 'request',
request: deepCopy(request),
provider: this
});
return fetchJson(this.connection, JSON.stringify(request), getResult).then(
(result) => {
this.emit('debug', {
action: 'response',
request: request,
response: result,
provider: this
});
return result;
},
(error) => {
this.emit('debug', {
action: 'response',
error: error,
request: request,
provider: this
});
throw error;
}
);
}
// eslint-disable-next-line consistent-return
prepareRequest(method: string, params: any): [string, Array<any>] {
switch (method) {
case RPC_ACTION.getChainInfo:
return ['chain.info', []];
case RPC_ACTION.getGasPrice:
return ['txpool.gas_price', []];
case RPC_ACTION.dryRun:
return ['contract.dry_run', [params.transaction]];
// case 'getBalance':
// return [
// 'eth_getBalance',
// [getLowerCase(params.address), params.blockTag],
// ];
// case 'getTransactionCount':
// return [
// 'eth_getTransactionCount',
// [getLowerCase(params.address), params.blockTag],
// ];
// case 'getCode':
// return ['eth_getCode', [getLowerCase(params.address), params.blockTag]];
//
// case 'getStorageAt':
// return [
// 'eth_getStorageAt',
// [getLowerCase(params.address), params.position, params.blockTag],
// ];
case RPC_ACTION.sendTransaction:
return ['txpool.submit_hex_transaction', [params.signedTransaction]];
case RPC_ACTION.getBlock:
if (params.blockNumber !== undefined) {
return ['chain.get_block_by_number', [params.blockNumber]];
}
if (params.blockHash !== undefined) {
return ['chain.get_block_by_hash', [params.blockHash]];
}
break;
case RPC_ACTION.getTransactionByHash:
return ['chain.get_transaction', [params.transactionHash]];
case RPC_ACTION.getTransactionInfo:
return ['chain.get_transaction_info', [params.transactionHash]];
case RPC_ACTION.getCode:
return ['contract.get_code', [params.moduleId]];
case RPC_ACTION.getResource:
return ['contract.get_resource', [params.address, params.structTag]];
case RPC_ACTION.getAccountState:
return ['state.get_account_state_set', [params.address]];
case RPC_ACTION.call:
return [
'contract.call',
[
params.request
]
];
// case 'estimateGas': {
// const hexlifyTransaction = getStatic<
// (
// t: TransactionRequest,
// a?: { [key: string]: boolean }
// ) => { [key: string]: string }
// >(this.constructor, 'hexlifyTransaction');
// return [
// 'eth_estimateGas',
// [hexlifyTransaction(params.transaction, { from: true })],
// ];
// }
case RPC_ACTION.getEvents:
return ['chain.get_events', [params.filter]];
default:
break;
// if (params instanceof Array) {
// return [method, params];
// } else {
// return [method, [params]];
// }
}
}
async perform(method: string, params: any): Promise<any> {
const args = this.prepareRequest(method, params);
if (args === undefined) {
logger.throwError(
`${method} not implemented`,
Logger.errors.NOT_IMPLEMENTED,
{ operation: method }
);
}
try {
return await this.send(args[0], args[1]);
} catch (error) {
return checkError(method, error, params);
}
}
_startEvent(event: Event): void {
if (event.tag === 'pending') {
// this._startPending();
logger.throwError(
'pending event not implemented',
Logger.errors.NOT_IMPLEMENTED,
{ operation: 'pending event' }
);
}
super._startEvent(event);
}
// _startPending(): void {
// if (this._pendingFilter != null) {
// return;
// }
// // eslint-disable-next-line @typescript-eslint/no-this-alias
// const self = this;
//
// const pendingFilter: Promise<number> = this.send(
// 'eth_newPendingTransactionFilter',
// []
// );
// this._pendingFilter = pendingFilter;
//
// pendingFilter
// .then(function (filterId) {
// function poll() {
// self
// .send('eth_getFilterChanges', [filterId])
// .then(function (hashes: Array<string>) {
// if (self._pendingFilter != pendingFilter) {
// return null;
// }
//
// let seq = Promise.resolve();
// hashes.forEach(function (hash) {
// // @TODO: This should be garbage collected at some point... How? When?
// // @ts-ignore
// self._emitted['t:' + hash.toLowerCase()] = CONSTANTS.pending;
// seq = seq.then(function () {
// return self.getTransaction(hash).then(function (tx) {
// self.emit(CONSTANTS.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;
// })
// // eslint-disable-next-line @typescript-eslint/no-empty-function
// .catch((error: Error) => {});
// }
// poll();
//
// return filterId;
// })
// // eslint-disable-next-line @typescript-eslint/no-empty-function
// .catch((error: Error) => {});
// }
_stopEvent(event: Event): void {
if (
event.tag === CONSTANTS.pending &&
this.listenerCount(CONSTANTS.pending) === 0
) {
this._pendingFilter = null;
}
super._stopEvent(event);
}
// // Convert an ethers.js transaction into a JSON-RPC transaction
// // - gasLimit => gas
// // - All values hexlified
// // - All numeric values zero-striped
// // - All addresses are lowercased
// // NOTE: This allows a TransactionRequest, but all values should be resolved
// // before this is called
// // @TODO: This will likely be removed in future versions and prepareRequest
// // will be the preferred method for this.
// static hexlifyTransaction(
// transaction: TransactionRequest,
// allowExtra?: { [key: string]: boolean }
// ): { [key: string]: string } {
// // Check only allowed properties are given
// const allowed = shallowCopy(allowedTransactionKeys);
// if (allowExtra) {
// for (const key in allowExtra) {
// if (allowExtra[key]) {
// allowed[key] = true;
// }
// }
// }
// checkProperties(transaction, allowed);
//
// const result: { [key: string]: string } = {};
//
// // Some nodes (INFURA ropsten; INFURA mainnet is fine) do not like leading zeros.
// ['gasLimit', 'gasPrice', 'nonce', 'value'].forEach(function (key) {
// if ((<any>transaction)[key] == null) {
// return;
// }
// const value = hexValue((<any>transaction)[key]);
// if (key === 'gasLimit') {
// key = 'gas';
// }
// result[key] = value;
// });
//
// ['from', 'to', 'data'].forEach(function (key) {
// if ((<any>transaction)[key] == null) {
// return;
// }
// result[key] = hexlify((<any>transaction)[key]);
// });
//
// return result;
// }
}