Skip to content

Commit a08f6d4

Browse files
authored
refactor: use new Config to get custom network abilities (#24)
1 parent b24bc5d commit a08f6d4

File tree

6 files changed

+78
-49
lines changed

6 files changed

+78
-49
lines changed

.pre-commit-config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ repos:
1010
- id: isort
1111

1212
- repo: https://github.com/psf/black
13-
rev: 23.12.0
13+
rev: 23.12.1
1414
hooks:
1515
- id: black
1616
name: black
1717

1818
- repo: https://github.com/pycqa/flake8
19-
rev: 6.1.0
19+
rev: 7.0.0
2020
hooks:
2121
- id: flake8
2222

2323
- repo: https://github.com/pre-commit/mirrors-mypy
24-
rev: v1.7.1
24+
rev: v1.8.0
2525
hooks:
2626
- id: mypy
2727
additional_dependencies: [types-setuptools, pydantic]

ape_polygon/ecosystem.py

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
from typing import Type, cast
1+
from typing import ClassVar, Dict, Tuple, cast
22

3-
from ape.api.config import PluginConfig
4-
from ape.api.networks import LOCAL_NETWORK_NAME
5-
from ape.utils import DEFAULT_LOCAL_TRANSACTION_ACCEPTANCE_TIMEOUT
6-
from ape_ethereum.ecosystem import Ethereum, ForkedNetworkConfig, NetworkConfig
3+
from ape_ethereum.ecosystem import (
4+
BaseEthereumConfig,
5+
Ethereum,
6+
NetworkConfig,
7+
create_network_config,
8+
)
79

810
NETWORKS = {
911
# chain_id, network_id
@@ -12,34 +14,10 @@
1214
}
1315

1416

15-
def _create_config(
16-
required_confirmations: int = 1,
17-
block_time: int = 2,
18-
cls: Type[NetworkConfig] = NetworkConfig,
19-
**kwargs,
20-
) -> NetworkConfig:
21-
return cls(required_confirmations=required_confirmations, block_time=block_time, **kwargs)
22-
23-
24-
def _create_local_config(**kwargs):
25-
return _create_config(
26-
block_time=0,
27-
default_provider=kwargs.pop("default_provider", None),
28-
gas_limit="max",
29-
required_confirmations=0,
30-
transaction_acceptance_timeout=DEFAULT_LOCAL_TRANSACTION_ACCEPTANCE_TIMEOUT,
31-
cls=ForkedNetworkConfig if kwargs.pop("use_fork", False) else NetworkConfig,
32-
**kwargs,
33-
)
34-
35-
36-
class PolygonConfig(PluginConfig):
37-
mainnet: NetworkConfig = _create_config()
38-
mainnet_fork: ForkedNetworkConfig = _create_local_config(use_fork=True)
39-
mumbai: NetworkConfig = _create_config()
40-
mumbai_fork: ForkedNetworkConfig = _create_local_config(use_fork=True)
41-
local: NetworkConfig = _create_local_config(default_provider="test")
42-
default_network: str = LOCAL_NETWORK_NAME
17+
class PolygonConfig(BaseEthereumConfig):
18+
NETWORKS: ClassVar[Dict[str, Tuple[int, int]]] = NETWORKS
19+
mainnet: NetworkConfig = create_network_config(block_time=2, required_confirmations=1)
20+
mumbai: NetworkConfig = create_network_config(block_time=2, required_confirmations=1)
4321

4422

4523
class Polygon(Ethereum):

setup.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
"hypothesis>=6.2.0,<7", # Strategy-based fuzzer
1111
],
1212
"lint": [
13-
"black>=23.12.0,<24", # Auto-formatter and linter
14-
"mypy>=1.7.1,<2", # Static type analyzer
13+
"black>=23.12.1,<24", # Auto-formatter and linter
14+
"mypy>=1.8.0,<2", # Static type analyzer
1515
"types-setuptools", # Needed for mypy type shed
16-
"flake8>=6.1.0,<7", # Style linter
16+
"flake8>=7.0.0,<8", # Style linter
1717
"flake8-breakpoint>=1.1.0,<2", # Detect breakpoints left in code
1818
"flake8-print>=5.0.0,<6", # Detect print statements left in code
1919
"isort>=5.10.1,<6", # Import sorting linter
@@ -60,7 +60,7 @@
6060
url="https://github.com/ApeWorX/ape-polygon",
6161
include_package_data=True,
6262
install_requires=[
63-
"eth-ape>=0.7.0,<0.8",
63+
"eth-ape>=0.7.6,<0.8",
6464
"ethpm-types", # Use same version as eth-ape
6565
],
6666
python_requires=">=3.8,<4",

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def polygon(networks):
1717
return networks.polygon
1818

1919

20-
@pytest.fixture
20+
@pytest.fixture(autouse=True)
2121
def eth_tester_provider():
2222
if not ape.networks.active_provider or ape.networks.provider.name != "test":
2323
with ape.networks.polygon.local.use_provider("test") as provider:

tests/test_config.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from ape_ethereum.transactions import TransactionType
2+
3+
from ape_polygon.ecosystem import PolygonConfig
4+
5+
6+
def test_gas_limit(polygon):
7+
assert polygon.config.local.gas_limit == "max"
8+
9+
10+
def test_default_transaction_type(polygon):
11+
assert polygon.config.mainnet.default_transaction_type == TransactionType.DYNAMIC
12+
13+
14+
def test_mainnet_fork_not_configured():
15+
obj = PolygonConfig.model_validate({})
16+
assert obj.mainnet_fork.required_confirmations == 0
17+
18+
19+
def test_mainnet_fork_configured():
20+
data = {"mainnet_fork": {"required_confirmations": 555}}
21+
obj = PolygonConfig.model_validate(data)
22+
assert obj.mainnet_fork.required_confirmations == 555
23+
24+
25+
def test_custom_network():
26+
data = {"apenet": {"required_confirmations": 333}}
27+
obj = PolygonConfig.model_validate(data)
28+
assert obj.apenet.required_confirmations == 333

tests/test_ecosystem.py

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,39 @@
33
from ethpm_types.abi import MethodABI
44

55

6-
def test_gas_limit(polygon):
7-
assert polygon.config.local.gas_limit == "max"
6+
@pytest.mark.parametrize(
7+
"tx_kwargs",
8+
[
9+
{"type": 0},
10+
{"gas_price": 0},
11+
{"gasPrice": 0},
12+
],
13+
)
14+
def test_create_transaction_type_0(polygon, tx_kwargs):
15+
txn = polygon.create_transaction(**tx_kwargs)
16+
assert txn.type == TransactionType.STATIC.value
17+
818

19+
@pytest.mark.parametrize(
20+
"tx_kwargs",
21+
[
22+
{}, # Default is type 2 in Polygon.
23+
{"type": 2},
24+
{"max_fee": 0},
25+
{"max_fee_per_gas": 0},
26+
{"maxFee": 0},
27+
{"max_priority_fee_per_gas": 0},
28+
{"max_priority_fee": 0},
29+
{"maxPriorityFeePerGas": 0},
30+
],
31+
)
32+
def test_create_transaction_type_2(polygon, tx_kwargs):
33+
"""
34+
Show is smart-enough to deduce type 2 transactions.
35+
"""
936

10-
# NOTE: None because we want to show the default is DYNAMIC
11-
@pytest.mark.parametrize("tx_type", (None, 2, "0x2"))
12-
def test_create_transaction(polygon, tx_type, eth_tester_provider):
13-
tx = polygon.create_transaction(type=tx_type)
14-
assert tx.type == TransactionType.DYNAMIC.value
15-
assert tx.gas_limit == eth_tester_provider.max_gas
37+
txn = polygon.create_transaction(**tx_kwargs)
38+
assert txn.type == TransactionType.DYNAMIC.value
1639

1740

1841
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)