|
|
@@ -1,5 +1,6 @@
|
|
|
import time
|
|
|
import logging
|
|
|
+import traceback
|
|
|
from web3_py_client import EthClient
|
|
|
from mexc_client import MexcClient
|
|
|
from decimal import Decimal, ROUND_DOWN
|
|
|
@@ -198,18 +199,22 @@ class ArbitrageProcess:
|
|
|
self.tx['maxPriorityFeePerGas'] = int(int(self.tx['maxPriorityFeePerGas']) * self.gas_price_multiplier)
|
|
|
self.tx['maxFeePerGas'] = int(int(latest_block['baseFeePerGas']) * 2 + self.tx['maxPriorityFeePerGas'])
|
|
|
|
|
|
+ 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)
|
|
|
+
|
|
|
estimated_gas_origin = web3.w3.eth.estimate_gas(self.tx)
|
|
|
estimated_gas = int(estimated_gas_origin * self.gas_limit_multiplier)
|
|
|
- estimated_wei = estimated_gas * (self.tx['maxPriorityFeePerGas'] + self.tx['maxFeePerGas'])
|
|
|
- estimated_eth = Decimal(estimated_wei / (10 ** 18))
|
|
|
+ estimated_wei = Decimal(estimated_gas) * gas_price
|
|
|
+ estimated_eth = Decimal(estimated_wei / (10 ** 18)) / Decimal(2) # 除以2才是比較接近正常消耗的gas費,否則會過於高估
|
|
|
estimated_eth = estimated_eth.quantize(Decimal('1e-8'), rounding=ROUND_DOWN)
|
|
|
|
|
|
- msg = f"估算的燃气量: {estimated_gas}, eth消耗: {estimated_eth},gas估算通過"
|
|
|
+ msg = f"估算的燃气量: {estimated_gas}, eth消耗: {estimated_eth}, gas price: {gas_price_gwei} gwei, gas估算通過"
|
|
|
logging.info(msg)
|
|
|
add_state_flow_entry(self.process_item, self.current_state, msg, "success")
|
|
|
|
|
|
# step3, 費用與利潤比較
|
|
|
- estimated_eth_value = (estimated_eth / Decimal(2)) * self.eth_price
|
|
|
+ estimated_eth_value = estimated_eth * self.eth_price
|
|
|
estimated_eth_value = estimated_eth_value.quantize(Decimal('1e-2'), rounding=ROUND_DOWN)
|
|
|
cost = estimated_eth_value + self.WITHDRAW_FEE # 成本
|
|
|
if self.profit < cost:
|
|
|
@@ -245,6 +250,8 @@ class ArbitrageProcess:
|
|
|
add_state_flow_entry(self.process_item, self.current_state, msg, "fail")
|
|
|
self._set_state(self.STATE_REJECT)
|
|
|
|
|
|
+ traceback.print_exc()
|
|
|
+
|
|
|
# 以下是每个状态对应的具体执行函数
|
|
|
def _execute_sell_on_exchange(self):
|
|
|
"""
|
|
|
@@ -288,6 +295,8 @@ class ArbitrageProcess:
|
|
|
add_state_flow_entry(self.process_item, self.current_state, msg, "fail")
|
|
|
self._set_state(self.STATE_FAILED)
|
|
|
|
|
|
+ traceback.print_exc()
|
|
|
+
|
|
|
def _wait_sell_confirm(self):
|
|
|
"""
|
|
|
等待交易所现货卖出订单确认(完全成交)
|
|
|
@@ -331,12 +340,14 @@ class ArbitrageProcess:
|
|
|
msg = f"交易所现货卖出订单失敗, 最後狀態:{last_order}。"
|
|
|
logging.info(msg)
|
|
|
add_state_flow_entry(self.process_item, self.current_state, msg, "fail")
|
|
|
- self._set_state(self.FAILED)
|
|
|
+ self._set_state(self.STATE_FAILED)
|
|
|
except Exception as e:
|
|
|
msg = f"查询交易所现货卖出订单状态时发生错误:{e}"
|
|
|
logging.error(msg)
|
|
|
add_state_flow_entry(self.process_item, self.current_state, msg, "fail")
|
|
|
- self._set_state("FAILED")
|
|
|
+ self._set_state(self.STATE_FAILED)
|
|
|
+
|
|
|
+ traceback.print_exc()
|
|
|
|
|
|
def _execute_buy_on_chain(self):
|
|
|
"""
|
|
|
@@ -374,6 +385,8 @@ class ArbitrageProcess:
|
|
|
add_state_flow_entry(self.process_item, self.current_state, msg, "fail")
|
|
|
self._set_state(self.STATE_WAITING_EXCHANGE_ROLLBACK)
|
|
|
|
|
|
+ traceback.print_exc()
|
|
|
+
|
|
|
def _wait_chain_confirm(self):
|
|
|
"""
|
|
|
等待链上交易确认
|
|
|
@@ -484,6 +497,8 @@ class ArbitrageProcess:
|
|
|
add_state_flow_entry(self.process_item, self.current_state, msg, "fail")
|
|
|
self._set_state(self.STATE_WAITING_EXCHANGE_ROLLBACK)
|
|
|
|
|
|
+ traceback.print_exc()
|
|
|
+
|
|
|
def _wait_exchange_rollback(self):
|
|
|
"""
|
|
|
市价进行交易所交易回滚
|
|
|
@@ -577,7 +592,9 @@ class ArbitrageProcess:
|
|
|
msg = f"【回滚】交易所回滚交易失败:{e}"
|
|
|
logging.error(msg)
|
|
|
add_state_flow_entry(self.process_item, self.current_state, msg, "fail")
|
|
|
- self._set_state("FAILED")
|
|
|
+ self._set_state(self.STATE_FAILED)
|
|
|
+
|
|
|
+ traceback.print_exc()
|
|
|
|
|
|
def _wait_transfer_arrive(self):
|
|
|
"""
|
|
|
@@ -670,13 +687,15 @@ class ArbitrageProcess:
|
|
|
logging.error(msg)
|
|
|
add_state_flow_entry(self.process_item, self.current_state, msg, "fail")
|
|
|
|
|
|
- self._set_state("FAILED")
|
|
|
+ self._set_state(self.STATE_FAILED)
|
|
|
except Exception as e:
|
|
|
msg = f"查询交易所到账状态时发生错误:{e}"
|
|
|
logging.error(msg)
|
|
|
add_state_flow_entry(self.process_item, self.current_state, msg, "fail")
|
|
|
|
|
|
- self._set_state("FAILED")
|
|
|
+ self._set_state(self.STATE_FAILED)
|
|
|
+
|
|
|
+ traceback.print_exc()
|
|
|
|
|
|
def _execute_transfer_to_chain(self):
|
|
|
"""
|
|
|
@@ -714,7 +733,9 @@ class ArbitrageProcess:
|
|
|
logging.error(msg)
|
|
|
add_state_flow_entry(self.process_item, self.current_state, msg, "fail")
|
|
|
|
|
|
- self._set_state("FAILED")
|
|
|
+ self._set_state(self.STATE_FAILED)
|
|
|
+
|
|
|
+ traceback.print_exc()
|
|
|
|
|
|
def _wait_withdrawal_confirm(self):
|
|
|
"""
|
|
|
@@ -766,13 +787,14 @@ class ArbitrageProcess:
|
|
|
logging.error(msg)
|
|
|
add_state_flow_entry(self.process_item, self.current_state, msg, "fail")
|
|
|
|
|
|
- self._set_state("FAILED")
|
|
|
+ self._set_state(self.STATE_FAILED)
|
|
|
except Exception as e:
|
|
|
msg = f"查询交易所提现状态时发生错误:{e}"
|
|
|
logging.error(msg)
|
|
|
add_state_flow_entry(self.process_item, self.current_state, msg, "fail")
|
|
|
-
|
|
|
- self._set_state("FAILED")
|
|
|
+ self._set_state(self.STATE_FAILED)
|
|
|
+
|
|
|
+ traceback.print_exc()
|
|
|
|
|
|
# 伪代码示例:如何使用这个类
|
|
|
if __name__ == "__main__":
|