-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
base-provider.ts
1074 lines (830 loc) · 36.5 KB
/
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
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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"use strict";
import {
Block, BlockTag, BlockWithTransactions, EventType, Filter, FilterByBlockHash, ForkEvent,
Listener, Log, Provider, TransactionReceipt, TransactionRequest, TransactionResponse
} from "@ethersproject/abstract-provider";
import { BigNumber, BigNumberish } from "@ethersproject/bignumber";
import { arrayify, hexDataLength, hexlify, hexValue, isHexString } from "@ethersproject/bytes";
import { namehash } from "@ethersproject/hash";
import { getNetwork, Network, Networkish } from "@ethersproject/networks";
import { defineReadOnly, getStatic, resolveProperties } from "@ethersproject/properties";
import { Transaction } from "@ethersproject/transactions";
import { toUtf8String } from "@ethersproject/strings";
import { poll } from "@ethersproject/web";
import { Logger } from "@ethersproject/logger";
import { version } from "./_version";
const logger = new Logger(version);
import { Formatter } from "./formatter";
//////////////////////////////
// Event Serializeing
function checkTopic(topic: string): string {
if (topic == null) { return "null"; }
if (hexDataLength(topic) !== 32) {
logger.throwArgumentError("invalid topic", "topic", topic);
}
return topic.toLowerCase();
}
function serializeTopics(topics: Array<string | Array<string>>): string {
// Remove trailing null AND-topics; they are redundant
topics = topics.slice();
while (topics[topics.length - 1] == null) { topics.pop(); }
return topics.map((topic) => {
if (Array.isArray(topic)) {
// Only track unique OR-topics
const unique: { [ topic: string ]: boolean } = { }
topic.forEach((topic) => {
unique[checkTopic(topic)] = true;
});
// The order of OR-topics does not matter
const sorted = Object.keys(unique);
sorted.sort();
return sorted.join("|");
} else {
return checkTopic(topic);
}
}).join("&");
}
function deserializeTopics(data: string): Array<string | Array<string>> {
return data.split(/&/g).map((topic) => {
return topic.split("|").map((topic) => {
return ((topic === "null") ? null: topic);
});
});
}
function getEventTag(eventName: EventType): string {
if (typeof(eventName) === "string") {
eventName = eventName.toLowerCase();
if (hexDataLength(eventName) === 32) {
return "tx:" + eventName;
}
if (eventName.indexOf(":") === -1) {
return eventName;
}
} else if (Array.isArray(eventName)) {
return "filter:*:" + serializeTopics(eventName);
} else if (ForkEvent.isForkEvent(eventName)) {
logger.warn("not implemented");
throw new Error("not implemented");
} else if (eventName && typeof(eventName) === "object") {
return "filter:" + (eventName.address || "*") + ":" + serializeTopics(eventName.topics || []);
}
throw new Error("invalid event - " + eventName);
}
//////////////////////////////
// Helper Object
function getTime() {
return (new Date()).getTime();
}
//////////////////////////////
// Provider Object
/**
* EventType
* - "block"
* - "pending"
* - "error"
* - filter
* - topics array
* - transaction hash
*/
export class Event {
readonly listener: Listener;
readonly once: boolean;
readonly tag: string;
constructor(tag: string, listener: Listener, once: boolean) {
defineReadOnly(this, "tag", tag);
defineReadOnly(this, "listener", listener);
defineReadOnly(this, "once", once);
}
get type(): string {
return this.tag.split(":")[0]
}
get hash(): string {
const comps = this.tag.split(":");
if (comps[0] !== "tx") { return null; }
return comps[1];
}
get filter(): Filter {
const comps = this.tag.split(":");
if (comps[0] !== "filter") { return null; }
const filter = {
address: comps[1],
topics: deserializeTopics(comps[2])
}
if (!filter.address || filter.address === "*") { delete filter.address; }
return filter;
}
pollable(): boolean {
return (this.tag.indexOf(":") >= 0 || this.tag === "block" || this.tag === "pending");
}
}
let defaultFormatter: Formatter = null;
let nextPollId = 1;
export class BaseProvider extends Provider {
_network: Network;
_events: Array<Event>;
formatter: Formatter;
// To help mitigate the eventually consistent nature of the blockchain
// we keep a mapping of events we emit. If we emit an event X, we expect
// that a user should be able to query for that event in the callback,
// if the node returns null, we stall the response until we get back a
// meaningful value, since we may be hitting a re-org, or a node that
// has not indexed the event yet.
// Events:
// - t:{hash} - Transaction hash
// - b:{hash} - BlockHash
// - block - The most recent emitted block
_emitted: { [ eventName: string ]: number | "pending" };
_pollingInterval: number;
_poller: NodeJS.Timer;
_lastBlockNumber: number;
_fastBlockNumber: number;
_fastBlockNumberPromise: Promise<number>;
_fastQueryDate: number;
_maxInternalBlockNumber: number;
_internalBlockNumber: Promise<{ blockNumber: number, reqTime: number, respTime: number }>;
/**
* ready
*
* A Promise<Network> that resolves only once the provider is ready.
*
* Sub-classes that call the super with a network without a chainId
* MUST set this. Standard named networks have a known chainId.
*
*/
ready: Promise<Network>;
constructor(network: Networkish | Promise<Network>) {
logger.checkNew(new.target, Provider);
super();
this.formatter = new.target.getFormatter();
if (network instanceof Promise) {
defineReadOnly(this, "ready", network.then((network) => {
defineReadOnly(this, "_network", network);
return network;
}));
// Squash any "unhandled promise" errors; that do not need to be handled
this.ready.catch((error) => { });
} else {
const knownNetwork = getStatic<(network: Networkish) => Network>(new.target, "getNetwork")(network);
if (knownNetwork) {
defineReadOnly(this, "_network", knownNetwork);
defineReadOnly(this, "ready", Promise.resolve(this._network));
} else {
logger.throwArgumentError("invalid network", "network", network);
}
}
this._maxInternalBlockNumber = -1024;
this._lastBlockNumber = -2;
// Events being listened to
this._events = [];
this._pollingInterval = 4000;
this._emitted = { block: -2 };
this._fastQueryDate = 0;
}
static getFormatter(): Formatter {
if (defaultFormatter == null) {
defaultFormatter = new Formatter();
}
return defaultFormatter;
}
static getNetwork(network: Networkish): Network {
return getNetwork((network == null) ? "homestead": network);
}
async _getInternalBlockNumber(maxAge: number): Promise<number> {
await this.ready;
const internalBlockNumber = this._internalBlockNumber;
if (maxAge > 0 && this._internalBlockNumber) {
const result = await internalBlockNumber;
if ((getTime() - result.respTime) <= maxAge) {
return result.blockNumber;
}
}
const reqTime = getTime();
this._internalBlockNumber = this.perform("getBlockNumber", { }).then((blockNumber) => {
const respTime = getTime();
blockNumber = BigNumber.from(blockNumber).toNumber();
if (blockNumber < this._maxInternalBlockNumber) { blockNumber = this._maxInternalBlockNumber; }
this._maxInternalBlockNumber = blockNumber;
this._setFastBlockNumber(blockNumber); // @TODO: Still need this?
return { blockNumber, reqTime, respTime };
});
return (await this._internalBlockNumber).blockNumber;
}
async poll(): Promise<void> {
const pollId = nextPollId++;
this.emit("willPoll", pollId);
// Track all running promises, so we can trigger a post-poll once they are complete
const runners: Array<Promise<void>> = [];
const blockNumber = await this._getInternalBlockNumber(100 + this.pollingInterval / 2);
this._setFastBlockNumber(blockNumber);
// If the block has not changed, meh.
if (blockNumber === this._lastBlockNumber) { return; }
// First polling cycle, trigger a "block" events
if (this._emitted.block === -2) {
this._emitted.block = blockNumber - 1;
}
// Notify all listener for each block that has passed
for (let i = (<number>this._emitted.block) + 1; i <= blockNumber; i++) {
this.emit("block", i);
}
// The emitted block was updated, check for obsolete events
if ((<number>this._emitted.block) !== blockNumber) {
this._emitted.block = blockNumber;
Object.keys(this._emitted).forEach((key) => {
// The block event does not expire
if (key === "block") { return; }
// The block we were at when we emitted this event
const eventBlockNumber = this._emitted[key];
// We cannot garbage collect pending transactions or blocks here
// They should be garbage collected by the Provider when setting
// "pending" events
if (eventBlockNumber === "pending") { return; }
// Evict any transaction hashes or block hashes over 12 blocks
// old, since they should not return null anyways
if (blockNumber - eventBlockNumber > 12) {
delete this._emitted[key];
}
});
}
// First polling cycle
if (this._lastBlockNumber === -2) {
this._lastBlockNumber = blockNumber - 1;
}
// Find all transaction hashes we are waiting on
this._events.forEach((event) => {
switch (event.type) {
case "tx": {
const hash = event.hash;
let runner = this.getTransactionReceipt(hash).then((receipt) => {
if (!receipt || receipt.blockNumber == null) { return null; }
this._emitted["t:" + hash] = receipt.blockNumber;
this.emit(hash, receipt);
return null;
}).catch((error: Error) => { this.emit("error", error); });
runners.push(runner);
break;
}
case "filter": {
const filter = event.filter;
filter.fromBlock = this._lastBlockNumber + 1;
filter.toBlock = blockNumber;
const runner = this.getLogs(filter).then((logs) => {
if (logs.length === 0) { return; }
logs.forEach((log: Log) => {
this._emitted["b:" + log.blockHash] = log.blockNumber;
this._emitted["t:" + log.transactionHash] = log.blockNumber;
this.emit(filter, log);
});
}).catch((error: Error) => { this.emit("error", error); });
runners.push(runner);
break;
}
}
});
this._lastBlockNumber = blockNumber;
Promise.all(runners).then(() => {
this.emit("didPoll", pollId);
});
return null;
}
resetEventsBlock(blockNumber: number): void {
this._lastBlockNumber = blockNumber - 1;
if (this.polling) { this.poll(); }
}
get network(): Network {
return this._network;
}
getNetwork(): Promise<Network> {
return this.ready;
}
get blockNumber(): number {
return this._fastBlockNumber;
}
get polling(): boolean {
return (this._poller != null);
}
set polling(value: boolean) {
setTimeout(() => {
if (value && !this._poller) {
this._poller = setInterval(this.poll.bind(this), this.pollingInterval);
this.poll();
} else if (!value && this._poller) {
clearInterval(this._poller);
this._poller = null;
}
}, 0);
}
get pollingInterval(): number {
return this._pollingInterval;
}
set pollingInterval(value: number) {
if (typeof(value) !== "number" || value <= 0 || parseInt(String(value)) != value) {
throw new Error("invalid polling interval");
}
this._pollingInterval = value;
if (this._poller) {
clearInterval(this._poller);
this._poller = setInterval(() => { this.poll() }, this._pollingInterval);
}
}
_getFastBlockNumber(): Promise<number> {
const now = getTime();
// Stale block number, request a newer value
if ((now - this._fastQueryDate) > 2 * this._pollingInterval) {
this._fastQueryDate = now;
this._fastBlockNumberPromise = this.getBlockNumber().then((blockNumber) => {
if (this._fastBlockNumber == null || blockNumber > this._fastBlockNumber) {
this._fastBlockNumber = blockNumber;
}
return this._fastBlockNumber;
});
}
return this._fastBlockNumberPromise;
}
_setFastBlockNumber(blockNumber: number): void {
// Older block, maybe a stale request
if (this._fastBlockNumber != null && blockNumber < this._fastBlockNumber) { return; }
// Update the time we updated the blocknumber
this._fastQueryDate = getTime();
// Newer block number, use it
if (this._fastBlockNumber == null || blockNumber > this._fastBlockNumber) {
this._fastBlockNumber = blockNumber;
this._fastBlockNumberPromise = Promise.resolve(blockNumber);
}
}
// @TODO: Add .poller which must be an event emitter with a 'start', 'stop' and 'block' event;
// this will be used once we move to the WebSocket or other alternatives to polling
async waitForTransaction(transactionHash: string, confirmations?: number, timeout?: number): Promise<TransactionReceipt> {
if (confirmations == null) { confirmations = 1; }
const receipt = await this.getTransactionReceipt(transactionHash);
// Receipt is already good
if ((receipt ? receipt.confirmations: 0) >= confirmations) { return receipt; }
// Poll until the receipt is good...
return new Promise((resolve, reject) => {
let timer: NodeJS.Timer = null;
let done = false;
const handler = (receipt: TransactionReceipt) => {
if (receipt.confirmations < confirmations) { return; }
if (timer) { clearTimeout(timer); }
if (done) { return; }
done = true;
this.removeListener(transactionHash, handler);
resolve(receipt);
}
this.on(transactionHash, handler);
if (typeof(timeout) === "number" && timeout > 0) {
timer = setTimeout(() => {
if (done) { return; }
timer = null;
done = true;
this.removeListener(transactionHash, handler);
reject(logger.makeError("timeout exceeded", Logger.errors.TIMEOUT, { timeout: timeout }));
}, timeout);
if (timer.unref) { timer.unref(); }
}
});
}
getBlockNumber(): Promise<number> {
return this._getInternalBlockNumber(0);
}
async getGasPrice(): Promise<BigNumber> {
await this.ready;
return BigNumber.from(await this.perform("getGasPrice", { }));
}
async getBalance(addressOrName: string | Promise<string>, blockTag?: BlockTag | Promise<BlockTag>): Promise<BigNumber> {
await this.ready;
const params = await resolveProperties({
address: this._getAddress(addressOrName),
blockTag: this._getBlockTag(blockTag)
});
return BigNumber.from(await this.perform("getBalance", params));
}
async getTransactionCount(addressOrName: string | Promise<string>, blockTag?: BlockTag | Promise<BlockTag>): Promise<number> {
await this.ready;
const params = await resolveProperties({
address: this._getAddress(addressOrName),
blockTag: this._getBlockTag(blockTag)
});
return BigNumber.from(await this.perform("getTransactionCount", params)).toNumber();
}
async getCode(addressOrName: string | Promise<string>, blockTag?: BlockTag | Promise<BlockTag>): Promise<string> {
await this.ready;
const params = await resolveProperties({
address: this._getAddress(addressOrName),
blockTag: this._getBlockTag(blockTag)
});
return hexlify(await this.perform("getCode", params));
}
async getStorageAt(addressOrName: string | Promise<string>, position: BigNumberish | Promise<BigNumberish>, blockTag?: BlockTag | Promise<BlockTag>): Promise<string> {
await this.ready;
const params = await resolveProperties({
address: this._getAddress(addressOrName),
blockTag: this._getBlockTag(blockTag),
position: Promise.resolve(position).then((p) => hexValue(p))
});
return hexlify(await this.perform("getStorageAt", params));
}
// This should be called by any subclass wrapping a TransactionResponse
_wrapTransaction(tx: Transaction, hash?: string): TransactionResponse {
if (hash != null && hexDataLength(hash) !== 32) { throw new Error("invalid response - sendTransaction"); }
const result = <TransactionResponse>tx;
// Check the hash we expect is the same as the hash the server reported
if (hash != null && tx.hash !== hash) {
logger.throwError("Transaction hash mismatch from Provider.sendTransaction.", Logger.errors.UNKNOWN_ERROR, { expectedHash: tx.hash, returnedHash: hash });
}
// @TODO: (confirmations? number, timeout? number)
result.wait = async (confirmations?: number) => {
// We know this transaction *must* exist (whether it gets mined is
// another story), so setting an emitted value forces us to
// wait even if the node returns null for the receipt
if (confirmations !== 0) {
this._emitted["t:" + tx.hash] = "pending";
}
const receipt = await this.waitForTransaction(tx.hash, confirmations)
if (receipt == null && confirmations === 0) { return null; }
// No longer pending, allow the polling loop to garbage collect this
this._emitted["t:" + tx.hash] = receipt.blockNumber;
if (receipt.status === 0) {
logger.throwError("transaction failed", Logger.errors.CALL_EXCEPTION, {
transactionHash: tx.hash,
transaction: tx,
receipt: receipt
});
}
return receipt;
};
return result;
}
async sendTransaction(signedTransaction: string | Promise<string>): Promise<TransactionResponse> {
await this.ready;
const hexTx = await Promise.resolve(signedTransaction).then(t => hexlify(t));
const tx = this.formatter.transaction(signedTransaction);
try {
const hash = await this.perform("sendTransaction", { signedTransaction: hexTx });
return this._wrapTransaction(tx, hash);
} catch (error) {
(<any>error).transaction = tx;
(<any>error).transactionHash = tx.hash;
throw error;
}
}
async _getTransactionRequest(transaction: TransactionRequest | Promise<TransactionRequest>): Promise<Transaction> {
const values: any = await transaction;
const tx: any = { };
["from", "to"].forEach((key) => {
if (values[key] == null) { return; }
tx[key] = Promise.resolve(values[key]).then((v) => (v ? this._getAddress(v): null))
});
["gasLimit", "gasPrice", "value"].forEach((key) => {
if (values[key] == null) { return; }
tx[key] = Promise.resolve(values[key]).then((v) => (v ? BigNumber.from(v): null));
});
["data"].forEach((key) => {
if (values[key] == null) { return; }
tx[key] = Promise.resolve(values[key]).then((v) => (v ? hexlify(v): null));
});
return this.formatter.transactionRequest(await resolveProperties(tx));
}
async _getFilter(filter: Filter | FilterByBlockHash | Promise<Filter | FilterByBlockHash>): Promise<Filter | FilterByBlockHash> {
if (filter instanceof Promise) { filter = await filter; }
const result: any = { };
if (filter.address != null) {
result.address = this._getAddress(filter.address);
}
["blockHash", "topics"].forEach((key) => {
if ((<any>filter)[key] == null) { return; }
result[key] = (<any>filter)[key];
});
["fromBlock", "toBlock"].forEach((key) => {
if ((<any>filter)[key] == null) { return; }
result[key] = this._getBlockTag((<any>filter)[key]);
});
return this.formatter.filter(await resolveProperties(filter));
}
async call(transaction: TransactionRequest | Promise<TransactionRequest>, blockTag?: BlockTag | Promise<BlockTag>): Promise<string> {
await this.ready;
const params = await resolveProperties({
transaction: this._getTransactionRequest(transaction),
blockTag: this._getBlockTag(blockTag)
});
return hexlify(await this.perform("call", params));
}
async estimateGas(transaction: TransactionRequest | Promise<TransactionRequest>): Promise<BigNumber> {
await this.ready;
const params = await resolveProperties({
transaction: this._getTransactionRequest(transaction)
});
return BigNumber.from(await this.perform("estimateGas", params));
}
async _getAddress(addressOrName: string | Promise<string>): Promise<string> {
const address = await this.resolveName(addressOrName);
if (address == null) {
logger.throwError("ENS name not configured", Logger.errors.UNSUPPORTED_OPERATION, {
operation: `resolveName(${ JSON.stringify(addressOrName) })`
});
}
return address;
}
async _getBlock(blockHashOrBlockTag: BlockTag | string | Promise<BlockTag | string>, includeTransactions?: boolean): Promise<Block | BlockWithTransactions> {
await this.ready;
if (blockHashOrBlockTag instanceof Promise) {
blockHashOrBlockTag = await blockHashOrBlockTag;
}
// If blockTag is a number (not "latest", etc), this is the block number
let blockNumber = -128;
const params: { [key: string]: any } = {
includeTransactions: !!includeTransactions
};
if (isHexString(blockHashOrBlockTag, 32)) {
params.blockHash = blockHashOrBlockTag;
} else {
try {
params.blockTag = this.formatter.blockTag(await this._getBlockTag(blockHashOrBlockTag));
if (isHexString(params.blockTag)) {
blockNumber = parseInt(params.blockTag.substring(2), 16);
}
} catch (error) {
logger.throwArgumentError("invalid block hash or block tag", "blockHashOrBlockTag", blockHashOrBlockTag);
}
}
return poll(async () => {
const block = await this.perform("getBlock", params);
// Block was not found
if (block == null) {
// For blockhashes, if we didn't say it existed, that blockhash may
// not exist. If we did see it though, perhaps from a log, we know
// it exists, and this node is just not caught up yet.
if (params.blockHash != null) {
if (this._emitted["b:" + params.blockHash] == null) { return null; }
}
// For block tags, if we are asking for a future block, we return null
if (params.blockTag != null) {
if (blockNumber > this._emitted.block) { return null; }
}
// Retry on the next block
return undefined;
}
// Add transactions
if (includeTransactions) {
let blockNumber: number = null;
for (let i = 0; i < block.transactions.length; i++) {
const tx = block.transactions[i];
if (tx.blockNumber == null) {
tx.confirmations = 0;
} else if (tx.confirmations == null) {
if (blockNumber == null) {
blockNumber = await this._getInternalBlockNumber(100 + 2 * this.pollingInterval);
}
// Add the confirmations using the fast block number (pessimistic)
let confirmations = (blockNumber - tx.blockNumber) + 1;
if (confirmations <= 0) { confirmations = 1; }
tx.confirmations = confirmations;
}
}
return this.formatter.blockWithTransactions(block);
}
return this.formatter.block(block);
}, { onceBlock: this });
}
getBlock(blockHashOrBlockTag: BlockTag | string | Promise<BlockTag | string>): Promise<Block> {
return <Promise<Block>>(this._getBlock(blockHashOrBlockTag, false));
}
getBlockWithTransactions(blockHashOrBlockTag: BlockTag | string | Promise<BlockTag | string>): Promise<BlockWithTransactions> {
return <Promise<BlockWithTransactions>>(this._getBlock(blockHashOrBlockTag, true));
}
async getTransaction(transactionHash: string | Promise<string>): Promise<TransactionResponse> {
await this.ready;
if (transactionHash instanceof Promise) { transactionHash = await transactionHash; }
const params = { transactionHash: this.formatter.hash(transactionHash, true) };
return poll(async () => {
const result = await this.perform("getTransaction", params);
if (result == null) {
if (this._emitted["t:" + transactionHash] == null) {
return null;
}
return undefined;
}
const tx = this.formatter.transactionResponse(result);
if (tx.blockNumber == null) {
tx.confirmations = 0;
} else if (tx.confirmations == null) {
const blockNumber = await this._getInternalBlockNumber(100 + 2 * this.pollingInterval);
// Add the confirmations using the fast block number (pessimistic)
let confirmations = (blockNumber - tx.blockNumber) + 1;
if (confirmations <= 0) { confirmations = 1; }
tx.confirmations = confirmations;
}
return this._wrapTransaction(tx);
}, { onceBlock: this });
}
async getTransactionReceipt(transactionHash: string | Promise<string>): Promise<TransactionReceipt> {
await this.ready;
if (transactionHash instanceof Promise) { transactionHash = await transactionHash; }
const params = { transactionHash: this.formatter.hash(transactionHash, true) };
return poll(async () => {
const result = await this.perform("getTransactionReceipt", params);
if (result == null) {
if (this._emitted["t:" + transactionHash] == null) {
return null;
}
return undefined;
}
// "geth-etc" returns receipts before they are ready
if (result.blockHash == null) { return undefined; }
const receipt = this.formatter.receipt(result);
if (receipt.blockNumber == null) {
receipt.confirmations = 0;
} else if (receipt.confirmations == null) {
const blockNumber = await this._getInternalBlockNumber(100 + 2 * this.pollingInterval);
// Add the confirmations using the fast block number (pessimistic)
let confirmations = (blockNumber - receipt.blockNumber) + 1;
if (confirmations <= 0) { confirmations = 1; }
receipt.confirmations = confirmations;
}
return receipt;
}, { onceBlock: this });
}
async getLogs(filter: Filter | FilterByBlockHash | Promise<Filter | FilterByBlockHash>): Promise<Array<Log>> {
await this.ready;
const params = await resolveProperties({ filter: this._getFilter(filter) });
const logs: Array<Log> = await this.perform("getLogs", params);
logs.forEach((log) => {
if (log.removed == null) { log.removed = false; }
});
return Formatter.arrayOf(this.formatter.filterLog.bind(this.formatter))(logs);
}
async getEtherPrice(): Promise<number> {
await this.ready;
return this.perform("getEtherPrice", { });
}
async _getBlockTag(blockTag: BlockTag | Promise<BlockTag>): Promise<BlockTag> {
if (blockTag instanceof Promise) {
blockTag = await blockTag;
}
if (typeof(blockTag) === "number" && blockTag < 0) {
if (blockTag % 1) {
logger.throwArgumentError("invalid BlockTag", "blockTag", blockTag);
}
let blockNumber = await this._getInternalBlockNumber(100 + 2 * this.pollingInterval);
blockNumber += blockTag;
if (blockNumber < 0) { blockNumber = 0; }
return this.formatter.blockTag(blockNumber)
}
return this.formatter.blockTag(blockTag);
}
async _getResolver(name: string): Promise<string> {
// Get the resolver from the blockchain
const network = await this.getNetwork();
// No ENS...
if (!network.ensAddress) {
logger.throwError(
"network does not support ENS",
Logger.errors.UNSUPPORTED_OPERATION,
{ operation: "ENS", network: network.name }
);
}
// keccak256("resolver(bytes32)")
const transaction = {
to: network.ensAddress,
data: ("0x0178b8bf" + namehash(name).substring(2))
};
return this.formatter.callAddress(await this.call(transaction));
}
async resolveName(name: string | Promise<string>): Promise<string> {
if (name instanceof Promise) { name = await name; }
// If it is already an address, nothing to resolve
try {
return Promise.resolve(this.formatter.address(name));
} catch (error) {
// If is is a hexstring, the address is bad (See #694)
if (isHexString(name)) { throw error; }
}
if (typeof(name) !== "string") {
logger.throwArgumentError("invalid ENS name", "name", name);
}
// Get the addr from the resovler
const resolverAddress = await this._getResolver(name);
if (!resolverAddress) { return null; }
// keccak256("addr(bytes32)")
const transaction = {
to: resolverAddress,
data: ("0x3b3b57de" + namehash(name).substring(2))
};
return this.formatter.callAddress(await this.call(transaction));
}
async lookupAddress(address: string | Promise<string>): Promise<string> {
if (address instanceof Promise) { address = await address; }
address = this.formatter.address(address);
const reverseName = address.substring(2).toLowerCase() + ".addr.reverse";
const resolverAddress = await this._getResolver(reverseName);
if (!resolverAddress) { return null; }
// keccak("name(bytes32)")
let bytes = arrayify(await this.call({
to: resolverAddress,
data: ("0x691f3431" + namehash(reverseName).substring(2))
}));
// Strip off the dynamic string pointer (0x20)
if (bytes.length < 32 || !BigNumber.from(bytes.slice(0, 32)).eq(32)) { return null; }
bytes = bytes.slice(32);
// Not a length-prefixed string
if (bytes.length < 32) { return null; }
// Get the length of the string (from the length-prefix)
const length = BigNumber.from(bytes.slice(0, 32)).toNumber();
bytes = bytes.slice(32);
// Length longer than available data
if (length > bytes.length) { return null; }
const name = toUtf8String(bytes.slice(0, length));
// Make sure the reverse record matches the foward record
const addr = await this.resolveName(name);
if (addr != address) { return null; }
return name;
}
perform(method: string, params: any): Promise<any> {
return logger.throwError(method + " not implemented", Logger.errors.NOT_IMPLEMENTED, { operation: method });
}
_startEvent(event: Event): void {
this.polling = (this._events.filter((e) => e.pollable()).length > 0);
}
_stopEvent(event: Event): void {
this.polling = (this._events.filter((e) => e.pollable()).length > 0);
}
_addEventListener(eventName: EventType, listener: Listener, once: boolean): this {
const event = new Event(getEventTag(eventName), listener, once)
this._events.push(event);
this._startEvent(event);
return this;
}
on(eventName: EventType, listener: Listener): this {
return this._addEventListener(eventName, listener, false);
}
once(eventName: EventType, listener: Listener): this {
return this._addEventListener(eventName, listener, true);
}
emit(eventName: EventType, ...args: Array<any>): boolean {
let result = false;
let stopped: Array<Event> = [ ];
let eventTag = getEventTag(eventName);
this._events = this._events.filter((event) => {
if (event.tag !== eventTag) { return true; }
setTimeout(() => {
event.listener.apply(this, args);
}, 0);
result = true;
if (event.once) {
stopped.push(event);