Skip to content

Commit 6554c24

Browse files
authored
fix: missing 0x on ContractLog.transaction_hash (#2501)
1 parent 5455d4a commit 6554c24

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/ape/types/events.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from eth_abi.packed import encode_packed
77
from eth_pydantic_types import HexBytes
88
from eth_typing import Hash32, HexStr
9-
from eth_utils import encode_hex, keccak, to_hex
9+
from eth_utils import encode_hex, is_hex, keccak, to_hex
1010
from ethpm_types.abi import EventABI
1111
from pydantic import BaseModel, field_serializer, field_validator, model_validator
1212
from web3.types import FilterParams
@@ -193,6 +193,18 @@ class ContractLog(ExtraAttributesMixin, BaseContractLog):
193193
Is `None` when from the pending block.
194194
"""
195195

196+
@field_validator("transaction_hash", mode="before")
197+
@classmethod
198+
def _validate_transaction_hash(cls, transaction_hash):
199+
if (
200+
isinstance(transaction_hash, str)
201+
and is_hex(transaction_hash)
202+
and not transaction_hash.startswith("0x")
203+
):
204+
return f"0x{transaction_hash}"
205+
206+
return transaction_hash
207+
196208
@field_serializer("transaction_hash", "block_hash")
197209
def _serialize_hashes(self, value, info):
198210
return self._serialize_value(value, info)

tests/functional/test_contract_event.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,3 +471,17 @@ def test_model_dump_json():
471471
'"MyEvent","log_index":0,'
472472
'"transaction_hash":"0x00000000000000000000000004d21f074916369e"}'
473473
)
474+
475+
476+
def test_transaction_hash():
477+
event_arguments = {"key": 123, "validators": [HexBytes(123)]}
478+
txn_hash = "a3430927834bd23"
479+
event = ContractLog(
480+
block_number=123,
481+
block_hash="block-hash",
482+
event_arguments=event_arguments,
483+
event_name="MyEvent",
484+
log_index=0,
485+
transaction_hash=txn_hash,
486+
)
487+
assert event.transaction_hash == f"0x{txn_hash}"

0 commit comments

Comments
 (0)