

# https://dexscreener.com/solana?rankBy=trendingScoreH24&order=desc&minLiq=50000&minAge=36

# 将油猴脚本获取的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_merge_path)
print('\n'*5)
print(f"{'{:<6}'.format('ENTER')} {baseclass.scriptfilename} ----------------NOTE-----------NOTE---------------")

def makescreenurl( urlname,urlparam):
    _url = urlname + urlparam
    return '=HYPERLINK("{}","{}")'.format(_url, _url)


def get_fir_swap_info(gdf):
    # 未来使用
    arr_firswap_type = gdf["TYPE"].tolist()
    arr_firswap_token_amount = gdf["Token"].tolist()
    arr_firswap_eth_amount = gdf["SOL"].tolist()
    arr_firswap_token_amount = [decimal.Decimal( token_amount) for token_amount in arr_firswap_token_amount  ]
    arr_firswap_eth_amount = [decimal.Decimal( token_amount) for token_amount in arr_firswap_eth_amount  ]
    while len(arr_firswap_eth_amount) > 0:
        if arr_firswap_type[0]=="Buy":
            break
        else:
            arr_firswap_eth_amount.pop(0)
            arr_firswap_token_amount.pop(0)
            arr_firswap_type.pop(0)
    if len(arr_firswap_eth_amount) == 0:
        return "0","-10"
    
    fir_swap_tokenamount = arr_firswap_token_amount[0]
    fir_swap_costmain = arr_firswap_eth_amount[0]
    fir_swap_earntmain = -fir_swap_costmain
    while len(arr_firswap_eth_amount) > 0:
        # 去除 type为buy的交易 
        if arr_firswap_type[0]=="Buy":
            arr_firswap_eth_amount.pop(0)
            arr_firswap_token_amount.pop(0)
            arr_firswap_type.pop(0)
            continue
        # 现在  全是sell token earn eth
        cur_swap_tokenamount = arr_firswap_token_amount[0]
        cur_swap_eth = arr_firswap_eth_amount[0]
        arr_firswap_eth_amount.pop(0)
        arr_firswap_token_amount.pop(0)
        arr_firswap_type.pop(0)
        if fir_swap_tokenamount>=cur_swap_tokenamount:
            fir_swap_earntmain+=cur_swap_eth
            fir_swap_tokenamount-=cur_swap_tokenamount
        else:
            fir_swap_earntmain = fir_swap_earntmain+  fir_swap_tokenamount/cur_swap_tokenamount * cur_swap_eth
            fir_swap_tokenamount=decimal.Decimal(0)
        if fir_swap_tokenamount==0:
            break
     
    fir_cost_main =('{0:.2f}'.format(fir_swap_costmain) ) 
    fir_earn_percent=  ('{0:.2f}'.format(fir_swap_earntmain / fir_swap_costmain) )    if fir_swap_costmain != 0 else "-10"

    return fir_cost_main , fir_earn_percent
def fun(gdf, tokenadd):
      
    gdf=gdf[gdf['TYPE'].isin(["Buy", "Sell"])].reset_index(drop=True)

    if len(gdf)==0:
        return  None
    
    Maker = gdf['MAKER'].tolist()[0]
    buy_mask =  gdf["TYPE"].isin(["Buy"])
    FirstSwaptime =  pd.Timestamp('1970-01-01 01:01:01')
    MinBuyPrice = 0
    if len(gdf[buy_mask])>0:
        # return None
 
        FirstSwaptime =  gdf[buy_mask ]['DATE'].min()
        MinBuyPrice = gdf[buy_mask ]['PRICE'].min()

    arr_type = gdf['TYPE'].drop_duplicates().tolist()
    # if ('Buy' not in arr_type or 'Sell' not in arr_type):
    #     return None
    Maker_isNormal ="1"
    if arr_type[0] =="Sell":
        Maker_isNormal = "0"
        # return None
    
    fir_cost_main , fir_earn_percent = get_fir_swap_info(gdf=gdf)
    if float(fir_cost_main) ==0 :
        Maker_isNormal ="0"
 
    buy_mask = gdf['TYPE'].isin(["Buy"]) 
    sell_mask = gdf['TYPE'].isin(["Sell"])   
    arr_buymoney = gdf[buy_mask]['SOL'].tolist()
    arr_sellmoney = gdf[sell_mask]['SOL'].tolist()
    arr_buytokenamount = gdf[buy_mask]['Token'].tolist()
    arr_selltokenamount = gdf[sell_mask]['Token'].tolist()
    Selltokenamount_dec = sum(
        [decimal.Decimal(token_dec) for token_dec in arr_selltokenamount])
    Buytokenamount_dec = sum(
        [decimal.Decimal(token_dec) for token_dec in arr_buytokenamount])
    Sellmoney_dec = sum(
        [decimal.Decimal(token_dec) for token_dec in arr_sellmoney])
    Buymoney_dec = sum(
        [decimal.Decimal(token_dec) for token_dec in arr_buymoney])
    Earn_dec = Sellmoney_dec - Buymoney_dec
    Earnper_dec = Earn_dec / \
        Buymoney_dec if Buymoney_dec != 0 else decimal.Decimal(
            -10)
    Amountper_dec = Selltokenamount_dec / Buytokenamount_dec if Buytokenamount_dec != 0 else decimal.Decimal(
        -10)

    Buymoney = '{0:.2f}'.format(Buymoney_dec)
    Sellmoney = '{0:.2f}'.format(Sellmoney_dec)
    Earn = '{0:.2f}'.format(Earn_dec)
    Earnper = '{0:.2f}'.format(Earnper_dec)
    Buyamount = '{0:.2f}'.format(Buytokenamount_dec)
    Sellamount = '{0:.2f}'.format(Selltokenamount_dec)
    Amountper = '{0:.2f}'.format(Amountper_dec)
    Buycount = len(gdf[buy_mask]['TXN'].drop_duplicates())
    Sellcount = len(gdf[sell_mask]['TXN'].drop_duplicates())

    return pd.DataFrame([[Maker,
                          Maker_isNormal,
                        FirstSwaptime,
                         MinBuyPrice,
                          Buymoney,
                          Sellmoney,
                          Earn,
                          Earnper,
                          Buycount,
                          Sellcount,
                          Buyamount,
                          Sellamount,
                          Amountper,
                          tokenadd,
                          fir_cost_main,
                          fir_earn_percent
                          ]])

import re
def filter_simple_dalao():

    res_df = None
    big_amount_df = None
    global arr_token_address

    for tokenadd in arr_token_address:
        df_pump =None
        df_raydium =None
        if  ( baseclass.dalao_merge_path /f"buytoken_df_{tokenadd}.xlsx").exists():
            continue

        # if ( baseclass.dalao_ori_tran_dexscreen_path / f"{tokenadd}_pump.csv").exists():
        #     df_pump = pd.read_csv(
        #     baseclass.dalao_ori_tran_dexscreen_path / f"{tokenadd}_pump.csv", dtype=object)

        #     arr_maker = df_pump["MAKER"].drop_duplicates().tolist()
        #     arr_special_maker = [maker for maker in arr_maker if len(maker)<15]
 
        #     special_df_pump = df_pump[df_pump["MAKER"].isin(arr_special_maker)]
        #     df_pump =   pd.concat([special_df_pump, df_pump], ignore_index=True)
        #     df_pump=df_pump.drop_duplicates(keep="first")
        #     df_pump.to_csv(baseclass.dalao_ori_tran_dexscreen_path / f"new_{tokenadd}_pump.csv",index=False)

        #     df_pump = df_pump[~df_pump["MAKER"].isin(arr_special_maker)].reset_index(drop=True)
        #     df_pump=df_pump[['DATE',
        #          'TYPE',
        #         'SOL',
        #          'Token',
        #          'MAKER',
        #          'TXN',
        #          ]]
        #     df_pump["DATE"] = pd.to_datetime(df_pump["DATE"], format='%m-%d %H:%M:%S')


        if ( baseclass.dalao_ori_tran_dexscreen_path / f"{tokenadd}_raydium.csv").exists():
            df_raydium =  pd.read_csv(
            baseclass.dalao_ori_tran_dexscreen_path / f"{tokenadd}_raydium.csv", dtype=object)
            df_raydium =df_raydium.drop_duplicates().reset_index(drop=True)
            df_raydium["DATE"] = pd.to_datetime(df_raydium["DATE"], format='%Y-%m-%d %H:%M:%S')
            
            df_raydium=df_raydium.sort_values(by=["DATE"],ascending=[True])
            df_raydium.to_csv(baseclass.dalao_ori_tran_dexscreen_path / f"{tokenadd}_raydium.csv", index=False)
            df_raydium=df_raydium[['DATE',
                 'TYPE',
                'SOL',
                 'Token',
                 'MAKER',
                 'TXN',
                 "PRICE",
                 ]]
                    
        # if df_pump is not None:
        # #     # pump 肯定小于 dexscreener 的 DATE 但是 dexscreener的Date 拿不到年月日 只能额外加时间 
        # #     实际上可以 不用 因为 在自己的电脑获取 date pump 和 dexscreen 的date 都是 当前utc+8 的时间
        #     df_raydium["DATE"] + pd.Timedelta(days=30)
        df = pd.concat([df_pump, df_raydium], ignore_index=True)
        df["PRICE"] = df["PRICE"].astype(float)
    
        df=df.sort_values(by=["DATE"],ascending=[True])
        df = df[df['TYPE'].isin(['Buy', 'Sell'])].reset_index(drop=True)
        # df.to_excel(baseclass.dalao_merge_path/f"total_dalao_txn_{tokenadd}.xlsx",index=False)
        
        print(f"enter {tokenadd}")

        # df["DATE"] = pd.to_datetime(df["DATE"], format='%m-%d %H:%M:%S')  
        # 去除 打新
        # min_date = df['DATE'].min()
        # daxin_max_date = min_date + pd.Timedelta(seconds=10)
        # daxin_mask = (df['DATE'] >= min_date) & (df['DATE'] <= daxin_max_date)
        # arr_daxin_dalaoadd = df[daxin_mask]['MAKER'].drop_duplicates().tolist()
        # drop_daxin_dalao_mask = (~ df['MAKER'].isin(arr_daxin_dalaoadd))
        # df = df[drop_daxin_dalao_mask].reset_index(drop=True)
        # df =df.sort_values(by=['DATE'],ascending=True)
        df["Maker_isNormal"] = "1"
        pattern = r"^00"
        new_tokenadd = tokenadd

        matches = re.findall(pattern, tokenadd)
        if len(matches) > 0:
            new_tokenadd=new_tokenadd[3:]


        df = df.groupby(by=['MAKER']).apply(
            lambda gdf: fun(gdf, new_tokenadd)).reset_index(drop=True)

        if (len(df) == 0):
            print("no transaction")
            continue
        df.columns = ['Maker',
                      "Maker_isNormal",
                      "FirstSwaptime",
                      "MinBuyPrice",
                      'Buymoney',
                      'Sellmoney',
                      'Earn',
                      'Earnper',
                      'Buycount',
                      'Sellcount',
                      'Buyamount',
                      'Sellamount',
                      'Amountper',
                      'TokenAddress',
                    'FirBuymoney',
                    'FirEarnper'
                      ]

        # Rank,Maker,Buymoney,Sellmoney,Earn,Earnper,Buycount,Sellcount,Buyamount,Sellamount,Amountper,TokenAddress
  

        df['FirstSwaptime'] = df['FirstSwaptime'].dt.strftime('%Y-%m-%d %H:%M:%S')

        df["Amountper"] = df["Amountper"].astype(float)
        df["Buymoney"] = df["Buymoney"].astype(float)
        df["Sellmoney"] = df["Sellmoney"].astype(float)
        df["Earn"] = df["Earn"].astype(float)
        df["Earnper"] = df["Earnper"].astype(float)
        df["Buycount"] = df["Buycount"].astype(int)
        df["Sellcount"] = df["Sellcount"].astype(int)
        df["Earnper"] = df['Earnper'].round(2)
        df["Amountper"] = df['Amountper'].round(2)
        df["FirBuymoney"] = df["FirBuymoney"].astype(float)
        df["FirEarnper"] = df["FirEarnper"].astype(float)
        df.insert(12, 'isNormal', -1)
        df.insert(1, 'DexScreenURL', -1)

        df["DexScreenURL"] = df.apply(lambda ser: makescreenurl(
            "https://dexscreener.com/solana/" , f"{new_tokenadd }?maker={ser['Maker']}" ), axis=1)
        df = df.sort_values(by=[ "FirstSwaptime"], ascending=[True])
     
        # df.to_excel(baseclass.dalao_merge_path/f"overview_{tokenadd}.xlsx",index=False)
        

        buytoken_mask  = (
        (df["Buymoney"] >= 0.3)
        # &(df["Amountper"] <=1)
        & (   ~(  ( df["Earnper"]<=0.4 )
            & (df["Amountper"]>=0.9)
            ) 
            )
        )
 

        buytoken_df = df[buytoken_mask].reset_index(drop=True)

        buytoken_df.to_excel(baseclass.dalao_merge_path /
                        f"buytoken_df_{tokenadd}.xlsx", index=False)
        # 基本到这一步就可以了

        # res_df = pd.concat([res_df, df], ignore_index=True)

    return

    res_df.to_excel(baseclass.dalao_merge_path /
                    "ori_merge.xlsx", index=False)
    
    
    nor_filter_mask = (
        (res_df["Amountper"] <= 1.01)
        & (res_df["FirEarnper"] >= 1)
        & (res_df["FirBuymoney"] >= 0.3)
        & ((res_df["Buycount"] <= 4) )
    )
    # nor_filter_mask = nor_filter_mask & False
    fir_big1_filter_mask = (
        (res_df["Amountper"] <= 1.1)
        & (res_df["FirEarnper"] >= 0.4)
        & (res_df["FirBuymoney"] >= 0.5)
        # & ((res_df["Buycount"] <= 4) )
    )
    big1_filter_mask = (
        (res_df["Amountper"] <= 1.1)
        & (res_df["Earnper"] >= 0.4)
        & (res_df["Buymoney"] >= 0.5)
        # & ((res_df["Buycount"] <= 4) )
    )
 

    filter_mask = (
        big1_filter_mask
        | fir_big1_filter_mask
        # nor_filter_mask
        # | big1_filter_mask
                     
                   )
    # add_filter_mask = ((res_df["Buymoney"] <= 400))
    
    
   
    res_df = res_df[filter_mask].reset_index(drop=True)
    res_df = res_df.sort_values(by=["TokenAddress","FirstSwaptime"], ascending=[True,True])
    # multiple_df = multiple_df.sort_values(by=["TokenAddress","FirstSwaptime"], ascending=[True,True])
    print(f"len df ={len(res_df)}")

    # 示例 获取结果如下https://dexscreener.com/solana/9s9sgqskfyzbygm45qblje4rt6calfa2dbhjrevlszrc?maker=47TwqZwwMGHc9CkJJxvVgFNGLDqBuSPy95KwUSTcLSti

    res_df['DexScreenURL'] = res_df.apply(lambda ser: makescreenurl(
        "https://dexscreener.com/solana/", f"{ser['TokenAddress']}?maker={ser['Maker']}"), axis=1)

    
    res_df.to_excel(baseclass.dalao_merge_path /
                    "un_onhand_dalao.xlsx", index=False)
 
    return

csv_file_list = [f for f in os.listdir(
    baseclass.dalao_ori_tran_dexscreen_path) if f.endswith(".csv") ]
arr_token_address = [tokenadd.split("_")[0] for tokenadd in csv_file_list  if "new_" not in tokenadd]
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---------------")
