Skip to content

Commit 854f7bd

Browse files
authored
Merge pull request #20 from talkincode/fix/oco-grouping-case-sensitivity
fix: 修正 OCO 分组常量大小写以匹配 HyperLiquid SDK (v0.1.8)
2 parents ab51679 + 7171093 commit 854f7bd

File tree

9 files changed

+135
-12
lines changed

9 files changed

+135
-12
lines changed

CHANGELOG.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,42 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [0.1.8] - 2025-10-28
9+
10+
### Fixed
11+
12+
- **关键修复**: 修正 OCO 分组常量大小写,解决止盈止损订单 422 错误
13+
- 修改 `normalTpSl` -> `normalTpsl` (小写 s)
14+
- 修改 `positionTpSl` -> `positionTpsl` (小写 s)
15+
- 与 HyperLiquid SDK `Grouping` 类型定义保持完全一致
16+
- 修复设置止盈止损时的 "Failed to deserialize the JSON body" 错误
17+
18+
### Added
19+
20+
- 新增调试脚本 `test_scripts/debug_tpsl.py` 用于验证订单格式
21+
- 新增验证脚本 `test_scripts/test_grouping_fix.py` 用于测试修复
22+
23+
### Changed
24+
25+
- 更新相关测试和验证脚本以匹配新的常量值
26+
- 改进订单结构注释说明
27+
828
## [0.1.6] - 2025-10-27
929

1030
### Fixed
31+
1132
- 清理错误的 v0.2.0 标签,恢复正确的版本历史
1233
- 维护版本一致性
1334

1435
## [0.1.5] - 2025-10-27
1536

1637
### Added
38+
1739
- 自动格式化代码(black 和 isort)
1840
- 优化 release.prompt 支持自动版本号递增
1941

2042
### Changed
43+
2144
- 清理冗余文档和重组文件
2245

2346
## [0.1.4] - Previous Release

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "hyperliquid-mcp-python"
3-
version = "0.1.7"
3+
version = "0.1.8"
44
description = "HyperLiquid MCP Server with FastMCP - Trading tools and account management"
55
authors = [
66
{name = "jettwang", email = "jamiesun.net@gmail.com"}

services/constants.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
"""HyperLiquid MCP 常量定义"""
22

33
# OCO 订单分组类型
4-
OCO_GROUP_NEW_POSITION = "normalTpSl" # 新仓位的括号订单
5-
OCO_GROUP_EXISTING_POSITION = "positionTpSl" # 现有仓位的止盈止损
4+
# 注意: 必须与 HyperLiquid SDK 中的 Grouping 类型定义一致
5+
# SDK 定义: Literal["na"], Literal["normalTpsl"], Literal["positionTpsl"]
6+
OCO_GROUP_NEW_POSITION = "normalTpsl" # 新仓位的括号订单 (小写 s)
7+
OCO_GROUP_EXISTING_POSITION = "positionTpsl" # 现有仓位的止盈止损 (小写 s)
68

79
# 订单类型常量
810
ORDER_TYPE_LIMIT_GTC = {"limit": {"tif": "Gtc"}}

services/hyperliquid_services.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -810,12 +810,12 @@ async def set_position_tpsl(
810810
# Add stop loss order if specified
811811
if sl_px is not None:
812812
# For SL orders, use market order for fast execution
813-
# No limit_px needed when isMarket=True
813+
# When isMarket=True, limit_px should be 0 or the trigger price
814814
sl_order = {
815815
"coin": coin,
816816
"is_buy": not is_long,
817817
"sz": float(position_size),
818-
"limit_px": float(sl_px), # Use trigger price as limit_px
818+
"limit_px": float(sl_px), # For market orders, use trigger price
819819
"order_type": {
820820
"trigger": {
821821
"triggerPx": float(sl_px),

test_scripts/debug_tpsl.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/env python3
2+
"""
3+
调试止盈止损订单的 JSON 格式
4+
"""
5+
6+
import json
7+
import os
8+
import sys
9+
10+
# Add parent directory to path
11+
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
12+
13+
from hyperliquid.utils.signing import order_type_to_wire
14+
15+
# 模拟一个止盈订单
16+
tp_order = {
17+
"coin": "BTC",
18+
"is_buy": False, # 平多仓
19+
"sz": 0.001,
20+
"limit_px": 95000.0,
21+
"order_type": {
22+
"trigger": {
23+
"triggerPx": 95000.0,
24+
"isMarket": False,
25+
"tpsl": "tp",
26+
}
27+
},
28+
"reduce_only": True,
29+
}
30+
31+
# 模拟一个止损订单
32+
sl_order = {
33+
"coin": "BTC",
34+
"is_buy": False, # 平多仓
35+
"sz": 0.001,
36+
"limit_px": 90000.0,
37+
"order_type": {
38+
"trigger": {
39+
"triggerPx": 90000.0,
40+
"isMarket": True,
41+
"tpsl": "sl",
42+
}
43+
},
44+
"reduce_only": True,
45+
}
46+
47+
print("=" * 60)
48+
print("止盈订单 (TP Order):")
49+
print("=" * 60)
50+
print(json.dumps(tp_order, indent=2))
51+
52+
print("\n" + "=" * 60)
53+
print("止损订单 (SL Order):")
54+
print("=" * 60)
55+
print(json.dumps(sl_order, indent=2))
56+
57+
# 测试 order_type 转换
58+
print("\n" + "=" * 60)
59+
print("TP Order Type Wire:")
60+
print("=" * 60)
61+
tp_wire_type = order_type_to_wire(tp_order["order_type"])
62+
print(json.dumps(tp_wire_type, indent=2))
63+
64+
print("\n" + "=" * 60)
65+
print("SL Order Type Wire:")
66+
print("=" * 60)
67+
sl_wire_type = order_type_to_wire(sl_order["order_type"])
68+
print(json.dumps(sl_wire_type, indent=2))
69+
70+
print("\n✅ 订单类型格式验证通过!")

test_scripts/test_grouping_fix.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python3
2+
"""测试 OCO 分组常量修复"""
3+
4+
from services.constants import OCO_GROUP_EXISTING_POSITION, OCO_GROUP_NEW_POSITION
5+
6+
print("=" * 60)
7+
print("OCO 分组常量验证")
8+
print("=" * 60)
9+
10+
print(f"\nOCO_GROUP_NEW_POSITION = {repr(OCO_GROUP_NEW_POSITION)}")
11+
print(f"OCO_GROUP_EXISTING_POSITION = {repr(OCO_GROUP_EXISTING_POSITION)}")
12+
13+
# 验证与 SDK 定义一致
14+
assert OCO_GROUP_NEW_POSITION == "normalTpsl", f"错误: {OCO_GROUP_NEW_POSITION}"
15+
assert OCO_GROUP_EXISTING_POSITION == "positionTpsl", (
16+
f"错误: {OCO_GROUP_EXISTING_POSITION}"
17+
)
18+
19+
print("\n✅ 所有常量值正确!")
20+
print("✅ 与 HyperLiquid SDK Grouping 类型定义一致 (小写 's')")
21+
print("\n修复说明:")
22+
print("- 之前: normalTpSl, positionTpSl (大写 S - 错误)")
23+
print("- 现在: normalTpsl, positionTpsl (小写 s - 正确)")
24+
print("\n这个修复解决了 '422 Failed to deserialize the JSON body' 错误")

test_scripts/verify_completion.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ def check_constants():
1010
OCO_GROUP_NEW_POSITION,
1111
)
1212

13-
assert OCO_GROUP_NEW_POSITION == "normalTpSl", "新仓位分组常量错误"
14-
assert OCO_GROUP_EXISTING_POSITION == "positionTpSl", "现有仓位分组常量错误"
13+
# 必须与 HyperLiquid SDK 中的 Grouping 类型定义完全一致(小写 's')
14+
assert OCO_GROUP_NEW_POSITION == "normalTpsl", "新仓位分组常量错误(应为小写s)"
15+
assert OCO_GROUP_EXISTING_POSITION == "positionTpsl", (
16+
"现有仓位分组常量错误(应为小写s)"
17+
)
1518
assert ADDRESS_PREFIX_LEN == 6, "地址前缀长度常量错误"
1619
print("✅ 常量检查通过")
1720

tests/unit/test_constants.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
)
1313

1414

15-
def test_oco_group_constants():
16-
"""测试 OCO 分组常量值"""
17-
assert OCO_GROUP_NEW_POSITION == "normalTpSl"
18-
assert OCO_GROUP_EXISTING_POSITION == "positionTpSl"
15+
def test_oco_grouping_constants():
16+
"""测试 OCO 分组常量与 SDK 定义一致"""
17+
# 必须与 HyperLiquid SDK 中的 Grouping 类型定义完全一致
18+
assert OCO_GROUP_NEW_POSITION == "normalTpsl" # 注意小写 's'
19+
assert OCO_GROUP_EXISTING_POSITION == "positionTpsl" # 注意小写 's'
1920

2021

2122
def test_order_type_constants():

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)