|
|
@@ -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
|