Skip to content

Commit

Permalink
fix: properly decode polled logs
Browse files Browse the repository at this point in the history
  • Loading branch information
z80dev committed Nov 7, 2023
1 parent 31d8c4a commit 91e7ab8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/ape/api/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from eth_typing import BlockNumber, HexStr
from eth_utils import add_0x_prefix, to_hex
from ethpm_types import HexBytes
from ethpm_types.abi import EventABI
from evm_trace import CallTreeNode as EvmCallTreeNode
from evm_trace import TraceFrame as EvmTraceFrame
from web3 import Web3
Expand Down Expand Up @@ -670,6 +671,7 @@ def poll_logs( # type: ignore[empty-body]
topics: Optional[List[Union[str, List[str]]]] = None,
required_confirmations: Optional[int] = None,
new_block_timeout: Optional[int] = None,
events: List[EventABI] = [],
) -> Iterator[ContractLog]:
"""
Poll new blocks. Optionally set a start block to include historical blocks.
Expand Down Expand Up @@ -1535,6 +1537,7 @@ def poll_logs(
topics: Optional[List[Union[str, List[str]]]] = None,
required_confirmations: Optional[int] = None,
new_block_timeout: Optional[int] = None,
events: List[EventABI] = [],
) -> Iterator[ContractLog]:
if required_confirmations is None:
required_confirmations = self.network.required_confirmations
Expand All @@ -1549,12 +1552,12 @@ def poll_logs(
log_params: Dict[str, int | AddressType | List[Union[str, List[str]]]] = {
"start_block": block.number,
"stop_block": block.number,
"events": events,
}
if address is not None:
log_params["addresses"] = [address]
if topics is not None:
log_params["topics"] = topics
log_params_obj = LogFilter(**log_params).dict()
yield from self.get_contract_logs(LogFilter(**log_params))

def block_ranges(self, start=0, stop=None, page=None):
Expand Down Expand Up @@ -1629,7 +1632,8 @@ def fetch_log_page(block_range):
# eth-tester expects a different format, let web3 handle the conversions for it
raw = "EthereumTester" not in self.client_version
logs = self._get_logs(page_filter.dict(), raw)
return self.network.ecosystem.decode_logs(logs, *log_filter.events)
decoded_logs = list(self.network.ecosystem.decode_logs(logs, *log_filter.events))
return decoded_logs

with ThreadPoolExecutor(self.concurrency) as pool:
for page in pool.map(fetch_log_page, block_ranges):
Expand Down
1 change: 1 addition & 0 deletions src/ape/contracts/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,7 @@ def poll_logs(
address=self.contract.address,
required_confirmations=required_confirmations,
new_block_timeout=new_block_timeout,
events=[self.abi],
)


Expand Down
6 changes: 3 additions & 3 deletions tests/functional/test_contract_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from ethpm_types import ContractType, HexBytes

from ape.api import ReceiptAPI
from ape.exceptions import ChainError
from ape.exceptions import ProviderError
from ape.types import ContractLog


Expand Down Expand Up @@ -234,11 +234,11 @@ def test_poll_logs_timeout(vyper_contract_instance, eth_tester_provider, owner,
new_block_timeout = 1
poller = vyper_contract_instance.NumberChange.poll_logs(new_block_timeout=new_block_timeout)

with pytest.raises(ChainError) as err:
with pytest.raises(ProviderError) as err:
with PollDaemon("logs", poller, lambda x: None, lambda: False):
time.sleep(1.5)

assert "Timed out waiting for new block (time_waited=1" in str(err.value)
assert "Timed out waiting for next block" in str(err.value)


def test_contract_two_events_with_same_name(
Expand Down

0 comments on commit 91e7ab8

Please sign in to comment.