Skip to content

Commit

Permalink
refactor: share config logic with ape-ethereum (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Jan 22, 2024
1 parent a2698c6 commit 83f6b26
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 39 deletions.
50 changes: 12 additions & 38 deletions ape_optimism/ecosystem.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from typing import Optional, Type, cast
from typing import cast

from ape.api.config import PluginConfig
from ape.api.networks import LOCAL_NETWORK_NAME
from ape.exceptions import ApeException
from ape.utils import DEFAULT_LOCAL_TRANSACTION_ACCEPTANCE_TIMEOUT
from ape_ethereum.ecosystem import Ethereum, ForkedNetworkConfig, NetworkConfig
from ape_ethereum.transactions import TransactionType
from ape_ethereum.ecosystem import (
BaseEthereumConfig,
Ethereum,
NetworkConfig,
create_network_config,
)

NETWORKS = {
# chain_id, network_id
Expand All @@ -21,38 +22,11 @@ class ApeOptimismError(ApeException):
"""


def _create_config(
required_confirmations: int = 1, block_time: int = 2, cls: Type = NetworkConfig, **kwargs
) -> NetworkConfig:
return cls(
required_confirmations=required_confirmations,
block_time=block_time,
default_transaction_type=TransactionType.DYNAMIC,
**kwargs,
)


def _create_local_config(default_provider: Optional[str] = None, use_fork: bool = False, **kwargs):
return _create_config(
block_time=0,
default_provider=default_provider,
gas_limit="max",
required_confirmations=0,
transaction_acceptance_timeout=DEFAULT_LOCAL_TRANSACTION_ACCEPTANCE_TIMEOUT,
cls=ForkedNetworkConfig if use_fork else NetworkConfig,
**kwargs,
)


class OptimismConfig(PluginConfig):
mainnet: NetworkConfig = _create_config()
mainnet_fork: ForkedNetworkConfig = _create_local_config(use_fork=True)
goerli: NetworkConfig = _create_config()
goerli_fork: ForkedNetworkConfig = _create_local_config(use_fork=True)
sepolia: NetworkConfig = _create_config()
sepolia_fork: ForkedNetworkConfig = _create_local_config(use_fork=True)
local: NetworkConfig = _create_local_config(default_provider="test")
default_network: str = LOCAL_NETWORK_NAME
# NOTE: Forked networks automatically are included.
class OptimismConfig(BaseEthereumConfig):
mainnet: NetworkConfig = create_network_config(block_time=2)
goerli: NetworkConfig = create_network_config(block_time=2)
sepolia: NetworkConfig = create_network_config(block_time=2)


class Optimism(Ethereum):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
url="https://github.com/ApeWorX/ape-optimism",
include_package_data=True,
install_requires=[
"eth-ape>=0.7.0,<0.8",
"eth-ape>=0.7.5,<0.8",
"ethpm-types", # Use same version as eth-ape
],
python_requires=">=3.8,<4",
Expand Down
12 changes: 12 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from ape_optimism.ecosystem import OptimismConfig


def test_mainnet_fork_not_configured():
obj = OptimismConfig.model_validate({})
assert obj.mainnet_fork.required_confirmations == 0


def test_mainnet_fork_configured():
data = {"mainnet_fork": {"required_confirmations": 555}}
obj = OptimismConfig.model_validate(data)
assert obj.mainnet_fork.required_confirmations == 555

0 comments on commit 83f6b26

Please sign in to comment.