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

Fix parsing of logs with zero topics #73

Merged
merged 3 commits into from
Oct 7, 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
4 changes: 3 additions & 1 deletion .github/workflows/pull_request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ jobs:
- name: Type Check (mypy)
run: mypy src
- name: Tests
run: pytest tests/e2e/test_blockchain_data.py
run: pytest tests/e2e/test_blockchain_data.py tests/e2e/test_imbalances_script.py
env:
NODE_URL: ${{ secrets.NODE_URL }}
17 changes: 9 additions & 8 deletions src/imbalances_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,15 @@ def extract_events(self, tx_receipt: dict) -> dict[str, list[dict]]:

events: dict[str, list[dict]] = {name: [] for name in EVENT_TOPICS}
for log in tx_receipt["logs"]:
log_topic = log["topics"][0].hex()
if log_topic in transfer_topics.values():
events["Transfer"].append(log)
else:
for event_name, topic in other_topics.items():
if log_topic == topic:
events[event_name].append(log)
break
if log["topics"]:
log_topic = log["topics"][0].hex()
if log_topic in transfer_topics.values():
events["Transfer"].append(log)
else:
for event_name, topic in other_topics.items():
if log_topic == topic:
events[event_name].append(log)
break
return events

def decode_event(self, event: dict) -> tuple[str | None, str | None, int | None]:
Expand Down
37 changes: 37 additions & 0 deletions tests/e2e/test_imbalances_script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from os import getenv, environ
from unittest.mock import Mock, patch

import pytest
from web3 import Web3


@pytest.fixture()
def set_env_variables(monkeypatch):
with patch.dict(environ, clear=True):
envvars = {
"CHAIN_SLEEP_TIME": "1",
}
for k, v in envvars.items():
monkeypatch.setenv(k, v)
yield # This is the magical bit which restore the environment after


def tests_process_single_transaction(set_env_variables):
# import has to happen after patching environment variable
from src.imbalances_script import RawTokenImbalances

web3 = Web3(Web3.HTTPProvider(getenv("NODE_URL")))
raw_imbalances = RawTokenImbalances(web3, "mainnet")
imbalances = raw_imbalances.compute_imbalances(
"0xb75e03b63d4f06c56549effd503e1e37f3ccfc3c00e6985a5aacc9b0534d7c5c"
)

assert imbalances == {
"0x72e4f9F808C49A2a61dE9C5896298920Dc4EEEa9": 3116463005,
"0x7Fc66500c84A76Ad7e9c93437bFc5Ac33E2DDaE9": 31552225710415395,
"0x812Ba41e071C7b7fA4EBcFB62dF5F45f6fA853Ee": 0,
"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48": 0,
"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2": 0,
"0xEE2a03Aa6Dacf51C18679C516ad5283d8E7C2637": 275548164523,
"0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE": 0,
}
Loading