Browse Source

一些調整。

skyfffire 5 months ago
parent
commit
1b49403506
4 changed files with 94 additions and 48 deletions
  1. 6 2
      arbitrage_process.py
  2. 41 1
      as.py
  3. 0 0
      monitor.html
  4. 47 45
      price_checker_ok.py

+ 6 - 2
arbitrage_process.py

@@ -159,10 +159,14 @@ class ArbitrageProcess:
             self._wait_transfer_arrive()
 
         elif self.current_state == "COMPLETED":
-            logging.info("套利流程成功完成!")
+            msg = "套利流程成功完成!"
+            logging.info(msg)
+            add_state_flow_entry(self.process_item, self.current_state, msg, "success")
 
         elif self.current_state == "FAILED":
-            logging.error("套利流程失败!")
+            msg = "套利流程失败!"
+            logging.info(msg)
+            add_state_flow_entry(self.process_item, self.current_state, msg, "fail")
 
     # 以下是每个状态对应的具体执行函数(伪代码)
 

+ 41 - 1
arbitrage_system.py → as.py

@@ -52,12 +52,50 @@ except Exception as e:
 app = Flask(__name__)
 CORS(app) # 在创建 app 实例后启用 CORS
 
+def move_completed_process_to_history(process_id_to_move: str) -> bool:
+    """
+    将一个完成的 process_item 从 processing_list 移动到 history_process_list。
+    此操作是线程安全的。
+
+    Args:
+        process_id_to_move (str): 要移动的 process_item 的 ID。
+
+    Returns:
+        bool: 如果成功找到并移动了 item,则返回 True,否则返回 False。
+    """
+    global processing_list, history_process_list # 因为我们要修改这两个列表
+    
+    item_to_move = None
+    moved_successfully = False
+
+    with list_lock:
+        # 查找并从 processing_list 中移除
+        found_index = -1
+        for i, item in enumerate(processing_list):
+            if item.get('id') == process_id_to_move:
+                found_index = i
+                break
+        
+        if found_index != -1:
+            item_to_move = processing_list.pop(found_index) # 从 processing_list 中移除并获取它
+            
+            # 假设在 item_to_move 中,其 currentState 已经被 arbitrage_process_flow 更新为 COMPLETED 或 FAILED
+            arbitrage_process.add_state_flow_entry(item_to_move, "MOVED_TO_HISTORY", f"流程处理完毕,移至历史记录。最终状态: {item_to_move.get('currentState', 'N/A')}", "info")
+            
+            history_process_list.append(item_to_move) # 添加到 history_process_list
+            logging.info(f"已将 process_id: {process_id_to_move} 从 processing_list 移动到 history_process_list。")
+            moved_successfully = True
+        else:
+            logging.warning(f"尝试移动到 history_list 时,在 processing_list 中未找到 process_id: {process_id_to_move}")
+            
+    return moved_successfully
+
 def arbitrage_process_flow(process_item):
     """
     在单独线程中执行的实际套利逻辑。
     会直接修改 'process_item' 字典。
     """
-    # process_id = process_item['id']
+    process_id = process_item['id']
     SYMBOL = process_item['symbol']
     tx = process_item['tx'] # 预期包含 'rawTransaction' (原始交易)
     FROM_TOKEN = process_item['fromToken']
@@ -101,6 +139,8 @@ def arbitrage_process_flow(process_item):
         logging.info("套利流程执行成功!")
     else:
         logging.info("套利流程执行失败!")
+
+    move_completed_process_to_history(process_id)
     
 
 @app.route('/submit_process', methods=['POST'])

+ 0 - 0
arbitrage_monitor.html → monitor.html


+ 47 - 45
price_checker_ok.py

@@ -98,7 +98,9 @@ data_lock = threading.Lock()
 
 def calculate_percentage_diff(price_a, price_b):
     if price_a is not None and price_b is not None and isinstance(price_a, decimal.Decimal) and isinstance(price_b, decimal.Decimal) and price_b != 0:
-        return (price_a - price_b) / price_b
+        rst = (price_a - price_b) / price_b
+        rst = rst.quantize(decimal.Decimal('1e-4'), rounding=decimal.ROUND_DOWN)
+        return rst
     return None
 
 def send_arb_msg(profit, data):
@@ -110,50 +112,50 @@ def send_arb_msg(profit, data):
     human_in_base = atomic_in_base / (10 ** in_dec)
     human_out_target = atomic_out_target / (10 ** out_dec)
 
-    # arbitrage_data = {
-    #     "tx": tx,
-    #     "profit": str(profit),
-    #     "profitLimit": str(PROFIT_LIMIT),
-    #     "symbol": MEXC_TARGET_PAIR_USDT,
-    #     "fromToken": IN_TOKEN_ADDRESS,
-    #     "fromTokenAmountHuman": str(human_in_base),
-    #     "fromTokenDecimal": IN_TOKEN_DECIMALS,                  
-    #     "toToken": OUT_TOKEN_ADDRESS,
-    #     "toTokenAmountHuman": str(human_out_target)
-    # }
-
-    # """
-    # 向 arb_executor 服务提交套利处理请求。
-    # """
-    # print(f"正在提交套利数据到 {ARB_EXECUTOR_URL}")
-
-    # try:
-    #     # 发送 POST 请求
-    #     response = requests.post(ARB_EXECUTOR_URL, json=arbitrage_data)
-
-    #     # 检查响应状态码
-    #     if response.status_code == 201:
-    #         print("\n请求成功! 套利流程已启动。")
-    #     elif response.status_code == 200:
-    #          print("\n请求接收成功,但未达到利润阈值,未启动套利流程。")
-    #     elif response.status_code == 400:
-    #          print("\n请求失败! 无效的请求数据。")
-    #     else:
-    #         print(f"\n请求失败! 状态码: {response.status_code}")
-
-    #     # 打印响应体
-    #     try:
-    #         print("响应体:")
-    #         print(json.dumps(response.json(), indent=4))
-    #     except json.JSONDecodeError:
-    #         print("响应体不是有效的 JSON:")
-    #         print(response.text)
-
-    # except requests.exceptions.ConnectionError as e:
-    #     print(f"\n连接错误: 无法连接到 {ARB_EXECUTOR_URL}。请确保 arb_executor.py 正在运行。")
-    #     print(f"错误详情: {e}")
-    # except Exception as e:
-    #     print(f"\n发送请求时发生未知错误: {e}")
+    """
+    向 arb_executor 服务提交套利处理请求。
+    """
+    arbitrage_data = {
+        "tx": tx,
+        "profit": str(profit),
+        "profitLimit": str(PROFIT_LIMIT),
+        "symbol": MEXC_TARGET_PAIR_USDT,
+        "fromToken": IN_TOKEN_ADDRESS,
+        "fromTokenAmountHuman": str(human_in_base),
+        "fromTokenDecimal": IN_TOKEN_DECIMALS,                  
+        "toToken": OUT_TOKEN_ADDRESS,
+        "toTokenAmountHuman": str(human_out_target)
+    }
+
+    print(f"正在提交套利数据到 {ARB_EXECUTOR_URL}")
+
+    try:
+        # 发送 POST 请求
+        response = requests.post(ARB_EXECUTOR_URL, json=arbitrage_data)
+
+        # 检查响应状态码
+        if response.status_code == 201:
+            print("\n请求成功! 套利流程已启动。")
+        elif response.status_code == 200:
+             print("\n请求接收成功,但未达到利润阈值,未启动套利流程。")
+        elif response.status_code == 400:
+             print("\n请求失败! 无效的请求数据。")
+        else:
+            print(f"\n请求失败! 状态码: {response.status_code}")
+
+        # 打印响应体
+        try:
+            print("响应体:")
+            print(json.dumps(response.json(), indent=4))
+        except json.JSONDecodeError:
+            print("响应体不是有效的 JSON:")
+            print(response.text)
+
+    except requests.exceptions.ConnectionError as e:
+        print(f"\n连接错误: 无法连接到 {ARB_EXECUTOR_URL}。请确保 arb_executor.py 正在运行。")
+        print(f"错误详情: {e}")
+    except Exception as e:
+        print(f"\n发送请求时发生未知错误: {e}")
 
     logging.info(f"套利消息已发送, {profit}, {human_in_base}, {human_out_target}")
     pass