-
-
Notifications
You must be signed in to change notification settings - Fork 124
/
providers.py
1189 lines (966 loc) · 38.4 KB
/
providers.py
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 atexit
import ctypes
import datetime
import logging
import platform
import shutil
import sys
import time
import warnings
from collections.abc import Iterable, Iterator
from logging import FileHandler, Formatter, Logger, getLogger
from pathlib import Path
from signal import SIGINT, SIGTERM, signal
from subprocess import DEVNULL, PIPE, Popen
from typing import Any, Optional, Union, cast
from eth_pydantic_types import HexBytes
from ethpm_types.abi import EventABI
from pydantic import Field, computed_field, model_validator
from ape.api.config import PluginConfig
from ape.api.networks import NetworkAPI
from ape.api.query import BlockTransactionQuery
from ape.api.trace import TraceAPI
from ape.api.transactions import ReceiptAPI, TransactionAPI
from ape.exceptions import (
APINotImplementedError,
ProviderError,
QueryEngineError,
RPCTimeoutError,
SubprocessError,
SubprocessTimeoutError,
VirtualMachineError,
)
from ape.logging import LogLevel, logger
from ape.types import AddressType, BlockID, ContractCode, ContractLog, LogFilter, SnapshotID
from ape.utils import BaseInterfaceModel, JoinableQueue, abstractmethod, cached_property, spawn
from ape.utils.misc import (
EMPTY_BYTES32,
_create_raises_not_implemented_error,
log_instead_of_fail,
raises_not_implemented,
)
class BlockAPI(BaseInterfaceModel):
"""
An abstract class representing a block and its attributes.
"""
# NOTE: All fields in this class (and it's subclasses) should not be `Optional`
# except the edge cases noted below
"""
The number of transactions in the block.
"""
num_transactions: int = 0
"""
The block hash identifier.
"""
hash: Optional[Any] = None # NOTE: pending block does not have a hash
"""
The block number identifier.
"""
number: Optional[int] = None # NOTE: pending block does not have a number
"""
The preceeding block's hash.
"""
parent_hash: Any = Field(
EMPTY_BYTES32, alias="parentHash"
) # NOTE: genesis block has no parent hash
"""
The timestamp the block was produced.
NOTE: The pending block uses the current timestamp.
"""
timestamp: int
_size: Optional[int] = None
@log_instead_of_fail(default="<BlockAPI>")
def __repr__(self) -> str:
return super().__repr__()
@property
def datetime(self) -> datetime.datetime:
"""
The block timestamp as a datetime object.
"""
return datetime.datetime.fromtimestamp(self.timestamp, tz=datetime.timezone.utc)
@model_validator(mode="before")
@classmethod
def convert_parent_hash(cls, data):
parent_hash = data.get("parent_hash", data.get("parentHash")) or EMPTY_BYTES32
data["parentHash"] = parent_hash
return data
@model_validator(mode="wrap")
@classmethod
def validate_size(cls, values, handler):
"""
A validator for handling non-computed size.
Saves it to a private member on this class and
gets returned in computed field "size".
"""
if isinstance(values, BlockAPI):
size = values.size
else:
if not hasattr(values, "pop"):
# Handle weird AttributeDict missing pop method.
# https://github.com/ethereum/web3.py/issues/3326
values = {**values}
size = values.pop("size", None)
model = handler(values)
if size is not None:
model._size = size
return model
@computed_field() # type: ignore[misc]
@cached_property
def transactions(self) -> list[TransactionAPI]:
"""
All transactions in a block.
"""
try:
query = BlockTransactionQuery(columns=["*"], block_id=self.hash)
return cast(list[TransactionAPI], list(self.query_manager.query(query)))
except QueryEngineError as err:
# NOTE: Re-raising a better error here because was confusing
# when doing anything with fields, and this would fail.
raise ProviderError(f"Unable to find block transactions: {err}") from err
@computed_field() # type: ignore[misc]
@cached_property
def size(self) -> int:
"""
The size of the block in gas. Most of the time,
this field is passed to the model at validation time,
but occassionally it is missing (like in `eth_subscribe:newHeads`),
in which case it gets calculated if and only if the user
requests it (or during serialization of this model to disk).
"""
if self._size is not None:
# The size was provided with the rest of the model
# (normal).
return self._size
raise APINotImplementedError()
class ProviderAPI(BaseInterfaceModel):
"""
An abstraction of a connection to a network in an ecosystem. Example ``ProviderAPI``
implementations include the `ape-infura <https://github.com/ApeWorX/ape-infura>`__
plugin or the `ape-hardhat <https://github.com/ApeWorX/ape-hardhat>`__ plugin.
"""
name: str
"""The name of the provider (should be the plugin name)."""
network: NetworkAPI
"""A reference to the network this provider provides."""
provider_settings: dict = {}
"""The settings for the provider, as overrides to the configuration."""
request_header: dict
"""A header to set on HTTP/RPC requests."""
block_page_size: int = 100
"""
The amount of blocks to fetch in a response, as a default.
This is particularly useful for querying logs across a block range.
"""
concurrency: int = 4
"""
How many parallel threads to use when fetching logs.
"""
@property
def data_folder(self) -> Path:
"""
The path to the provider's data,
e.g. ``$HOME/.api/{self.name}`` unless overridden.
"""
return self.config_manager.DATA_FOLDER / self.name
@property
@abstractmethod
def is_connected(self) -> bool:
"""
``True`` if currently connected to the provider. ``False`` otherwise.
"""
@property
def connection_str(self) -> str:
"""
The str representing how to connect
to the node, such as an HTTP URL
or an IPC path.
"""
return ""
@abstractmethod
def connect(self):
"""
Connect a to a provider, such as start-up a process or create an HTTP connection.
"""
@abstractmethod
def disconnect(self):
"""
Disconnect from a provider, such as tear-down a process or quit an HTTP session.
"""
@property
def http_uri(self) -> Optional[str]:
"""
Return the raw HTTP/HTTPS URI to connect to this provider, if supported.
"""
return None
@property
def ws_uri(self) -> Optional[str]:
"""
Return the raw WS/WSS URI to connect to this provider, if supported.
"""
return None
@property
def settings(self) -> PluginConfig:
"""
The combination of settings from ``ape-config.yaml`` and ``.provider_settings``.
"""
CustomConfig = self.config.__class__
data = {**self.config.model_dump(), **self.provider_settings}
return CustomConfig.model_validate(data)
@property
def connection_id(self) -> Optional[str]:
"""
A connection ID to uniquely identify and manage multiple
connections to providers, especially when working with multiple
providers of the same type, like multiple Geth --dev nodes.
"""
try:
chain_id = self.chain_id
except Exception:
if chain_id := self.settings.get("chain_id"):
pass
else:
# A connection is required to obtain a chain ID for this provider.
return None
# NOTE: If other provider settings are different, ``.update_settings()``
# should be called.
return f"{self.network_choice}:{chain_id}"
@abstractmethod
def update_settings(self, new_settings: dict):
"""
Change a provider's setting, such as configure a new port to run on.
May require a reconnect.
Args:
new_settings (dict): The new provider settings.
"""
@property
@abstractmethod
def chain_id(self) -> int:
"""
The blockchain ID.
See `ChainList <https://chainlist.org/>`__ for a comprehensive list of IDs.
"""
@abstractmethod
def get_balance(self, address: AddressType, block_id: Optional[BlockID] = None) -> int:
"""
Get the balance of an account.
Args:
address (:class:`~ape.types.address.AddressType`): The address of the account.
block_id (:class:`~ape.types.BlockID`): Optionally specify a block
ID. Defaults to using the latest block.
Returns:
int: The account balance.
"""
@abstractmethod
def get_code(self, address: AddressType, block_id: Optional[BlockID] = None) -> ContractCode:
"""
Get the bytes a contract.
Args:
address (:class:`~ape.types.address.AddressType`): The address of the contract.
block_id (Optional[:class:`~ape.types.BlockID`]): The block ID
for checking a previous account nonce.
Returns:
:class:`~ape.types.ContractCode`: The contract bytecode.
"""
@property
def network_choice(self) -> str:
"""
The connected network choice string.
"""
if self.network.is_adhoc and self.connection_str:
# `custom` is not a real network and is same
# as using raw connection str
return self.connection_str
elif self.network.is_adhoc:
raise ProviderError("Custom network provider missing `connection_str`.")
return f"{self.network.choice}:{self.name}"
@abstractmethod
def make_request(self, rpc: str, parameters: Optional[Iterable] = None) -> Any:
"""
Make a raw RPC request to the provider.
Advanced featues such as tracing may utilize this to by-pass unnecessary
class-serializations.
"""
@raises_not_implemented
def stream_request( # type: ignore[empty-body]
self, method: str, params: Iterable, iter_path: str = "result.item"
) -> Iterator[Any]:
"""
Stream a request, great for large requests like events or traces.
Args:
method (str): The RPC method to call.
params (Iterable): Parameters for the method.s
iter_path (str): The response dict-path to the items.
Returns:
An iterator of items.
"""
def get_storage_at(self, *args, **kwargs) -> HexBytes:
warnings.warn(
"'provider.get_storage_at()' is deprecated. Use 'provider.get_storage()'.",
DeprecationWarning,
)
return self.get_storage(*args, **kwargs)
@raises_not_implemented
def get_storage( # type: ignore[empty-body]
self, address: AddressType, slot: int, block_id: Optional[BlockID] = None
) -> HexBytes:
"""
Gets the raw value of a storage slot of a contract.
Args:
address (AddressType): The address of the contract.
slot (int): Storage slot to read the value of.
block_id (Optional[:class:`~ape.types.BlockID`]): The block ID
for checking a previous storage value.
Returns:
HexBytes: The value of the storage slot.
"""
@abstractmethod
def get_nonce(self, address: AddressType, block_id: Optional[BlockID] = None) -> int:
"""
Get the number of times an account has transacted.
Args:
address (AddressType): The address of the account.
block_id (Optional[:class:`~ape.types.BlockID`]): The block ID
for checking a previous account nonce.
Returns:
int
"""
@abstractmethod
def estimate_gas_cost(self, txn: TransactionAPI, block_id: Optional[BlockID] = None) -> int:
"""
Estimate the cost of gas for a transaction.
Args:
txn (:class:`~ape.api.transactions.TransactionAPI`):
The transaction to estimate the gas for.
block_id (Optional[:class:`~ape.types.BlockID`]): The block ID
to use when estimating the transaction. Useful for checking a
past estimation cost of a transaction.
Returns:
int: The estimated cost of gas to execute the transaction
reported in the fee-currency's smallest unit, e.g. Wei. If the
provider's network has been configured with a gas limit override, it
will be returned. If the gas limit configuration is "max" this will
return the block maximum gas limit.
"""
@property
@abstractmethod
def gas_price(self) -> int:
"""
The price for what it costs to transact
(pre-`EIP-1559 <https://eips.ethereum.org/EIPS/eip-1559>`__).
"""
@property
@abstractmethod
def max_gas(self) -> int:
"""
The max gas limit value you can use.
"""
@property
def config(self) -> PluginConfig:
"""
The provider's configuration.
"""
return self.config_manager.get_config(self.name)
@property
def priority_fee(self) -> int:
"""
A miner tip to incentivize them to include your transaction in a block.
Raises:
NotImplementedError: When the provider does not implement
`EIP-1559 <https://eips.ethereum.org/EIPS/eip-1559>`__ typed transactions.
"""
raise APINotImplementedError("priority_fee is not implemented by this provider")
@property
def supports_tracing(self) -> bool:
"""
``True`` when the provider can provide transaction traces.
"""
return False
@property
def base_fee(self) -> int:
"""
The minimum value required to get your transaction included on the next block.
Only providers that implement `EIP-1559 <https://eips.ethereum.org/EIPS/eip-1559>`__
will use this property.
Raises:
NotImplementedError: When this provider does not implement
`EIP-1559 <https://eips.ethereum.org/EIPS/eip-1559>`__.
"""
raise APINotImplementedError("base_fee is not implemented by this provider")
@abstractmethod
def get_block(self, block_id: BlockID) -> BlockAPI:
"""
Get a block.
Args:
block_id (:class:`~ape.types.BlockID`): The ID of the block to get.
Can be ``"latest"``, ``"earliest"``, ``"pending"``, a block hash or a block number.
Raises:
:class:`~ape.exceptions.BlockNotFoundError`: Likely the exception raised when a block
is not found (depends on implementation).
Returns:
:class:`~ape.types.BlockID`: The block for the given ID.
"""
@abstractmethod
def send_call(
self,
txn: TransactionAPI,
block_id: Optional[BlockID] = None,
state: Optional[dict] = None,
**kwargs,
) -> HexBytes: # Return value of function
"""
Execute a new transaction call immediately without creating a
transaction on the block chain.
Args:
txn: :class:`~ape.api.transactions.TransactionAPI`
block_id (Optional[:class:`~ape.types.BlockID`]): The block ID
to use to send a call at a historical point of a contract.
Useful for checking a past estimation cost of a transaction.
state (Optional[dict]): Modify the state of the blockchain
prior to sending the call, for testing purposes.
**kwargs: Provider-specific extra kwargs.
Returns:
str: The result of the transaction call.
"""
@abstractmethod
def get_receipt(self, txn_hash: str, **kwargs) -> ReceiptAPI:
"""
Get the information about a transaction from a transaction hash.
Args:
txn_hash (str): The hash of the transaction to retrieve.
kwargs: Any other kwargs that other providers might allow when fetching a receipt.
Returns:
:class:`~api.providers.ReceiptAPI`:
The receipt of the transaction with the given hash.
"""
@abstractmethod
def get_transactions_by_block(self, block_id: BlockID) -> Iterator[TransactionAPI]:
"""
Get the information about a set of transactions from a block.
Args:
block_id (:class:`~ape.types.BlockID`): The ID of the block.
Returns:
Iterator[:class: `~ape.api.transactions.TransactionAPI`]
"""
@raises_not_implemented
def get_transactions_by_account_nonce( # type: ignore[empty-body]
self,
account: AddressType,
start_nonce: int = 0,
stop_nonce: int = -1,
) -> Iterator[ReceiptAPI]:
"""
Get account history for the given account.
Args:
account (:class:`~ape.types.address.AddressType`): The address of the account.
start_nonce (int): The nonce of the account to start the search with.
stop_nonce (int): The nonce of the account to stop the search with.
Returns:
Iterator[:class:`~ape.api.transactions.ReceiptAPI`]
"""
@abstractmethod
def send_transaction(self, txn: TransactionAPI) -> ReceiptAPI:
"""
Send a transaction to the network.
Args:
txn (:class:`~ape.api.transactions.TransactionAPI`): The transaction to send.
Returns:
:class:`~ape.api.transactions.ReceiptAPI`
"""
@abstractmethod
def get_contract_logs(self, log_filter: LogFilter) -> Iterator[ContractLog]:
"""
Get logs from contracts.
Args:
log_filter (:class:`~ape.types.LogFilter`): A mapping of event ABIs to
topic filters. Defaults to getting all events.
Returns:
Iterator[:class:`~ape.types.ContractLog`]
"""
def send_private_transaction(self, txn: TransactionAPI, **kwargs) -> ReceiptAPI:
"""
Send a transaction through a private mempool (if supported by the Provider).
Raises:
:class:`~ape.exceptions.APINotImplementedError`: If using a non-local
network and not implemented by the provider.
Args:
txn (:class:`~ape.api.transactions.TransactionAPI`): The transaction
to privately publish.
**kwargs: Additional kwargs to be optionally handled by the provider.
Returns:
:class:`~ape.api.transactions.ReceiptAPI`
"""
if self.network.is_dev:
# Send the transaction as normal so testers can verify private=True
# and the txn still goes through.
logger.warning(
f"private=True is set but connected to network '{self.network.name}' ."
f"Using regular '{self.send_transaction.__name__}()' method (not private)."
)
return self.send_transaction(txn)
# What happens normally from `raises_not_implemented()` decorator.
raise _create_raises_not_implemented_error(self.send_private_transaction)
@raises_not_implemented
def snapshot(self) -> SnapshotID: # type: ignore[empty-body]
"""
Defined to make the ``ProviderAPI`` interchangeable with a
:class:`~ape.api.providers.TestProviderAPI`, as in
:class:`ape.managers.chain.ChainManager`.
Raises:
:class:`~ape.exceptions.APINotImplementedError`: Unless overriden.
"""
@raises_not_implemented
def restore(self, snapshot_id: SnapshotID):
"""
Defined to make the ``ProviderAPI`` interchangeable with a
:class:`~ape.api.providers.TestProviderAPI`, as in
:class:`ape.managers.chain.ChainManager`.
Raises:
:class:`~ape.exceptions.APINotImplementedError`: Unless overriden.
"""
@raises_not_implemented
def set_timestamp(self, new_timestamp: int):
"""
Defined to make the ``ProviderAPI`` interchangeable with a
:class:`~ape.api.providers.TestProviderAPI`, as in
:class:`ape.managers.chain.ChainManager`.
Raises:
:class:`~ape.exceptions.APINotImplementedError`: Unless overriden.
"""
@raises_not_implemented
def mine(self, num_blocks: int = 1):
"""
Defined to make the ``ProviderAPI`` interchangeable with a
:class:`~ape.api.providers.TestProviderAPI`, as in
:class:`ape.managers.chain.ChainManager`.
Raises:
:class:`~ape.exceptions.APINotImplementedError`: Unless overriden.
"""
@raises_not_implemented
def set_balance(self, address: AddressType, amount: int):
"""
Change the balance of an account.
Args:
address (AddressType): An address on the network.
amount (int): The balance to set in the address.
"""
@log_instead_of_fail(default="<ProviderAPI>")
def __repr__(self) -> str:
return f"<{self.name.capitalize()} chain_id={self.chain_id}>"
@raises_not_implemented
def set_code( # type: ignore[empty-body]
self, address: AddressType, code: ContractCode
) -> bool:
"""
Change the code of a smart contract, for development purposes.
Test providers implement this method when they support it.
Args:
address (AddressType): An address on the network.
code (:class:`~ape.types.ContractCode`): The new bytecode.
"""
@raises_not_implemented
def set_storage( # type: ignore[empty-body]
self, address: AddressType, slot: int, value: HexBytes
):
"""
Sets the raw value of a storage slot of a contract.
Args:
address (str): The address of the contract.
slot (int): Storage slot to write the value to.
value: (HexBytes): The value to overwrite the raw storage slot with.
"""
@raises_not_implemented
def unlock_account(self, address: AddressType) -> bool: # type: ignore[empty-body]
"""
Ask the provider to allow an address to submit transactions without validating
signatures. This feature is intended to be subclassed by a
:class:`~ape.api.providers.TestProviderAPI` so that during a fork-mode test,
a transaction can be submitted by an arbitrary account or contract without a private key.
Raises:
NotImplementedError: When this provider does not support unlocking an account.
Args:
address (:class:`~ape.types.address.AddressType`): The address to unlock.
Returns:
bool: ``True`` if successfully unlocked account and ``False`` otherwise.
"""
@raises_not_implemented
def get_transaction_trace( # type: ignore[empty-body]
self, txn_hash: Union[HexBytes, str]
) -> TraceAPI:
"""
Provide a detailed description of opcodes.
Args:
transaction_hash (Union[HexBytes, str]): The hash of a transaction
to trace.
Returns:
:class:`~ape.api.trace.TraceAPI`: A transaction trace.
"""
@raises_not_implemented
def poll_blocks( # type: ignore[empty-body]
self,
stop_block: Optional[int] = None,
required_confirmations: Optional[int] = None,
new_block_timeout: Optional[int] = None,
) -> Iterator[BlockAPI]:
"""
Poll new blocks.
**NOTE**: When a chain reorganization occurs, this method logs an error and
yields the missed blocks, even if they were previously yielded with different
block numbers.
**NOTE**: This is a daemon method; it does not terminate unless an exception occurs
or a ``stop_block`` is given.
Args:
stop_block (Optional[int]): Optionally set a future block number to stop at.
Defaults to never-ending.
required_confirmations (Optional[int]): The amount of confirmations to wait
before yielding the block. The more confirmations, the less likely a reorg will occur.
Defaults to the network's configured required confirmations.
new_block_timeout (Optional[float]): The amount of time to wait for a new block before
timing out. Defaults to 10 seconds for local networks or ``50 * block_time`` for live
networks.
Returns:
Iterator[:class:`~ape.api.providers.BlockAPI`]
"""
@raises_not_implemented
def poll_logs( # type: ignore[empty-body]
self,
stop_block: Optional[int] = None,
address: Optional[AddressType] = None,
topics: Optional[list[Union[str, list[str]]]] = None,
required_confirmations: Optional[int] = None,
new_block_timeout: Optional[int] = None,
events: Optional[list[EventABI]] = None,
) -> Iterator[ContractLog]:
"""
Poll new blocks. Optionally set a start block to include historical blocks.
**NOTE**: This is a daemon method; it does not terminate unless an exception occurs.
Usage example::
for new_log in contract.MyEvent.poll_logs():
print(f"New event log found: block_number={new_log.block_number}")
Args:
stop_block (Optional[int]): Optionally set a future block number to stop at.
Defaults to never-ending.
address (Optional[str]): The address of the contract to filter logs by.
Defaults to all addresses.
topics (Optional[list[Union[str, list[str]]]]): The topics to filter logs by.
Defaults to all topics.
required_confirmations (Optional[int]): The amount of confirmations to wait
before yielding the block. The more confirmations, the less likely a reorg will occur.
Defaults to the network's configured required confirmations.
new_block_timeout (Optional[int]): The amount of time to wait for a new block before
quitting. Defaults to 10 seconds for local networks or ``50 * block_time`` for live
networks.
events (Optional[list[``EventABI``]]): An optional list of events to listen on.
Returns:
Iterator[:class:`~ape.types.ContractLog`]
"""
def prepare_transaction(self, txn: TransactionAPI) -> TransactionAPI:
"""
Set default values on the transaction.
Raises:
:class:`~ape.exceptions.TransactionError`: When given negative required confirmations.
Args:
txn (:class:`~ape.api.transactions.TransactionAPI`): The transaction to prepare.
Returns:
:class:`~ape.api.transactions.TransactionAPI`
"""
return txn
def get_virtual_machine_error(self, exception: Exception, **kwargs) -> VirtualMachineError:
"""
Get a virtual machine error from an error returned from your RPC.
Args:
exception (Exception): The error returned from your RPC client.
Returns:
:class:`~ape.exceptions.VirtualMachineError`: An error representing what
went wrong in the call.
"""
return VirtualMachineError(base_err=exception, **kwargs)
class TestProviderAPI(ProviderAPI):
"""
An API for providers that have development functionality, such as snapshotting.
"""
@cached_property
def test_config(self) -> PluginConfig:
return self.config_manager.get_config("test")
@abstractmethod
def snapshot(self) -> SnapshotID:
"""
Record the current state of the blockchain with intent to later
call the method :meth:`~ape.managers.chain.ChainManager.revert`
to go back to this point. This method is for local networks only.
Returns:
:class:`~ape.types.SnapshotID`: The snapshot ID.
"""
@abstractmethod
def restore(self, snapshot_id: SnapshotID):
"""
Regress the current call using the given snapshot ID.
Allows developers to go back to a previous state.
Args:
snapshot_id (str): The snapshot ID.
"""
@abstractmethod
def set_timestamp(self, new_timestamp: int):
"""
Change the pending timestamp.
Args:
new_timestamp (int): The timestamp to set.
Returns:
int: The new timestamp.
"""
@abstractmethod
def mine(self, num_blocks: int = 1):
"""
Advance by the given number of blocks.
Args:
num_blocks (int): The number of blocks allotted to mine. Defaults to ``1``.
"""
@property
@abstractmethod
def auto_mine(self) -> bool:
"""
Whether automine is enabled.
"""
@auto_mine.setter
@abstractmethod
def auto_mine(self) -> bool:
"""
Enable or disbale automine.
"""
def _increment_call_func_coverage_hit_count(self, txn: TransactionAPI):
"""
A helper method for incrementing a method call function hit count in a
non-orthodox way. This is because Hardhat does not support call traces yet.
"""
if (
not txn.receiver
or not self._test_runner
or not self._test_runner.config_wrapper.track_coverage
):
return
if not (contract_type := self.chain_manager.contracts.get(txn.receiver)) or not (
contract_src := self.local_project._create_contract_source(contract_type)
):
return
method_id = txn.data[:4]
if method_id in contract_type.view_methods:
method = contract_type.methods[method_id]
self._test_runner.coverage_tracker.hit_function(contract_src, method)
class UpstreamProvider(ProviderAPI):
"""
A provider that can also be set as another provider's upstream.
"""
class SubprocessProvider(ProviderAPI):
"""
A provider that manages a process, such as for ``ganache``.
"""
PROCESS_WAIT_TIMEOUT: int = 15
process: Optional[Popen] = None
is_stopping: bool = False
stdout_queue: Optional[JoinableQueue] = None
stderr_queue: Optional[JoinableQueue] = None
@property
@abstractmethod
def process_name(self) -> str:
"""The name of the process, such as ``Hardhat node``."""
@abstractmethod
def build_command(self) -> list[str]:
"""
Get the command as a list of ``str``.
Subclasses should override and add command arguments if needed.
Returns:
list[str]: The command to pass to ``subprocess.Popen``.
"""
@property
def base_logs_path(self) -> Path:
return self.config_manager.DATA_FOLDER / self.name / "subprocess_output"
@property
def stdout_logs_path(self) -> Path:
return self.base_logs_path / "stdout.log"
@property
def stderr_logs_path(self) -> Path:
return self.base_logs_path / "stderr.log"
@cached_property
def _stdout_logger(self) -> Logger:
return self._get_process_output_logger("stdout", self.stdout_logs_path)
@cached_property
def _stderr_logger(self) -> Logger:
return self._get_process_output_logger("stderr", self.stderr_logs_path)
@property
def connection_id(self) -> Optional[str]:
cmd_id = ",".join(self.build_command())
return f"{self.network_choice}:{cmd_id}"
def _get_process_output_logger(self, name: str, path: Path):
logger = getLogger(f"{self.name}_{name}_subprocessProviderLogger")
path.parent.mkdir(parents=True, exist_ok=True)
if path.is_file():
path.unlink()
path.touch()
handler = FileHandler(str(path))
handler.setFormatter(Formatter("%(message)s"))
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
return logger
def connect(self):
"""
Start the process and connect to it.
Subclasses handle the connection-related tasks.
"""
if self.is_connected:
raise ProviderError("Cannot connect twice. Call disconnect before connecting again.")
# Always disconnect after,
# unless running tests with `disconnect_providers_after: false`.
disconnect_after = (
self._test_runner is None
or self.config_manager.get_config("test").disconnect_providers_after
)
if disconnect_after:
atexit.register(self.disconnect)