From 398accb91d2a694a1f8ee4f0f92fef525301dee7 Mon Sep 17 00:00:00 2001 From: antazoey Date: Tue, 23 Jan 2024 12:31:41 -0600 Subject: [PATCH] refactor: share and use BaseEthereumConfig (#22) --- ape_bsc/ecosystem.py | 47 +++++++++++++---------------------------- setup.py | 2 +- tests/test_config.py | 28 ++++++++++++++++++++++++ tests/test_ecosystem.py | 4 ---- 4 files changed, 44 insertions(+), 37 deletions(-) create mode 100644 tests/test_config.py diff --git a/ape_bsc/ecosystem.py b/ape_bsc/ecosystem.py index 10a3e77..730269a 100644 --- a/ape_bsc/ecosystem.py +++ b/ape_bsc/ecosystem.py @@ -1,9 +1,11 @@ -from typing import Optional, Type, 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 typing import ClassVar, Dict, Tuple, cast + +from ape_ethereum.ecosystem import ( + BaseEthereumConfig, + Ethereum, + NetworkConfig, + create_network_config, +) from ape_ethereum.transactions import TransactionType NETWORKS = { @@ -13,39 +15,20 @@ } -def _create_config( - required_confirmations: int = 1, block_time: int = 3, cls: Type = NetworkConfig, **kwargs -) -> NetworkConfig: - return cls( - block_time=block_time, - default_transaction_type=TransactionType.STATIC, - required_confirmations=required_confirmations, - **kwargs, - ) - +def _create_config() -> NetworkConfig: + return create_network_config(block_time=3, default_transaction_type=TransactionType.STATIC) -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 BSCConfig(PluginConfig): +class BSCConfig(BaseEthereumConfig): + DEFAULT_TRANSACTION_TYPE: ClassVar[int] = TransactionType.STATIC.value + NETWORKS: ClassVar[Dict[str, Tuple[int, int]]] = NETWORKS mainnet: NetworkConfig = _create_config() - mainnet_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 BSC(Ethereum): + fee_token_symbol: str = "BNB" + @property def config(self) -> BSCConfig: # type: ignore[override] return cast(BSCConfig, self.config_manager.get_config("bsc")) diff --git a/setup.py b/setup.py index 04d6fcf..0404141 100644 --- a/setup.py +++ b/setup.py @@ -60,7 +60,7 @@ url="https://github.com/ApeWorX/ape-bsc", include_package_data=True, install_requires=[ - "eth-ape>=0.7.5,<0.8", + "eth-ape>=0.7.6,<0.8", ], python_requires=">=3.8,<4", extras_require=extras_require, diff --git a/tests/test_config.py b/tests/test_config.py new file mode 100644 index 0000000..c7ff726 --- /dev/null +++ b/tests/test_config.py @@ -0,0 +1,28 @@ +from ape_ethereum.transactions import TransactionType + +from ape_bsc.ecosystem import BSCConfig + + +def test_gas_limit(bsc): + assert bsc.config.local.gas_limit == "max" + + +def test_default_transaction_type(bsc): + assert bsc.config.mainnet.default_transaction_type == TransactionType.STATIC + + +def test_mainnet_fork_not_configured(): + obj = BSCConfig.model_validate({}) + assert obj.mainnet_fork.required_confirmations == 0 + + +def test_mainnet_fork_configured(): + data = {"mainnet_fork": {"required_confirmations": 555}} + obj = BSCConfig.model_validate(data) + assert obj.mainnet_fork.required_confirmations == 555 + + +def test_custom_network(): + data = {"apenet": {"required_confirmations": 333}} + obj = BSCConfig.model_validate(data) + assert obj.apenet.required_confirmations == 333 diff --git a/tests/test_ecosystem.py b/tests/test_ecosystem.py index 181cc9a..50088ac 100644 --- a/tests/test_ecosystem.py +++ b/tests/test_ecosystem.py @@ -3,10 +3,6 @@ from ethpm_types import MethodABI -def test_gas_limit(bsc): - assert bsc.config.local.gas_limit == "max" - - @pytest.mark.parametrize( "tx_kwargs", [