|
|
@@ -0,0 +1,363 @@
|
|
|
+# 代码迁移映射表
|
|
|
+
|
|
|
+## 概述
|
|
|
+
|
|
|
+本文档详细列出 `strategy.py` 中需要迁移到交易所层的代码片段及其目标位置。
|
|
|
+
|
|
|
+---
|
|
|
+
|
|
|
+## 1. API客户端初始化迁移
|
|
|
+
|
|
|
+### 源位置:strategy.py 第130-149行
|
|
|
+
|
|
|
+```python
|
|
|
+# 旧代码(strategy.py)
|
|
|
+lighter_config = config.get('lighter', {})
|
|
|
+self.account_index = lighter_config.get('account_index', 318163)
|
|
|
+self.api_key_index = lighter_config.get('api_key_index', 0)
|
|
|
+
|
|
|
+self.api_client = lighter.ApiClient()
|
|
|
+self.account_api = lighter.AccountApi(self.api_client)
|
|
|
+self.transaction_api = lighter.TransactionApi(self.api_client)
|
|
|
+self.signer_client = lighter.SignerClient(...)
|
|
|
+```
|
|
|
+
|
|
|
+### 目标位置:exchange/lighter.py
|
|
|
+
|
|
|
+```python
|
|
|
+# 新代码(exchange/lighter.py)
|
|
|
+class LighterExchange:
|
|
|
+ def __init__(self, config):
|
|
|
+ self.account_index = config.get('account_index', 318163)
|
|
|
+ self.api_key_index = config.get('api_key_index', 0)
|
|
|
+
|
|
|
+ self.api_client = lighter.ApiClient()
|
|
|
+ self.account_api = lighter.AccountApi(self.api_client)
|
|
|
+ self.transaction_api = lighter.TransactionApi(self.api_client)
|
|
|
+ self.signer_client = lighter.SignerClient(...)
|
|
|
+
|
|
|
+ err = self.signer_client.check_client()
|
|
|
+ if err is not None:
|
|
|
+ raise RuntimeError(f"SignerClient CheckClient error: {err}")
|
|
|
+```
|
|
|
+
|
|
|
+### 策略层新代码
|
|
|
+
|
|
|
+```python
|
|
|
+# 新代码(strategy.py)
|
|
|
+from exchange.lighter import LighterExchange
|
|
|
+
|
|
|
+class TradingStrategy:
|
|
|
+ def __init__(self, config):
|
|
|
+ self.exchange = LighterExchange(config.get('lighter', {}))
|
|
|
+ # ... 其他初始化
|
|
|
+```
|
|
|
+
|
|
|
+---
|
|
|
+
|
|
|
+## 2. 订单创建和签名迁移
|
|
|
+
|
|
|
+### 源位置:strategy.py 第670-727行
|
|
|
+
|
|
|
+```python
|
|
|
+# 旧代码:create_order_tx()
|
|
|
+async def create_order_tx(self, market_info, quantity, price, is_ask=True,
|
|
|
+ reduce_only=False, order_type=None, nonce=-1):
|
|
|
+ # 精度转换
|
|
|
+ base_amount = int(quantity * (10 ** market_info.get('supported_size_decimals', 0)))
|
|
|
+ formatted_price = int(price * (10 ** market_info.get('supported_price_decimals', 6)))
|
|
|
+
|
|
|
+ # 生成客户端订单ID
|
|
|
+ client_order_index = int(time.time() * 1000)
|
|
|
+
|
|
|
+ # 根据订单类型调用签名
|
|
|
+ if order_type == self.signer_client.ORDER_TYPE_LIMIT:
|
|
|
+ tx_info, error = self.signer_client.sign_create_order(...)
|
|
|
+ else:
|
|
|
+ tx_info, error = self.signer_client.sign_create_order(...)
|
|
|
+
|
|
|
+ return tx_info, client_order_index, None
|
|
|
+```
|
|
|
+
|
|
|
+### 目标位置:exchange/lighter.py
|
|
|
+
|
|
|
+```python
|
|
|
+# 新代码
|
|
|
+class LighterExchange:
|
|
|
+ async def create_order(self, market_info, quantity, price, is_ask=True,
|
|
|
+ reduce_only=False, order_type=None, nonce=-1):
|
|
|
+ """创建并签名订单"""
|
|
|
+ base_amount = int(quantity * (10 ** market_info.get('supported_size_decimals', 0)))
|
|
|
+ formatted_price = int(price * (10 ** market_info.get('supported_price_decimals', 6)))
|
|
|
+ client_order_index = int(time.time() * 1000)
|
|
|
+
|
|
|
+ if order_type == self.signer_client.ORDER_TYPE_LIMIT:
|
|
|
+ tx_info, error = self.signer_client.sign_create_order(...)
|
|
|
+ else:
|
|
|
+ tx_info, error = self.signer_client.sign_create_order(...)
|
|
|
+
|
|
|
+ if error is not None:
|
|
|
+ return None, None, error
|
|
|
+
|
|
|
+ return tx_info, client_order_index, None
|
|
|
+```
|
|
|
+
|
|
|
+### 策略层调用方式
|
|
|
+
|
|
|
+```python
|
|
|
+# 旧方式
|
|
|
+tx_info, oid, error = await self.create_order_tx(market_info, quantity, price, ...)
|
|
|
+
|
|
|
+# 新方式
|
|
|
+tx_info, oid, error = await self.exchange.create_order(market_info, quantity, price, ...)
|
|
|
+```
|
|
|
+
|
|
|
+---
|
|
|
+
|
|
|
+## 3. 交易发送迁移
|
|
|
+
|
|
|
+### 源位置:strategy.py 第729-747行
|
|
|
+
|
|
|
+```python
|
|
|
+# 旧代码:send_order_tx()
|
|
|
+async def send_order_tx(self, tx_info):
|
|
|
+ tx_response = await self.transaction_api.send_tx(
|
|
|
+ self.signer_client.TX_TYPE_CREATE_ORDER, tx_info)
|
|
|
+
|
|
|
+ if tx_response.code != 200:
|
|
|
+ error_msg = f"code={tx_response.code}, message={tx_response.message}"
|
|
|
+ return None, error_msg
|
|
|
+
|
|
|
+ return tx_response.tx_hash, None
|
|
|
+```
|
|
|
+
|
|
|
+### 目标位置:exchange/lighter.py
|
|
|
+
|
|
|
+```python
|
|
|
+# 新代码
|
|
|
+class LighterExchange:
|
|
|
+ async def send_order(self, tx_info):
|
|
|
+ """发送订单交易"""
|
|
|
+ tx_response = await self.transaction_api.send_tx(
|
|
|
+ self.signer_client.TX_TYPE_CREATE_ORDER, tx_info)
|
|
|
+
|
|
|
+ if tx_response.code != 200:
|
|
|
+ error_msg = f"code={tx_response.code}, message={tx_response.message}"
|
|
|
+ return None, error_msg
|
|
|
+
|
|
|
+ return tx_response.tx_hash, None
|
|
|
+```
|
|
|
+
|
|
|
+---
|
|
|
+
|
|
|
+## 4. 订单替换迁移
|
|
|
+
|
|
|
+### 源位置:strategy.py 第749-822行
|
|
|
+
|
|
|
+```python
|
|
|
+# 旧代码:replace_order()
|
|
|
+async def replace_order(self, old_order_id, market_info, quantity, price,
|
|
|
+ is_ask=True, reduce_only=False, order_type=None):
|
|
|
+ # 获取nonce
|
|
|
+ next_nonce = await self.transaction_api.next_nonce(...)
|
|
|
+ nonce_value = next_nonce.nonce
|
|
|
+
|
|
|
+ # 签名取消订单
|
|
|
+ cancel_tx_info, error = self.signer_client.sign_cancel_order(...)
|
|
|
+
|
|
|
+ # 签名创建新订单
|
|
|
+ new_order_tx_info, new_oid, error = await self.create_order_tx(...)
|
|
|
+
|
|
|
+ # 批量发送
|
|
|
+ tx_response = await self.transaction_api.send_tx_batch(tx_types, tx_infos)
|
|
|
+
|
|
|
+ return tx_hash, new_oid, None
|
|
|
+```
|
|
|
+
|
|
|
+### 目标位置:exchange/lighter.py
|
|
|
+
|
|
|
+```python
|
|
|
+# 新代码
|
|
|
+class LighterExchange:
|
|
|
+ async def replace_order(self, old_order_id, market_info, quantity, price,
|
|
|
+ is_ask=True, reduce_only=False, order_type=None):
|
|
|
+ """替换订单"""
|
|
|
+ # 获取nonce
|
|
|
+ next_nonce = await self.transaction_api.next_nonce(...)
|
|
|
+ nonce_value = next_nonce.nonce
|
|
|
+
|
|
|
+ # 签名取消订单
|
|
|
+ cancel_tx_info, error = self.signer_client.sign_cancel_order(...)
|
|
|
+
|
|
|
+ # 签名创建新订单
|
|
|
+ new_order_tx_info, new_oid, error = await self.create_order(...)
|
|
|
+
|
|
|
+ # 批量发送
|
|
|
+ tx_response = await self.transaction_api.send_tx_batch(...)
|
|
|
+
|
|
|
+ return tx_hash, new_oid, None
|
|
|
+```
|
|
|
+
|
|
|
+---
|
|
|
+
|
|
|
+## 5. 账户信息查询迁移
|
|
|
+
|
|
|
+### 源位置:strategy.py 第281-289行
|
|
|
+
|
|
|
+```python
|
|
|
+# 旧代码(在do_strategy中)
|
|
|
+account_response = await self.account_api.account(
|
|
|
+ by="index", value=f"{self.account_index}")
|
|
|
+```
|
|
|
+
|
|
|
+### 目标位置:exchange/lighter.py
|
|
|
+
|
|
|
+```python
|
|
|
+# 新代码
|
|
|
+class LighterExchange:
|
|
|
+ async def get_account_info(self):
|
|
|
+ """获取账户信息"""
|
|
|
+ account_response = await self.account_api.account(
|
|
|
+ by="index", value=f"{self.account_index}")
|
|
|
+
|
|
|
+ if account_response.code != 200:
|
|
|
+ return None, f"code={account_response.code}"
|
|
|
+
|
|
|
+ return account_response, None
|
|
|
+```
|
|
|
+
|
|
|
+### 策略层调用方式
|
|
|
+
|
|
|
+```python
|
|
|
+# 旧方式
|
|
|
+account_response = await self.account_api.account(...)
|
|
|
+
|
|
|
+# 新方式
|
|
|
+account_response, error = await self.exchange.get_account_info()
|
|
|
+if error:
|
|
|
+ logger.error(f"获取账户信息失败: {error}")
|
|
|
+ return
|
|
|
+```
|
|
|
+
|
|
|
+---
|
|
|
+
|
|
|
+## 6. 预签名机制迁移
|
|
|
+
|
|
|
+### 源位置:strategy.py 第172-242行
|
|
|
+
|
|
|
+```python
|
|
|
+# 旧代码:_presign_open_orders()
|
|
|
+async def _presign_open_orders(self, market_info, binance_price):
|
|
|
+ # 检查是否需要重新签名
|
|
|
+ if self.presigned_prices is None:
|
|
|
+ need_presign = True
|
|
|
+ else:
|
|
|
+ price_change = self._calculate_bps_change(binance_price_float, self.presigned_prices)
|
|
|
+ if price_change >= self.presigned_bps_threshold:
|
|
|
+ need_presign = True
|
|
|
+
|
|
|
+ # 签名做多订单
|
|
|
+ long_tx_info, long_oid, error = await self.create_order_tx(...)
|
|
|
+ self.presigned_long_tx = (long_tx_info, long_oid, ...)
|
|
|
+
|
|
|
+ # 签名做空订单
|
|
|
+ short_tx_info, short_oid, error = await self.create_order_tx(...)
|
|
|
+ self.presigned_short_tx = (short_tx_info, short_oid, ...)
|
|
|
+```
|
|
|
+
|
|
|
+### 目标位置:exchange/order.py
|
|
|
+
|
|
|
+```python
|
|
|
+# 新代码
|
|
|
+class PresignedOrderCache:
|
|
|
+ def __init__(self, exchange, bps_threshold=5):
|
|
|
+ self.exchange = exchange
|
|
|
+ self.bps_threshold = bps_threshold
|
|
|
+ self.long_order = None
|
|
|
+ self.short_order = None
|
|
|
+ self.last_price = None
|
|
|
+
|
|
|
+ async def update(self, market_info, binance_price):
|
|
|
+ """更新预签名订单"""
|
|
|
+ if self.last_price is None or self._price_changed(binance_price):
|
|
|
+ # 重新签名
|
|
|
+ await self._presign_orders(market_info, binance_price)
|
|
|
+
|
|
|
+ async def _presign_orders(self, market_info, binance_price):
|
|
|
+ # 签名做多和做空订单
|
|
|
+ pass
|
|
|
+```
|
|
|
+
|
|
|
+### 策略层调用方式
|
|
|
+
|
|
|
+```python
|
|
|
+# 旧方式
|
|
|
+await self._presign_open_orders(market_info, binance_price)
|
|
|
+
|
|
|
+# 新方式
|
|
|
+await self.order_manager.presigned_cache.update(market_info, binance_price)
|
|
|
+```
|
|
|
+
|
|
|
+---
|
|
|
+
|
|
|
+## 7. 组合操作迁移
|
|
|
+
|
|
|
+### 源位置:strategy.py 第632-668行
|
|
|
+
|
|
|
+```python
|
|
|
+# 旧代码:create_order_and_send_tx()
|
|
|
+async def create_order_and_send_tx(self, market_info, quantity, price, ...):
|
|
|
+ tx_info, oid, error = await self.create_order_tx(...)
|
|
|
+ if error is not None:
|
|
|
+ return None, None, error
|
|
|
+
|
|
|
+ tx_hash, error = await self.send_order_tx(tx_info)
|
|
|
+ if error is not None:
|
|
|
+ return None, None, error
|
|
|
+
|
|
|
+ return tx_hash, oid, None
|
|
|
+```
|
|
|
+
|
|
|
+### 目标位置:exchange/order.py
|
|
|
+
|
|
|
+```python
|
|
|
+# 新代码
|
|
|
+class OrderManager:
|
|
|
+ async def create_and_send(self, market_info, quantity, price, ...):
|
|
|
+ """创建并发送订单"""
|
|
|
+ tx_info, oid, error = await self.exchange.create_order(...)
|
|
|
+ if error is not None:
|
|
|
+ return None, None, error
|
|
|
+
|
|
|
+ tx_hash, error = await self.exchange.send_order(tx_info)
|
|
|
+ if error is not None:
|
|
|
+ return None, None, error
|
|
|
+
|
|
|
+ return tx_hash, oid, None
|
|
|
+```
|
|
|
+
|
|
|
+---
|
|
|
+
|
|
|
+## 迁移优先级
|
|
|
+
|
|
|
+| 优先级 | 模块 | 依赖 | 工作量 |
|
|
|
+|--------|------|------|--------|
|
|
|
+| 1 | exchange/base.py | 无 | 低 |
|
|
|
+| 2 | exchange/lighter.py | base.py | 中 |
|
|
|
+| 3 | models/order.py | 无 | 低 |
|
|
|
+| 4 | exchange/order.py | lighter.py | 中 |
|
|
|
+| 5 | strategy.py重构 | 1-4 | 高 |
|
|
|
+| 6 | 测试编写 | 1-5 | 高 |
|
|
|
+
|
|
|
+---
|
|
|
+
|
|
|
+## 验证检查表
|
|
|
+
|
|
|
+- [ ] 所有API调用已迁移
|
|
|
+- [ ] 错误处理已迁移
|
|
|
+- [ ] 日志记录已迁移
|
|
|
+- [ ] 类型提示已添加
|
|
|
+- [ ] 文档已更新
|
|
|
+- [ ] 测试已编写
|
|
|
+- [ ] 集成测试通过
|
|
|
+
|