Skip to content

Commit fcac052

Browse files
committed
feat: Add CCXT client testing guide and implement admin API for user management
- Created a comprehensive testing guide for CCXT client integration with Quicksilver. - Implemented admin API endpoints for user management including create, list, get, update, and delete users. - Added unit tests for all new admin API functionalities to ensure reliability and correctness. - Developed a CCXT client testing script to validate public and private API endpoints.
1 parent 829379b commit fcac052

8 files changed

Lines changed: 1379 additions & 28 deletions

File tree

CHECKLIST.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,14 @@
206206

207207
### 中优先级 ⚠️
208208

209-
1. **CCXT 格式转换未实现** - 无法与 CCXT 客户端对接
210-
2. **GetMarkets 硬编码** - 交易对列表应从配置读取
209+
1. ~~**CCXT 格式转换未实现**~~ - ✅ 已完成 (100% 覆盖)
210+
2. ~~**GetMarkets 硬编码**~~ - ✅ 已从配置读取
211211
3. **Binance 数据源未实现** - 可靠性不足
212+
4. **CCXT 客户端集成测试** - 需要 Python 测试脚本验证
212213

213214
### 低优先级
214215

215-
4. **缺少 API 速率限制** - 可能被滥用
216+
1. **缺少 API 速率限制** - 可能被滥用
216217

217218
---
218219

docs/ccxt-testing-guide.md

Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
1+
# CCXT 客户端测试脚本使用指南
2+
3+
本文档说明如何使用 `scripts/test_ccxt_client.py` 测试 Quicksilver 与 CCXT 的兼容性。
4+
5+
## 快速开始
6+
7+
### 1. 安装依赖
8+
9+
```bash
10+
# 安装 CCXT 库
11+
pip install ccxt
12+
13+
# 或使用虚拟环境
14+
python3 -m venv venv
15+
source venv/bin/activate # Linux/Mac
16+
# venv\Scripts\activate # Windows
17+
pip install ccxt
18+
```
19+
20+
### 2. 启动 Quicksilver 服务
21+
22+
```bash
23+
# 启动数据库
24+
docker-compose up -d postgres
25+
26+
# 启动服务
27+
make run
28+
#
29+
make dev
30+
```
31+
32+
### 3. 准备测试用户
33+
34+
```bash
35+
# 创建测试用户并初始化余额
36+
./scripts/init_test_user.sh
37+
```
38+
39+
这将创建一个测试用户并显示:
40+
41+
```
42+
API Key: qs_test_1234567890abcdef
43+
API Secret: secret_1234567890abcdef1234567890abcdef
44+
```
45+
46+
### 4. 运行测试
47+
48+
#### 仅测试公开 API(无需认证)
49+
50+
```bash
51+
python scripts/test_ccxt_client.py
52+
```
53+
54+
#### 测试完整功能(包括私有 API)
55+
56+
```bash
57+
python scripts/test_ccxt_client.py \
58+
--api-key "qs_test_1234567890abcdef" \
59+
--api-secret "secret_1234567890abcdef1234567890abcdef"
60+
```
61+
62+
#### 测试远程服务器
63+
64+
```bash
65+
python scripts/test_ccxt_client.py \
66+
--url "https://your-quicksilver-instance.com" \
67+
--api-key "YOUR_API_KEY" \
68+
--api-secret "YOUR_API_SECRET"
69+
```
70+
71+
## 测试覆盖范围
72+
73+
### 公开 API(无需认证)
74+
75+
| 端点 | CCXT 方法 | 测试内容 |
76+
| ------------------------ | --------------- | ------------------- |
77+
| `GET /v1/time` | `publicGetTime` | 服务器时间 |
78+
| `GET /v1/markets` | `fetchMarkets` | 交易对列表 |
79+
| `GET /v1/ticker/:symbol` | `fetchTicker` | 行情数据(BTC/ETH) |
80+
| `GET /v1/trades/:symbol` | `fetchTrades` | 最近成交记录 |
81+
82+
### 私有 API(需要认证)
83+
84+
| 端点 | CCXT 方法 | 测试内容 |
85+
| ---------------------- | ----------------- | --------------------- |
86+
| `GET /v1/balance` | `fetchBalance` | 账户余额 |
87+
| `POST /v1/order` | `createOrder` | 创建订单(限价/市价) |
88+
| `GET /v1/order/:id` | `fetchOrder` | 查询单个订单 |
89+
| `DELETE /v1/order/:id` | `cancelOrder` | 撤销订单 |
90+
| `GET /v1/orders` | `fetchOrders` | 查询所有订单 |
91+
| `GET /v1/orders/open` | `fetchOpenOrders` | 查询未完成订单 |
92+
| `GET /v1/myTrades` | `fetchMyTrades` | 查询我的成交记录 |
93+
94+
## 示例输出
95+
96+
### 成功示例
97+
98+
```
99+
============================================================
100+
Quicksilver CCXT 兼容性测试
101+
============================================================
102+
103+
📂 Public API Tests
104+
------------------------------------------------------------
105+
🔍 Testing: Server Time
106+
✅ PASS | GET /v1/time
107+
Server Time: 2025-11-05T13:45:30.123Z
108+
109+
🔍 Testing: Fetch Markets
110+
✅ PASS | GET /v1/markets
111+
Total Markets: 2
112+
Sample: BTC/USDT
113+
114+
🔍 Testing: Fetch Ticker (BTC/USDT)
115+
✅ PASS | GET /v1/ticker/BTC/USDT
116+
Last Price: 109965.5
117+
24h Volume: 123.45
118+
119+
...
120+
121+
📂 Private API Tests (Authenticated)
122+
------------------------------------------------------------
123+
🔍 Testing: Fetch Balance
124+
✅ PASS | GET /v1/balance
125+
Assets: 2
126+
127+
🔍 Testing: Create Order (buy limit)
128+
✅ PASS | POST /v1/order
129+
Order ID: 123
130+
Status: new
131+
132+
...
133+
134+
============================================================
135+
测试结果汇总
136+
============================================================
137+
✅ Passed: 12
138+
❌ Failed: 0
139+
📊 Success Rate: 100.0%
140+
============================================================
141+
```
142+
143+
### 失败示例
144+
145+
```
146+
🔍 Testing: Fetch Ticker (BTC/USDT)
147+
❌ FAIL | GET /v1/ticker/BTC/USDT
148+
└─ Error: 缺少字段: baseVolume
149+
150+
============================================================
151+
测试结果汇总
152+
============================================================
153+
✅ Passed: 8
154+
❌ Failed: 1
155+
📊 Success Rate: 88.9%
156+
157+
❌ Failed Tests:
158+
- GET /v1/ticker/BTC/USDT: 缺少字段: baseVolume
159+
============================================================
160+
```
161+
162+
## 故障排查
163+
164+
### 问题 1: 连接失败
165+
166+
```
167+
Error: [Errno 61] Connection refused
168+
```
169+
170+
**解决方案**:
171+
172+
- 确认 Quicksilver 服务正在运行: `curl http://localhost:8080/health`
173+
- 检查端口是否正确: 默认 8080
174+
175+
### 问题 2: 认证失败
176+
177+
```
178+
Error: user not authenticated
179+
```
180+
181+
**解决方案**:
182+
183+
- 确认 API Key/Secret 正确
184+
- 检查测试用户是否存在: `psql -h localhost -U quicksilver -d quicksilver -c "SELECT * FROM users;"`
185+
- 重新创建测试用户: `./scripts/init_test_user.sh`
186+
187+
### 问题 3: 数据为空
188+
189+
```
190+
Total Trades: 0
191+
```
192+
193+
**解决方案**:
194+
195+
- 确认行情数据已同步: `curl http://localhost:8080/v1/ticker/BTC-USDT`
196+
- 检查撮合引擎是否运行
197+
- 手动创建订单触发成交
198+
199+
### 问题 4: 格式不兼容
200+
201+
```
202+
Error: 缺少字段: baseVolume
203+
```
204+
205+
**解决方案**:
206+
207+
- 检查 `internal/ccxt/transformer.go` 的格式转换逻辑
208+
- 运行单元测试: `make test-unit`
209+
- 查看 CCXT 标准文档: https://docs.ccxt.com/
210+
211+
## 集成到 CI/CD
212+
213+
### GitHub Actions 示例
214+
215+
```yaml
216+
name: CCXT Integration Tests
217+
218+
on: [push, pull_request]
219+
220+
jobs:
221+
test:
222+
runs-on: ubuntu-latest
223+
224+
services:
225+
postgres:
226+
image: postgres:16
227+
env:
228+
POSTGRES_USER: quicksilver
229+
POSTGRES_PASSWORD: quicksilver123
230+
POSTGRES_DB: quicksilver
231+
ports:
232+
- 5432:5432
233+
234+
steps:
235+
- uses: actions/checkout@v3
236+
237+
- name: Set up Go
238+
uses: actions/setup-go@v4
239+
with:
240+
go-version: "1.24"
241+
242+
- name: Set up Python
243+
uses: actions/setup-python@v4
244+
with:
245+
python-version: "3.11"
246+
247+
- name: Install CCXT
248+
run: pip install ccxt
249+
250+
- name: Build Quicksilver
251+
run: make build
252+
253+
- name: Start Quicksilver
254+
run: |
255+
./bin/quicksilver &
256+
sleep 5
257+
258+
- name: Create test user
259+
run: ./scripts/init_test_user.sh
260+
261+
- name: Run CCXT tests
262+
run: |
263+
python scripts/test_ccxt_client.py \
264+
--api-key "$TEST_API_KEY" \
265+
--api-secret "$TEST_API_SECRET"
266+
```
267+
268+
## 扩展测试
269+
270+
### 添加新的测试用例
271+
272+
`QuicksilverTester` 类中添加新方法:
273+
274+
```python
275+
def test_your_feature(self):
276+
"""测试新功能"""
277+
print("🔍 Testing: Your Feature")
278+
try:
279+
response = self.exchange.yourApiMethod()
280+
281+
# 验证逻辑
282+
assert 'field' in response, "缺少字段"
283+
284+
self.log_test("Your Test", True)
285+
return True
286+
except Exception as e:
287+
self.log_test("Your Test", False, str(e))
288+
return False
289+
```
290+
291+
然后在 `run_all_tests()` 中调用:
292+
293+
```python
294+
def run_all_tests(self):
295+
# ...现有测试...
296+
self.test_your_feature()
297+
```
298+
299+
## 相关文档
300+
301+
- CCXT 官方文档: https://docs.ccxt.com/
302+
- Quicksilver API 文档: `docs/api-reference.md`
303+
- 系统设计文档: `docs/system-design-mvp.md`

0 commit comments

Comments
 (0)