viem 0.0.0 → 0.0.1-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1032 @@
1
+ import {
2
+ BaseError,
3
+ __publicField,
4
+ checksumAddress,
5
+ encodeHex,
6
+ format,
7
+ formatBlock,
8
+ formatFeeHistory,
9
+ formatLog,
10
+ formatTransaction,
11
+ formatTransactionReceipt,
12
+ formatTransactionRequest,
13
+ getAddress,
14
+ getCache,
15
+ hexToNumber,
16
+ numberToHex,
17
+ wait,
18
+ withCache
19
+ } from "./chunk-GI67STNV.js";
20
+
21
+ // src/actions/wallet/addChain.ts
22
+ async function addChain(client, chain) {
23
+ const { id, name, nativeCurrency, rpcUrls, blockExplorers } = chain;
24
+ await client.request({
25
+ method: "wallet_addEthereumChain",
26
+ params: [
27
+ {
28
+ chainId: numberToHex(id),
29
+ chainName: name,
30
+ nativeCurrency,
31
+ rpcUrls: rpcUrls.default.http,
32
+ blockExplorerUrls: blockExplorers ? Object.values(blockExplorers).map(({ url }) => url) : void 0
33
+ }
34
+ ]
35
+ });
36
+ }
37
+
38
+ // src/actions/wallet/getAccounts.ts
39
+ async function getAccounts(client) {
40
+ const addresses = await client.request({ method: "eth_accounts" });
41
+ return addresses.map((address) => checksumAddress(address));
42
+ }
43
+
44
+ // src/actions/wallet/getPermissions.ts
45
+ async function getPermissions(client) {
46
+ const permissions = await client.request({ method: "wallet_getPermissions" });
47
+ return permissions;
48
+ }
49
+
50
+ // src/actions/wallet/requestAccounts.ts
51
+ async function requestAccounts(client) {
52
+ const addresses = await client.request({ method: "eth_requestAccounts" });
53
+ return addresses.map((address) => getAddress(address));
54
+ }
55
+
56
+ // src/actions/wallet/requestPermissions.ts
57
+ async function requestPermissions(client, permissions) {
58
+ return client.request({
59
+ method: "wallet_requestPermissions",
60
+ params: [permissions]
61
+ });
62
+ }
63
+
64
+ // src/actions/wallet/sendTransaction.ts
65
+ async function sendTransaction(client, { chain, request }) {
66
+ if (request.maxFeePerGas !== void 0 && request.maxPriorityFeePerGas !== void 0 && request.maxFeePerGas < request.maxPriorityFeePerGas)
67
+ throw new InvalidGasArgumentsError();
68
+ const request_ = format(request, {
69
+ formatter: chain?.formatters?.transactionRequest || formatTransactionRequest
70
+ });
71
+ const hash = await client.request({
72
+ method: "eth_sendTransaction",
73
+ params: [request_]
74
+ });
75
+ return { hash };
76
+ }
77
+ var InvalidGasArgumentsError = class extends BaseError {
78
+ constructor() {
79
+ super("`maxFeePerGas` cannot be less than `maxPriorityFeePerGas`");
80
+ __publicField(this, "name", "InvalidGasArgumentsError");
81
+ }
82
+ };
83
+
84
+ // src/actions/wallet/signMessage.ts
85
+ async function signMessage(client, { from, data: data_ }) {
86
+ let data;
87
+ if (typeof data_ === "string") {
88
+ if (!data_.startsWith("0x"))
89
+ throw new BaseError(
90
+ `data ("${data_}") must be a hex value. Encode it first to a hex with the \`encodeHex\` util.`,
91
+ {
92
+ docsPath: "/TODO"
93
+ }
94
+ );
95
+ data = data_;
96
+ } else {
97
+ data = encodeHex(data_);
98
+ }
99
+ const signed = await client.request({
100
+ method: "personal_sign",
101
+ params: [data, from]
102
+ });
103
+ return signed;
104
+ }
105
+
106
+ // src/actions/wallet/switchChain.ts
107
+ async function switchChain(client, { id }) {
108
+ await client.request({
109
+ method: "wallet_switchEthereumChain",
110
+ params: [
111
+ {
112
+ chainId: numberToHex(id)
113
+ }
114
+ ]
115
+ });
116
+ }
117
+
118
+ // src/actions/wallet/watchAsset.ts
119
+ async function watchAsset(client, params) {
120
+ const added = await client.request({
121
+ method: "wallet_watchAsset",
122
+ params: [params]
123
+ });
124
+ return added;
125
+ }
126
+
127
+ // src/actions/public/call.ts
128
+ async function call(client, { blockNumber, blockTag = "latest", chain, request }) {
129
+ if (request.maxFeePerGas !== void 0 && request.maxPriorityFeePerGas !== void 0 && request.maxFeePerGas < request.maxPriorityFeePerGas)
130
+ throw new InvalidGasArgumentsError();
131
+ const blockNumberHex = blockNumber ? numberToHex(blockNumber) : void 0;
132
+ const request_ = format(request, {
133
+ formatter: chain?.formatters?.transactionRequest || formatTransactionRequest
134
+ });
135
+ const data = await client.request({
136
+ method: "eth_call",
137
+ params: [request_, blockNumberHex || blockTag]
138
+ });
139
+ if (data === "0x")
140
+ return { data: void 0 };
141
+ return { data };
142
+ }
143
+
144
+ // src/actions/public/createPendingTransactionFilter.ts
145
+ async function createPendingTransactionFilter(client) {
146
+ const id = await client.request({
147
+ method: "eth_newPendingTransactionFilter"
148
+ });
149
+ return { id, type: "transaction" };
150
+ }
151
+
152
+ // src/actions/public/createBlockFilter.ts
153
+ async function createBlockFilter(client) {
154
+ const id = await client.request({
155
+ method: "eth_newBlockFilter"
156
+ });
157
+ return { id, type: "block" };
158
+ }
159
+
160
+ // src/actions/public/estimateGas.ts
161
+ async function estimateGas(client, {
162
+ blockNumber,
163
+ blockTag = "latest",
164
+ request: {
165
+ data,
166
+ from,
167
+ gas,
168
+ gasPrice,
169
+ maxFeePerGas,
170
+ maxPriorityFeePerGas,
171
+ to,
172
+ value
173
+ }
174
+ }) {
175
+ const blockNumberHex = blockNumber ? numberToHex(blockNumber) : void 0;
176
+ const parameters = {
177
+ data,
178
+ from,
179
+ gas: gas ? numberToHex(gas) : void 0,
180
+ gasPrice: gasPrice ? numberToHex(gasPrice) : void 0,
181
+ maxFeePerGas: maxFeePerGas ? numberToHex(maxFeePerGas) : void 0,
182
+ maxPriorityFeePerGas: maxPriorityFeePerGas ? numberToHex(maxPriorityFeePerGas) : void 0,
183
+ to,
184
+ value: value ? numberToHex(value) : void 0
185
+ };
186
+ const balance = await client.request({
187
+ method: "eth_estimateGas",
188
+ params: [parameters, blockNumberHex || blockTag]
189
+ });
190
+ return BigInt(balance);
191
+ }
192
+
193
+ // src/actions/public/getBalance.ts
194
+ async function getBalance(client, { address, blockNumber, blockTag = "latest" }) {
195
+ const blockNumberHex = blockNumber ? numberToHex(blockNumber) : void 0;
196
+ const balance = await client.request({
197
+ method: "eth_getBalance",
198
+ params: [address, blockNumberHex || blockTag]
199
+ });
200
+ return BigInt(balance);
201
+ }
202
+
203
+ // src/actions/public/getBlock.ts
204
+ async function getBlock(client, {
205
+ blockHash,
206
+ blockNumber,
207
+ blockTag = "latest",
208
+ includeTransactions = false
209
+ } = {}) {
210
+ const blockNumberHex = blockNumber !== void 0 ? numberToHex(blockNumber) : void 0;
211
+ let block = null;
212
+ if (blockHash) {
213
+ block = await client.request({
214
+ method: "eth_getBlockByHash",
215
+ params: [blockHash, includeTransactions]
216
+ });
217
+ } else {
218
+ block = await client.request({
219
+ method: "eth_getBlockByNumber",
220
+ params: [blockNumberHex || blockTag, includeTransactions]
221
+ });
222
+ }
223
+ if (!block)
224
+ throw new BlockNotFoundError({ blockHash, blockNumber });
225
+ return format(block, {
226
+ formatter: client.chain?.formatters?.block || formatBlock
227
+ });
228
+ }
229
+ var BlockNotFoundError = class extends BaseError {
230
+ constructor({
231
+ blockHash,
232
+ blockNumber
233
+ }) {
234
+ let identifier = "Block";
235
+ if (blockHash)
236
+ identifier = `Block at hash "${blockHash}"`;
237
+ if (blockNumber)
238
+ identifier = `Block at number "${blockNumber}"`;
239
+ super(`${identifier} could not be found.`);
240
+ __publicField(this, "name", "BlockNotFoundError");
241
+ }
242
+ };
243
+
244
+ // src/actions/public/getBlockNumber.ts
245
+ var cacheKey = (id) => `blockNumber.${id}`;
246
+ function getBlockNumberCache(id) {
247
+ return getCache(cacheKey(id));
248
+ }
249
+ async function getBlockNumber(client, { maxAge = client.pollingInterval } = {}) {
250
+ const blockNumberHex = await withCache(
251
+ () => client.request({
252
+ method: "eth_blockNumber"
253
+ }),
254
+ { cacheKey: cacheKey(client.uid), maxAge }
255
+ );
256
+ return BigInt(blockNumberHex);
257
+ }
258
+
259
+ // src/actions/public/getBlockTransactionCount.ts
260
+ async function getBlockTransactionCount(client, {
261
+ blockHash,
262
+ blockNumber,
263
+ blockTag = "latest"
264
+ } = {}) {
265
+ const blockNumberHex = blockNumber !== void 0 ? numberToHex(blockNumber) : void 0;
266
+ let count = null;
267
+ if (blockHash) {
268
+ count = await client.request({
269
+ method: "eth_getBlockTransactionCountByHash",
270
+ params: [blockHash]
271
+ });
272
+ } else {
273
+ count = await client.request({
274
+ method: "eth_getBlockTransactionCountByNumber",
275
+ params: [blockNumberHex || blockTag]
276
+ });
277
+ }
278
+ return hexToNumber(count);
279
+ }
280
+
281
+ // src/actions/public/getChainId.ts
282
+ async function getChainId(client) {
283
+ const chainIdHex = await client.request({ method: "eth_chainId" });
284
+ return hexToNumber(chainIdHex);
285
+ }
286
+
287
+ // src/actions/public/getFeeHistory.ts
288
+ async function getFeeHistory(client, {
289
+ blockCount,
290
+ blockNumber,
291
+ blockTag = "latest",
292
+ rewardPercentiles
293
+ }) {
294
+ const blockNumberHex = blockNumber ? numberToHex(blockNumber) : void 0;
295
+ const feeHistory = await client.request({
296
+ method: "eth_feeHistory",
297
+ params: [
298
+ numberToHex(blockCount),
299
+ blockNumberHex || blockTag,
300
+ rewardPercentiles
301
+ ]
302
+ });
303
+ return formatFeeHistory(feeHistory);
304
+ }
305
+
306
+ // src/actions/public/getFilterChanges.ts
307
+ async function getFilterChanges(client, { filter }) {
308
+ const logs = await client.request({
309
+ method: "eth_getFilterChanges",
310
+ params: [filter.id]
311
+ });
312
+ return logs.map(
313
+ (log) => typeof log === "string" ? log : formatLog(log)
314
+ );
315
+ }
316
+
317
+ // src/actions/public/getGasPrice.ts
318
+ async function getGasPrice(client) {
319
+ const gasPrice = await client.request({
320
+ method: "eth_gasPrice"
321
+ });
322
+ return BigInt(gasPrice);
323
+ }
324
+
325
+ // src/actions/public/getTransaction.ts
326
+ async function getTransaction(client, {
327
+ blockHash,
328
+ blockNumber,
329
+ blockTag = "latest",
330
+ hash,
331
+ index
332
+ }) {
333
+ const blockNumberHex = blockNumber !== void 0 ? numberToHex(blockNumber) : void 0;
334
+ let transaction = null;
335
+ if (hash) {
336
+ transaction = await client.request({
337
+ method: "eth_getTransactionByHash",
338
+ params: [hash]
339
+ });
340
+ } else if (blockHash) {
341
+ transaction = await client.request({
342
+ method: "eth_getTransactionByBlockHashAndIndex",
343
+ params: [blockHash, numberToHex(index)]
344
+ });
345
+ } else if (blockNumberHex || blockTag) {
346
+ transaction = await client.request({
347
+ method: "eth_getTransactionByBlockNumberAndIndex",
348
+ params: [blockNumberHex || blockTag, numberToHex(index)]
349
+ });
350
+ }
351
+ if (!transaction)
352
+ throw new TransactionNotFoundError({
353
+ blockHash,
354
+ blockNumber,
355
+ blockTag,
356
+ hash,
357
+ index
358
+ });
359
+ return format(transaction, {
360
+ formatter: client.chain?.formatters?.transaction || formatTransaction
361
+ });
362
+ }
363
+ var TransactionNotFoundError = class extends BaseError {
364
+ constructor({
365
+ blockHash,
366
+ blockNumber,
367
+ blockTag,
368
+ hash,
369
+ index
370
+ }) {
371
+ let identifier = "Transaction";
372
+ if (blockTag && index !== void 0)
373
+ identifier = `Transaction at block time "${blockTag}" at index "${index}"`;
374
+ if (blockHash && index !== void 0)
375
+ identifier = `Transaction at block hash "${blockHash}" at index "${index}"`;
376
+ if (blockNumber && index !== void 0)
377
+ identifier = `Transaction at block number "${blockNumber}" at index "${index}"`;
378
+ if (hash)
379
+ identifier = `Transaction with hash "${hash}"`;
380
+ super(`${identifier} could not be found.`);
381
+ __publicField(this, "name", "TransactionNotFoundError");
382
+ }
383
+ };
384
+
385
+ // src/actions/public/getTransactionConfirmations.ts
386
+ async function getTransactionConfirmations(client, { hash, transactionReceipt }) {
387
+ const [blockNumber, transaction] = await Promise.all([
388
+ getBlockNumber(client),
389
+ hash ? getTransaction(client, { hash }) : void 0
390
+ ]);
391
+ const transactionBlockNumber = transactionReceipt?.blockNumber || transaction?.blockNumber;
392
+ if (!transactionBlockNumber)
393
+ return 0n;
394
+ return blockNumber - transactionBlockNumber + 1n;
395
+ }
396
+
397
+ // src/actions/public/getTransactionCount.ts
398
+ async function getTransactionCount(client, { address, blockTag = "latest", blockNumber }) {
399
+ const count = await client.request({
400
+ method: "eth_getTransactionCount",
401
+ params: [address, blockNumber ? numberToHex(blockNumber) : blockTag]
402
+ });
403
+ return hexToNumber(count ?? "0x0");
404
+ }
405
+
406
+ // src/actions/public/getTransactionReceipt.ts
407
+ async function getTransactionReceipt(client, { hash }) {
408
+ const receipt = await client.request({
409
+ method: "eth_getTransactionReceipt",
410
+ params: [hash]
411
+ });
412
+ if (!receipt)
413
+ throw new TransactionReceiptNotFoundError({ hash });
414
+ return format(receipt, {
415
+ formatter: client.chain?.formatters?.transactionReceipt || formatTransactionReceipt
416
+ });
417
+ }
418
+ var TransactionReceiptNotFoundError = class extends BaseError {
419
+ constructor({ hash }) {
420
+ super(
421
+ `Transaction receipt with hash "${hash}" could not be found. The Transaction may not be processed on a block yet.`
422
+ );
423
+ __publicField(this, "name", "TransactionReceiptNotFoundError");
424
+ }
425
+ };
426
+
427
+ // src/actions/public/uninstallFilter.ts
428
+ async function uninstallFilter(client, { filter }) {
429
+ return client.request({
430
+ method: "eth_uninstallFilter",
431
+ params: [filter.id]
432
+ });
433
+ }
434
+
435
+ // src/utils/observe.ts
436
+ var listenersCache = /* @__PURE__ */ new Map();
437
+ var cleanupCache = /* @__PURE__ */ new Map();
438
+ var callbackCount = 0;
439
+ function observe(observerId, callbacks, fn) {
440
+ const callbackId = ++callbackCount;
441
+ const getListeners = () => listenersCache.get(observerId) || [];
442
+ const unsubscribe = () => {
443
+ const listeners2 = getListeners();
444
+ listenersCache.set(
445
+ observerId,
446
+ listeners2.filter((cb) => cb.id !== callbackId)
447
+ );
448
+ };
449
+ const unwatch = () => {
450
+ const cleanup2 = cleanupCache.get(observerId);
451
+ if (getListeners().length === 1 && cleanup2)
452
+ cleanup2();
453
+ unsubscribe();
454
+ };
455
+ const listeners = getListeners();
456
+ listenersCache.set(observerId, [
457
+ ...listeners,
458
+ { id: callbackId, fns: callbacks }
459
+ ]);
460
+ if (listeners && listeners.length > 0)
461
+ return unwatch;
462
+ let emit = {};
463
+ for (const key in callbacks) {
464
+ emit[key] = (...args) => {
465
+ const listeners2 = getListeners();
466
+ if (listeners2.length === 0)
467
+ return;
468
+ listeners2.forEach((listener) => listener.fns[key]?.(...args));
469
+ };
470
+ }
471
+ const cleanup = fn(emit);
472
+ if (typeof cleanup === "function")
473
+ cleanupCache.set(observerId, cleanup);
474
+ return unwatch;
475
+ }
476
+
477
+ // src/actions/public/waitForTransactionReceipt.ts
478
+ async function waitForTransactionReceipt(client, {
479
+ confirmations = 1,
480
+ hash,
481
+ onReplaced,
482
+ pollingInterval = client.pollingInterval,
483
+ timeout
484
+ }) {
485
+ const observerId = JSON.stringify([
486
+ "waitForTransactionReceipt",
487
+ client.uid,
488
+ hash
489
+ ]);
490
+ let transaction;
491
+ let replacedTransaction;
492
+ let receipt;
493
+ return new Promise((resolve, reject) => {
494
+ if (timeout)
495
+ setTimeout(
496
+ () => reject(new WaitForTransactionReceiptTimeoutError({ hash })),
497
+ timeout
498
+ );
499
+ const unobserve = observe(
500
+ observerId,
501
+ { onReplaced, resolve, reject },
502
+ (emit) => {
503
+ const unwatch = watchBlockNumber(client, {
504
+ emitMissed: true,
505
+ emitOnBegin: true,
506
+ pollingInterval,
507
+ async onBlockNumber(blockNumber) {
508
+ const done = async (fn) => {
509
+ unwatch();
510
+ fn();
511
+ unobserve();
512
+ };
513
+ try {
514
+ if (receipt) {
515
+ if (blockNumber - receipt.blockNumber + 1n < confirmations)
516
+ return;
517
+ done(() => emit.resolve(receipt));
518
+ return;
519
+ }
520
+ transaction = await getTransaction(client, { hash });
521
+ receipt = await getTransactionReceipt(client, { hash });
522
+ if (blockNumber - receipt.blockNumber + 1n < confirmations)
523
+ return;
524
+ done(() => emit.resolve(receipt));
525
+ } catch (err) {
526
+ if (transaction && (err instanceof TransactionNotFoundError || err instanceof TransactionReceiptNotFoundError)) {
527
+ replacedTransaction = transaction;
528
+ const block = await getBlock(client, {
529
+ blockNumber,
530
+ includeTransactions: true
531
+ });
532
+ const replacementTransaction = block.transactions.find(
533
+ ({ from, nonce }) => from === replacedTransaction.from && nonce === replacedTransaction.nonce
534
+ );
535
+ if (!replacementTransaction)
536
+ return;
537
+ receipt = await getTransactionReceipt(client, {
538
+ hash: replacementTransaction.hash
539
+ });
540
+ if (blockNumber - receipt.blockNumber + 1n < confirmations)
541
+ return;
542
+ let reason = "replaced";
543
+ if (replacementTransaction.to === replacedTransaction.to && replacementTransaction.value === replacedTransaction.value) {
544
+ reason = "repriced";
545
+ } else if (replacementTransaction.from === replacementTransaction.to && replacementTransaction.value === 0n) {
546
+ reason = "cancelled";
547
+ }
548
+ done(() => {
549
+ emit.onReplaced?.({
550
+ reason,
551
+ replacedTransaction,
552
+ transaction: replacementTransaction,
553
+ transactionReceipt: receipt
554
+ });
555
+ emit.resolve(receipt);
556
+ });
557
+ } else {
558
+ done(() => emit.reject(err));
559
+ }
560
+ }
561
+ }
562
+ });
563
+ return unwatch;
564
+ }
565
+ );
566
+ });
567
+ }
568
+ var WaitForTransactionReceiptTimeoutError = class extends BaseError {
569
+ constructor({ hash }) {
570
+ super(
571
+ `Timed out while waiting for transaction with hash "${hash}" to be confirmed.`
572
+ );
573
+ __publicField(this, "name", "WaitForTransactionReceiptTimeoutError");
574
+ }
575
+ };
576
+
577
+ // src/utils/poll.ts
578
+ function poll(fn, { emitOnBegin, initialWaitTime, interval }) {
579
+ let active = true;
580
+ const unwatch = () => active = false;
581
+ const watch = async () => {
582
+ let data;
583
+ if (emitOnBegin)
584
+ data = await fn({ unpoll: unwatch });
585
+ const initialWait = await initialWaitTime?.(data) ?? interval;
586
+ await wait(initialWait);
587
+ const poll2 = async () => {
588
+ if (!active)
589
+ return;
590
+ await fn({ unpoll: unwatch });
591
+ await wait(interval);
592
+ poll2();
593
+ };
594
+ poll2();
595
+ };
596
+ watch();
597
+ return unwatch;
598
+ }
599
+
600
+ // src/actions/public/watchBlockNumber.ts
601
+ function watchBlockNumber(client, {
602
+ emitOnBegin = false,
603
+ emitMissed = false,
604
+ onBlockNumber,
605
+ onError,
606
+ pollingInterval = client.pollingInterval
607
+ }) {
608
+ const observerId = JSON.stringify([
609
+ "watchBlockNumber",
610
+ client.uid,
611
+ emitOnBegin,
612
+ emitMissed,
613
+ pollingInterval
614
+ ]);
615
+ let prevBlockNumber;
616
+ return observe(
617
+ observerId,
618
+ { onBlockNumber, onError },
619
+ (emit) => poll(
620
+ async () => {
621
+ try {
622
+ const blockNumber = await getBlockNumber(client, { maxAge: 0 });
623
+ if (prevBlockNumber) {
624
+ if (blockNumber === prevBlockNumber)
625
+ return;
626
+ if (blockNumber - prevBlockNumber > 1 && emitMissed) {
627
+ for (let i = prevBlockNumber + 1n; i < blockNumber; i++) {
628
+ emit.onBlockNumber(i, prevBlockNumber);
629
+ prevBlockNumber = i;
630
+ }
631
+ }
632
+ }
633
+ prevBlockNumber = blockNumber;
634
+ emit.onBlockNumber(blockNumber, prevBlockNumber);
635
+ } catch (err) {
636
+ emit.onError?.(err);
637
+ }
638
+ },
639
+ {
640
+ emitOnBegin,
641
+ interval: pollingInterval
642
+ }
643
+ )
644
+ );
645
+ }
646
+
647
+ // src/actions/public/watchBlocks.ts
648
+ function watchBlocks(client, {
649
+ blockTag = "latest",
650
+ emitMissed = false,
651
+ emitOnBegin = false,
652
+ onBlock,
653
+ onError,
654
+ includeTransactions = false,
655
+ pollingInterval = client.pollingInterval
656
+ }) {
657
+ const observerId = JSON.stringify([
658
+ "watchBlocks",
659
+ client.uid,
660
+ emitMissed,
661
+ emitOnBegin,
662
+ includeTransactions,
663
+ pollingInterval
664
+ ]);
665
+ let prevBlock;
666
+ return observe(
667
+ observerId,
668
+ { onBlock, onError },
669
+ (emit) => poll(
670
+ async () => {
671
+ try {
672
+ const block = await getBlock(client, {
673
+ blockTag,
674
+ includeTransactions
675
+ });
676
+ if (block.number && prevBlock?.number) {
677
+ if (block.number === prevBlock.number)
678
+ return;
679
+ if (block.number - prevBlock.number > 1 && emitMissed) {
680
+ for (let i = prevBlock?.number + 1n; i < block.number; i++) {
681
+ const block2 = await getBlock(client, {
682
+ blockNumber: i,
683
+ includeTransactions
684
+ });
685
+ emit.onBlock(block2, prevBlock);
686
+ prevBlock = block2;
687
+ }
688
+ }
689
+ }
690
+ emit.onBlock(block, prevBlock);
691
+ prevBlock = block;
692
+ } catch (err) {
693
+ emit.onError?.(err);
694
+ }
695
+ },
696
+ {
697
+ emitOnBegin,
698
+ interval: pollingInterval
699
+ }
700
+ )
701
+ );
702
+ }
703
+
704
+ // src/actions/public/watchPendingTransactions.ts
705
+ function watchPendingTransactions(client, {
706
+ batch = true,
707
+ onError,
708
+ onTransactions,
709
+ pollingInterval = client.pollingInterval
710
+ }) {
711
+ const observerId = JSON.stringify([
712
+ "watchPendingTransactions",
713
+ client.uid,
714
+ batch,
715
+ pollingInterval
716
+ ]);
717
+ return observe(observerId, { onTransactions, onError }, (emit) => {
718
+ let filter;
719
+ const unwatch = poll(
720
+ async () => {
721
+ try {
722
+ if (!filter) {
723
+ try {
724
+ filter = await createPendingTransactionFilter(client);
725
+ return;
726
+ } catch (err) {
727
+ unwatch();
728
+ throw err;
729
+ }
730
+ }
731
+ const hashes = await getFilterChanges(client, { filter });
732
+ if (batch)
733
+ emit.onTransactions(hashes);
734
+ else
735
+ hashes.forEach((hash) => emit.onTransactions([hash]));
736
+ } catch (err) {
737
+ emit.onError?.(err);
738
+ }
739
+ },
740
+ {
741
+ emitOnBegin: true,
742
+ interval: pollingInterval
743
+ }
744
+ );
745
+ return async () => {
746
+ if (filter)
747
+ await uninstallFilter(client, { filter });
748
+ unwatch();
749
+ };
750
+ });
751
+ }
752
+
753
+ // src/actions/test/dropTransaction.ts
754
+ async function dropTransaction(client, { hash }) {
755
+ return await client.request({
756
+ method: `${client.mode}_dropTransaction`,
757
+ params: [hash]
758
+ });
759
+ }
760
+
761
+ // src/actions/test/getAutomine.ts
762
+ async function getAutomine(client) {
763
+ return await client.request({
764
+ method: `${client.mode}_getAutomine`
765
+ });
766
+ }
767
+
768
+ // src/actions/test/getTxpoolContent.ts
769
+ async function getTxpoolContent(client) {
770
+ return await client.request({
771
+ method: `txpool_content`
772
+ });
773
+ }
774
+
775
+ // src/actions/test/getTxpoolStatus.ts
776
+ async function getTxpoolStatus(client) {
777
+ const { pending, queued } = await client.request({
778
+ method: `txpool_status`
779
+ });
780
+ return {
781
+ pending: hexToNumber(pending),
782
+ queued: hexToNumber(queued)
783
+ };
784
+ }
785
+
786
+ // src/actions/test/impersonateAccount.ts
787
+ async function impersonateAccount(client, { address }) {
788
+ return await client.request({
789
+ method: `${client.mode}_impersonateAccount`,
790
+ params: [address]
791
+ });
792
+ }
793
+
794
+ // src/actions/test/increaseTime.ts
795
+ async function increaseTime(client, { seconds }) {
796
+ return await client.request({
797
+ method: "evm_increaseTime",
798
+ params: [numberToHex(seconds)]
799
+ });
800
+ }
801
+
802
+ // src/actions/test/inspectTxpool.ts
803
+ async function inspectTxpool(client) {
804
+ return await client.request({
805
+ method: `txpool_inspect`
806
+ });
807
+ }
808
+
809
+ // src/actions/test/mine.ts
810
+ async function mine(client, { blocks, interval }) {
811
+ return await client.request({
812
+ method: `${client.mode}_mine`,
813
+ params: [numberToHex(blocks), numberToHex(interval || 0)]
814
+ });
815
+ }
816
+
817
+ // src/actions/test/removeBlockTimestampInterval.ts
818
+ async function removeBlockTimestampInterval(client) {
819
+ return await client.request({
820
+ method: `${client.mode}_removeBlockTimestampInterval`
821
+ });
822
+ }
823
+
824
+ // src/actions/test/reset.ts
825
+ async function reset(client, { blockNumber, jsonRpcUrl } = {}) {
826
+ return await client.request({
827
+ method: `${client.mode}_reset`,
828
+ params: [{ forking: { blockNumber: Number(blockNumber), jsonRpcUrl } }]
829
+ });
830
+ }
831
+
832
+ // src/actions/test/revert.ts
833
+ async function revert(client, { id }) {
834
+ return await client.request({
835
+ method: `evm_revert`,
836
+ params: [id]
837
+ });
838
+ }
839
+
840
+ // src/actions/test/sendUnsignedTransaction.ts
841
+ async function sendUnsignedTransaction(client, { request }) {
842
+ const request_ = formatTransactionRequest(request);
843
+ const hash = await client.request({
844
+ method: "eth_sendUnsignedTransaction",
845
+ params: [request_]
846
+ });
847
+ return { hash };
848
+ }
849
+
850
+ // src/actions/test/setAutomine.ts
851
+ async function setAutomine(client, enabled) {
852
+ return await client.request({
853
+ method: "evm_setAutomine",
854
+ params: [enabled]
855
+ });
856
+ }
857
+
858
+ // src/actions/test/setBalance.ts
859
+ async function setBalance(client, { address, value }) {
860
+ return await client.request({
861
+ method: `${client.mode}_setBalance`,
862
+ params: [address, numberToHex(value)]
863
+ });
864
+ }
865
+
866
+ // src/actions/test/setBlockGasLimit.ts
867
+ async function setBlockGasLimit(client, { gasLimit }) {
868
+ return await client.request({
869
+ method: "evm_setBlockGasLimit",
870
+ params: [numberToHex(gasLimit)]
871
+ });
872
+ }
873
+
874
+ // src/actions/test/setBlockTimestampInterval.ts
875
+ async function setBlockTimestampInterval(client, { interval }) {
876
+ return await client.request({
877
+ method: `${client.mode}_setBlockTimestampInterval`,
878
+ params: [interval]
879
+ });
880
+ }
881
+
882
+ // src/actions/test/setCode.ts
883
+ async function setCode(client, { address, bytecode }) {
884
+ return await client.request({
885
+ method: `${client.mode}_setCode`,
886
+ params: [address, bytecode]
887
+ });
888
+ }
889
+
890
+ // src/actions/test/setCoinbase.ts
891
+ async function setCoinbase(client, { address }) {
892
+ return await client.request({
893
+ method: `${client.mode}_setCoinbase`,
894
+ params: [address]
895
+ });
896
+ }
897
+
898
+ // src/actions/test/setIntervalMining.ts
899
+ async function setIntervalMining(client, { interval }) {
900
+ return await client.request({
901
+ method: "evm_setIntervalMining",
902
+ params: [interval]
903
+ });
904
+ }
905
+
906
+ // src/actions/test/setLoggingEnabled.ts
907
+ async function setLoggingEnabled(client, enabled) {
908
+ return await client.request({
909
+ method: `${client.mode}_setLoggingEnabled`,
910
+ params: [enabled]
911
+ });
912
+ }
913
+
914
+ // src/actions/test/setMinGasPrice.ts
915
+ async function setMinGasPrice(client, { gasPrice }) {
916
+ return await client.request({
917
+ method: `${client.mode}_setMinGasPrice`,
918
+ params: [numberToHex(gasPrice)]
919
+ });
920
+ }
921
+
922
+ // src/actions/test/setNextBlockBaseFeePerGas.ts
923
+ async function setNextBlockBaseFeePerGas(client, { baseFeePerGas }) {
924
+ return await client.request({
925
+ method: `${client.mode}_setNextBlockBaseFeePerGas`,
926
+ params: [numberToHex(baseFeePerGas)]
927
+ });
928
+ }
929
+
930
+ // src/actions/test/setNextBlockTimestamp.ts
931
+ async function setNextBlockTimestamp(client, { timestamp }) {
932
+ return await client.request({
933
+ method: `evm_setNextBlockTimestamp`,
934
+ params: [numberToHex(timestamp)]
935
+ });
936
+ }
937
+
938
+ // src/actions/test/setNonce.ts
939
+ async function setNonce(client, { address, nonce }) {
940
+ return await client.request({
941
+ method: `${client.mode}_setNonce`,
942
+ params: [address, numberToHex(nonce)]
943
+ });
944
+ }
945
+
946
+ // src/actions/test/setStorageAt.ts
947
+ async function setStorageAt(client, { address, index, value }) {
948
+ return await client.request({
949
+ method: `${client.mode}_setStorageAt`,
950
+ params: [
951
+ address,
952
+ typeof index === "number" ? numberToHex(index) : index,
953
+ value
954
+ ]
955
+ });
956
+ }
957
+
958
+ // src/actions/test/snapshot.ts
959
+ async function snapshot(client) {
960
+ return await client.request({
961
+ method: `evm_snapshot`
962
+ });
963
+ }
964
+
965
+ // src/actions/test/stopImpersonatingAccount.ts
966
+ async function stopImpersonatingAccount(client, { address }) {
967
+ return await client.request({
968
+ method: `${client.mode}_stopImpersonatingAccount`,
969
+ params: [address]
970
+ });
971
+ }
972
+
973
+ export {
974
+ addChain,
975
+ getAccounts,
976
+ getPermissions,
977
+ requestAccounts,
978
+ requestPermissions,
979
+ sendTransaction,
980
+ signMessage,
981
+ switchChain,
982
+ watchAsset,
983
+ call,
984
+ createPendingTransactionFilter,
985
+ createBlockFilter,
986
+ estimateGas,
987
+ getBalance,
988
+ getBlock,
989
+ getBlockNumberCache,
990
+ getBlockNumber,
991
+ getBlockTransactionCount,
992
+ getChainId,
993
+ getFeeHistory,
994
+ getFilterChanges,
995
+ getGasPrice,
996
+ getTransaction,
997
+ getTransactionConfirmations,
998
+ getTransactionCount,
999
+ getTransactionReceipt,
1000
+ uninstallFilter,
1001
+ waitForTransactionReceipt,
1002
+ watchBlockNumber,
1003
+ watchBlocks,
1004
+ watchPendingTransactions,
1005
+ dropTransaction,
1006
+ getAutomine,
1007
+ getTxpoolContent,
1008
+ getTxpoolStatus,
1009
+ impersonateAccount,
1010
+ increaseTime,
1011
+ inspectTxpool,
1012
+ mine,
1013
+ removeBlockTimestampInterval,
1014
+ reset,
1015
+ revert,
1016
+ sendUnsignedTransaction,
1017
+ setAutomine,
1018
+ setBalance,
1019
+ setBlockGasLimit,
1020
+ setBlockTimestampInterval,
1021
+ setCode,
1022
+ setCoinbase,
1023
+ setIntervalMining,
1024
+ setLoggingEnabled,
1025
+ setMinGasPrice,
1026
+ setNextBlockBaseFeePerGas,
1027
+ setNextBlockTimestamp,
1028
+ setNonce,
1029
+ setStorageAt,
1030
+ snapshot,
1031
+ stopImpersonatingAccount
1032
+ };