Skip to content

Commit

Permalink
fix: bug where ENS domains were not handled in account history queries (
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Jul 5, 2024
1 parent 9d79ecc commit b6bdb8d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ repos:
name: black

- repo: https://github.com/pycqa/flake8
rev: 7.0.0
rev: 7.1.0
hooks:
- id: flake8

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.10.0
rev: v1.10.1
hooks:
- id: mypy
additional_dependencies: [
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
],
"lint": [
"black>=24.4.2,<25", # Auto-formatter and linter
"mypy>=1.10.0,<2", # Static type analyzer
"mypy>=1.10.1,<2", # Static type analyzer
"types-PyYAML", # Needed due to mypy typeshed
"types-requests", # Needed due to mypy typeshed
"types-setuptools", # Needed due to mypy typeshed
"pandas-stubs>=2.2.1.240316", # Needed due to mypy typeshed
"types-toml", # Needed due to mypy typeshed
"types-SQLAlchemy>=1.4.49", # Needed due to mypy typeshed
"types-python-dateutil", # Needed due to mypy typeshed
"flake8>=7.0.0,<8", # Style linter
"flake8>=7.1.0,<8", # Style linter
"flake8-breakpoint>=1.1.0,<2", # Detect breakpoints left in code
"flake8-print>=4.0.1,<5", # Detect print statements left in code
"isort>=5.13.2,<6", # Import sorting linter
Expand Down
22 changes: 18 additions & 4 deletions src/ape/managers/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,20 +540,34 @@ def _get_receipt() -> Optional[ReceiptAPI]:
except Exception:
return None

is_account = False
if not account_or_hash.startswith("0x"):
# Attempt converting.
try:
account_or_hash = self.conversion_manager.convert(account_or_hash, AddressType)
except Exception:
# Pretend this never happened.
pass
else:
is_account = True

try:
address = self.provider.network.ecosystem.decode_address(account_or_hash)
history = self._get_account_history(address)
if len(history) > 0:
return history

except Exception as err:
msg = f"'{account_or_hash}' is not a known address or transaction hash."
if is_account:
raise ChainError(msg) from err

# Try to treat as transaction hash.
if receipt := _get_receipt():
elif receipt := _get_receipt():
return receipt

raise ChainError(
f"'{account_or_hash}' is not a known address or transaction hash."
) from err
# Not an account or tx hash (with success).
raise ChainError(msg) from err

# No account history found. Check for transaction hash.
if receipt := _get_receipt():
Expand Down
22 changes: 20 additions & 2 deletions tests/functional/test_chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import pytest

from ape.exceptions import APINotImplementedError, ChainError
from ape.managers.chain import AccountHistory
from ape.types import AddressType


def test_snapshot_and_restore(chain, owner, receiver, vyper_contract_instance):
Expand Down Expand Up @@ -85,7 +87,7 @@ def test_isolate(chain, vyper_contract_instance, owner):
assert vyper_contract_instance.myNumber() == number_at_start


def test_get_receipt_uses_cache(mocker, eth_tester_provider, chain, vyper_contract_instance, owner):
def test_history_uses_cache(mocker, eth_tester_provider, chain, vyper_contract_instance, owner):
expected = vyper_contract_instance.setNumber(3, sender=owner)
eth = eth_tester_provider.web3.eth
rpc_spy = mocker.spy(eth, "get_transaction")
Expand All @@ -105,14 +107,30 @@ def test_get_receipt_uses_cache(mocker, eth_tester_provider, chain, vyper_contra
assert rpc_spy.call_count == 1 # Not changed


def test_get_receipt_from_history(chain, vyper_contract_instance, owner):
def test_history_getitem_receipt(chain, vyper_contract_instance, owner):
expected = vyper_contract_instance.setNumber(3, sender=owner)
actual = chain.history[expected.txn_hash]
assert actual.txn_hash == expected.txn_hash
assert actual.sender == expected.sender
assert actual.receiver == expected.receiver


def test_history_getitem_account(chain, vyper_contract_instance, owner):
actual = chain.history[owner.address]
assert isinstance(actual, AccountHistory)
assert actual.address == owner.address


def test_history_getitem_account_ens(mocker, chain, vyper_contract_instance, owner):
conversion_spy = mocker.spy(chain.history.conversion_manager, "convert")
value = "this will not work, but would if given ens and using ape-ens"
expected_err = rf"'{value}' is not a known address or transaction hash\."
with pytest.raises(ChainError, match=expected_err):
_ = chain.history[value]

conversion_spy.assert_called_once_with(value, AddressType)


def test_set_pending_timestamp(chain):
start_timestamp = chain.pending_timestamp
chain.pending_timestamp += 3600
Expand Down

0 comments on commit b6bdb8d

Please sign in to comment.