# 将dune 上 special的add 的swap和transfer合并在一起
import pandas as pd
import sys

import datetime
import pytz

def makeurl( url,url_show):
    return '=HYPERLINK("{}","{}")'.format(url,  url_show)
def get_nowdate():

    timestamp = datetime.datetime.now().timestamp()
    # print(timestamp)
    dt = datetime.datetime.fromtimestamp(timestamp)
    tz = pytz.timezone('Asia/Shanghai')
    normal_time_str = dt.astimezone(tz).strftime('%Y-%m-%d %H:%M:%S')
    time_str = dt.astimezone(tz).strftime('%m%d%H%M')
    return time_str


def utc_2_shanghaidate(df , utc_time_col ):
# 将字符串转换为UTC时间的datetime对象
    df[utc_time_col] = pd.to_datetime(df[utc_time_col], utc=True)
    # 定义UTC+8时区
    utc_plus_8 = pytz.timezone('Asia/Shanghai')
    # 转换为UTC+8时间
    # df['shanghai_date'] = df['shanghai_date'].dt.tz_convert(utc_plus_8)
    # # 提取时间字符串
    # df['shanghai_date'] = df['shanghai_date'].dt.strftime('%Y-%m-%d %H:%M')
    df[utc_time_col] = df[utc_time_col].dt.tz_convert(utc_plus_8)
    # 提取时间字符串
    df[utc_time_col] = df[utc_time_col].dt.strftime('%Y-%m-%d %H:%M')
    return df

swap_row_file = ''
transfer_row_file = ''
def main():
    df_swap_row = pd.read_csv(swap_row_file,dtype=object)
    df_swap_row=df_swap_row.astype({'token_swap_M':float ,
                                    'eth_swap_amount':float ,
                                    'block_slot':float,
                                    'pri':float})

    df_transfer_row = pd.read_csv(transfer_row_file,dtype=object)
    df_transfer_row = df_transfer_row.astype({'token_transfer_M':float , 'block_slot':float})


    df_swap_row.rename(columns={'token_swap_M': 'token_M' }, inplace=True)
    
    
    df_swap_row = utc_2_shanghaidate(df=df_swap_row, utc_time_col='block_time')
    
    df_transfer_row = utc_2_shanghaidate(df=df_transfer_row, utc_time_col='block_time')


    # 创建两个DataFrame，一个用于'from'列，另一个用于'to'列
    df_from = df_transfer_row[[ 'tx_id',	'block_time',	'block_slot',	'token_transfer_M' ,'from_owner']].copy()
    df_from.rename(columns={'from_owner': 'trader_id' , 'token_transfer_M':'token_M'}, inplace=True)
    df_from['token_M'] = -df_from['token_M']

    df_to = df_transfer_row[[ 'tx_id',	'block_time',	'block_slot',	'token_transfer_M' ,'to_owner']].copy()
    df_to.rename(columns={'to_owner': 'trader_id','token_transfer_M':'token_M'}, inplace=True)
    df_to['token_M'] = df_to['token_M']

    # 合并两个DataFrame
    df_transfer_row = pd.concat([df_from, df_to], ignore_index=True)
    df_transfer_row['action_type'] = 't'
    df_transfer_row['token_M'] = df_transfer_row['token_M'].round(4)

    merged_df =  pd.concat([ df_swap_row , df_transfer_row], ignore_index=True)
    merged_df = merged_df.sort_values(by=['trader_id' , 'block_slot'],ascending=True)
    

    merged_df["oklink"] = merged_df.apply(lambda ser: makeurl(
     f"https://www.oklink.com/zh-hans/sol/tx/{ser['tx_id']}" , "oklink" ), axis=1)

    merged_df["scan"] = merged_df.apply(lambda ser: makeurl(
     f"https://solscan.io/tx/{ser['tx_id']}" , "scan" ), axis=1)
    
    merged_df.to_excel(  f'005_merge_swap_transfer_row_{get_nowdate()}.xlsx'  ,index=False)
    
    return


if __name__ == "__main__":
   
    swap_row_file = 'dune_get_tokenswap_row_special_add.csv'
    transfer_row_file = 'dune_transfer_00_0116日0200.csv'
    main()