@@ -148,7 +148,7 @@ def get_pool_info(
148148 tkn1 : Dict [str , Any ],
149149 pool_data_keys : frozenset ,
150150) -> Dict [str , Any ]:
151- fee_raw = eval ( pool ["fee" ])
151+ fee_raw = pool ["fee" ]
152152 pool_info = {
153153 "exchange_name" : pool ["exchange_name" ],
154154 "address" : pool ["address" ],
@@ -184,32 +184,35 @@ def get_pool_info(
184184
185185 return pool_info
186186
187- def sanitize_token_symbol (token_symbol : str , token_address : str ) -> str :
187+ def sanitize_token_symbol (token_symbol : str , token_address : str , read_only : bool ) -> str :
188188 """
189189 This function ensures token symbols are compatible with the bot's data structures.
190190 If a symbol is not compatible with Dataframes or CSVs, this function will return the token's address.
191191
192192 :param token_symbol: the token's symbol
193193 :param token_address: the token's address
194+ :param read_only: bool indicating whether the bot is running in read_only mode
194195
195196 returns: str
196197 """
197198 sanitization_path = os .path .normpath ("fastlane_bot/data/data_sanitization_center/sanitary_data.csv" )
198199 try :
199- token_pd = pd .DataFrame ([{"symbol" : token_symbol }], columns = ["symbol" ])
200- token_pd .to_csv (sanitization_path )
200+ if not read_only :
201+ token_pd = pd .DataFrame ([{"symbol" : token_symbol }], columns = ["symbol" ])
202+ token_pd .to_csv (sanitization_path )
201203 return token_symbol
202204 except Exception :
203205 return token_address
204206
205207
206208def add_token_info (
207- pool_info : Dict [str , Any ], tkn0 : Dict [str , Any ], tkn1 : Dict [str , Any ]
209+ pool_info : Dict [str , Any ], tkn0 : Dict [str , Any ], tkn1 : Dict [str , Any ], read_only : bool
208210) -> Dict [str , Any ]:
211+ print (f"called add_token_info" )
209212 tkn0_symbol = tkn0 ["symbol" ].replace ("/" , "_" ).replace ("-" , "_" )
210213 tkn1_symbol = tkn1 ["symbol" ].replace ("/" , "_" ).replace ("-" , "_" )
211- tkn0_symbol = sanitize_token_symbol (token_symbol = tkn0_symbol , token_address = tkn0 ["address" ])
212- tkn1_symbol = sanitize_token_symbol (token_symbol = tkn1_symbol , token_address = tkn1 ["address" ])
214+ tkn0_symbol = sanitize_token_symbol (token_symbol = tkn0_symbol , token_address = tkn0 ["address" ], read_only = read_only )
215+ tkn1_symbol = sanitize_token_symbol (token_symbol = tkn1_symbol , token_address = tkn1 ["address" ], read_only = read_only )
213216 tkn0 ["symbol" ] = tkn0_symbol
214217 tkn1 ["symbol" ] = tkn1_symbol
215218
@@ -310,30 +313,45 @@ def process_contract_chunks(
310313 subset : List [str ],
311314 func : Callable ,
312315 df_combined : pd .DataFrame = None ,
316+ read_only : bool = False ,
313317) -> pd .DataFrame :
318+ lst = []
314319 # write chunks to csv
315320 for idx , chunk in enumerate (chunks ):
316321 loop = asyncio .get_event_loop ()
317322 df = loop .run_until_complete (func (chunk ))
318- df .to_csv (f"{ dirname } /{ base_filename } { idx } .csv" , index = False )
323+ if not read_only :
324+ df .to_csv (f"{ dirname } /{ base_filename } { idx } .csv" , index = False )
325+ else :
326+ lst .append (df )
319327
320- # concatenate and deduplicate
321328 filepaths = glob (f"{ dirname } /*.csv" )
322- if filepaths :
323- df_orig = df_combined .copy () if df_combined is not None else None
324- df_combined = pd .concat ([pd .read_csv (filepath ) for filepath in filepaths ])
325- df_combined = (
326- pd .concat ([df_orig , df_combined ]) if df_orig is not None else df_combined
327- )
328- df_combined = df_combined .drop_duplicates (subset = subset )
329- df_combined .to_csv (filename , index = False )
330329
331- # clear temp dir
332- for filepath in filepaths :
333- try :
334- os .remove (filepath )
335- except Exception as e :
336- cfg .logger .error (f"Failed to remove { filepath } { e } ??? This is spooky..." )
330+ if not read_only :
331+ # concatenate and deduplicate
332+
333+ if filepaths :
334+ df_orig = df_combined .copy () if df_combined is not None else None
335+ df_combined = pd .concat ([pd .read_csv (filepath ) for filepath in filepaths ])
336+ df_combined = (
337+ pd .concat ([df_orig , df_combined ]) if df_orig is not None else df_combined
338+ )
339+ df_combined = df_combined .drop_duplicates (subset = subset )
340+ df_combined .to_csv (filename , index = False )
341+ # clear temp dir
342+ for filepath in filepaths :
343+ try :
344+ os .remove (filepath )
345+ except Exception as e :
346+ cfg .logger .error (f"Failed to remove { filepath } { e } ??? This is spooky..." )
347+ else :
348+ if lst :
349+ dfs = pd .concat (lst )
350+ dfs = dfs .drop_duplicates (subset = subset )
351+ if df_combined is not None :
352+ df_combined = pd .concat ([df_combined , dfs ])
353+ else :
354+ df_combined = dfs
337355
338356 return df_combined
339357
@@ -392,6 +410,7 @@ def async_update_pools_from_contracts(mgr: Any, current_block: int, logging_path
392410 filename = "tokens_and_fee_df.csv" ,
393411 subset = ["exchange_name" , "address" , "cid" , "tkn0_address" , "tkn1_address" ],
394412 func = main_get_tokens_and_fee ,
413+ read_only = mgr .read_only ,
395414 )
396415
397416 contracts , tokens_df = get_token_contracts (mgr , tokens_and_fee_df )
@@ -405,16 +424,18 @@ def async_update_pools_from_contracts(mgr: Any, current_block: int, logging_path
405424 df_combined = pd .read_csv (
406425 f"fastlane_bot/data/blockchain_data/{ mgr .blockchain } /tokens.csv"
407426 ),
427+ read_only = mgr .read_only ,
408428 )
409429 tokens_df ["symbol" ] = (
410430 tokens_df ["symbol" ]
411431 .str .replace (" " , "_" )
412432 .str .replace ("/" , "_" )
413433 .str .replace ("-" , "_" )
414434 )
415- tokens_df .to_csv (
416- f"fastlane_bot/data/blockchain_data/{ mgr .blockchain } /tokens.csv" , index = False
417- )
435+ if not mgr .read_only :
436+ tokens_df .to_csv (
437+ f"fastlane_bot/data/blockchain_data/{ mgr .blockchain } /tokens.csv" , index = False
438+ )
418439 tokens_df ["address" ] = tokens_df ["address" ].apply (
419440 lambda x : Web3 .to_checksum_address (x )
420441 )
@@ -440,44 +461,6 @@ def async_update_pools_from_contracts(mgr: Any, current_block: int, logging_path
440461 ]
441462 )
442463
443- # def correct_tkn(tkn_address, keyname):
444- # try:
445- # return tokens_df[tokens_df["address"] == tkn_address][keyname].values[0]
446- # except IndexError:
447- # return np.nan
448- #
449- # static_pool_data = new_pool_data_df.copy()
450- # static_pool_data["tkn0_address"] = static_pool_data["tkn0_address"].apply(
451- # lambda x: Web3.to_checksum_address(x)
452- # )
453- # static_pool_data["tkn1_address"] = static_pool_data["tkn1_address"].apply(
454- # lambda x: Web3.to_checksum_address(x)
455- # )
456- # static_pool_data["tkn0_decimals"] = static_pool_data["tkn0_address"].apply(
457- # lambda x: correct_tkn(x, "decimals")
458- # )
459- # static_pool_data["tkn1_decimals"] = static_pool_data["tkn1_address"].apply(
460- # lambda x: correct_tkn(x, "decimals")
461- # )
462- # static_pool_data["tkn0_key"] = static_pool_data["tkn0_address"].apply(
463- # lambda x: correct_tkn(x, "key")
464- # )
465- # static_pool_data["tkn1_key"] = static_pool_data["tkn1_address"].apply(
466- # lambda x: correct_tkn(x, "key")
467- # )
468- # static_pool_data["tkn0_symbol"] = static_pool_data["tkn0_address"].apply(
469- # lambda x: correct_tkn(x, "symbol")
470- # )
471- # static_pool_data["tkn1_symbol"] = static_pool_data["tkn1_address"].apply(
472- # lambda x: correct_tkn(x, "symbol")
473- # )
474- # static_pool_data["pair_name"] = (
475- # static_pool_data["tkn0_key"] + "/" + static_pool_data["tkn1_key"]
476- # )
477- #
478- # new_pool_data_df = static_pool_data.copy()
479- # del static_pool_data
480-
481464 new_pool_data_df ["descr" ] = (
482465 new_pool_data_df ["exchange_name" ]
483466 + " "
@@ -502,9 +485,6 @@ def async_update_pools_from_contracts(mgr: Any, current_block: int, logging_path
502485 )
503486
504487 duplicate_new_pool_ct = len (duplicate_cid_rows )
505- # assert len(mgr.pools_to_add_from_contracts) == (
506- # len(new_pool_data_df) + duplicate_new_pool_ct
507- # )
508488
509489 all_pools_df = (
510490 pd .DataFrame (mgr .pool_data )
0 commit comments