Skip to content

Commit

Permalink
fix: issue with multiple ecosystems
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Sep 28, 2023
1 parent f36855a commit df8cbb9
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/ape/managers/networks.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from functools import cached_property
from typing import Dict, Iterator, List, Optional, Set, Union

import yaml
Expand Down Expand Up @@ -137,7 +138,7 @@ def provider_names(self) -> Set[str]:

return names

@property
@cached_property
def ecosystems(self) -> Dict[str, EcosystemAPI]:
"""
All the registered ecosystems in ``ape``, such as ``ethereum``.
Expand Down
9 changes: 6 additions & 3 deletions src/ape_ethereum/ecosystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,10 +602,13 @@ def create_transaction(self, **kwargs) -> TransactionAPI:
if "type" in kwargs:
if kwargs["type"] is None:
version = TransactionType.DYNAMIC
elif not isinstance(kwargs["type"], int):
version = TransactionType(self.conversion_manager.convert(kwargs["type"], int))
else:
elif isinstance(kwargs["type"], TransactionType):
version = kwargs["type"]
elif isinstance(kwargs["type"], int):
version = TransactionType(kwargs["type"])
else:
# Using hex values or alike.
version = TransactionType(self.conversion_manager.convert(kwargs["type"], int))

elif "gas_price" in kwargs:
version = TransactionType.STATIC
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@ def ethereum(networks):


@pytest.fixture(autouse=True)
def eth_tester_provider():
def eth_tester_provider(ethereum):
if not ape.networks.active_provider or ape.networks.provider.name != "test":
with ape.networks.ethereum.local.use_provider("test") as provider:
with ethereum.local.use_provider("test") as provider:
yield provider
else:
yield ape.networks.provider
Expand Down
4 changes: 2 additions & 2 deletions tests/functional/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import ape
from ape.api import EcosystemAPI, NetworkAPI, TransactionAPI
from ape.api.networks import LOCAL_NETWORK_NAME
from ape.contracts import ContractContainer, ContractInstance
from ape.exceptions import ChainError, ContractLogicError
from ape.logging import LogLevel
Expand Down Expand Up @@ -416,9 +415,10 @@ def use_debug(logger):

@pytest.fixture
def dummy_live_network(chain):
original_network = chain.provider.network.name
chain.provider.network.name = "goerli"
yield chain.provider.network
chain.provider.network.name = LOCAL_NETWORK_NAME
chain.provider.network.name = original_network


@pytest.fixture(scope="session")
Expand Down
20 changes: 11 additions & 9 deletions tests/functional/test_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,10 +500,12 @@ def test_declare(contract_container, sender):
assert not receipt.failed


@pytest.mark.parametrize(
"tx_type,params", [(0, ["gas_price"]), (2, ["max_fee", "max_priority_fee"])]
)
def test_prepare_transaction_using_auto_gas(sender, ethereum, tx_type, params):
@pytest.mark.parametrize("tx_type", (TransactionType.STATIC, TransactionType.DYNAMIC))
def test_prepare_transaction_using_auto_gas(sender, ethereum, tx_type):
params = (
("gas_price",) if tx_type is TransactionType.STATIC else ("max_fee", "max_priority_fee")
)

def clear_network_property_cached():
for field in ("gas_limit", "auto_gas_multiplier"):
if field in ethereum.local.__dict__:
Expand All @@ -515,6 +517,7 @@ def clear_network_property_cached():
try:
ethereum.config.local.gas_limit = auto_gas
clear_network_property_cached()
assert ethereum.local.gas_limit == auto_gas, "Setup failed - auto gas not set."

# NOTE: Must create tx _after_ setting network gas value.
tx = ethereum.create_transaction(type=tx_type)
Expand All @@ -523,7 +526,7 @@ def clear_network_property_cached():
assert tx.nonce is None
for param in params:
# Custom fields depending on type.
assert getattr(tx, param) is None
assert getattr(tx, param) is None, f"'{param}' unexpectedly set."

# Gas should NOT yet be estimated, as that happens closer to sending.
assert tx.gas_limit is None
Expand All @@ -538,15 +541,14 @@ def clear_network_property_cached():
# Show multipliers work. First, reset network to use one (hack).
gas_smaller = tx.gas_limit

clear_network_property_cached()
auto_gas.multiplier = 1.1
tx.provider.network.config.local.gas_limit = auto_gas
assert tx.provider.network.gas_limit == auto_gas
ethereum.config.local.gas_limit = auto_gas
clear_network_property_cached()
assert ethereum.local.gas_limit == auto_gas, "Setup failed - auto gas multiplier not set."

tx2 = ethereum.create_transaction(type=tx_type)
tx2 = sender.prepare_transaction(tx2)
gas_bigger = tx2.gas_limit

assert gas_smaller < gas_bigger

for param in params:
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/test_ecosystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ def test_decode_return_data_non_empty_padding_bytes(ethereum):
@pytest.mark.parametrize("tx_type", TransactionType)
def test_create_transaction_uses_network_gas_limit(tx_type, ethereum, eth_tester_provider, owner):
tx = ethereum.create_transaction(type=tx_type.value, sender=owner.address)
assert tx.type == tx_type
assert tx.type == tx_type.value
assert tx.gas_limit == eth_tester_provider.max_gas


Expand Down

0 comments on commit df8cbb9

Please sign in to comment.