diff --git a/.github/workflows/pull_request.yaml b/.github/workflows/pull_request.yaml index 06cb01d..1f54b20 100644 --- a/.github/workflows/pull_request.yaml +++ b/.github/workflows/pull_request.yaml @@ -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 }} diff --git a/src/imbalances_script.py b/src/imbalances_script.py index b655ef6..ec91f63 100644 --- a/src/imbalances_script.py +++ b/src/imbalances_script.py @@ -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]: diff --git a/tests/e2e/test_imbalances_script.py b/tests/e2e/test_imbalances_script.py new file mode 100644 index 0000000..dfbaa34 --- /dev/null +++ b/tests/e2e/test_imbalances_script.py @@ -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, + }