007_get_configtoken_st_solanafm.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. is_raydium =False
  19. # 保留与raudium 有关的token 的transfer 即只参与了swap的token
  20. raydium_mask = ((gdf["source"] == Raydium_Authority_V4) | (
  21. gdf["destination"] == Raydium_Authority_V4))
  22. filter_dalao_mask = ((gdf["source"] == str_dalaoaddress) | (
  23. gdf["destination"] == str_dalaoaddress))
  24. if raydium_mask.any():
  25. is_raydium =True
  26. gdf["platform"]="raydium"
  27. arr_raydium_token =None
  28. arr_dalao_token =None
  29. if is_raydium:
  30. arr_raydium_token = gdf[raydium_mask]["token"].drop_duplicates().tolist()
  31. gdf = gdf[gdf["token"].isin(arr_raydium_token)]
  32. else:
  33. arr_dalao_token = gdf[filter_dalao_mask]["token"].drop_duplicates().tolist()
  34. gdf = gdf[gdf["token"].isin(arr_dalao_token)]
  35. if not is_raydium:
  36. dalao_intoken_mask = gdf["destination"] == str_dalaoaddress
  37. dalao_outtoken_mask = gdf["source"] == str_dalaoaddress
  38. arr_dalao_intoken = gdf[dalao_intoken_mask]["token"].drop_duplicates(
  39. ).tolist()
  40. arr_dalao_outtoken = gdf[dalao_outtoken_mask]["token"].drop_duplicates(
  41. ).tolist()
  42. if (len(arr_dalao_intoken) == 0 or len(arr_dalao_outtoken) == 0):
  43. # dalao 没有token in 或者out 有问题 此sign 不是swap
  44. return None
  45. arr_dalao_token = list(set(arr_dalao_intoken) | set(arr_dalao_outtoken))
  46. if (len(arr_dalao_token) != 2):
  47. # dalao 的token数目不对 除了 sol token 还有其他的token 不是我想要的sign return
  48. return None
  49. if WSOL_Token not in arr_dalao_token:
  50. # 没有sol 不是我想要的
  51. return None
  52. arr_dalao_token.remove(WSOL_Token)
  53. # token in ,wsol out 或者 token out ,wsol in
  54. str_token_address = arr_dalao_token[0]
  55. # if str_token_address in arr_dalao_intoken and
  56. if WSOL_Token in arr_dalao_intoken:
  57. arr_dalao_intoken.remove(WSOL_Token)
  58. if WSOL_Token in arr_dalao_outtoken:
  59. arr_dalao_outtoken.remove(WSOL_Token)
  60. # if str_token_address in arr_dalao_intoken and
  61. if len(arr_dalao_intoken) +len(arr_dalao_outtoken)!=1:
  62. return None
  63. if str_token_address in arr_dalao_outtoken:
  64. gdf["swap_type"] = "sell"
  65. else:
  66. gdf["swap_type"] = "buy"
  67. gdf["swap_tokenaddress"] = str_token_address
  68. else:
  69. raydium_intoken_mask = gdf["destination"] == Raydium_Authority_V4
  70. raydium_outtoken_mask = gdf["source"] == Raydium_Authority_V4
  71. arr_raydium_intoken = gdf[raydium_intoken_mask]["token"].drop_duplicates(
  72. ).tolist()
  73. arr_raydium_outtoken = gdf[raydium_outtoken_mask]["token"].drop_duplicates(
  74. ).tolist()
  75. if (len(arr_raydium_intoken) == 0 or len(arr_raydium_outtoken) == 0):
  76. # raydium 没有token in 或者out 有问题 此sign 不是swap
  77. return None
  78. arr_raydiumtoken = list(set(arr_raydium_intoken) | set(arr_raydium_outtoken))
  79. if (len(arr_raydiumtoken) != 2):
  80. # raydium 的token数目不对 除了 sol token 还有其他的token 不是我想要的sign return
  81. return None
  82. if WSOL_Token not in arr_raydiumtoken:
  83. # 没有sol 不是我想要的
  84. return None
  85. arr_raydiumtoken.remove(WSOL_Token)
  86. # token in ,wsol out 或者 token out ,wsol in
  87. str_token_address = arr_raydiumtoken[0]
  88. # if str_token_address in arr_raydiumintoken and
  89. if WSOL_Token in arr_raydium_intoken:
  90. arr_raydium_intoken.remove(WSOL_Token)
  91. if WSOL_Token in arr_raydium_outtoken:
  92. arr_raydium_outtoken.remove(WSOL_Token)
  93. # if str_token_address in arr_raydiumintoken and
  94. if len(arr_raydium_intoken) +len(arr_raydium_outtoken)!=1:
  95. return None
  96. if str_token_address in arr_raydium_outtoken:
  97. gdf["swap_type"] = "buy"
  98. else:
  99. gdf["swap_type"] = "sell"
  100. gdf["swap_tokenaddress"] = str_token_address
  101. return gdf
  102. def setswaptoken(gdf, str_dalaoaddress):
  103. arr_dalao_token =gdf["token"].drop_duplicate().tolist()
  104. # dalao_intoken_mask = gdf["destination"] == str_dalaoaddress
  105. # dalao_outtoken_mask = gdf["source"] == str_dalaoaddress
  106. # arr_dalao_intoken = gdf[dalao_intoken_mask]["token"].drop_duplicates(
  107. # ).tolist()
  108. # arr_dalao_outtoken = gdf[dalao_outtoken_mask]["token"].drop_duplicates(
  109. # ).tolist()
  110. # arr_dalao_token = list(set(arr_dalao_intoken) | set(arr_dalao_outtoken))
  111. arr_dalao_token.remove(WSOL_Token)
  112. swapTokenAddress = arr_dalao_token[0]
  113. gdf["swap_tokenaddress"] = swapTokenAddress
  114. return gdf
  115. pow_ten_list = [
  116. '1', '10', '100', '1000',
  117. '10000', '100000', '1000000',
  118. '10000000', '100000000', '1000000000',
  119. '10000000000', '100000000000', '1000000000000',
  120. '10000000000000', '100000000000000', '1000000000000000',
  121. '10000000000000000', '100000000000000000', '1000000000000000000',
  122. '10000000000000000000', '100000000000000000000', '1000000000000000000000',
  123. '10000000000000000000000', '100000000000000000000000', '1000000000000000000000000'
  124. ]
  125. # swap_ethamount swap_tokenamount
  126. def compute_eth_amount_onerow(row, str_dalaoaddress):
  127. row = row.copy()
  128. if (row["token"] != WSOL_Token):
  129. return row
  130. amount = decimal.Decimal(row['amount'])
  131. row['amount'] = '{0:.4f}'.format(
  132. amount / decimal.Decimal(pow_ten_list[9]))
  133. platform = row["platform"]
  134. if platform=="raydium":
  135. if (row['source'] == Raydium_Authority_V4):
  136. row['swap_eth_amount'] = '{0:.4f}'.format(decimal.Decimal(
  137. row['swap_eth_amount']) + decimal.Decimal(row['amount']))
  138. if (row['destination'] == Raydium_Authority_V4):
  139. row['swap_eth_amount'] = '{0:.4f}'.format(decimal.Decimal(
  140. row['swap_eth_amount']) - decimal.Decimal(row['amount']))
  141. else:
  142. if (row['source'] == str_dalaoaddress):
  143. if row['swap_type']=="sell":
  144. # sell token 只关注 获取eth 不关注其他
  145. return row
  146. else:
  147. row['swap_eth_amount'] = '{0:.4f}'.format(decimal.Decimal(
  148. row['swap_eth_amount']) - decimal.Decimal(row['amount']))
  149. if (row['destination'] == str_dalaoaddress):
  150. row['swap_eth_amount'] = '{0:.4f}'.format(decimal.Decimal(
  151. row['swap_eth_amount']) + decimal.Decimal(row['amount']))
  152. return row
  153. # def compute_eth_amount_gdf(gdf ):
  154. # # 判断此hash 是buy 还是 sell
  155. # ,,,,
  156. # return
  157. def compute_eth_amount(df, str_dalaoaddress):
  158. # 將主流幣全部替換為wbnb,amount 以及 token 都替換,
  159. # 添加swap_ethamount 列
  160. # 计算每条hash 的 bnb 的value
  161. df["swap_eth_amount"]="0"
  162. # 待添加
  163. # df = df.groupby(by=["sign"], group_keys=False).apply(
  164. # lambda gdf: compute_eth_amount_gdf(gdf))
  165. df = df.apply(lambda row: compute_eth_amount_onerow(
  166. row, str_dalaoaddress), axis=1)
  167. return df
  168. def compute_token_amount_onerow(row, str_dalaoaddress):
  169. row = row.copy()
  170. if (row["token"] == WSOL_Token):
  171. return row
  172. amount = decimal.Decimal(row['amount'])
  173. row['amount'] = '{0:.0f}'.format(
  174. amount / decimal.Decimal(pow_ten_list[6]))
  175. platform = row["platform"]
  176. if platform=="raydium":
  177. if (row['source'] == Raydium_Authority_V4):
  178. row['swap_token_amount'] = '{0:.0f}'.format(decimal.Decimal(
  179. row['swap_token_amount']) + decimal.Decimal(row['amount']))
  180. if (row['destination'] == Raydium_Authority_V4):
  181. row['swap_token_amount'] = '{0:.0f}'.format(decimal.Decimal(
  182. row['swap_token_amount']) - decimal.Decimal(row['amount']))
  183. else:
  184. if (row['source'] == str_dalaoaddress):
  185. row['swap_token_amount'] = '{0:.0f}'.format(decimal.Decimal(
  186. row['swap_token_amount']) - decimal.Decimal(row['amount']))
  187. if (row['destination'] == str_dalaoaddress):
  188. row['swap_token_amount'] = '{0:.0f}'.format(decimal.Decimal(
  189. row['swap_token_amount']) + decimal.Decimal(row['amount']))
  190. return row
  191. def compute_token_amount(df, str_dalaoaddress):
  192. df = df.apply(lambda row: compute_token_amount_onerow(
  193. row, str_dalaoaddress), axis=1)
  194. return df
  195. def get_mul_configtoken_st():
  196. success_address_list = arr_str_dalaoaddress.copy()
  197. for idx in range(0, len(arr_str_dalaoaddress), batchSize):
  198. thread_list = []
  199. for cur_idx in range(idx, min(idx+batchSize, len(arr_str_dalaoaddress))):
  200. thread = threading.Thread(
  201. target=get_one_configtoken_st,
  202. args=(arr_str_dalaoaddress[cur_idx],
  203. success_address_list)
  204. )
  205. thread_list.append(thread)
  206. for thread in thread_list:
  207. thread.start()
  208. for thread in thread_list:
  209. thread.join()
  210. return
  211. def get_one_configtoken_st(str_dalaoaddress, success_address_list):
  212. print(f"enter str_dalaoaddress= {str_dalaoaddress}")
  213. if not (baseclass.dalao_transaction_st_solanafm_path /
  214. f"{str_dalaoaddress}.csv").exists():
  215. return
  216. df = pd.read_csv(baseclass.dalao_transaction_st_solanafm_path /
  217. f"{str_dalaoaddress}.csv", dtype=object)
  218. if (len(df) == 0):
  219. return
  220. # token可能为kong 代表sol相关 用wsol填充
  221. df["token"] = df["token"].fillna(WSOL_Token)
  222. prelen = len(df)
  223. df["platform"]="unknown"
  224. df["swap_type"]="unknow"
  225. df["swap_tokenaddress"] = ""
  226. df = df.groupby(by=["sign"], group_keys=False).apply(
  227. lambda gdf: checkgdfisswap(gdf, str_dalaoaddress))
  228. # 屏蔽pump 只考虑raydium
  229. # df =df[ df["platform"]=="raydium"].reset_index(drop=True)
  230. aftlen = len(df)
  231. if prelen != aftlen:
  232. print(f"{str_dalaoaddress} pre= {prelen} aft= {aftlen}")
  233. if aftlen == 0:
  234. return
  235. # df = df.groupby(by=["sign"], group_keys=False).apply(
  236. # lambda gdf: setswaptoken(gdf, str_dalaoaddress))
  237. df["swap_eth_amount"] = "0"
  238. df = compute_eth_amount(df=df, str_dalaoaddress=str_dalaoaddress)
  239. df["swap_token_amount"] = "0"
  240. df = compute_token_amount(df=df, str_dalaoaddress=str_dalaoaddress)
  241. df["timestamp"] = df["timestamp"].astype(int)
  242. df["token_idx"] = -1
  243. gettokenidx_df = df[["timestamp", "swap_tokenaddress"]].copy()
  244. gettokenidx_df = gettokenidx_df.sort_values(
  245. by=["timestamp"], ascending=[True])
  246. list_tokenaddress = gettokenidx_df["swap_tokenaddress"].drop_duplicates(
  247. ).tolist()
  248. for str_tokenaddress_idx in range(0, len(list_tokenaddress)):
  249. str_tokenaddress = list_tokenaddress[str_tokenaddress_idx]
  250. df.loc[df["swap_tokenaddress"] == str_tokenaddress,
  251. "token_idx"] = str_tokenaddress_idx+1
  252. df["dalaofirsttimestamp"] = df.groupby("token_idx")[
  253. "timestamp"].transform("min")
  254. df['dalaofirsttimestamp'] = pd.to_datetime(
  255. df['dalaofirsttimestamp'], unit='s')
  256. df = df.sort_values(by=["dalaofirsttimestamp", "token_idx", "timestamp"], ascending=[
  257. False, True, True]).reset_index(drop=True)
  258. df.to_csv(baseclass.dalao_configtoken_st_solanafm_path /
  259. f"configtoken_{str_dalaoaddress}.csv", index=False)
  260. success_address_list.remove(str_dalaoaddress)
  261. print(
  262. f"{str_dalaoaddress} success remainder_len={len(success_address_list)}\n", end='')
  263. batchSize = 170
  264. df = pd.read_csv(baseclass.dalao_merge_path /
  265. "filter_dalao.csv", dtype=object)
  266. arr_str_dalaoaddress = df["dalaoAddress"].tolist()
  267. print('arr_str_dalaoaddress', len(arr_str_dalaoaddress))
  268. # arr_str_dalaoaddress=arr_str_dalaoaddress[0:10]
  269. get_mul_configtoken_st()
  270. print(f"{'{:<6}'.format('END')} {baseclass.scriptfilename} ----------------NOTE-----------NOTE---------------")