Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: share and use BaseEthereumConfig #22

Merged
merged 5 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 15 additions & 32 deletions ape_bsc/ecosystem.py
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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"))
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-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,
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_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
4 changes: 0 additions & 4 deletions tests/test_ecosystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
[
Expand Down