Skip to content

Commit

Permalink
fix: missing 0x on ContractLog.transaction_hash (#2501)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Feb 11, 2025
1 parent 5455d4a commit 6554c24
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
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}"

0 comments on commit 6554c24

Please sign in to comment.