2
2
import os
3
3
from typing import Optional , Any , Union
4
4
import pandas as pd
5
- import copy # Added for deepcopy
6
5
7
6
import requests
8
7
from driftpy .decode .utils import decode_name
@@ -289,12 +288,12 @@ def human_amm_df(df):
289
288
290
289
def serialize_perp_market (market : PerpMarketAccount ):
291
290
# Prepare market data by stringifying sumtypes and Pubkeys
292
- market_dict_prepared = _prepare_for_serialization (copy . deepcopy ( market .__dict__ ) )
293
- amm_dict_prepared = _prepare_for_serialization (copy . deepcopy ( market .amm .__dict__ ) )
294
- hist_oracle_data_prepared = _prepare_for_serialization (copy . deepcopy ( market .amm .historical_oracle_data .__dict__ ) )
295
- fee_pool_prepared = _prepare_for_serialization (copy . deepcopy ( market .amm .fee_pool .__dict__ ) )
296
- insurance_claim_prepared = _prepare_for_serialization (copy . deepcopy ( market .insurance_claim .__dict__ ) )
297
- pnl_pool_prepared = _prepare_for_serialization (copy . deepcopy ( market .pnl_pool .__dict__ ) )
291
+ market_dict_prepared = _prepare_for_serialization (market .__dict__ )
292
+ amm_dict_prepared = _prepare_for_serialization (market .amm .__dict__ )
293
+ hist_oracle_data_prepared = _prepare_for_serialization (market .amm .historical_oracle_data .__dict__ )
294
+ fee_pool_prepared = _prepare_for_serialization (market .amm .fee_pool .__dict__ )
295
+ insurance_claim_prepared = _prepare_for_serialization (market .insurance_claim .__dict__ )
296
+ pnl_pool_prepared = _prepare_for_serialization (market .pnl_pool .__dict__ )
298
297
299
298
market_df = pd .json_normalize (market_dict_prepared ).drop (['amm' , 'insurance_claim' , 'pnl_pool' ],axis = 1 , errors = 'ignore' ).pipe (human_market_df )
300
299
# 'name' is bytes, decode_name handles it; 'pubkey' is already stringified by _prepare_for_serialization if it was a Pubkey object
@@ -319,25 +318,17 @@ def serialize_perp_market(market: PerpMarketAccount):
319
318
320
319
result_df = pd .concat ([market_df , amm_df , amm_hist_oracle_df , market_amm_pool_df , market_if_df , market_pool_df ],axis = 1 )
321
320
322
- # Final conversion of object columns to string for Arrow compatibility
323
- for col in result_df .columns :
324
- if result_df [col ].dtype == 'object' :
325
- try :
326
- result_df [col ] = result_df [col ].astype (str )
327
- except Exception :
328
- # Fallback if astype(str) fails for any reason on a column
329
- result_df [col ] = result_df [col ].apply (lambda x : str (x ) if pd .notnull (x ) else x )
330
321
return result_df
331
322
332
323
333
324
def serialize_spot_market (spot_market : SpotMarketAccount ):
334
325
# Prepare spot_market data
335
- spot_market_dict_prepared = _prepare_for_serialization (copy . deepcopy ( spot_market .__dict__ ) )
336
- insurance_fund_prepared = _prepare_for_serialization (copy . deepcopy ( spot_market .insurance_fund .__dict__ ) )
337
- hist_oracle_data_prepared = _prepare_for_serialization (copy . deepcopy ( spot_market .historical_oracle_data .__dict__ ) )
338
- hist_index_data_prepared = _prepare_for_serialization (copy . deepcopy ( spot_market .historical_index_data .__dict__ ) )
339
- revenue_pool_prepared = _prepare_for_serialization (copy . deepcopy ( spot_market .revenue_pool .__dict__ ) )
340
- spot_fee_pool_prepared = _prepare_for_serialization (copy . deepcopy ( spot_market .spot_fee_pool .__dict__ ) )
326
+ spot_market_dict_prepared = _prepare_for_serialization (spot_market .__dict__ )
327
+ insurance_fund_prepared = _prepare_for_serialization (spot_market .insurance_fund .__dict__ )
328
+ hist_oracle_data_prepared = _prepare_for_serialization (spot_market .historical_oracle_data .__dict__ )
329
+ hist_index_data_prepared = _prepare_for_serialization (spot_market .historical_index_data .__dict__ )
330
+ revenue_pool_prepared = _prepare_for_serialization (spot_market .revenue_pool .__dict__ )
331
+ spot_fee_pool_prepared = _prepare_for_serialization (spot_market .spot_fee_pool .__dict__ )
341
332
342
333
spot_market_df = pd .json_normalize (spot_market_dict_prepared ).drop ([
343
334
'historical_oracle_data' , 'historical_index_data' ,
@@ -370,12 +361,4 @@ def serialize_spot_market(spot_market: SpotMarketAccount):
370
361
371
362
result_df = pd .concat ([spot_market_df , if_df , hist_oracle_df , hist_index_df , market_pool_df , market_fee_df ],axis = 1 )
372
363
373
- # Final conversion of object columns to string for Arrow compatibility
374
- for col in result_df .columns :
375
- if result_df [col ].dtype == 'object' :
376
- try :
377
- result_df [col ] = result_df [col ].astype (str )
378
- except Exception :
379
- # Fallback if astype(str) fails for any reason on a column
380
- result_df [col ] = result_df [col ].apply (lambda x : str (x ) if pd .notnull (x ) else x )
381
364
return result_df
0 commit comments