-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathprovider.ts
1968 lines (1659 loc) · 58.6 KB
/
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
//import { resolveAddress } from "@ethersproject/address";
import {
defineProperties, getBigInt, getNumber, hexlify, resolveProperties,
assert, assertArgument, isError, makeError
} from "../utils/index.js";
import { accessListify } from "../transaction/index.js";
import type { AddressLike, NameResolver } from "../address/index.js";
import type { BigNumberish, EventEmitterable } from "../utils/index.js";
import type { Signature } from "../crypto/index.js";
import type { AccessList, AccessListish, TransactionLike } from "../transaction/index.js";
import type { ContractRunner } from "./contracts.js";
import type { Network } from "./network.js";
const BN_0 = BigInt(0);
/**
* A **BlockTag** specifies a specific block.
*
* **numeric value** - specifies the block height, where
* the genesis block is block 0; many operations accept a negative
* value which indicates the block number should be deducted from
* the most recent block. A numeric value may be a ``number``, ``bigint``,
* or a decimal of hex string.
*
* **blockhash** - specifies a specific block by its blockhash; this allows
* potentially orphaned blocks to be specifed, without ambiguity, but many
* backends do not support this for some operations.
*/
export type BlockTag = BigNumberish | string;
import {
BlockParams, LogParams, TransactionReceiptParams,
TransactionResponseParams
} from "./formatting.js";
// -----------------------
function getValue<T>(value: undefined | null | T): null | T {
if (value == null) { return null; }
return value;
}
function toJson(value: null | bigint): null | string {
if (value == null) { return null; }
return value.toString();
}
// @TODO? <T extends FeeData = { }> implements Required<T>
/**
* A **FeeData** wraps all the fee-related values associated with
* the network.
*/
export class FeeData {
/**
* The gas price for legacy networks.
*/
readonly gasPrice!: null | bigint;
/**
* The maximum fee to pay per gas.
*
* The base fee per gas is defined by the network and based on
* congestion, increasing the cost during times of heavy load
* and lowering when less busy.
*
* The actual fee per gas will be the base fee for the block
* and the priority fee, up to the max fee per gas.
*
* This will be ``null`` on legacy networks (i.e. [pre-EIP-1559](link-eip-1559))
*/
readonly maxFeePerGas!: null | bigint;
/**
* The additional amout to pay per gas to encourage a validator
* to include the transaction.
*
* The purpose of this is to compensate the validator for the
* adjusted risk for including a given transaction.
*
* This will be ``null`` on legacy networks (i.e. [pre-EIP-1559](link-eip-1559))
*/
readonly maxPriorityFeePerGas!: null | bigint;
/**
* Creates a new FeeData for %%gasPrice%%, %%maxFeePerGas%% and
* %%maxPriorityFeePerGas%%.
*/
constructor(gasPrice?: null | bigint, maxFeePerGas?: null | bigint, maxPriorityFeePerGas?: null | bigint) {
defineProperties<FeeData>(this, {
gasPrice: getValue(gasPrice),
maxFeePerGas: getValue(maxFeePerGas),
maxPriorityFeePerGas: getValue(maxPriorityFeePerGas)
});
}
/**
* Returns a JSON-friendly value.
*/
toJSON(): any {
const {
gasPrice, maxFeePerGas, maxPriorityFeePerGas
} = this;
return {
_type: "FeeData",
gasPrice: toJson(gasPrice),
maxFeePerGas: toJson(maxFeePerGas),
maxPriorityFeePerGas: toJson(maxPriorityFeePerGas),
};
}
}
/**
* A **TransactionRequest** is a transactions with potentially various
* properties not defined, or with less strict types for its values.
*
* This is used to pass to various operations, which will internally
* coerce any types and populate any necessary values.
*/
export interface TransactionRequest {
/**
* The transaction type.
*/
type?: null | number;
/**
* The target of the transaction.
*/
to?: null | AddressLike;
/**
* The sender of the transaction.
*/
from?: null | AddressLike;
/**
* The nonce of the transaction, used to prevent replay attacks.
*/
nonce?: null | number;
/**
* The maximum amount of gas to allow this transaction to consime.
*/
gasLimit?: null | BigNumberish;
/**
* The gas price to use for legacy transactions or transactions on
* legacy networks.
*
* Most of the time the ``max*FeePerGas`` is preferred.
*/
gasPrice?: null | BigNumberish;
/**
* The [[link-eip-1559]] maximum priority fee to pay per gas.
*/
maxPriorityFeePerGas?: null | BigNumberish;
/**
* The [[link-eip-1559]] maximum total fee to pay per gas. The actual
* value used is protocol enforced to be the block's base fee.
*/
maxFeePerGas?: null | BigNumberish;
/**
* The transaction data.
*/
data?: null | string;
/**
* The transaction value (in wei).
*/
value?: null | BigNumberish;
/**
* The chain ID for the network this transaction is valid on.
*/
chainId?: null | BigNumberish;
/**
* The [[link-eip-2930]] access list. Storage slots included in the access
* list are //warmed// by pre-loading them, so their initial cost to
* fetch is guaranteed, but then each additional access is cheaper.
*/
accessList?: null | AccessListish;
/**
* A custom object, which can be passed along for network-specific
* values.
*/
customData?: any;
// Only meaningful when used for call
/**
* When using ``call`` or ``estimateGas``, this allows a specific
* block to be queried. Many backends do not support this and when
* unsupported errors are silently squelched and ``"latest"`` is used.
*/
blockTag?: BlockTag;
/**
* When using ``call``, this enables CCIP-read, which permits the
* provider to be redirected to web-based content during execution,
* which is then further validated by the contract.
*
* There are potential security implications allowing CCIP-read, as
* it could be used to expose the IP address or user activity during
* the fetch to unexpected parties.
*/
enableCcipRead?: boolean;
// Todo?
//gasMultiplier?: number;
};
/**
* A **PreparedTransactionRequest** is identical to a [[TransactionRequest]]
* except all the property types are strictly enforced.
*/
export interface PreparedTransactionRequest {
/**
* The transaction type.
*/
type?: number;
/**
* The target of the transaction.
*/
to?: AddressLike;
/**
* The sender of the transaction.
*/
from?: AddressLike;
/**
* The nonce of the transaction, used to prevent replay attacks.
*/
nonce?: number;
/**
* The maximum amount of gas to allow this transaction to consime.
*/
gasLimit?: bigint;
/**
* The gas price to use for legacy transactions or transactions on
* legacy networks.
*
* Most of the time the ``max*FeePerGas`` is preferred.
*/
gasPrice?: bigint;
/**
* The [[link-eip-1559]] maximum priority fee to pay per gas.
*/
maxPriorityFeePerGas?: bigint;
/**
* The [[link-eip-1559]] maximum total fee to pay per gas. The actual
* value used is protocol enforced to be the block's base fee.
*/
maxFeePerGas?: bigint;
/**
* The transaction data.
*/
data?: string;
/**
* The transaction value (in wei).
*/
value?: bigint;
/**
* The chain ID for the network this transaction is valid on.
*/
chainId?: bigint;
/**
* The [[link-eip-2930]] access list. Storage slots included in the access
* list are //warmed// by pre-loading them, so their initial cost to
* fetch is guaranteed, but then each additional access is cheaper.
*/
accessList?: AccessList;
/**
* A custom object, which can be passed along for network-specific
* values.
*/
customData?: any;
/**
* When using ``call`` or ``estimateGas``, this allows a specific
* block to be queried. Many backends do not support this and when
* unsupported errors are silently squelched and ``"latest"`` is used.
*/
blockTag?: BlockTag;
/**
* When using ``call``, this enables CCIP-read, which permits the
* provider to be redirected to web-based content during execution,
* which is then further validated by the contract.
*
* There are potential security implications allowing CCIP-read, as
* it could be used to expose the IP address or user activity during
* the fetch to unexpected parties.
*/
enableCcipRead?: boolean;
}
/**
* Returns a copy of %%req%% with all properties coerced to their strict
* types.
*/
export function copyRequest(req: TransactionRequest): PreparedTransactionRequest {
const result: any = { };
// These could be addresses, ENS names or Addressables
if (req.to) { result.to = req.to; }
if (req.from) { result.from = req.from; }
if (req.data) { result.data = hexlify(req.data); }
const bigIntKeys = "chainId,gasLimit,gasPrice,maxFeePerGas,maxPriorityFeePerGas,value".split(/,/);
for (const key of bigIntKeys) {
if (!(key in req) || (<any>req)[key] == null) { continue; }
result[key] = getBigInt((<any>req)[key], `request.${ key }`);
}
const numberKeys = "type,nonce".split(/,/);
for (const key of numberKeys) {
if (!(key in req) || (<any>req)[key] == null) { continue; }
result[key] = getNumber((<any>req)[key], `request.${ key }`);
}
if (req.accessList) {
result.accessList = accessListify(req.accessList);
}
if ("blockTag" in req) { result.blockTag = req.blockTag; }
if ("enableCcipRead" in req) {
result.enableCcipRead = !!req.enableCcipRead
}
if ("customData" in req) {
result.customData = req.customData;
}
return result;
}
//////////////////////
// Block
/**
* An Interface to indicate a [[Block]] has been included in the
* blockchain. This asserts a Type Guard that necessary properties
* are non-null.
*
* Before a block is included, it is a //pending// block.
*/
export interface MinedBlock extends Block {
/**
* The block number also known as the block height.
*/
readonly number: number;
/**
* The block hash.
*/
readonly hash: string;
/**
* The block timestamp, in seconds from epoch.
*/
readonly timestamp: number;
/**
* The block date, created from the [[timestamp]].
*/
readonly date: Date;
/**
* The miner of the block, also known as the ``author`` or
* block ``producer``.
*/
readonly miner: string;
}
/**
* A **Block** represents the data associated with a full block on
* Ethereum.
*/
export class Block implements BlockParams, Iterable<string> {
/**
* The provider connected to the block used to fetch additional details
* if necessary.
*/
readonly provider!: Provider;
/**
* The block number, sometimes called the block height. This is a
* sequential number that is one higher than the parent block.
*/
readonly number!: number;
/**
* The block hash.
*
* This hash includes all properties, so can be safely used to identify
* an exact set of block properties.
*/
readonly hash!: null | string;
/**
* The timestamp for this block, which is the number of seconds since
* epoch that this block was included.
*/
readonly timestamp!: number;
/**
* The block hash of the parent block.
*/
readonly parentHash!: string;
/**
* The nonce.
*
* On legacy networks, this is the random number inserted which
* permitted the difficulty target to be reached.
*/
readonly nonce!: string;
/**
* The difficulty target.
*
* On legacy networks, this is the proof-of-work target required
* for a block to meet the protocol rules to be included.
*
* On modern networks, this is a random number arrived at using
* randao. @TODO: Find links?
*/
readonly difficulty!: bigint;
/**
* The total gas limit for this block.
*/
readonly gasLimit!: bigint;
/**
* The total gas used in this block.
*/
readonly gasUsed!: bigint;
/**
* The miner coinbase address, wihch receives any subsidies for
* including this block.
*/
readonly miner!: string;
/**
* Any extra data the validator wished to include.
*/
readonly extraData!: string;
/**
* The base fee per gas that all transactions in this block were
* charged.
*
* This adjusts after each block, depending on how congested the network
* is.
*/
readonly baseFeePerGas!: null | bigint;
readonly #transactions: Array<string | TransactionResponse>;
/**
* Create a new **Block** object.
*
* This should generally not be necessary as the unless implementing a
* low-level library.
*/
constructor(block: BlockParams, provider: Provider) {
this.#transactions = block.transactions.map((tx) => {
if (typeof(tx) !== "string") {
return new TransactionResponse(tx, provider);
}
return tx;
});
defineProperties<Block>(this, {
provider,
hash: getValue(block.hash),
number: block.number,
timestamp: block.timestamp,
parentHash: block.parentHash,
nonce: block.nonce,
difficulty: block.difficulty,
gasLimit: block.gasLimit,
gasUsed: block.gasUsed,
miner: block.miner,
extraData: block.extraData,
baseFeePerGas: getValue(block.baseFeePerGas)
});
}
/**
* Returns the list of transaction hashes.
*/
get transactions(): ReadonlyArray<string> {
return this.#transactions.map((tx) => {
if (typeof(tx) === "string") { return tx; }
return tx.hash;
});
}
/**
* Returns the complete transactions for blocks which
* prefetched them, by passing ``true`` to %%prefetchTxs%%
* into [[Provider-getBlock]].
*/
get prefetchedTransactions(): Array<TransactionResponse> {
const txs = this.#transactions.slice();
// Doesn't matter...
if (txs.length === 0) { return [ ]; }
// Make sure we prefetched the transactions
assert(typeof(txs[0]) === "object", "transactions were not prefetched with block request", "UNSUPPORTED_OPERATION", {
operation: "transactionResponses()"
});
return <Array<TransactionResponse>>txs;
}
/**
* Returns a JSON-friendly value.
*/
toJSON(): any {
const {
baseFeePerGas, difficulty, extraData, gasLimit, gasUsed, hash,
miner, nonce, number, parentHash, timestamp, transactions
} = this;
return {
_type: "Block",
baseFeePerGas: toJson(baseFeePerGas),
difficulty: toJson(difficulty),
extraData,
gasLimit: toJson(gasLimit),
gasUsed: toJson(gasUsed),
hash, miner, nonce, number, parentHash, timestamp,
transactions,
};
}
[Symbol.iterator](): Iterator<string> {
let index = 0;
const txs = this.transactions;
return {
next: () => {
if (index < this.length) {
return {
value: txs[index++], done: false
}
}
return { value: undefined, done: true };
}
};
}
/**
* The number of transactions in this block.
*/
get length(): number { return this.#transactions.length; }
/**
* The [[link-js-date]] this block was included at.
*/
get date(): null | Date {
if (this.timestamp == null) { return null; }
return new Date(this.timestamp * 1000);
}
/**
* Get the transaction at %%indexe%% within this block.
*/
async getTransaction(indexOrHash: number | string): Promise<TransactionResponse> {
// Find the internal value by its index or hash
let tx: string | TransactionResponse | undefined = undefined;
if (typeof(indexOrHash) === "number") {
tx = this.#transactions[indexOrHash];
} else {
const hash = indexOrHash.toLowerCase();
for (const v of this.#transactions) {
if (typeof(v) === "string") {
if (v !== hash) { continue; }
tx = v;
break;
} else {
if (v.hash === hash) { continue; }
tx = v;
break;
}
}
}
if (tx == null) { throw new Error("no such tx"); }
if (typeof(tx) === "string") {
return <TransactionResponse>(await this.provider.getTransaction(tx));
} else {
return tx;
}
}
/**
* If a **Block** was fetched with a request to include the transactions
* this will allow synchronous access to those transactions.
*
* If the transactions were not prefetched, this will throw.
*/
getPrefetchedTransaction(indexOrHash: number | string): TransactionResponse {
const txs = this.prefetchedTransactions;
if (typeof(indexOrHash) === "number") {
return txs[indexOrHash];
}
indexOrHash = indexOrHash.toLowerCase();
for (const tx of txs) {
if (tx.hash === indexOrHash) { return tx; }
}
assertArgument(false, "no matching transaction", "indexOrHash", indexOrHash);
}
/**
* Returns true if this block been mined. This provides a type guard
* for all properties on a [[MinedBlock]].
*/
isMined(): this is MinedBlock { return !!this.hash; }
/**
* Returns true if this block is an [[link-eip-2930]] block.
*/
isLondon(): this is (Block & { baseFeePerGas: bigint }) {
return !!this.baseFeePerGas;
}
/**
* @_ignore:
*/
orphanedEvent(): OrphanFilter {
if (!this.isMined()) { throw new Error(""); }
return createOrphanedBlockFilter(this);
}
}
//////////////////////
// Log
/**
* A **Log** in Ethereum represents an event that has been included in a
* transaction using the ``LOG*`` opcodes, which are most commonly used by
* Solidity's emit for announcing events.
*/
export class Log implements LogParams {
/**
* The provider connected to the log used to fetch additional details
* if necessary.
*/
readonly provider: Provider;
/**
* The transaction hash of the transaction this log occurred in. Use the
* [[Log-getTransaction]] to get the [[TransactionResponse]].
*/
readonly transactionHash!: string;
/**
* The block hash of the block this log occurred in. Use the
* [[Log-getBlock]] to get the [[Block]].
*/
readonly blockHash!: string;
/**
* The block number of the block this log occurred in. It is preferred
* to use the [[Block-hash]] when fetching the related [[Block]],
* since in the case of an orphaned block, the block at that height may
* have changed.
*/
readonly blockNumber!: number;
/**
* If the **Log** represents a block that was removed due to an orphaned
* block, this will be true.
*
* This can only happen within an orphan event listener.
*/
readonly removed!: boolean;
/**
* The address of the contract that emitted this log.
*/
readonly address!: string;
/**
* The data included in this log when it was emitted.
*/
readonly data!: string;
/**
* The indexed topics included in this log when it was emitted.
*
* All topics are included in the bloom filters, so they can be
* efficiently filtered using the [[Provider-getLogs]] method.
*/
readonly topics!: ReadonlyArray<string>;
/**
* The index within the block this log occurred at. This is generally
* not useful to developers, but can be used with the various roots
* to proof inclusion within a block.
*/
readonly index!: number;
/**
* The index within the transaction of this log.
*/
readonly transactionIndex!: number;
/**
* @_ignore:
*/
constructor(log: LogParams, provider: Provider) {
this.provider = provider;
const topics = Object.freeze(log.topics.slice());
defineProperties<Log>(this, {
transactionHash: log.transactionHash,
blockHash: log.blockHash,
blockNumber: log.blockNumber,
removed: log.removed,
address: log.address,
data: log.data,
topics,
index: log.index,
transactionIndex: log.transactionIndex,
});
}
/**
* Returns a JSON-compatible object.
*/
toJSON(): any {
const {
address, blockHash, blockNumber, data, index,
removed, topics, transactionHash, transactionIndex
} = this;
return {
_type: "log",
address, blockHash, blockNumber, data, index,
removed, topics, transactionHash, transactionIndex
};
}
/**
* Returns the block that this log occurred in.
*/
async getBlock(): Promise<Block> {
const block = await this.provider.getBlock(this.blockHash);
assert(!!block, "failed to find transaction", "UNKNOWN_ERROR", { });
return block;
}
/**
* Returns the transaction that this log occurred in.
*/
async getTransaction(): Promise<TransactionResponse> {
const tx = await this.provider.getTransaction(this.transactionHash);
assert(!!tx, "failed to find transaction", "UNKNOWN_ERROR", { });
return tx;
}
/**
* Returns the transaction receipt fot the transaction that this
* log occurred in.
*/
async getTransactionReceipt(): Promise<TransactionReceipt> {
const receipt = await this.provider.getTransactionReceipt(this.transactionHash);
assert(!!receipt, "failed to find transaction receipt", "UNKNOWN_ERROR", { });
return receipt;
}
/**
* @_ignore:
*/
removedEvent(): OrphanFilter {
return createRemovedLogFilter(this);
}
}
//////////////////////
// Transaction Receipt
/*
export interface LegacyTransactionReceipt {
byzantium: false;
status: null;
root: string;
}
export interface ByzantiumTransactionReceipt {
byzantium: true;
status: number;
root: null;
}
*/
/**
* A **TransactionReceipt** includes additional information about a
* transaction that is only available after it has been mined.
*/
export class TransactionReceipt implements TransactionReceiptParams, Iterable<Log> {
/**
* The provider connected to the log used to fetch additional details
* if necessary.
*/
readonly provider!: Provider;
/**
* The address the transaction was send to.
*/
readonly to!: null | string;
/**
* The sender of the transaction.
*/
readonly from!: string;
/**
* The address of the contract if the transaction was directly
* responsible for deploying one.
*
* This is non-null **only** if the ``to`` is empty and the ``data``
* was successfully executed as initcode.
*/
readonly contractAddress!: null | string;
/**
* The transaction hash.
*/
readonly hash!: string;
/**
* The index of this transaction within the block transactions.
*/
readonly index!: number;
/**
* The block hash of the [[Block]] this transaction was included in.
*/
readonly blockHash!: string;
/**
* The block number of the [[Block]] this transaction was included in.
*/
readonly blockNumber!: number;
/**
* The bloom filter bytes that represent all logs that occurred within
* this transaction. This is generally not useful for most developers,
* but can be used to validate the included logs.
*/
readonly logsBloom!: string;
/**
* The actual amount of gas used by this transaction.
*
* When creating a transaction, the amount of gas that will be used can
* only be approximated, but the sender must pay the gas fee for the
* entire gas limit. After the transaction, the difference is refunded.
*/
readonly gasUsed!: bigint;
/**
* The amount of gas used by all transactions within the block for this
* and all transactions with a lower ``index``.
*
* This is generally not useful for developers but can be used to
* validate certain aspects of execution.
*/
readonly cumulativeGasUsed!: bigint;
/**
* The actual gas price used during execution.
*
* Due to the complexity of [[link-eip-1559]] this value can only
* be caluclated after the transaction has been mined, snce the base
* fee is protocol-enforced.
*/
readonly gasPrice!: bigint;
/**
* The [[link-eip-2718]] transaction type.
*/
readonly type!: number;
//readonly byzantium!: boolean;
/**
* The status of this transaction, indicating success (i.e. ``1``) or
* a revert (i.e. ``0``).
*
* This is available in post-byzantium blocks, but some backends may
* backfill this value.
*/
readonly status!: null | number;
/**
* The root hash of this transaction.
*
* This is no present and was only included in pre-byzantium blocks, but
* could be used to validate certain parts of the receipt.
*/
readonly root!: null | string;
readonly #logs: ReadonlyArray<Log>;
/**
* @_ignore:
*/
constructor(tx: TransactionReceiptParams, provider: Provider) {
this.#logs = Object.freeze(tx.logs.map((log) => {
return new Log(log, provider);
}));
let gasPrice = BN_0;
if (tx.effectiveGasPrice != null) {
gasPrice = tx.effectiveGasPrice;
} else if (tx.gasPrice != null) {
gasPrice = tx.gasPrice;
}
defineProperties<TransactionReceipt>(this, {
provider,
to: tx.to,
from: tx.from,
contractAddress: tx.contractAddress,
hash: tx.hash,
index: tx.index,
blockHash: tx.blockHash,
blockNumber: tx.blockNumber,
logsBloom: tx.logsBloom,
gasUsed: tx.gasUsed,
cumulativeGasUsed: tx.cumulativeGasUsed,
gasPrice,
type: tx.type,
//byzantium: tx.byzantium,
status: tx.status,
root: tx.root
});
}
/**
* The logs for this transaction.
*/
get logs(): ReadonlyArray<Log> { return this.#logs; }