Skip to content

Commit

Permalink
refactor: use new Config to get custom network abilities (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Jan 23, 2024
1 parent b24bc5d commit a08f6d4
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 49 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
44 changes: 11 additions & 33 deletions ape_polygon/ecosystem.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from typing import 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,10 @@
}


def _create_config(
required_confirmations: int = 1,
block_time: int = 2,
cls: Type[NetworkConfig] = NetworkConfig,
**kwargs,
) -> NetworkConfig:
return cls(required_confirmations=required_confirmations, block_time=block_time, **kwargs)


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


class PolygonConfig(PluginConfig):
mainnet: NetworkConfig = _create_config()
mainnet_fork: ForkedNetworkConfig = _create_local_config(use_fork=True)
mumbai: NetworkConfig = _create_config()
mumbai_fork: ForkedNetworkConfig = _create_local_config(use_fork=True)
local: NetworkConfig = _create_local_config(default_provider="test")
default_network: str = LOCAL_NETWORK_NAME
class PolygonConfig(BaseEthereumConfig):
NETWORKS: ClassVar[Dict[str, Tuple[int, int]]] = NETWORKS
mainnet: NetworkConfig = create_network_config(block_time=2, required_confirmations=1)
mumbai: NetworkConfig = create_network_config(block_time=2, required_confirmations=1)


class Polygon(Ethereum):
Expand Down
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-polygon",
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
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def polygon(networks):
return networks.polygon


@pytest.fixture
@pytest.fixture(autouse=True)
def eth_tester_provider():
if not ape.networks.active_provider or ape.networks.provider.name != "test":
with ape.networks.polygon.local.use_provider("test") as provider:
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_polygon.ecosystem import PolygonConfig


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


def test_default_transaction_type(polygon):
assert polygon.config.mainnet.default_transaction_type == TransactionType.DYNAMIC


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


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


def test_custom_network():
data = {"apenet": {"required_confirmations": 333}}
obj = PolygonConfig.model_validate(data)
assert obj.apenet.required_confirmations == 333
39 changes: 31 additions & 8 deletions tests/test_ecosystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,39 @@
from ethpm_types.abi import MethodABI


def test_gas_limit(polygon):
assert polygon.config.local.gas_limit == "max"
@pytest.mark.parametrize(
"tx_kwargs",
[
{"type": 0},
{"gas_price": 0},
{"gasPrice": 0},
],
)
def test_create_transaction_type_0(polygon, tx_kwargs):
txn = polygon.create_transaction(**tx_kwargs)
assert txn.type == TransactionType.STATIC.value


@pytest.mark.parametrize(
"tx_kwargs",
[
{}, # Default is type 2 in Polygon.
{"type": 2},
{"max_fee": 0},
{"max_fee_per_gas": 0},
{"maxFee": 0},
{"max_priority_fee_per_gas": 0},
{"max_priority_fee": 0},
{"maxPriorityFeePerGas": 0},
],
)
def test_create_transaction_type_2(polygon, tx_kwargs):
"""
Show is smart-enough to deduce type 2 transactions.
"""

# NOTE: None because we want to show the default is DYNAMIC
@pytest.mark.parametrize("tx_type", (None, 2, "0x2"))
def test_create_transaction(polygon, tx_type, eth_tester_provider):
tx = polygon.create_transaction(type=tx_type)
assert tx.type == TransactionType.DYNAMIC.value
assert tx.gas_limit == eth_tester_provider.max_gas
txn = polygon.create_transaction(**tx_kwargs)
assert txn.type == TransactionType.DYNAMIC.value


@pytest.mark.parametrize(
Expand Down

0 comments on commit a08f6d4

Please sign in to comment.