Prechádzať zdrojové kódy

feat(strategy): 添加DOGE市场订单创建功能并更新账户配置

更新账户索引和API密钥索引配置
添加异步main函数实现DOGE市场订单创建流程
包括获取账户信息、生成nonce、签名订单和发送交易
skyfffire 1 týždeň pred
rodič
commit
40cce827f9
1 zmenil súbory, kde vykonal 75 pridanie a 6 odobranie
  1. 75 6
      src/record/strategy.py

+ 75 - 6
src/record/strategy.py

@@ -10,6 +10,8 @@ from enum import Enum
 from datetime import datetime
 import os
 import lighter
+import json
+import time
 
 
 # 配置日志
@@ -41,21 +43,23 @@ class TradingStrategy:
         self.target_symbol = "DOGE"     # 目标交易对
         self.account_info = None        # 存储账户信息
 
-        self.account_index = 281474976643718
+        self.account_index = 318163
+        self.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(  
             url='https://mainnet.zklighter.elliot.ai',  
-            private_key='0xf3625c4662ab0b338e405f61b7555e90aeda8fa28dd607588c9e275dc6f326ddcbd9341e18ca2950',  
+            private_key='72aafa0426f7ff2806c68625ca5c88de153e34fcb23567f3b872cd56334d2848fb223466efff9c21',  
             account_index=self.account_index,
-            api_key_index=0
+            api_key_index=self.api_key_index
         )
         
         # Check client connection
         err = self.signer_client.check_client()
         if err is not None:
-            logger.error(f"SignerClient CheckClient error: {trim_exception(err)}")
+            logger.error(f"SignerClient CheckClient error: {err}")
             return
         
         logger.info("策略初始化完成,当前状态: WAITING_INIT")
@@ -166,7 +170,72 @@ class TradingStrategy:
         self.state = StrategyState.IDLE_MONITORING
         logger.info("状态转换: POSITION_CLOSED -> IDLE_MONITORING")
 
-if __name__ == '__main__':
+async def main():    
     strategy = TradingStrategy()
+    account = await strategy.account_api.account(by="index", value=f"{strategy.account_index}")
+
+    # [AccountPosition(market_id=3, symbol='DOGE', initial_margin_fraction='10.00', open_order_count=0, pending_order_count=0, position_tied_order_count=0, sign=1, position='1', avg_entry_price='0.194368', position_value='0.194360', unrealized_pnl='-0.000008', realized_pnl='0.000000', liquidation_price='0', total_funding_paid_out=None, margin_mode=0, allocated_margin='0.000000', additional_properties={})]
+    print(account.accounts[0].positions)
+
+    doge_market = {
+        "symbol": "DOGE",
+        "market_id": 3,
+        "status": "active",
+        "taker_fee": "0.0000",
+        "maker_fee": "0.0000",
+        "liquidation_fee": "1.0000",
+        "min_base_amount": "10",
+        "min_quote_amount": "10.000000",
+        "order_quote_limit": "",
+        "supported_size_decimals": 0,
+        "supported_price_decimals": 6,
+        "supported_quote_decimals": 6
+    }
+
+    next_nonce = await strategy.transaction_api.next_nonce(account_index=strategy.account_index, api_key_index=strategy.api_key_index)
+    nonce_value = next_nonce.nonce
+
+    base_amount = int(1 * (10 ** doge_market['supported_size_decimals']))
+    avg_execution_price = int(0.190000 * (10 ** doge_market['supported_price_decimals']))
+    
+    # 打印所有参数
+    client_order_index = int(time.time() * 1000)
+    print("=== 创建订单参数 ===")
+    print(f"market_index: {doge_market['market_id']}")
+    print(f"client_order_index: {client_order_index}")
+    print(f"base_amount: {base_amount}")
+    print(f"price: {avg_execution_price}")
+    print(f"is_ask: True")
+    print(f"order_type: {strategy.signer_client.ORDER_TYPE_MARKET}")
+    print(f"time_in_force: {strategy.signer_client.ORDER_TIME_IN_FORCE_IMMEDIATE_OR_CANCEL}")
+    print(f"reduce_only: True")
+    print(f"trigger_price: 0")
+    print(f"nonce: {nonce_value}")
+    print(f"order_expiry: 0")
+    print("==================")
+    
+    tx_info, error = strategy.signer_client.sign_create_order(
+        market_index=doge_market["market_id"],
+        client_order_index=client_order_index,
+        base_amount=base_amount,
+        price=avg_execution_price,
+        is_ask=True,
+        order_type=strategy.signer_client.ORDER_TYPE_MARKET,
+        time_in_force=strategy.signer_client.ORDER_TIME_IN_FORCE_IMMEDIATE_OR_CANCEL,
+        reduce_only=True,
+        trigger_price=0,
+        nonce=nonce_value,
+        order_expiry=0, # 所有市价单(包括减仓市价单)都必须使用NilOrderExpiry (0)
+    )
+    if error is not None:
+        print(f"Error signing first order (first batch): {error}")
+        return
+    
+    print(tx_info)
+    tx_hash = await strategy.transaction_api.send_tx(strategy.signer_client.TX_TYPE_CREATE_ORDER, tx_info)
+    print(tx_hash)
+
+if __name__ == '__main__':
+    import asyncio
 
-    
+    asyncio.run(main())