1111from data_handler .handlers .loan_states .zklend .events import ZkLendState
1212from shared .constants import TOKEN_SETTINGS
1313
14- from dashboard_app .data_conector import DataConnector
14+ from dashboard_app .data_conector import DataConnectorAsync
1515from dashboard_app .helpers .loans_table import get_loans_table_data , get_protocol
1616from dashboard_app .helpers .protocol_stats import (
1717 get_collateral_stats ,
@@ -34,46 +34,56 @@ def __init__(self):
3434 """
3535 Initialize the data handler.
3636 """
37- self .data_connector = DataConnector ()
37+ self .data_connector = DataConnectorAsync ()
3838 self .underlying_addresses_to_decimals = defaultdict (dict )
39- self .zklend_state = self . _init_zklend_state ()
39+ self .zklend_state = None
4040 self .prices = None
41+ self .states = []
42+
43+ @classmethod
44+ async def create (cls ):
45+ """
46+ Factory method to create and initialize an instance with async operations.
47+ """
48+ instance = cls ()
49+ instance .zklend_state = await instance ._init_zklend_state ()
4150 # TODO add also nostra states
42- self .states = [
43- self .zklend_state ,
51+ instance .states = [
52+ instance .zklend_state ,
4453 # nostra_alpha_state,
4554 # nostra_mainnet_state,
4655 ]
56+ return instance
4757
48- def _init_zklend_state (self ) -> ZkLendState :
58+ async def _init_zklend_state (self ) -> ZkLendState :
4959 """
5060 Initialize ZkLend state.
5161 Fetch data from the database and initialize the state.
5262 :return: Initialized ZkLend state.
5363 """
5464 logger .info ("Initializing ZkLend state." )
5565 zklend_state = ZkLendState ()
56- self ._fetch_and_process_zklend_data (zklend_state )
57- self ._set_zklend_interest_rates (zklend_state )
66+ await self ._fetch_and_process_zklend_data (zklend_state )
67+ await self ._set_zklend_interest_rates (zklend_state )
5868 return zklend_state
5969
60- def _fetch_and_process_zklend_data (self , zklend_state ):
70+ async def _fetch_and_process_zklend_data (self , zklend_state ):
6171 PROTOCOL_ZKLEND = "zkLend"
6272 BATCH_SIZE = 1000
6373 start = monotonic ()
6474
6575 try :
66- first_block = self .data_connector .fetch_protocol_first_block_number (
76+ first_block = await self .data_connector .fetch_protocol_first_block_number (
6777 PROTOCOL_ZKLEND
6878 )
69- last_block = self .data_connector .fetch_protocol_last_block_number (
79+ last_block = await self .data_connector .fetch_protocol_last_block_number (
7080 PROTOCOL_ZKLEND
7181 )
7282
7383 current_block = first_block
7484 while current_block <= last_block :
7585 end_block = min (current_block + BATCH_SIZE - 1 , last_block )
76- batch = self .data_connector .fetch_data (
86+ batch = await self .data_connector .fetch_data (
7787 self .data_connector .ZKLEND_SQL_QUERY ,
7888 protocol = PROTOCOL_ZKLEND ,
7989 batch_size = BATCH_SIZE ,
@@ -104,8 +114,8 @@ def _fetch_and_process_zklend_data(self, zklend_state):
104114 zklend_state .last_block_number = last_block
105115 logger .info ("Initialized ZkLend state in %.2fs" , monotonic () - start )
106116
107- def _set_zklend_interest_rates (self , zklend_state ):
108- zklend_interest_rate_data = self .data_connector .fetch_data (
117+ async def _set_zklend_interest_rates (self , zklend_state ):
118+ zklend_interest_rate_data = await self .data_connector .fetch_data (
109119 self .data_connector .ZKLEND_INTEREST_RATE_SQL_QUERY
110120 )
111121
0 commit comments