@@ -321,18 +321,9 @@ async def process_block_header_event(self, event_data: Any) -> None:
321321 block = await self .fetch_block_by_number (event .block_number )
322322
323323 if block :
324- block_hash = block .get ("hash" )
325-
326- if block_hash is not None :
327- # Convert block_hash to hex string with 0x prefix
328- block_hash_hex = (
329- block_hash .hex ()
330- if isinstance (block_hash , bytes )
331- else block_hash
332- )
333- if not block_hash_hex .startswith ("0x" ):
334- block_hash_hex = "0x" + block_hash_hex
324+ block_hash_hex = self ._normalize_block_hash (block .get ("hash" ))
335325
326+ if block_hash_hex is not None :
336327 # Submit the block header using BlockSubmitter
337328 success = (
338329 await self .block_submitter .submit_block_header (
@@ -391,17 +382,11 @@ async def push_latest_block_header(self) -> None:
391382 block = await self .fetch_block_by_number (block_num )
392383
393384 if block :
394- block_hash = block .get ("hash" )
395-
396- if block_hash is not None :
397- block_hash_hex = (
398- block_hash .hex ()
399- if isinstance (block_hash , bytes )
400- else block_hash
401- )
402- if not block_hash_hex .startswith ("0x" ):
403- block_hash_hex = "0x" + block_hash_hex
385+ block_hash_hex = self ._normalize_block_hash (
386+ block .get ("hash" )
387+ )
404388
389+ if block_hash_hex is not None :
405390 block_numbers .append (block_num )
406391 block_hashes .append (block_hash_hex )
407392 else :
@@ -515,16 +500,10 @@ async def watch_addresses_for_interactions(self) -> None:
515500
516501 block = await self .fetch_block_by_number (block_number )
517502 if block :
518- block_hash = block .get ("hash" )
519- if block_hash is not None :
520- block_hash_hex = (
521- block_hash .hex ()
522- if isinstance (block_hash , bytes )
523- else block_hash
524- )
525- if not block_hash_hex .startswith ("0x" ):
526- block_hash_hex = "0x" + block_hash_hex
527-
503+ block_hash_hex = self ._normalize_block_hash (
504+ block .get ("hash" )
505+ )
506+ if block_hash_hex is not None :
528507 blocks_with_interactions .append (block_number )
529508 block_hashes_to_submit .append (block_hash_hex )
530509 else :
@@ -575,16 +554,8 @@ async def watch_addresses_for_interactions(self) -> None:
575554 # Fetch the block to get its hash
576555 block = await self .fetch_block_by_number (heartbeat_block )
577556 if block :
578- block_hash = block .get ("hash" )
579- if block_hash is not None :
580- block_hash_hex = (
581- block_hash .hex ()
582- if isinstance (block_hash , bytes )
583- else block_hash
584- )
585- if not block_hash_hex .startswith ("0x" ):
586- block_hash_hex = "0x" + block_hash_hex
587-
557+ block_hash_hex = self ._normalize_block_hash (block .get ("hash" ))
558+ if block_hash_hex is not None :
588559 success = await self .block_submitter .submit_block_header (
589560 heartbeat_block , block_hash_hex
590561 )
@@ -670,6 +641,18 @@ async def _check_block_for_interactions(self, block_number: int) -> bool:
670641 )
671642 return False
672643
644+ def _normalize_block_hash (self , block_hash : bytes | str | None ) -> str | None :
645+ """
646+ Normalize block hash to hex string with 0x prefix.
647+
648+ :param block_hash: Block hash as bytes, hex string, or None
649+ :return: Normalized hex string with 0x prefix, or None if input is None
650+ """
651+ if block_hash is None :
652+ return None
653+ hex_str = block_hash .hex () if isinstance (block_hash , bytes ) else block_hash
654+ return hex_str if hex_str .startswith ("0x" ) else f"0x{ hex_str } "
655+
673656 def _is_watched_transaction (self , tx : Any ) -> bool :
674657 """
675658 Check if a transaction involves any watched addresses.
@@ -840,16 +823,12 @@ async def watch_token_transfers(self) -> None:
840823
841824 for block_num in sorted_blocks :
842825 block = await self .fetch_block_by_number (block_num )
843- if block and block .get ("hash" ):
844- block_hash = block ["hash" ]
845- block_hash_hex = (
846- block_hash .hex ()
847- if isinstance (block_hash , bytes )
848- else block_hash
826+ if block :
827+ block_hash_hex = self ._normalize_block_hash (
828+ block .get ("hash" )
849829 )
850- if not block_hash_hex .startswith ("0x" ):
851- block_hash_hex = "0x" + block_hash_hex
852- block_hashes_to_submit .append (block_hash_hex )
830+ if block_hash_hex :
831+ block_hashes_to_submit .append (block_hash_hex )
853832
854833 if len (sorted_blocks ) == len (block_hashes_to_submit ):
855834 success = (
0 commit comments