Skip to content

Commit

Permalink
feat: is local helper properties on NetworkAPI [APE-1518] (#1730)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Nov 6, 2023
1 parent a7e6701 commit 098f680
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 22 deletions.
21 changes: 21 additions & 0 deletions src/ape/api/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,27 @@ def explorer(self) -> Optional["ExplorerAPI"]:

return None # May not have an block explorer

@property
def is_fork(self) -> bool:
"""
True when using a forked network.
"""
return self.name.endswith("-fork")

@property
def is_local(self) -> bool:
"""
True when using the local network.
"""
return self.name == LOCAL_NETWORK_NAME

@property
def is_dev(self) -> bool:
"""
True when using a local network, including forks.
"""
return self.is_local or self.is_fork

@cached_property
def providers(self): # -> Dict[str, Partial[ProviderAPI]]
"""
Expand Down
14 changes: 9 additions & 5 deletions src/ape/api/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ def send_private_transaction(self, txn: TransactionAPI, **kwargs) -> ReceiptAPI:
Returns:
:class:`~ape.api.transactions.ReceiptAPI`
"""
if self.network.name == LOCAL_NETWORK_NAME or self.network.name.endswith("-fork"):
if self.network.is_dev:
# Send the transaction as normal so testers can verify private=True
# and the txn still goes through.
logger.warning(
Expand Down Expand Up @@ -944,10 +944,14 @@ def estimate_gas_cost(self, txn: TransactionAPI, **kwargs) -> int:
@cached_property
def chain_id(self) -> int:
default_chain_id = None
if self.network.name not in (
"adhoc",
LOCAL_NETWORK_NAME,
) and not self.network.name.endswith("-fork"):
if (
self.network.name
not in (
"adhoc",
LOCAL_NETWORK_NAME,
)
and not self.network.is_fork
):
# If using a live network, the chain ID is hardcoded.
default_chain_id = self.network.chain_id

Expand Down
18 changes: 5 additions & 13 deletions src/ape/managers/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from ape.api import BlockAPI, ReceiptAPI
from ape.api.address import BaseAddress
from ape.api.networks import LOCAL_NETWORK_NAME, NetworkAPI, ProxyInfoAPI
from ape.api.networks import NetworkAPI, ProxyInfoAPI
from ape.api.query import (
AccountTransactionQuery,
BlockQuery,
Expand Down Expand Up @@ -271,14 +271,9 @@ def poll_blocks(
Returns:
Iterator[:class:`~ape.api.providers.BlockAPI`]
"""
network_name = self.provider.network.name
block_time = self.provider.network.block_time
timeout = (
(
10.0
if network_name == LOCAL_NETWORK_NAME or network_name.endswith("-fork")
else 50 * block_time
)
(10.0 if self.provider.network.is_dev else 50 * block_time)
if new_block_timeout is None
else new_block_timeout
)
Expand Down Expand Up @@ -316,10 +311,7 @@ def _try_timeout():
if time.time() - time_since_last > timeout:
time_waited = round(time.time() - time_since_last, 4)
message = f"Timed out waiting for new block (time_waited={time_waited})."
if (
self.provider.network.name == LOCAL_NETWORK_NAME
or self.provider.network.name.endswith("-fork")
):
if self.provider.network.is_dev:
message += (
" If using a local network, try configuring mining to mine on an interval "
"or adjusting the block time."
Expand Down Expand Up @@ -735,7 +727,7 @@ def _is_live_network(self) -> bool:
if not self.network_manager.active_provider:
return False

return self._network.name != LOCAL_NETWORK_NAME and not self._network.name.endswith("-fork")
return not self._network.is_dev

@property
def _data_network_name(self) -> str:
Expand Down Expand Up @@ -1103,7 +1095,7 @@ def get(

return contract_type

if self._network.name == LOCAL_NETWORK_NAME:
if self._network.is_local:
# Don't check disk-cache or explorer when using local
if default:
self._local_contract_types[address_key] = default
Expand Down
4 changes: 1 addition & 3 deletions src/ape/managers/project/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from ethpm_types.utils import AnyUrl, Hex

from ape.api import DependencyAPI, ProjectAPI
from ape.api.networks import LOCAL_NETWORK_NAME
from ape.contracts import ContractContainer, ContractInstance, ContractNamespace
from ape.exceptions import ApeAttributeError, APINotImplementedError, ChainError, ProjectError
from ape.logging import logger
Expand Down Expand Up @@ -698,8 +697,7 @@ def track_deployment(self, contract: ContractInstance):
to track as a deployment of the project.
"""

network = self.provider.network.name
if network == LOCAL_NETWORK_NAME or network.endswith("-fork"):
if self.provider.network.is_dev:
raise ProjectError("Can only publish deployments on a live network.")

if not (contract_name := contract.contract_type.name):
Expand Down
2 changes: 1 addition & 1 deletion src/ape_cache/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def database_connection(self):
Returns:
Optional[`sqlalchemy.engine.Connection`]
"""
if self.provider.network.name == LOCAL_NETWORK_NAME:
if self.provider.network.is_local:
return None

if not self.network_manager.active_provider:
Expand Down

0 comments on commit 098f680

Please sign in to comment.