
from base_class import BaseVariableFunction
from base_class import *

# from base_library import BaseLibrary

old_print = print


def timestamped_print(*args, **kwargs):
    old_print(datetime.datetime.utcnow().replace(
        microsecond=0), *args, **kwargs)


print = timestamped_print
baseclass = BaseVariableFunction(__file__)

baselibrary= baseclass.open_base_library()
 
baseclass.makedirpath(baseclass.dalao_buysell_count_path)
print('\n'*5)
print(f"{'{:<6}'.format('ENTER')} {baseclass.scriptfilename} ----------------NOTE-----------NOTE---------------")
            
arr_token_transfer_file = [file for file in os.listdir(baseclass.contracttokentx_path) if file.endswith(".csv")]


for file in arr_token_transfer_file:
    tokenaddress = file.replace(".csv","")
    df =pd.read_csv(baseclass.contracttokentx_path/f"{file}" , dtype=object)  
    # df.groupby(by=["hash"] ).apply(lambda gdf: func( gdf=gdf ))
    sell_count_df = df.groupby('from')['hash'].nunique()
    # 计算 buy_count_df
    buy_count_df = df.groupby('to')['hash'].nunique()
    
    dalao_count_df = pd.DataFrame({'dalaoaddress': list(set(df['from'].unique()) | set(df['to'].unique()))})

    # 将 sell_count_df 和 buy_count_df 合并到 dalao_count_df 中
    dalao_count_df['buy_count'] = dalao_count_df['dalaoaddress'].map(buy_count_df)
    dalao_count_df['sell_count'] = dalao_count_df['dalaoaddress'].map(sell_count_df)
    dalao_count_df['buy_count'] = dalao_count_df['buy_count'].fillna(0)
    dalao_count_df['sell_count'] = dalao_count_df['sell_count'].fillna(0)
    
    dalao_count_df = dalao_count_df.sort_values(by='dalaoaddress')
    dalao_count_df =dalao_count_df [~dalao_count_df["dalaoaddress"].isin(list(baselibrary["arr_exclued_address"].keys()))].reset_index(drop=True)
        
 
    
    dalao_count_df.to_excel(baseclass.dalao_buysell_count_path / f"{tokenaddress}.xlsx",index=False)
    
 
         
         
             

  
             

  
 
 
print(f"{'{:<6}'.format('END')} {baseclass.scriptfilename} ----------------NOTE-----------NOTE---------------")

