Browse Source

一個新的狀態兼容。

skyfffire 5 months ago
parent
commit
dbaaf16489
5 changed files with 30 additions and 10 deletions
  1. 3 1
      2025-6-5學費.txt
  2. 15 1
      as.py
  3. 6 2
      erc20_to_mexc.py
  4. 1 1
      submit_process_demo.py
  5. 5 5
      web3_py_client.py

+ 3 - 1
2025-6-5學費.txt

@@ -4,4 +4,6 @@
 
 链上交易失敗。{'chainId': '1', 'chainIndex': '1', 'errorMsg': 'execution reverted-Min return not reached', 'fromAddress': '0xc36b5466d88d3ebe9e538ed650f61b7f9902e6cc', 'gasLimit': '285000', 'gasPrice': '3776039664', 'gasUsed': '258617', 'height': '22641798', 'status': 'fail', 'txFee': '', 'txHash': '0x55262bc84dbc970a44866280964d61443b49317a126583ac20260195268a5fd1', 'txTime': '1749167483', 'txType': ''}
 
-链上交易已确认。{'chainId': '1', 'chainIndex': '1', 'dexRouter': '0x6088d94c5a40cecd3ae2d4e0710ca687b91c61d0', 'errorMsg': '', 'fromAddress': '0xc36b5466d88d3ebe9e538ed650f61b7f9902e6cc', 'fromTokenDetails': {'amount': '287100000', 'symbol': 'USDT', 'tokenAddress': '0xdac17f958d2ee523a2206206994597c13d831ec7'}, 'gasLimit': '285000', 'gasPrice': '7638035446', 'gasUsed': '218819', 'height': '22640410', 'referralAmount': '', 'status': 'success', 'toAddress': '0xd493ddc634928546e0ac9740ad542bc6eeeb6c25', 'toTokenDetails': {'amount': '45926476656820689', 'symbol': 'RATO', 'tokenAddress': '0xf816507e690f5aa4e29d164885eb5fa7a5627860'}, 'txFee': '', 'txHash': '0x2d0fe49837c201d54bd4996bf82769b3fabee8650907096bbf47fa1acd26e025', 'txTime': '1749150683', 'txType': 'swap'}
+链上交易已确认。{'chainId': '1', 'chainIndex': '1', 'dexRouter': '0x6088d94c5a40cecd3ae2d4e0710ca687b91c61d0', 'errorMsg': '', 'fromAddress': '0xc36b5466d88d3ebe9e538ed650f61b7f9902e6cc', 'fromTokenDetails': {'amount': '287100000', 'symbol': 'USDT', 'tokenAddress': '0xdac17f958d2ee523a2206206994597c13d831ec7'}, 'gasLimit': '285000', 'gasPrice': '7638035446', 'gasUsed': '218819', 'height': '22640410', 'referralAmount': '', 'status': 'success', 'toAddress': '0xd493ddc634928546e0ac9740ad542bc6eeeb6c25', 'toTokenDetails': {'amount': '45926476656820689', 'symbol': 'RATO', 'tokenAddress': '0xf816507e690f5aa4e29d164885eb5fa7a5627860'}, 'txFee': '', 'txHash': '0x2d0fe49837c201d54bd4996bf82769b3fabee8650907096bbf47fa1acd26e025', 'txTime': '1749150683', 'txType': 'swap'}
+
+链上交易已确认。{'chainId': '1', 'chainIndex': '1', 'dexRouter': '0x6088d94c5a40cecd3ae2d4e0710ca687b91c61d0', 'errorMsg': '', 'fromAddress': '0xc36b5466d88d3ebe9e538ed650f61b7f9902e6cc', 'fromTokenDetails': None, 'gasLimit': '558000', 'gasPrice': '2950534070', 'gasUsed': '390627', 'height': '22642421', 'referralAmount': '', 'status': 'success', 'toAddress': '0xd493ddc634928546e0ac9740ad542bc6eeeb6c25', 'toTokenDetails': None, 'txFee': '', 'txHash': '0x6d6a3dec8527f696b69752dc76e8fc32218dd7a3a71ff3f79ac8066e5265fee9', 'txTime': '1749174983', 'txType': 'swap'}

+ 15 - 1
as.py

@@ -65,6 +65,7 @@ pending_data = {
     # "0xaf181bbbf5bf56d9204bd18cd25abd90e51890e848525e7788b410689c0c26a4": {
     #     "block_number": 22570370,                   # 提交pending時的區塊,隔幾個區塊去獲取數據會更準確
     #     "tx_details": None,                         # okapi解析的數據, None就是還沒有獲取到
+    #     "reponse": None,                            # okapi最後一次獲取到的數據
     # },
 }
 pending_lock = threading.Lock()
@@ -286,6 +287,11 @@ def update_tx_data():
 
                     # 調用ok的api,直接獲取詳細交易
                     ok_rst = ok_chain_client.history(CHAIN_ID, tx)
+                    # 存儲最後一次獲取的細節
+                    with pending_lock:
+                        pending_data[tx]['response'] = ok_rst
+
+                    # 錯誤響應
                     if ok_rst['code'] != '0':
                         raise RuntimeError("API 返回错误响应", ok_rst)
                     
@@ -295,10 +301,18 @@ def update_tx_data():
                         time.sleep(1)
 
                         continue
+                    
+                    details = ok_rst['data']
+                    # 有時候不會馬上識別出成交數量
+                    if details['fromTokenDetails'] is None or details['toTokenDetails']:
+                        # 每一個之間等待1s
+                        time.sleep(1)
+
+                        continue
 
                     # 成功獲取之後直接調用更新
                     with pending_lock:
-                        pending_data[tx]['tx_details'] = ok_rst['data']
+                        pending_data[tx]['tx_details'] = details
 
                         formated_data = pformat(ok_rst['data'], indent=2) # indent=2 让格式更整齐
                         logger.info(f"獲取成功: \n{formated_data}")

+ 6 - 2
erc20_to_mexc.py

@@ -394,6 +394,7 @@ class ArbitrageProcess:
                 self.pending_data[self.chain_tx_hash] = {
                     "block_number": block_number,
                     "tx_details": None,
+                    "reponse": None,
                 }
 
             # 交易成功
@@ -563,8 +564,11 @@ class ArbitrageProcess:
                 break
             
             # 如果300秒都沒確認成功,該交易大概率沒有上鏈
-            if waiting_times <= 0:                
-                msg = f"链上交易确认失败:{chain_tx_hash}"
+            if waiting_times <= 0:
+                with self.pending_lock:
+                    response = copy.deepcopy(self.pending_data[chain_tx_hash]['response'])
+                response_formated = pformat(reponse, indent=2)
+                msg = f"链上交易确认失败:{chain_tx_hash}\n{response_formated}"
                 logger.error(msg)
                 add_state_flow_entry(self.process_item, self.current_state, msg, "fail")
                 self._set_state(self.STATE_WAITING_EXCHANGE_ROLLBACK)

+ 1 - 1
submit_process_demo.py

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

+ 5 - 5
web3_py_client.py

@@ -456,7 +456,7 @@ if __name__ == "__main__":
             'msg': ''
         }
         '''
-        # pprint(ok_chain_client.history(CHAIN_ID, '0x344d2d0a9efdfb46b6130a58a40e117bb3bf6181b03b09b65b4d6cc8256f1e2f'))
+        pprint(ok_chain_client.history(CHAIN_ID, '0x6d6a3dec8527f696b69752dc76e8fc32218dd7a3a71ff3f79ac8066e5265fee9'))
 
         # pprint(ok_chain_client.gas_limit(CHAIN_ID, tx['from'], tx['to'], tx['value']))
 
@@ -474,10 +474,10 @@ if __name__ == "__main__":
 
 
         # ok api發交易測試
-        signed_tx = client._sign(tx)
-        pprint(signed_tx)
-        tx_hash = client.w3.to_hex(signed_tx.hash)
-        pprint(tx_hash)
+        # signed_tx = client._sign(tx)
+        # pprint(signed_tx)
+        # tx_hash = client.w3.to_hex(signed_tx.hash)
+        # pprint(tx_hash)
         # client.w3.eth.send_raw_transaction(signed_tx.raw_transaction)
         # raw_transaction = client.w3.to_hex(signed_tx.raw_transaction)
         # broadcast_rst = ok_chain_client.broadcast(CHAIN_ID, USER_WALLET, raw_transaction)