007_get_configtoken_st_solanafm_old.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import json
  2. import shutil
  3. import urllib.parse
  4. from base_class import BaseVariableFunction
  5. from base_class import *
  6. old_print = print
  7. def timestamped_print(*args, **kwargs):
  8. old_print(datetime.datetime.utcnow().replace(
  9. microsecond=0), *args, **kwargs)
  10. print = timestamped_print
  11. baseclass = BaseVariableFunction(__file__)
  12. print('\n'*5)
  13. print(f"{'{:<6}'.format('ENTER')} {baseclass.scriptfilename} ----------------NOTE-----------NOTE---------------")
  14. baseclass.makedirpath(baseclass.dalao_configtoken_st_solanafm_path)
  15. Raydium_Authority_V4 = "5Q544fKrFoe6tsEbD7S8EmxGTJYAKtTVhAW5Q5pge4j1"
  16. WSOL_Token = "So11111111111111111111111111111111111111112"
  17. def checkgdfisswap(gdf, str_dalaoaddress):
  18. # 保留与raudium 有关的token 的transfer 即只参与了swap的token
  19. raydium_mask = ((gdf["source"] == Raydium_Authority_V4) | (
  20. gdf["destination"] == Raydium_Authority_V4))
  21. list_raydium_token = gdf[raydium_mask]["token"].drop_duplicates().tolist()
  22. gdf = gdf[gdf["token"].isin(list_raydium_token)]
  23. dalao_intoken_mask = gdf["destination"] == str_dalaoaddress
  24. dalao_outtoken_mask = gdf["source"] == str_dalaoaddress
  25. list_dalao_intoken = gdf[dalao_intoken_mask]["token"].drop_duplicates(
  26. ).tolist()
  27. list_dalao_outtoken = gdf[dalao_outtoken_mask]["token"].drop_duplicates(
  28. ).tolist()
  29. if (len(list_dalao_intoken) == 0 or len(list_dalao_outtoken) == 0):
  30. # dalao 没有token in 或者out 有问题 此sign 不是swap
  31. return None
  32. list_dalao_token = list(set(list_dalao_intoken) | set(list_dalao_outtoken))
  33. if (len(list_dalao_token) != 2):
  34. # dalao 的token数目不对 除了 sol token 还有其他的token 不是我想要的sign return
  35. return None
  36. if WSOL_Token not in list_dalao_token:
  37. # 没有sol 不是我想要的
  38. return None
  39. return gdf
  40. def setswaptoken(gdf, str_dalaoaddress):
  41. dalao_intoken_mask = gdf["destination"] == str_dalaoaddress
  42. dalao_outtoken_mask = gdf["source"] == str_dalaoaddress
  43. list_dalao_intoken = gdf[dalao_intoken_mask]["token"].drop_duplicates(
  44. ).tolist()
  45. list_dalao_outtoken = gdf[dalao_outtoken_mask]["token"].drop_duplicates(
  46. ).tolist()
  47. list_dalao_token = list(set(list_dalao_intoken) | set(list_dalao_outtoken))
  48. list_dalao_token.remove(WSOL_Token)
  49. swapTokenAddress = list_dalao_token[0]
  50. gdf["swap_tokenaddress"] = swapTokenAddress
  51. return gdf
  52. pow_ten_list = [
  53. '1', '10', '100', '1000',
  54. '10000', '100000', '1000000',
  55. '10000000', '100000000', '1000000000',
  56. '10000000000', '100000000000', '1000000000000',
  57. '10000000000000', '100000000000000', '1000000000000000',
  58. '10000000000000000', '100000000000000000', '1000000000000000000',
  59. '10000000000000000000', '100000000000000000000', '1000000000000000000000',
  60. '10000000000000000000000', '100000000000000000000000', '1000000000000000000000000'
  61. ]
  62. # swap_ethamount swap_tokenamount
  63. def compute_eth_amount_onerow(row, str_dalaoaddress):
  64. row = row.copy()
  65. if (row["token"] != WSOL_Token):
  66. return row
  67. amount = decimal.Decimal(row['amount'])
  68. row['amount'] = '{0:.4f}'.format(
  69. amount / decimal.Decimal(pow_ten_list[9]))
  70. if (row['source'] == str_dalaoaddress):
  71. row['swap_eth_amount'] = '{0:.4f}'.format(decimal.Decimal(
  72. row['swap_eth_amount']) - decimal.Decimal(row['amount']))
  73. if (row['destination'] == str_dalaoaddress):
  74. row['swap_eth_amount'] = '{0:.4f}'.format(decimal.Decimal(
  75. row['swap_eth_amount']) + decimal.Decimal(row['amount']))
  76. return row
  77. # def compute_eth_amount_gdf(gdf ):
  78. # # 判断此hash 是buy 还是 sell
  79. # ,,,,
  80. # return
  81. def compute_eth_amount(df, str_dalaoaddress):
  82. # 將主流幣全部替換為wbnb,amount 以及 token 都替換,
  83. # 添加swap_ethamount 列
  84. # 计算每条hash 的 bnb 的value
  85. df["swap_eth_amount"]="0"
  86. df["actiontype"]=None
  87. # 待添加
  88. # df = df.groupby(by=["sign"], group_keys=False).apply(
  89. # lambda gdf: compute_eth_amount_gdf(gdf))
  90. df = df.apply(lambda row: compute_eth_amount_onerow(
  91. row, str_dalaoaddress), axis=1)
  92. return df
  93. def compute_token_amount_onerow(row, str_dalaoaddress):
  94. row = row.copy()
  95. if (row["token"] == WSOL_Token):
  96. return row
  97. amount = decimal.Decimal(row['amount'])
  98. row['amount'] = '{0:.0f}'.format(
  99. amount / decimal.Decimal(pow_ten_list[6]))
  100. if (row['source'] == str_dalaoaddress):
  101. row['swap_token_amount'] = '{0:.0f}'.format(decimal.Decimal(
  102. row['swap_token_amount']) - decimal.Decimal(row['amount']))
  103. if (row['destination'] == str_dalaoaddress):
  104. row['swap_token_amount'] = '{0:.0f}'.format(decimal.Decimal(
  105. row['swap_token_amount']) + decimal.Decimal(row['amount']))
  106. return row
  107. def compute_token_amount(df, str_dalaoaddress):
  108. df = df.apply(lambda row: compute_token_amount_onerow(
  109. row, str_dalaoaddress), axis=1)
  110. return df
  111. def get_mul_configtoken_st():
  112. success_address_list = arr_str_dalaoaddress.copy()
  113. for idx in range(0, len(arr_str_dalaoaddress), batchSize):
  114. thread_list = []
  115. for cur_idx in range(idx, min(idx+batchSize, len(arr_str_dalaoaddress))):
  116. thread = threading.Thread(
  117. target=get_one_configtoken_st,
  118. args=(arr_str_dalaoaddress[cur_idx],
  119. success_address_list)
  120. )
  121. thread_list.append(thread)
  122. for thread in thread_list:
  123. thread.start()
  124. for thread in thread_list:
  125. thread.join()
  126. return
  127. def get_one_configtoken_st(str_dalaoaddress, success_address_list):
  128. print(f"enter str_dalaoaddress= {str_dalaoaddress}")
  129. if not (baseclass.dalao_transaction_st_solanafm_path /
  130. f"{str_dalaoaddress}.csv").exists():
  131. return
  132. df = pd.read_csv(baseclass.dalao_transaction_st_solanafm_path /
  133. f"{str_dalaoaddress}.csv", dtype=object)
  134. if (len(df) == 0):
  135. return
  136. df["token"] = df["token"].fillna(WSOL_Token)
  137. prelen = len(df)
  138. df = df.groupby(by=["sign"], group_keys=False).apply(
  139. lambda gdf: checkgdfisswap(gdf, str_dalaoaddress))
  140. aftlen = len(df)
  141. if prelen != aftlen:
  142. print(f"{str_dalaoaddress} pre= {prelen} aft= {aftlen}")
  143. if aftlen == 0:
  144. return
  145. df["swap_tokenaddress"] = ""
  146. df = df.groupby(by=["sign"], group_keys=False).apply(
  147. lambda gdf: setswaptoken(gdf, str_dalaoaddress))
  148. df["swap_eth_amount"] = "0"
  149. df = compute_eth_amount(df=df, str_dalaoaddress=str_dalaoaddress)
  150. df["swap_token_amount"] = "0"
  151. df = compute_token_amount(df=df, str_dalaoaddress=str_dalaoaddress)
  152. df["timestamp"] = df["timestamp"].astype(int)
  153. df["token_idx"] = -1
  154. # df["TokenFirstTime"] = df.groupby("swap_tokenaddress")[
  155. # "timestamp"].transform("min")
  156. gettokenidx_df = df[["timestamp", "swap_tokenaddress"]].copy()
  157. gettokenidx_df = gettokenidx_df.sort_values(
  158. by=["timestamp"], ascending=[True])
  159. list_tokenaddress = gettokenidx_df["swap_tokenaddress"].drop_duplicates(
  160. ).tolist()
  161. for str_tokenaddress_idx in range(0, len(list_tokenaddress)):
  162. str_tokenaddress = list_tokenaddress[str_tokenaddress_idx]
  163. df.loc[df["swap_tokenaddress"] == str_tokenaddress,
  164. "token_idx"] = str_tokenaddress_idx+1
  165. df["dalaofirsttimestamp"] = df.groupby("token_idx")[
  166. "timestamp"].transform("min")
  167. df['dalaofirsttimestamp'] = pd.to_datetime(
  168. df['dalaofirsttimestamp'], unit='s')
  169. df = df.sort_values(by=["dalaofirsttimestamp", "token_idx", "timestamp"], ascending=[
  170. False, True, True]).reset_index(drop=True)
  171. df.to_csv(baseclass.dalao_configtoken_st_solanafm_path /
  172. f"configtoken_{str_dalaoaddress}.csv", index=False)
  173. success_address_list.remove(str_dalaoaddress)
  174. print(
  175. f"{str_dalaoaddress} success remainder_len={len(success_address_list)}\n", end='')
  176. batchSize = 170
  177. df = pd.read_csv(baseclass.dalao_merge_path /
  178. "filter_dalao.csv", dtype=object)
  179. arr_str_dalaoaddress = df["dalaoAddress"].tolist()
  180. print('arr_str_dalaoaddress', len(arr_str_dalaoaddress))
  181. get_mul_configtoken_st()
  182. print(f"{'{:<6}'.format('END')} {baseclass.scriptfilename} ----------------NOTE-----------NOTE---------------")