@@ -49,6 +49,41 @@ class DataConnector:
49
49
WHERE protocol_id = 'zkLend' AND block = (SELECT max_block FROM max_block);
50
50
"""
51
51
52
+ VESU_POSITIONS_SQL_QUERY = """
53
+ SELECT
54
+ vp.user,
55
+ vp.pool_id,
56
+ vp.collateral_asset,
57
+ vp.debt_asset,
58
+ vp.block_number
59
+ FROM
60
+ vesu_positions AS vp
61
+ WHERE
62
+ vp.block_number = (
63
+ SELECT MAX(block_number)
64
+ FROM vesu_positions
65
+ WHERE user = vp.user
66
+ AND pool_id = vp.pool_id
67
+ );
68
+ """
69
+
70
+ VESU_HEALTH_FACTORS_SQL_QUERY = """
71
+ SELECT
72
+ hrl.user_id as user,
73
+ hrl.protocol_id as pool_id,
74
+ hrl.value as health_factor,
75
+ hrl.timestamp
76
+ FROM
77
+ health_ratio_level AS hrl
78
+ WHERE
79
+ hrl.timestamp = (
80
+ SELECT MAX(timestamp)
81
+ FROM health_ratio_level
82
+ WHERE user_id = hrl.user_id
83
+ AND protocol_id = hrl.protocol_id
84
+ );
85
+ """
86
+
52
87
def __init__ (self ):
53
88
"""
54
89
Initialize the DataConnector with database connection details.
@@ -209,12 +244,40 @@ def fetch_data_from_csv(self, file_path: str) -> pd.DataFrame:
209
244
logger .error (f"Error reading CSV file: { e } " )
210
245
return None
211
246
247
+ def fetch_vesu_positions (self ) -> pd .DataFrame :
248
+ """
249
+ Fetch Vesu positions data using the predefined query.
250
+
251
+ :return: DataFrame containing Vesu positions data
252
+ """
253
+ try :
254
+ with self .engine .connect () as connection :
255
+ return pd .read_sql (self .VESU_POSITIONS_SQL_QUERY , connection )
256
+ except sqlalchemy .exc .SQLAlchemyError as e :
257
+ logger .error (f"Database error: { e } " )
258
+ raise DatabaseConnectionError (f"Failed to fetch Vesu positions: { str (e )} " )
259
+
260
+ def fetch_vesu_health_factors (self ) -> pd .DataFrame :
261
+ """
262
+ Fetch Vesu health factors data using the predefined query.
263
+
264
+ :return: DataFrame containing Vesu health factors data
265
+ """
266
+ try :
267
+ with self .engine .connect () as connection :
268
+ return pd .read_sql (self .VESU_HEALTH_FACTORS_SQL_QUERY , connection )
269
+ except sqlalchemy .exc .SQLAlchemyError as e :
270
+ logger .error (f"Database error: { e } " )
271
+ raise DatabaseConnectionError (
272
+ f"Failed to fetch Vesu health factors: { str (e )} "
273
+ )
274
+
212
275
213
276
class DataConnectorAsync (DataConnector ):
214
277
"""
215
278
Handles database connection and fetches data asynchronously.
216
279
"""
217
-
280
+
218
281
async def fetch_data (
219
282
self ,
220
283
query : str ,
@@ -298,7 +361,7 @@ async def fetch_data(
298
361
return pd .DataFrame ()
299
362
300
363
return pd .concat (all_data , ignore_index = True )
301
-
364
+
302
365
async def fetch_protocol_first_block_number (self , protocol : str ) -> int :
303
366
"""
304
367
Asynchronously fetch the first block number for a specific protocol.
0 commit comments