11import logging
22from functools import cache
3- from typing import List , Optional , Sequence , Tuple
3+ from typing import Optional , Sequence
44
55from django .conf import settings
66from django .db import transaction
@@ -73,8 +73,8 @@ def __init__(
7373 self .supported_entry_points = supported_entry_points
7474
7575 def get_user_operation_hashes_from_logs (
76- self , safe_address : ChecksumAddress , logs : [ Sequence [LogReceipt ] ]
77- ) -> List [HexBytes ]:
76+ self , safe_address : ChecksumAddress , logs : Sequence [LogReceipt ]
77+ ) -> list [HexBytes ]:
7878 """
7979 :param safe_address:
8080 :param logs:
@@ -109,7 +109,7 @@ def index_safe_operation_confirmations(
109109 signature : bytes ,
110110 safe_operation_model : SafeOperationModel ,
111111 safe_operation : SafeOperation ,
112- ) -> List [SafeOperationConfirmationModel ]:
112+ ) -> list [SafeOperationConfirmationModel ]:
113113 """
114114 Creates missing ``SafeOperationConfirmations``
115115
@@ -144,7 +144,7 @@ def index_safe_operation(
144144 user_operation_model : UserOperationModel ,
145145 user_operation : UserOperation ,
146146 user_operation_receipt : UserOperationReceipt ,
147- ) -> Optional [Tuple [SafeOperationModel , SafeOperation ]]:
147+ ) -> Optional [tuple [SafeOperationModel , SafeOperation ]]:
148148 """
149149 Creates or updates a Safe Operation
150150
@@ -206,7 +206,7 @@ def index_safe_operation(
206206
207207 def index_user_operation_receipt (
208208 self , user_operation_model : UserOperationModel
209- ) -> Tuple [UserOperationReceiptModel , UserOperationReceipt ]:
209+ ) -> tuple [UserOperationReceiptModel , UserOperationReceipt ]:
210210 """
211211 Stores UserOperationReceipt. Can never be updated as if ``UserOperationReceipt`` is on database indexing
212212 ``UserOperation`` is not required
@@ -277,7 +277,7 @@ def index_user_operation(
277277 safe_address : ChecksumAddress ,
278278 user_operation_hash : HexBytes ,
279279 ethereum_tx : history_models .EthereumTx ,
280- ) -> Tuple [UserOperationModel , UserOperation ]:
280+ ) -> tuple [UserOperationModel , UserOperation ] | None :
281281 """
282282 Index ``UserOperation``, ``SafeOperation`` and ``UserOperationReceipt`` for the given ``UserOperation`` log
283283
@@ -294,72 +294,73 @@ def index_user_operation(
294294 safe_address ,
295295 user_operation_hash_hex ,
296296 )
297- else :
297+ return None
298+
299+ logger .debug (
300+ "[%s] Retrieving UserOperation from Bundler with user-operation-hash=%s on tx-hash=%s" ,
301+ safe_address ,
302+ user_operation_hash_hex ,
303+ ethereum_tx .tx_hash ,
304+ )
305+ user_operation = self .bundler_client .get_user_operation_by_hash (
306+ user_operation_hash_hex
307+ )
308+ if not user_operation :
309+ self .bundler_client .get_user_operation_by_hash .cache_clear ()
310+ raise BundlerClientException (
311+ f"user-operation={ user_operation_hash_hex } returned `null`"
312+ )
313+ if isinstance (user_operation , UserOperationV07 ):
314+ raise UserOperationNotSupportedException (
315+ f"user-operation={ user_operation_hash_hex } for EntryPoint v0.7.0 is not supported"
316+ )
317+
318+ try :
319+ user_operation_model = UserOperationModel .objects .get (
320+ hash = user_operation_hash_hex
321+ )
298322 logger .debug (
299- "[%s] Retrieving UserOperation from Bundler with user-operation-hash =%s on tx-hash=%s" ,
323+ "[%s] Updating UserOperation with user-operation=%s on tx-hash=%s" ,
300324 safe_address ,
301325 user_operation_hash_hex ,
302326 ethereum_tx .tx_hash ,
303327 )
304- user_operation = self .bundler_client .get_user_operation_by_hash (
305- user_operation_hash_hex
306- )
307- if not user_operation :
308- self .bundler_client .get_user_operation_by_hash .cache_clear ()
309- raise BundlerClientException (
310- f"user-operation={ user_operation_hash_hex } returned `null`"
311- )
312- if isinstance (user_operation , UserOperationV07 ):
313- raise UserOperationNotSupportedException (
314- f"user-operation={ user_operation_hash_hex } for EntryPoint v0.7.0 is not supported"
315- )
316-
317- try :
318- user_operation_model = UserOperationModel .objects .get (
319- hash = user_operation_hash_hex
320- )
321- logger .debug (
322- "[%s] Updating UserOperation with user-operation=%s on tx-hash=%s" ,
323- safe_address ,
324- user_operation_hash_hex ,
325- ethereum_tx .tx_hash ,
326- )
327- user_operation_model .signature = user_operation .signature
328- user_operation_model .ethereum_tx = ethereum_tx
329- user_operation_model .save (update_fields = ["signature" , "ethereum_tx" ])
330- except UserOperationModel .DoesNotExist :
331- logger .debug (
332- "[%s] Storing UserOperation with user-operation=%s on tx-hash=%s" ,
333- safe_address ,
334- user_operation_hash_hex ,
335- ethereum_tx .tx_hash ,
336- )
337- user_operation_model = UserOperationModel .objects .create (
338- ethereum_tx = ethereum_tx ,
339- hash = user_operation_hash_hex ,
340- sender = user_operation .sender ,
341- nonce = user_operation .nonce ,
342- init_code = user_operation .init_code ,
343- call_data = user_operation .call_data ,
344- call_gas_limit = user_operation .call_gas_limit ,
345- verification_gas_limit = user_operation .verification_gas_limit ,
346- pre_verification_gas = user_operation .pre_verification_gas ,
347- max_fee_per_gas = user_operation .max_fee_per_gas ,
348- max_priority_fee_per_gas = user_operation .max_priority_fee_per_gas ,
349- paymaster = user_operation .paymaster ,
350- paymaster_data = user_operation .paymaster_data ,
351- signature = user_operation .signature ,
352- entry_point = user_operation .entry_point ,
353- )
354-
355- _ , user_operation_receipt = self .index_user_operation_receipt (
356- user_operation_model
328+ user_operation_model .signature = user_operation .signature
329+ user_operation_model .ethereum_tx = ethereum_tx
330+ user_operation_model .save (update_fields = ["signature" , "ethereum_tx" ])
331+ except UserOperationModel .DoesNotExist :
332+ logger .debug (
333+ "[%s] Storing UserOperation with user-operation=%s on tx-hash=%s" ,
334+ safe_address ,
335+ user_operation_hash_hex ,
336+ ethereum_tx .tx_hash ,
357337 )
358- self .index_safe_operation (
359- user_operation_model , user_operation , user_operation_receipt
338+ user_operation_model = UserOperationModel .objects .create (
339+ ethereum_tx = ethereum_tx ,
340+ hash = user_operation_hash_hex ,
341+ sender = user_operation .sender ,
342+ nonce = user_operation .nonce ,
343+ init_code = user_operation .init_code ,
344+ call_data = user_operation .call_data ,
345+ call_gas_limit = user_operation .call_gas_limit ,
346+ verification_gas_limit = user_operation .verification_gas_limit ,
347+ pre_verification_gas = user_operation .pre_verification_gas ,
348+ max_fee_per_gas = user_operation .max_fee_per_gas ,
349+ max_priority_fee_per_gas = user_operation .max_priority_fee_per_gas ,
350+ paymaster = user_operation .paymaster ,
351+ paymaster_data = user_operation .paymaster_data ,
352+ signature = user_operation .signature ,
353+ entry_point = user_operation .entry_point ,
360354 )
361355
362- return user_operation_model , user_operation
356+ _ , user_operation_receipt = self .index_user_operation_receipt (
357+ user_operation_model
358+ )
359+ self .index_safe_operation (
360+ user_operation_model , user_operation , user_operation_receipt
361+ )
362+
363+ return user_operation_model , user_operation
363364
364365 def process_aa_transaction (
365366 self , safe_address : ChecksumAddress , ethereum_tx : history_models .EthereumTx
0 commit comments