


 
# 将油猴脚本获取的top trader
from base_class import BaseVariableFunction
from base_class import *
baseclass = BaseVariableFunction(__file__)

old_print = print


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


print = timestamped_print
 
baseclass.makedirpath(baseclass.dalao_ori_tran_gmgnOrdex_path)
baseclass.makedirpath(baseclass.dalao_tran_gmgnOrdex_path)
baseclass.makedirpath(baseclass.dalao_merge_path)
 

print('\n'*5)
print(f"{'{:<6}'.format('ENTER')} {baseclass.scriptfilename} ----------------NOTE-----------NOTE---------------")

def makescreenurl( urlname,urlparam):
    # pairAddress = series[address_str]
    urlname = urlname + urlparam
    return '=HYPERLINK("{}","{}")'.format(urlname, urlname)

 
        
import re
def filter_simple_dalao():

    global arr_token_address

    for tokenadd in arr_token_address:
     
        df_gmgn =None
        df_dex = None
        df =None
  
        if (baseclass.dalao_tran_gmgnOrdex_path /  f"{tokenadd}_top.xlsx").exists():
            print(f"{tokenadd}_top.xlsx exist")
            continue
            
        
        if ( baseclass.dalao_ori_tran_gmgnOrdex_path / f"{tokenadd}_top_dexscreen_sol.csv").exists():
            df_dex =  pd.read_csv( baseclass.dalao_ori_tran_gmgnOrdex_path / f"{tokenadd}_top_dexscreen_sol.csv", dtype=object)
            df_dex =df_dex.drop_duplicates().reset_index(drop=True)
            df_dex =df_dex.astype({
                "Buymoney":float,
                "Sellmoney":float,
                "Earn":float,
            })
            df_dex["Buy_sol"] = df_dex["Buymoney"] / 150
            df_dex["Sell_sol"] = df_dex["Sellmoney"] / 150
            df_dex["Pnl_sol"] = df_dex["Earn"] / 150
            df_dex["Pnl_per"] = np.where(
                    df_dex['Buy_sol'] != 0,
                    df_dex['Pnl_sol'] / df_dex['Buy_sol'],
                    999999,
                )
            df_dex =df_dex[df_dex["Pnl_per"]>0].reset_index(drop=True)
            
            df_dex=df_dex[[
                'Maker',
                'Buy_sol',
                'Sell_sol',
                'Pnl_sol',
                'Pnl_per',
                 ]]
        

            
                    
 
        if ( baseclass.dalao_ori_tran_gmgnOrdex_path / f"{tokenadd}_top_gmgn_sol.csv").exists():
            df_gmgn =  pd.read_csv( baseclass.dalao_ori_tran_gmgnOrdex_path / f"{tokenadd}_top_gmgn_sol.csv", dtype=object)
            df_gmgn =df_gmgn.drop_duplicates().reset_index(drop=True)
            df_gmgn =df_gmgn.astype({
                "Buy_sol":float,
                "Sell_sol":float,
                "Pnl_sol":float,
                "Pnl_per":float,
            })
            df_gmgn =df_gmgn[df_gmgn["Pnl_per"]>0].reset_index(drop=True)
             
            
     
       
 
        df = pd.concat([ df_gmgn, df_dex  ], ignore_index=True)
        df["Pnl_per"] = df["Pnl_per"].round(2)
        # df =df[df["Pnl_per"]>=2].reset_index(drop=True)
        
        df =df.sort_values(by=["Pnl_per"],ascending=False)
        df["beizhu"] = None
        df.loc[df["Pnl_per"]==999999,"beizhu"] = "neimu"
  
  
        print(f"enter {tokenadd}")
        df["TokenAddress"] = tokenadd
        
        df =df.drop_duplicates(subset=["Maker"]).reset_index(drop=True)
        # 示例 获取结果如下 https://www.defined.fi/sol/D6NdKrKNQPmRZCCnG1GqXtF7MMoHB7qR6GU5TkG59Qz1?maker=3ACp4T3ptTdayzWryEhT65NKZSKLjwXviBjWEy54aFdW 
        df['DefinedURL'] = df.apply(lambda ser: makescreenurl(
        "https://www.defined.fi/sol/", f"{ser['TokenAddress']}?maker={ser['Maker']}"), axis=1)
            
        df =df[ [ 
                'Maker',
                "beizhu",
                 "Buy_sol" ,
                "Sell_sol" ,
                "Pnl_sol" ,
                "Pnl_per",
                "TokenAddress",
                "DefinedURL"
                ] ]
        df.to_excel(baseclass.dalao_tran_gmgnOrdex_path /  f"{tokenadd}_top.xlsx",index=False )
        

 
    return

csv_file_list = [f for f in os.listdir(
    baseclass.dalao_ori_tran_gmgnOrdex_path) if f.endswith(".csv") ]
arr_token_address = [tokenadd.split("_")[0] for tokenadd in csv_file_list  ]
arr_token_address=list(set(arr_token_address))


print("arr_token_address= ",arr_token_address)

filter_simple_dalao()

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