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: missing 0x on ContractLog.transaction_hash #2501

Merged
merged 1 commit into from
Feb 11, 2025
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
14 changes: 13 additions & 1 deletion src/ape/types/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from eth_abi.packed import encode_packed
from eth_pydantic_types import HexBytes
from eth_typing import Hash32, HexStr
from eth_utils import encode_hex, keccak, to_hex
from eth_utils import encode_hex, is_hex, keccak, to_hex
from ethpm_types.abi import EventABI
from pydantic import BaseModel, field_serializer, field_validator, model_validator
from web3.types import FilterParams
Expand Down Expand Up @@ -193,6 +193,18 @@ class ContractLog(ExtraAttributesMixin, BaseContractLog):
Is `None` when from the pending block.
"""

@field_validator("transaction_hash", mode="before")
@classmethod
def _validate_transaction_hash(cls, transaction_hash):
if (
isinstance(transaction_hash, str)
and is_hex(transaction_hash)
and not transaction_hash.startswith("0x")
):
return f"0x{transaction_hash}"

return transaction_hash

@field_serializer("transaction_hash", "block_hash")
def _serialize_hashes(self, value, info):
return self._serialize_value(value, info)
Expand Down
14 changes: 14 additions & 0 deletions tests/functional/test_contract_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,3 +471,17 @@ def test_model_dump_json():
'"MyEvent","log_index":0,'
'"transaction_hash":"0x00000000000000000000000004d21f074916369e"}'
)


def test_transaction_hash():
event_arguments = {"key": 123, "validators": [HexBytes(123)]}
txn_hash = "a3430927834bd23"
event = ContractLog(
block_number=123,
block_hash="block-hash",
event_arguments=event_arguments,
event_name="MyEvent",
log_index=0,
transaction_hash=txn_hash,
)
assert event.transaction_hash == f"0x{txn_hash}"
Loading