forked from ross-32/API-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync-config.js
More file actions
256 lines (218 loc) · 8.69 KB
/
sync-config.js
File metadata and controls
256 lines (218 loc) · 8.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/usr/bin/env node
/**
* Configuration Sync Script / 配置同步脚本
*
* This script syncs the shared-config.js to all demo directories
* 此脚本将shared-config.js同步到所有示例目录
*
* Usage / 使用方法:
* $ node sync-config.js
*/
const fs = require('fs');
const path = require('path');
// Color codes for terminal output / 终端输出颜色代码
const colors = {
reset: '\x1b[0m',
green: '\x1b[32m',
yellow: '\x1b[33m',
red: '\x1b[31m',
blue: '\x1b[34m',
cyan: '\x1b[36m',
};
/**
* Print colored message / 打印彩色消息
*/
function print(message, color = 'reset') {
console.log(`${colors[color]}${message}${colors.reset}`);
}
/**
* Read shared configuration / 读取共享配置
*/
function readSharedConfig() {
const configPath = path.join(__dirname, 'shared-config.js');
if (!fs.existsSync(configPath)) {
print('❌ Error: shared-config.js not found!', 'red');
print(' Please create shared-config.js first.', 'red');
print(' 请先创建 shared-config.js 文件。', 'red');
process.exit(1);
}
try {
const config = require(configPath);
return config;
} catch (error) {
print(`❌ Error reading shared-config.js: ${error.message}`, 'red');
process.exit(1);
}
}
/**
* Generate config content for a specific directory / 为特定目录生成配置内容
*/
function generateConfigContent(sharedConfig, type) {
const configs = {
spot: {
baseUrl: sharedConfig.SPOT_BASE_URL || 'https://sapi.asterdex.com',
needsWallet: true,
symbol: sharedConfig.DEFAULT_SYMBOL || 'BNBUSDT',
},
futures: {
baseUrl: sharedConfig.FUTURES_BASE_URL || 'https://fapi.asterdex.com',
needsWallet: false,
symbol: sharedConfig.DEFAULT_SYMBOL || 'BTCUSDT',
},
'futures-v3': {
baseUrl: sharedConfig.FUTURES_BASE_URL || 'https://fapi.asterdex.com',
needsWallet: false,
symbol: sharedConfig.DEFAULT_SYMBOL || 'BTCUSDT',
},
};
const typeConfig = configs[type];
let content = `/**
* API Configuration / API配置
*
* ⚠️ This file is auto-generated by sync-config.js
* ⚠️ 此文件由 sync-config.js 自动生成
*
* To update, edit shared-config.js and run: node sync-config.js
* 要更新,请编辑 shared-config.js 并运行:node sync-config.js
*/
module.exports = {
// Base URL / 基础URL
BASE_URL: '${typeConfig.baseUrl}',
// API Key / API密钥
API_KEY: '${sharedConfig.API_KEY}',
// Secret Key / 密钥
SECRET_KEY: '${sharedConfig.SECRET_KEY}',
`;
if (typeConfig.needsWallet) {
content += `
// Wallet Private Key (for wallet signature endpoints) / 钱包私钥(用于钱包签名接口)
PRIVATE_KEY: '${sharedConfig.PRIVATE_KEY}',
// Wallet Address / 钱包地址
WALLET_ADDRESS: '${sharedConfig.WALLET_ADDRESS}',
`;
}
content += `
// Default trading pair / 默认交易对
DEFAULT_SYMBOL: '${typeConfig.symbol}',
// Receive window (milliseconds) / 接收窗口(毫秒)
RECV_WINDOW: ${sharedConfig.RECV_WINDOW || 5000}
};
`;
return content;
}
/**
* Sync configuration to a directory / 同步配置到目录
*/
function syncToDirectory(sharedConfig, directory, type) {
const configPath = path.join(__dirname, directory, 'config.js');
const content = generateConfigContent(sharedConfig, type);
try {
fs.writeFileSync(configPath, content, 'utf8');
print(` ✓ Synced to ${directory}/config.js`, 'green');
return true;
} catch (error) {
print(` ✗ Failed to sync to ${directory}/config.js: ${error.message}`, 'red');
return false;
}
}
/**
* Validate configuration / 验证配置
*/
function validateConfig(config) {
const warnings = [];
const errors = [];
// Check required fields / 检查必需字段
if (!config.API_KEY || config.API_KEY === 'your_api_key_here') {
warnings.push('API_KEY is not configured / API_KEY未配置');
}
if (!config.SECRET_KEY || config.SECRET_KEY === 'your_secret_key_here') {
warnings.push('SECRET_KEY is not configured / SECRET_KEY未配置');
}
// Check wallet config / 检查钱包配置
if (config.PRIVATE_KEY && config.PRIVATE_KEY !== '0x0000000000000000000000000000000000000000000000000000000000000000') {
if (!config.PRIVATE_KEY.match(/^0x[a-fA-F0-9]{64}$/)) {
errors.push('PRIVATE_KEY format invalid (should be 0x + 64 hex chars) / PRIVATE_KEY格式无效');
}
}
if (config.WALLET_ADDRESS && config.WALLET_ADDRESS !== '0x0000000000000000000000000000000000000000') {
if (!config.WALLET_ADDRESS.match(/^0x[a-fA-F0-9]{40}$/)) {
errors.push('WALLET_ADDRESS format invalid (should be 0x + 40 hex chars) / WALLET_ADDRESS格式无效');
}
}
return { warnings, errors };
}
/**
* Main function / 主函数
*/
function main() {
print('\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━', 'cyan');
print(' Configuration Sync / 配置同步', 'cyan');
print('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n', 'cyan');
// Read shared config / 读取共享配置
print('Reading shared-config.js... / 读取共享配置中...', 'blue');
const sharedConfig = readSharedConfig();
print('✓ Shared configuration loaded / 共享配置已加载\n', 'green');
// Validate config / 验证配置
print('Validating configuration... / 验证配置中...', 'blue');
const { warnings, errors } = validateConfig(sharedConfig);
if (errors.length > 0) {
print('\n❌ Configuration Errors / 配置错误:', 'red');
errors.forEach(err => print(` • ${err}`, 'red'));
print('\nPlease fix errors in shared-config.js / 请修复 shared-config.js 中的错误\n', 'red');
process.exit(1);
}
if (warnings.length > 0) {
print('\n⚠️ Configuration Warnings / 配置警告:', 'yellow');
warnings.forEach(warn => print(` • ${warn}`, 'yellow'));
print('');
} else {
print('✓ Configuration looks good / 配置看起来不错\n', 'green');
}
// Sync to all directories / 同步到所有目录
print('Syncing to all directories... / 同步到所有目录中...', 'blue');
const directories = [
{ dir: 'spot-demo', type: 'spot' },
{ dir: 'futures-demo', type: 'futures' },
{ dir: 'futures-v3-demo', type: 'futures-v3' },
];
let successCount = 0;
directories.forEach(({ dir, type }) => {
if (syncToDirectory(sharedConfig, dir, type)) {
successCount++;
}
});
// Summary / 总结
print('\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━', 'cyan');
if (successCount === directories.length) {
print('✓ All configurations synced successfully!', 'green');
print('✓ 所有配置已成功同步!', 'green');
print('\n🎉 Now you can use all API examples!', 'green');
print('🎉 现在您可以使用所有API示例了!', 'green');
} else {
print(`⚠️ ${successCount}/${directories.length} configurations synced`, 'yellow');
print(`⚠️ ${successCount}/${directories.length} 个配置已同步`, 'yellow');
}
print('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n', 'cyan');
// Next steps / 下一步
if (warnings.length > 0) {
print('📝 Next steps / 下一步:', 'blue');
print(' 1. Edit shared-config.js to fill in your credentials', 'blue');
print(' 编辑 shared-config.js 填写您的凭证', 'blue');
print(' 2. Run this script again: node sync-config.js', 'blue');
print(' 再次运行此脚本:node sync-config.js', 'blue');
print(' 3. Test with: cd spot-demo && node 01_ping.js', 'blue');
print(' 测试:cd spot-demo && node 01_ping.js\n', 'blue');
} else {
print('🚀 Ready to use! Try running:', 'green');
print(' cd spot-demo && node 01_ping.js', 'green');
print(' cd futures-demo && node 01_ping.js\n', 'green');
}
}
// Run main function / 运行主函数
try {
main();
} catch (error) {
print(`\n❌ Unexpected error / 意外错误: ${error.message}`, 'red');
print(error.stack, 'red');
process.exit(1);
}