Skip to content

Commit

Permalink
refactor: share BaseEthereumConfig class (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Jan 23, 2024
1 parent 44e6f58 commit 078db03
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 37 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ repos:
- id: isort

- repo: https://github.com/psf/black
rev: 23.12.0
rev: 23.12.1
hooks:
- id: black
name: black

- repo: https://github.com/pycqa/flake8
rev: 6.1.0
rev: 7.0.0
hooks:
- id: flake8

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.7.1
rev: v1.8.0
hooks:
- id: mypy
additional_dependencies: [types-setuptools, pydantic]
Expand Down
43 changes: 13 additions & 30 deletions ape_fantom/ecosystem.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from typing import Optional, Type, cast
from typing import ClassVar, Dict, Tuple, cast

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

NETWORKS = {
# chain_id, network_id
Expand All @@ -12,34 +14,15 @@
}


def _create_config(
required_confirmations: int = 1, block_time: int = 1, cls: Type = NetworkConfig, **kwargs
) -> NetworkConfig:
return cls(required_confirmations=required_confirmations, block_time=block_time, **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 FantomConfig(PluginConfig):
opera: NetworkConfig = _create_config()
opera_fork: ForkedNetworkConfig = _create_local_config(use_fork=True)
testnet: NetworkConfig = _create_config()
testnet_fork: ForkedNetworkConfig = _create_local_config(use_fork=True)
local: NetworkConfig = _create_local_config(default_provider="test")
default_network: str = LOCAL_NETWORK_NAME
class FantomConfig(BaseEthereumConfig):
NETWORKS: ClassVar[Dict[str, Tuple[int, int]]] = NETWORKS
opera: NetworkConfig = create_network_config(block_time=0, required_confirmations=0)
testnet: NetworkConfig = create_network_config(block_time=0, required_confirmations=0)


class Fantom(Ethereum):
fee_token_symbol: str = "FTM"

@property
def config(self) -> FantomConfig: # type: ignore[override]
return cast(FantomConfig, self.config_manager.get_config("fantom"))
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
"hypothesis>=6.2.0,<7", # Strategy-based fuzzer
],
"lint": [
"black>=23.12.0,<24", # Auto-formatter and linter
"mypy>=1.7.1,<2", # Static type analyzer
"black>=23.12.1,<24", # Auto-formatter and linter
"mypy>=1.8.0,<2", # Static type analyzer
"types-setuptools", # Needed for mypy type shed
"flake8>=6.1.0,<7", # Style linter
"flake8>=7.0.0,<8", # Style linter
"flake8-breakpoint>=1.1.0,<2", # Detect breakpoints left in code
"flake8-print>=5.0.0,<6", # Detect print statements left in code
"isort>=5.10.1,<6", # Import sorting linter
Expand Down Expand Up @@ -60,7 +60,7 @@
url="https://github.com/ApeWorX/ape-fantom",
include_package_data=True,
install_requires=[
"eth-ape>=0.7.0,<0.8",
"eth-ape>=0.7.6,<0.8",
"ethpm-types", # Use same version as eth-ape
],
python_requires=">=3.8,<4",
Expand Down
28 changes: 28 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from ape_ethereum.transactions import TransactionType

from ape_fantom.ecosystem import FantomConfig


def test_gas_limit(fantom):
assert fantom.config.local.gas_limit == "max"


def test_default_transaction_type(fantom):
assert fantom.config.opera.default_transaction_type == TransactionType.DYNAMIC


def test_opera_fork_not_configured():
obj = FantomConfig.model_validate({})
assert obj.opera_fork.required_confirmations == 0


def test_opera_fork_configured():
data = {"opera_fork": {"required_confirmations": 555}}
obj = FantomConfig.model_validate(data)
assert obj.opera_fork.required_confirmations == 555


def test_custom_network():
data = {"apenet": {"required_confirmations": 333}}
obj = FantomConfig.model_validate(data)
assert obj.apenet.required_confirmations == 333

0 comments on commit 078db03

Please sign in to comment.