|
|
@@ -300,7 +300,7 @@ class ArbitrageProcess:
|
|
|
logging.info(msg)
|
|
|
add_state_flow_entry(self.process_item, self.current_state, msg, "success")
|
|
|
|
|
|
- self._set_state(self.STATE_TRANSFERRING_TO_CHAIN)
|
|
|
+ self._set_state(self.STATE_WAITING_TRANSFER_ARRIVE)
|
|
|
return
|
|
|
else:
|
|
|
# 继续等待成交
|
|
|
@@ -315,6 +315,55 @@ class ArbitrageProcess:
|
|
|
add_state_flow_entry(self.process_item, self.current_state, msg, "fail")
|
|
|
self._set_state("FAILED")
|
|
|
|
|
|
+ def _wait_transfer_arrive(self):
|
|
|
+ """
|
|
|
+ 等待资产在交易所内到账
|
|
|
+ """
|
|
|
+ msg = f"等待资产在交易所到账..."
|
|
|
+ logging.info(msg)
|
|
|
+ add_state_flow_entry(self.process_item, self.current_state, msg, "pending")
|
|
|
+
|
|
|
+ try:
|
|
|
+ is_arrived = False
|
|
|
+
|
|
|
+ # 最多等待30分钟
|
|
|
+ waiting_times = 60
|
|
|
+ last_deposit_state = None
|
|
|
+ while waiting_times > 0:
|
|
|
+ deposit_list = mexc.wallet.get_deposit_list()
|
|
|
+ for deposit in deposit_list:
|
|
|
+ if deposit['transHash'] != self.arbitrage_details['chain_buy_tx_hash']:
|
|
|
+ continue
|
|
|
+
|
|
|
+ last_deposit_state = deposit
|
|
|
+
|
|
|
+ logging.info(f"等待资产在交易所到账...({deposit['confirmTimes']}/{deposit['unlockConfirm']})")
|
|
|
+ if deposit['confirmTimes'] >= deposit['unlockConfirm']:
|
|
|
+ is_arrived = True
|
|
|
+
|
|
|
+ if is_arrived:
|
|
|
+ msg = f"资产已在交易所到账。{last_deposit_state}"
|
|
|
+ logging.error(msg)
|
|
|
+ add_state_flow_entry(self.process_item, self.current_state, msg, "success")
|
|
|
+
|
|
|
+ self._set_state(self.STATE_TRANSFERRING_TO_CHAIN)
|
|
|
+ return
|
|
|
+
|
|
|
+ time.sleep(30)
|
|
|
+ waiting_times = waiting_times - 1
|
|
|
+
|
|
|
+ msg = f"等待充值到账超时(超过30分钟): {last_deposit_state}"
|
|
|
+ logging.error(msg)
|
|
|
+ add_state_flow_entry(self.process_item, self.current_state, msg, "fail")
|
|
|
+
|
|
|
+ self._set_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")
|
|
|
+
|
|
|
def _execute_transfer_to_chain(self):
|
|
|
"""
|
|
|
将交易后获得的计价资产(例如USDT)转账回链上
|
|
|
@@ -324,29 +373,38 @@ class ArbitrageProcess:
|
|
|
add_state_flow_entry(self.process_item, self.current_state, msg, "pending")
|
|
|
|
|
|
try:
|
|
|
- balances = mexc.trade.get_account_info()['balances']
|
|
|
- for balance in balances:
|
|
|
- if balance['asset'] == 'USDT':
|
|
|
- pseudo_withdraw_amount = balance['free']
|
|
|
-
|
|
|
- withdraw_params = {
|
|
|
- 'coin': 'USDT',
|
|
|
- 'netWork': 'ETH',
|
|
|
- 'address': self.user_wallet,
|
|
|
- 'amount': pseudo_withdraw_amount
|
|
|
- }
|
|
|
- withdraw_rst = mexc.wallet.post_withdraw(withdraw_params)
|
|
|
- if "id" not in withdraw_rst:
|
|
|
- logging.info(withdraw_rst)
|
|
|
-
|
|
|
- exchange_withdrawal_id = withdraw_rst["id"]
|
|
|
-
|
|
|
- msg = f"交易所提现已发送, 提现ID: {exchange_withdrawal_id}"
|
|
|
- logging.info(msg)
|
|
|
- add_state_flow_entry(self.process_item, self.current_state, msg, "success")
|
|
|
-
|
|
|
- self.arbitrage_details["exchange_withdrawl_id"] = withdraw_rst["id"]
|
|
|
- self._set_state(self.STATE_WAITING_WITHDRAWAL_CONFIRM)
|
|
|
+ times = 10
|
|
|
+ while times > 0:
|
|
|
+ balances = mexc.trade.get_account_info()['balances']
|
|
|
+ for balance in balances:
|
|
|
+ if balance['asset'] == 'USDT':
|
|
|
+ pseudo_withdraw_amount = str(int(float(balance['free'])))
|
|
|
+
|
|
|
+ withdraw_params = {
|
|
|
+ 'coin': 'USDT',
|
|
|
+ 'netWork': 'ETH',
|
|
|
+ 'address': self.user_wallet,
|
|
|
+ 'amount': pseudo_withdraw_amount
|
|
|
+ }
|
|
|
+ withdraw_rst = mexc.wallet.post_withdraw(withdraw_params)
|
|
|
+ if "id" not in withdraw_rst:
|
|
|
+ logging.error(f"提现失败, 10秒后重试{times}/10")
|
|
|
+ logging.error(withdraw_params)
|
|
|
+ logging.error(withdraw_rst)
|
|
|
+
|
|
|
+ exchange_withdrawal_id = withdraw_rst["id"]
|
|
|
+
|
|
|
+ msg = f"交易所提现已发送, 提现ID: {exchange_withdrawal_id}"
|
|
|
+ logging.info(msg)
|
|
|
+ add_state_flow_entry(self.process_item, self.current_state, msg, "success")
|
|
|
+
|
|
|
+ self.arbitrage_details["exchange_withdrawl_id"] = withdraw_rst["id"]
|
|
|
+ self._set_state(self.STATE_WAITING_WITHDRAWAL_CONFIRM)
|
|
|
+
|
|
|
+ return
|
|
|
+
|
|
|
+ times = times - 1
|
|
|
+ time.sleep(10)
|
|
|
|
|
|
except Exception as e:
|
|
|
msg = f"转账回链上失败:{e}"
|
|
|
@@ -367,8 +425,8 @@ class ArbitrageProcess:
|
|
|
try:
|
|
|
is_arrived = False
|
|
|
|
|
|
- # 最多等待20分钟
|
|
|
- waiting_times = 1200
|
|
|
+ # 最多等待30分钟
|
|
|
+ waiting_times = 60
|
|
|
last_deposit_state = None
|
|
|
while waiting_times > 0:
|
|
|
withdraw_list = mexc.wallet.get_withdraw_list()
|
|
|
@@ -395,13 +453,13 @@ class ArbitrageProcess:
|
|
|
logging.info(msg)
|
|
|
add_state_flow_entry(self.process_item, self.current_state, msg, "success")
|
|
|
|
|
|
- self._set_state(self.STATE_WAITING_TRANSFER_ARRIVE)
|
|
|
+ self._set_state(self.STATE_COMPLETED)
|
|
|
return
|
|
|
|
|
|
- time.sleep(1)
|
|
|
+ time.sleep(30)
|
|
|
waiting_times = waiting_times - 1
|
|
|
|
|
|
- msg = f"等待提现到账超时(超过20分钟): {last_deposit_state}"
|
|
|
+ msg = f"等待提现到账超时(超过30分钟): {last_deposit_state}"
|
|
|
logging.error(msg)
|
|
|
add_state_flow_entry(self.process_item, self.current_state, msg, "fail")
|
|
|
|
|
|
@@ -413,54 +471,6 @@ class ArbitrageProcess:
|
|
|
|
|
|
self._set_state("FAILED")
|
|
|
|
|
|
- def _wait_transfer_arrive(self):
|
|
|
- """
|
|
|
- 等待资产在交易所内到账
|
|
|
- """
|
|
|
- msg = f"等待资产在交易所到账..."
|
|
|
- logging.error(msg)
|
|
|
- add_state_flow_entry(self.process_item, self.current_state, msg, "pending")
|
|
|
-
|
|
|
- try:
|
|
|
- is_arrived = False
|
|
|
-
|
|
|
- # 最多等待20分钟
|
|
|
- waiting_times = 1200
|
|
|
- last_deposit_state = None
|
|
|
- while waiting_times > 0:
|
|
|
- deposit_list = mexc.wallet.get_deposit_list()
|
|
|
- for deposit in deposit_list:
|
|
|
- if deposit['transHash'] != self.arbitrage_details['chain_buy_tx_hash']:
|
|
|
- continue
|
|
|
-
|
|
|
- last_deposit_state = deposit
|
|
|
-
|
|
|
- if deposit['status'] == 9:
|
|
|
- is_arrived = True
|
|
|
-
|
|
|
- if is_arrived:
|
|
|
- msg = "资产已在交易所到账。{last_deposit_state}"
|
|
|
- logging.error(msg)
|
|
|
- add_state_flow_entry(self.process_item, self.current_state, msg, "success")
|
|
|
-
|
|
|
- self._set_state(self.STATE_COMPLETED)
|
|
|
- return
|
|
|
-
|
|
|
- time.sleep(1)
|
|
|
- waiting_times = waiting_times - 1
|
|
|
-
|
|
|
- msg = f"等待充值到账超时(超过20分钟): {last_deposit_state}"
|
|
|
- logging.error(msg)
|
|
|
- add_state_flow_entry(self.process_item, self.current_state, msg, "fail")
|
|
|
-
|
|
|
- self._set_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")
|
|
|
-
|
|
|
# 伪代码示例:如何使用这个类
|
|
|
if __name__ == "__main__":
|
|
|
import ok_chain_client
|
|
|
@@ -499,22 +509,26 @@ if __name__ == "__main__":
|
|
|
process_item = {
|
|
|
"stateFlow": [], # 状态流转记录
|
|
|
}
|
|
|
- arbitrage_system = ArbitrageProcess(tx, 2, 1.2,
|
|
|
+ arbitrage_process = ArbitrageProcess(tx, 2, 1.2,
|
|
|
FROM_TOKEN, TO_TOKEN,
|
|
|
FROM_TOKEN_AMOUNT_HUMAM,
|
|
|
USER_EXCHANGE_WALLET, USER_WALLET,
|
|
|
SYMBOL, process_item)
|
|
|
|
|
|
# 一般都是从这个流程开始,测试时可以稍作修改、测试后续流程
|
|
|
- arbitrage_system._set_state(arbitrage_system.STATE_BUYING_ON_CHAIN)
|
|
|
+ arbitrage_process._set_state(arbitrage_process.STATE_BUYING_ON_CHAIN)
|
|
|
|
|
|
# 在主循环中周期性调用 run_arbitrage_step
|
|
|
- while arbitrage_system.current_state != "COMPLETED" and arbitrage_system.current_state != "FAILED":
|
|
|
- arbitrage_system.run_arbitrage_step()
|
|
|
- time.sleep(1) # 暂停一段时间执行实际操作延迟
|
|
|
+ while arbitrage_process.current_state != "COMPLETED" and arbitrage_process.current_state != "FAILED":
|
|
|
+ arbitrage_process.run_arbitrage_step()
|
|
|
+
|
|
|
+ if arbitrage_process.current_state == arbitrage_process.STATE_WAITING_TRANSFER_ARRIVE or arbitrage_process.current_state == arbitrage_process.STATE_WAITING_WITHDRAWAL_CONFIRM:
|
|
|
+ time.sleep(10)
|
|
|
+ else:
|
|
|
+ time.sleep(1)
|
|
|
|
|
|
logging.info(process_item)
|
|
|
- if arbitrage_system.current_state == "COMPLETED":
|
|
|
+ if arbitrage_process.current_state == "COMPLETED":
|
|
|
logging.info("套利流程执行成功!")
|
|
|
else:
|
|
|
logging.info("套利流程执行失败!")
|