Browse Source

json用pprint美化后輸出

skyfffire 5 months ago
parent
commit
3cf4f5b638
4 changed files with 52 additions and 32 deletions
  1. 47 27
      erc20_to_mexc.py
  2. 2 2
      monitor.html
  3. 2 2
      submit_process_demo.py
  4. 1 1
      toto.readme

+ 47 - 27
erc20_to_mexc.py

@@ -6,6 +6,7 @@ from mexc_client import MexcClient
 from decimal import Decimal, ROUND_DOWN
 from as_utils import add_state_flow_entry
 from checker.logger_config import get_logger
+from pprint import pformat
 
 web3 = EthClient()
 mexc = MexcClient()
@@ -280,10 +281,13 @@ class ArbitrageProcess:
                 "type": "MARKET",
                 "quantity": int(pseudo_amount_to_sell),
             }
-            logger.info(order_params)
+            order_params_formated = pformat(order_params, indent=2)
+            
             exchange_sell_order = mexc.trade.post_order(order_params)
+            exchange_sell_order_formated = pformat(exchange_sell_order, indent=2)
+
+            msg = f"交易所现货卖出订单已发送 \n params:{order_params_formated} \n rst: {exchange_sell_order_formated}"
             if 'orderId' not in exchange_sell_order:
-                msg = f"交易所现货卖出下单失败:{exchange_sell_order}, {order_params}"
                 logger.error(msg)
                 add_state_flow_entry(self.process_item, self.current_state, msg, "fail")
                 self._set_state(self.STATE_FAILED)
@@ -292,7 +296,6 @@ class ArbitrageProcess:
 
             self.exchange_sell_order_id = exchange_sell_order['orderId']
             
-            msg = f"交易所现货卖出订单已发送, 订单ID: {self.exchange_sell_order_id}"
             logger.info(msg)
             add_state_flow_entry(self.process_item, self.current_state, msg, "success")
 
@@ -314,10 +317,10 @@ class ArbitrageProcess:
         logger.info(msg)
         add_state_flow_entry(self.process_item, self.current_state, msg, "pending")
 
-        last_order = None
         try:
             # 查询交易所订单状态
             waiting_times = 30
+            last_order = None
             while waiting_times > 0:
                 params = {
                     "symbol": self.symbol.replace('_', ''),
@@ -333,7 +336,8 @@ class ArbitrageProcess:
                     self.sell_price = money / amount
                     self.sell_price = self.sell_price.quantize(Decimal('1e-8'), rounding=ROUND_DOWN)
 
-                    msg = f"交易所现货卖出订单已完成, 价格:{self.sell_price}, money: {money}, {order}"
+                    order_formated = pformat(order, indent=2)
+                    msg = f"交易所现货卖出订单已完成, 价格:{self.sell_price}, money: {money}\n order: {order_formated}"
                     logger.info(msg)
                     add_state_flow_entry(self.process_item, self.current_state, msg, "success")
 
@@ -345,7 +349,8 @@ class ArbitrageProcess:
                     time.sleep(1)
                     waiting_times = waiting_times - 1
 
-            msg = f"交易所现货卖出订单失敗, 最後狀態:{last_order}。"
+            last_order_formated = pformat(last_order, indent=2)
+            msg = f"交易所现货卖出订单失敗, 最後狀態:\n{last_order_formated}。"
             logger.info(msg)
             add_state_flow_entry(self.process_item, self.current_state, msg, "fail")
             self._set_state(self.STATE_FAILED)
@@ -441,7 +446,8 @@ class ArbitrageProcess:
 
                     break
 
-                msg = f"链上交易已确认。{tx_details}"
+                tx_details_formated = pformat(tx_details, indent=2)
+                msg = f"链上交易已确认。\n details: {tx_details_formated}"
                 logger.info(msg)
                 add_state_flow_entry(self.process_item, self.current_state, msg, "success")
 
@@ -483,10 +489,12 @@ class ArbitrageProcess:
                         "type": "MARKET",
                         "quantity": int(diff),
                     }
-                    logger.info(order_params)
+                    order_params_formated = pformat(order_params, indent=2)
                     exchange_sell_order = mexc.trade.post_order(order_params)
+                    exchange_sell_order_formated = pformat(exchange_sell_order, indent=2)
+
                     if 'orderId' not in exchange_sell_order:
-                        msg = f"交易所现货二卖下单失败:{exchange_sell_order}, {order_params}"
+                        msg = f"交易所现货二卖下单失败 \n params:{order_params_formated} \n rst: {exchange_sell_order_formated}"
                         logger.error(msg)
                         add_state_flow_entry(self.process_item, self.current_state, msg, "fail")
                     else:
@@ -494,18 +502,20 @@ class ArbitrageProcess:
 
                         # 查询交易所订单状态
                         waiting_times_inner = 30
+                        last_order = None
                         while waiting_times_inner > 0:
                             params = {
                                 "symbol": self.symbol.replace('_', ''),
                                 "orderId": oid
                             }
                             order = mexc.trade.get_order(params)
+                            order_formated = pformat(order, indent=2)
                             last_order = order
 
                             if order['status'] in ["FILLED", "PARTIALLY_CANCELED"]:
                                 money = Decimal(order['cummulativeQuoteQty'])
 
-                                msg = f"交易所现货二卖订单已完成。money: {money}"
+                                msg = f"交易所现货二卖订单已完成, money: {money}。\n order: {order_formated}"
                                 logger.info(msg)
                                 add_state_flow_entry(self.process_item, self.current_state, msg, "success")
 
@@ -517,7 +527,9 @@ class ArbitrageProcess:
                                 waiting_times_inner = waiting_times_inner - 1
 
                         if waiting_times_inner <= 0:
-                            msg = f"交易所现货二卖订单失敗, 最後狀態:{last_order}。"
+                            last_order_formated = pformat(last_order, indent=2)
+
+                            msg = f"交易所现货二卖订单失敗, 最後狀態:{last_order_formated}。"
                             logger.info(msg)
                             add_state_flow_entry(self.process_item, self.current_state, msg, "fail")     
                 else:
@@ -551,11 +563,8 @@ class ArbitrageProcess:
                 break
             
             # 如果300秒都沒確認成功,該交易大概率沒有上鏈
-            if waiting_times <= 0:
-                with self.pending_lock:
-                    tx_details = copy.deepcopy(self.pending_data[chain_tx_hash]['tx_details'])
-                
-                msg = f"链上交易确认失败:{chain_tx_hash}, {tx_details}"
+            if waiting_times <= 0:                
+                msg = f"链上交易确认失败:{chain_tx_hash}"
                 logger.error(msg)
                 add_state_flow_entry(self.process_item, self.current_state, msg, "fail")
                 self._set_state(self.STATE_WAITING_EXCHANGE_ROLLBACK)
@@ -604,10 +613,11 @@ class ArbitrageProcess:
                 "type": "MARKET",
                 "quoteOrderQty": int(pseudo_amount_to_buy),
             }
-            logger.info(order_params)
+            order_params_formated = pformat(order_params, indent=2)
             exchange_buy_order = mexc.trade.post_order(order_params)
+            exchange_buy_order_formated = pformat(exchange_buy_order, indent=2)
             if 'orderId' not in exchange_buy_order:
-                msg = f"【回滚】交易所现货买入下单失败:{exchange_buy_order}"
+                msg = f"【回滚】交易所现货买入下单失败\n params:{order_params_formated}\norder: {exchange_buy_order_formated}"
                 logger.error(msg)
                 add_state_flow_entry(self.process_item, self.current_state, msg, "fail")
                 self._set_state("FAILED")
@@ -629,6 +639,7 @@ class ArbitrageProcess:
                     "orderId": exchange_buy_order_id
                 }
                 order = mexc.trade.get_order(params)
+                order_formated = pformat(order, indent=2)
                 last_query_rst = order
 
                 if order['status'] == "FILLED":
@@ -637,7 +648,7 @@ class ArbitrageProcess:
                     price = money / amount
                     price = price.quantize(Decimal('1e-8'), rounding=ROUND_DOWN)
 
-                    msg = f"【回滚】交易所现货买入订单已完全成交, 价格:{price}。{order}"
+                    msg = f"【回滚】交易所现货买入订单已完全成交, 价格:{price}。\norder: {order_formated}"
 
                     logger.info(msg)
                     add_state_flow_entry(self.process_item, self.current_state, msg, "success")
@@ -651,7 +662,8 @@ class ArbitrageProcess:
                 time.sleep(1)
                 waiting_times = waiting_times - 1
             
-            msg = f"【回滚】回滚交易订单查询超时, 订单ID: {exchange_buy_order_id},最终状态:{last_query_rst}"
+            last_query_rst_formated = pformat(last_query_rst, indent=2)
+            msg = f"【回滚】回滚交易订单查询超时, 订单ID: {exchange_buy_order_id}\n最终状态:{last_query_rst_formated}"
             logger.info(msg)
             add_state_flow_entry(self.process_item, self.current_state, msg, "fail")
 
@@ -715,7 +727,8 @@ class ArbitrageProcess:
                     
                     # 最終判斷
                     if exchange_sell_amount + asset_balance > pending_amount:
-                        msg = f"【flash】资产可以進行快速提現。{last_deposit_state}"
+                        last_deposit_state_formated = pformat(last_deposit_state, indent=2)
+                        msg = f"【flash】资产可以進行快速提現。\n{last_deposit_state_formated}"
                         logger.info(msg)
                         add_state_flow_entry(self.process_item, self.current_state, msg, "success")
 
@@ -730,6 +743,7 @@ class ArbitrageProcess:
             # 最多等待30分钟
             waiting_times = 30
             last_deposit_state = None
+            last_deposit_state_formated = None
             while waiting_times > 0:
                 deposit_list = mexc.wallet.get_deposit_list()
                 for deposit in deposit_list:
@@ -737,13 +751,15 @@ class ArbitrageProcess:
                         continue
 
                     last_deposit_state = deposit
+                    last_deposit_state_formated = pformat(last_deposit_state, indent=2)
 
                     logger.info(f"等待资产在交易所到账...({deposit['confirmTimes']}/{deposit['unlockConfirm']})")
                     if Decimal(deposit['confirmTimes']) >= Decimal(deposit['unlockConfirm']):
                         is_arrived = True
 
                 if is_arrived:
-                    msg = f"资产已在交易所到账。{last_deposit_state}"
+                    
+                    msg = f"资产已在交易所到账。\n{last_deposit_state_formated}"
                     logger.info(msg)
                     add_state_flow_entry(self.process_item, self.current_state, msg, "success")
 
@@ -753,7 +769,7 @@ class ArbitrageProcess:
                 time.sleep(60)
                 waiting_times = waiting_times - 1
 
-            msg = f"等待充值到账超时(超过30分钟): {last_deposit_state}"
+            msg = f"等待充值到账超时(超过30分钟)。\n{last_deposit_state_formated}"
             logger.error(msg)
             add_state_flow_entry(self.process_item, self.current_state, msg, "fail")
 
@@ -784,16 +800,18 @@ class ArbitrageProcess:
                 'address': self.user_wallet,
                 'amount': pseudo_withdrawal_amount
             }
+            withdrawal_params_formated = pformat(withdrawal_params, indent=2)
             withdrawal_rst = mexc.wallet.post_withdraw(withdrawal_params)
+            withdrawal_rst_formated = pformat(withdrawal_rst, indent=2)
             if "id" not in withdrawal_rst:
-                msg = f"交易所提现失败\n參數: {withdrawal_params}\n響應: {withdrawal_rst}"
+                msg = f"交易所提现失败\n參數: {withdrawal_params_formated}\n響應: {withdrawal_rst_formated}"
                 logger.error(msg)
                 add_state_flow_entry(self.process_item, self.current_state, msg, "fail")
 
                 self._set_state(self.STATE_FAILED)
             else:
                 self.exchange_withdrawal_id = withdrawal_rst["id"]
-                msg = f"交易所提现已发送, 提现ID: {self.exchange_withdrawal_id}"
+                msg = f"交易所提现已发送\n{withdrawal_rst_formated}"
                 logger.info(msg)
                 add_state_flow_entry(self.process_item, self.current_state, msg, "success")
 
@@ -822,6 +840,7 @@ class ArbitrageProcess:
             # 最多等待30分钟
             waiting_times = 60
             last_deposit_state = None
+            last_deposit_state_formated = None
             while waiting_times > 0:
                 withdrawal_list = mexc.wallet.get_withdraw_list()
 
@@ -838,12 +857,13 @@ class ArbitrageProcess:
                         continue
 
                     last_deposit_state = withdrawal
+                    last_deposit_state_formated = pformat(last_deposit_state, indent=2)
 
                     if withdrawal['status'] == 7:
                         is_arrived = True
 
                 if is_arrived:
-                    msg = f"提现请求已上链: {last_deposit_state}"
+                    msg = f"提现请求已上链:\n{last_deposit_state_formated}"
                     logger.info(msg)
                     add_state_flow_entry(self.process_item, self.current_state, msg, "success")
 
@@ -853,7 +873,7 @@ class ArbitrageProcess:
                 time.sleep(30)
                 waiting_times = waiting_times - 1
 
-            msg = f"等待提现到账超时(超过30分钟): {last_deposit_state}"
+            msg = f"等待提现到账超时(超过30分钟):\n{last_deposit_state_formated}"
             logger.error(msg)
             add_state_flow_entry(self.process_item, self.current_state, msg, "fail")
 

+ 2 - 2
monitor.html

@@ -26,7 +26,7 @@
             white-space: pre-wrap;
             word-break: break-all;
             /* max-height is now set via inline style in JS for easier adjustment */
-            overflow-y: auto;
+            /* overflow-y: auto; */
             font-size: 0.82em; /* Slightly larger log font */
             line-height: 1.45; /* Increased line height for log messages */
             background-color: #f9fafb;
@@ -201,7 +201,7 @@
                         <td class="px-3 py-2 sm:px-4 whitespace-nowrap text-gray-700">${state.timestamp || 'N/A'}</td>
                         <td class="px-3 py-2 sm:px-4 text-gray-700">${state.stateName || 'N/A'}</td>
                         <td class="px-3 py-2 sm:px-4 text-gray-700">
-                            <div class="log-msg" style="max-height: 150px;">${state.msg || ''}</div>
+                            <div class="log-msg">${state.msg || ''}</div>
                         </td>
                         <td class="px-3 py-2 sm:px-4 ${getStatusTextClass(actualStatus)}">${actualStatus || 'N/A'}</td>
                     </tr>

+ 2 - 2
submit_process_demo.py

@@ -19,8 +19,8 @@ def create_mock_arbitrage_data():
     CHAIN_ID = 1
     IN_TOKEN_ADDRESS = '0xdAC17F958D2ee523a2206206994597C13D831ec7' # USDT on Ethereum
     IN_TOKEN_DECIMALS = 6
-    EXCHANGE_OUT_AMOUNT = Decimal(650000)
-    IN_AMOUNT_TO_QUERY = Decimal(8)
+    EXCHANGE_OUT_AMOUNT = Decimal(2300000)
+    IN_AMOUNT_TO_QUERY = Decimal(12)
     OUT_TOKEN_ADDRESS = '0xf816507E690f5Aa4E29d164885EB5fa7a5627860' # RATO on Ethereum
     USER_WALLET = '0xb1f33026Db86a86372493a3B124d7123e9045Bb4'
     USER_EXCHANGE_WALLET = '0xc71835a042F4d870B0F4296cc89cAeb921a9f3DA'

+ 1 - 1
toto.readme

@@ -11,7 +11,7 @@
 [-] 链上买入失败
 [-] 鏈上買入成功,但是okx返回的空狀態,詳見2025-6-5學費
 [-] 解密HASH進行鑒權
-[ ] json用pprint美化后輸出
+[-] json用pprint美化后輸出
 [ ] 查询交易所到账状态时发生错误:'balances'
 
 2025-06-07