Przeglądaj źródła

成本統計,計算純利潤

skyfffire 5 miesięcy temu
rodzic
commit
41c9401574
4 zmienionych plików z 34 dodań i 8 usunięć
  1. 2 2
      as.py
  2. 26 2
      erc20_to_mexc.py
  3. 1 1
      submit_process_demo.py
  4. 5 3
      toto.readme

+ 2 - 2
as.py

@@ -9,7 +9,7 @@ import threading
 import uuid # 用于生成唯一的流程ID
 import time
 import logging
-import erc20_to_mexc_first_sell
+import erc20_to_mexc
 import web3_py_client
 import traceback
 import copy
@@ -166,7 +166,7 @@ def arbitrage_process_flow(process_item):
     global core_lock
     global pending_data
     global pending_lock
-    ap = erc20_to_mexc_first_sell.ArbitrageProcess(tx, gas_limit_multiplier, gas_price_multiplier, process_item, 
+    ap = erc20_to_mexc.ArbitrageProcess(tx, gas_limit_multiplier, gas_price_multiplier, process_item, 
         core_data, core_lock,
         pending_data, pending_lock,
     )

+ 26 - 2
erc20_to_mexc_first_sell.py → erc20_to_mexc.py

@@ -75,6 +75,7 @@ class ArbitrageProcess:
         self.exchange_sell_order_id = None                                      # 交易所卖出id
         self.exchange_withdrawal_id = None                                      # 交易所提现id
         self.exchange_withdrawal_amount = None                                  # 交易所提现数量
+        self.actual_profit = Decimal(0)                                         # 實際利潤
 
         # 定义可能的状态
         self.STATES = [
@@ -208,12 +209,12 @@ class ArbitrageProcess:
 
             gas_price = Decimal(self.tx['maxPriorityFeePerGas'] + self.tx['maxFeePerGas'])
             gas_price_gwei = gas_price / Decimal('1e9')
-            gas_price_gwei = gas_price_gwei.quantize(Decimal('1e-4'), rounding=ROUND_DOWN)
+            gas_price_gwei = gas_price_gwei.quantize(Decimal('1e-9'), rounding=ROUND_DOWN)
 
             estimated_gas_origin = web3.w3.eth.estimate_gas(self.tx)
             estimated_gas = int(estimated_gas_origin * self.gas_limit_multiplier)
             estimated_wei = Decimal(estimated_gas) * gas_price
-            estimated_eth = Decimal(estimated_wei / (10 ** 18)) / Decimal(2)        # 除以2才是比較接近正常消耗的gas費,否則會過於高估
+            estimated_eth = Decimal(estimated_wei / Decimal('1e18')) / Decimal(2)        # 除以2才是比較接近正常消耗的gas費,否則會過於高估
             estimated_eth = estimated_eth.quantize(Decimal('1e-8'), rounding=ROUND_DOWN)
 
             msg = f"估算的燃气量: {estimated_gas}, eth消耗: {estimated_eth}, gas price: {gas_price_gwei} gwei, gas估算通過"
@@ -455,6 +456,7 @@ class ArbitrageProcess:
                 self.buy_price = from_token_amount_human / to_token_amount_human
                 self.buy_price = self.buy_price.quantize(Decimal('1e-8'), rounding=ROUND_DOWN)               
 
+                # 交易預估利潤百分比計算
                 rate = self.sell_price / self.buy_price
                 rate = rate.quantize(Decimal('1e-4'), rounding=ROUND_DOWN)
 
@@ -465,6 +467,7 @@ class ArbitrageProcess:
                 # 判斷快速二賣條件
                 diff = int(to_token_amount_human - self.exchange_sell_amount)
                 value = diff * self.sell_price
+                value = value.quantize(Decimal('1e-4'), rounding=ROUND_DOWN)
                 if value > 2:
                     msg = f"滿足二賣條件,{diff}*{self.sell_price} = {value}"
                     logger.info(msg)
@@ -518,6 +521,27 @@ class ArbitrageProcess:
                     logger.info(msg)
                     add_state_flow_entry(self.process_item, self.current_state, msg, "fail")
 
+                # 計算實際利潤
+                actual_profit = value
+                actual_gas_price = Decimal(tx_details['gasPrice'])
+                actual_gas_price_gwei = actual_gas_price / Decimal('1e9')
+                actual_gas_price_gwei = actual_gas_price_gwei.quantize(Decimal('1e-9'), rounding=ROUND_DOWN)
+                actual_gas_used = Decimal(tx_details['gasUsed'])
+                actual_wei = actual_gas_price * actual_gas_used
+                actual_eth = actual_wei / Decimal('1e18')
+                actual_eth = actual_eth.quantize(Decimal('1e-8'), rounding=ROUND_DOWN)
+                actual_fee_used = actual_eth * self.eth_price
+                actual_fee_used = actual_fee_used.quantize(Decimal('1e-4'), rounding=ROUND_DOWN)
+
+                actual_profit = value - actual_fee_used - self.WITHDRAW_FEE
+
+                msg = f"【最終利潤】{actual_profit}{self.base_coin}(已扣除所有手續費、滑點)\
+                \n鏈上ETH使用: {actual_eth}({actual_fee_used} USD), gas_price: {actual_gas_price_gwei} GWEI, gas_used: {actual_gas_used}\
+                \n交易所出售代幣利潤: {value}, 提現手續費: {self.WITHDRAW_FEE}\
+                "
+                logger.info(msg)
+                add_state_flow_entry(self.process_item, self.current_state, msg, "success")
+
                 self._set_state(self.STATE_WAITING_TRANSFER_ARRIVE)
 
                 break

+ 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(850000)
+    EXCHANGE_OUT_AMOUNT = Decimal(650000)
     IN_AMOUNT_TO_QUERY = Decimal(8)
     OUT_TOKEN_ADDRESS = '0xf816507E690f5Aa4E29d164885EB5fa7a5627860' # RATO on Ethereum
     USER_WALLET = '0xb1f33026Db86a86372493a3B124d7123e9045Bb4'

+ 5 - 3
toto.readme

@@ -5,11 +5,13 @@
 2025-06-04
 [-] 緊急檢修ok api超頻問題
 [-] 日志輸出到文件
-[ ] 成本統計,計算純利潤
+[-] 成本統計,計算純利潤
 [ ] 解密HASH進行鑒權
-[ ] json用pprint美化后輸出
-[ ] 查询交易所到账状态时发生错误:'balances'
 
 2025-06-05
+[ ] json用pprint美化后輸出
+[ ] 查询交易所到账状态时发生错误:'balances'
 [ ] 做另一個方向之前,需要先整理策略層架構,當前架構如何兼容多策略
+
+2025-06-07
 [ ] 另一個方向