2020)
2121from dipdup .datasources .tzkt .cache import BigMapCache , OperationCache
2222from dipdup .datasources .tzkt .enums import TzktMessageType
23+ from dipdup .datasources .tzkt .proxy import TzktRequestProxy
2324from dipdup .models import (
2425 BigMapAction ,
2526 BigMapContext ,
6970
7071
7172class TzktDatasource :
72- def __init__ (self , url : str ):
73+ def __init__ (self , url : str , cache : bool ):
7374 super ().__init__ ()
7475 self ._url = url .rstrip ('/' )
7576 self ._logger = logging .getLogger (__name__ )
@@ -85,6 +86,7 @@ def __init__(self, url: str):
8586 self ._operation_cache = OperationCache ()
8687 self ._big_map_cache = BigMapCache ()
8788 self ._rollback_fn : Optional [Callable [[int , int ], Awaitable [None ]]] = None
89+ self ._proxy = TzktRequestProxy (cache )
8890
8991 async def add_index (self , index_name : str , index_config : Union [OperationIndexConfig , BigMapIndexConfig , BlockIndexConfig ]):
9092 self ._logger .info ('Adding index `%s`' , index_name )
@@ -203,7 +205,7 @@ async def subscribe_to_big_maps(self, address: Address, path: Path) -> None:
203205 async def _fetch_operations (self , addresses : List [str ], offset : int , first_level : int , last_level : int ) -> List [Dict [str , Any ]]:
204206 self ._logger .info ('Fetching levels %s-%s with offset %s' , first_level , last_level , offset )
205207
206- async with http_request (
208+ operations = await self . _proxy . http_request (
207209 'get' ,
208210 url = f'{ self ._url } /v1/operations/transactions' ,
209211 params = {
@@ -215,10 +217,9 @@ async def _fetch_operations(self, addresses: List[str], offset: int, first_level
215217 "select" : ',' .join (OPERATION_FIELDS ),
216218 "status" : "applied" ,
217219 },
218- ) as resp :
219- operations = await resp .json ()
220+ )
220221
221- async with http_request (
222+ target_operations = await self . _proxy . http_request (
222223 'get' ,
223224 url = f'{ self ._url } /v1/operations/transactions' ,
224225 params = {
@@ -230,8 +231,7 @@ async def _fetch_operations(self, addresses: List[str], offset: int, first_level
230231 "select" : ',' .join (OPERATION_FIELDS ),
231232 "status" : "applied" ,
232233 },
233- ) as resp :
234- target_operations = await resp .json ()
234+ )
235235
236236 sender_operation_keys = {op ['id' ] for op in operations }
237237 for op in target_operations :
@@ -297,7 +297,7 @@ async def _fetch_big_maps(
297297 ) -> List [Dict [str , Any ]]:
298298 self ._logger .info ('Fetching levels %s-%s with offset %s' , first_level , last_level , offset )
299299
300- async with http_request (
300+ big_maps = await self . _proxy . http_request (
301301 'get' ,
302302 url = f'{ self ._url } /v1/bigmaps/updates' ,
303303 params = {
@@ -308,8 +308,7 @@ async def _fetch_big_maps(
308308 "level.gt" : first_level ,
309309 "level.le" : last_level ,
310310 },
311- ) as resp :
312- big_maps = await resp .json ()
311+ )
313312
314313 self ._logger .info ('%s big map updates fetched' , len (big_maps ))
315314 self ._logger .debug (big_maps )
@@ -369,11 +368,10 @@ async def _process_level_big_maps(big_maps):
369368
370369 async def fetch_jsonschemas (self , address : str ) -> Dict [str , Any ]:
371370 self ._logger .info ('Fetching jsonschemas for address `%s' , address )
372- async with http_request (
371+ jsonschemas = await self . _proxy . http_request (
373372 'get' ,
374373 url = f'{ self ._url } /v1/contracts/{ address } /interface' ,
375- ) as response :
376- jsonschemas = await response .json ()
374+ )
377375 self ._logger .debug (jsonschemas )
378376 return jsonschemas
379377
@@ -597,10 +595,10 @@ def convert_big_map(cls, big_map_json: Dict[str, Any]) -> BigMapData:
597595
598596 async def get_latest_block (self ) -> Dict [str , Any ]:
599597 self ._logger .info ('Fetching latest block' )
600- async with http_request (
598+ block = await self . _proxy . http_request (
601599 'get' ,
602600 url = f'{ self ._url } /v1/head' ,
603- ) as resp :
604- block = await resp . json ( )
601+ skip_cache = True ,
602+ )
605603 self ._logger .debug (block )
606604 return block
0 commit comments