@clonegod/ttd-core 3.1.4 → 3.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/app_config/EnvArgs.d.ts +2 -1
- package/dist/app_config/EnvArgs.js +47 -27
- package/dist/app_config/env_loader.d.ts +3 -0
- package/dist/app_config/env_loader.js +102 -0
- package/dist/app_config/env_registry.d.ts +11 -0
- package/dist/app_config/env_registry.js +38 -0
- package/dist/app_config/index.d.ts +2 -0
- package/dist/app_config/index.js +2 -0
- package/dist/constants/service_ports.d.ts +1 -1
- package/dist/constants/service_ports.js +1 -1
- package/package.json +1 -1
|
@@ -2,39 +2,59 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.EnvArgs = void 0;
|
|
4
4
|
const __1 = require("..");
|
|
5
|
+
const env_loader_1 = require("./env_loader");
|
|
6
|
+
const ENVARGS_KEYS = [
|
|
7
|
+
'app_name', 'app_base_dir', 'chain_id', 'dex_id',
|
|
8
|
+
'redis_host', 'redis_port',
|
|
9
|
+
'config_center_host', 'trade_analyze_url',
|
|
10
|
+
'rpc_endpoint', 'ws_endpoint', 'grpc_endpoint', 'grpc_token',
|
|
11
|
+
'auto_quote_enable', 'auto_quote_interval_mills',
|
|
12
|
+
'quote_pair', 'quote_amount_usd', 'quote_pool_address', 'quote_pool_name', 'quote_pool_fee_rate',
|
|
13
|
+
'trade_group_id', 'trade_pair',
|
|
14
|
+
'token_price_cache_seconds', 'wallet_dir',
|
|
15
|
+
];
|
|
5
16
|
class EnvArgs {
|
|
6
17
|
constructor(chain_id, dex_id) {
|
|
7
|
-
var _a
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
18
|
+
var _a;
|
|
19
|
+
const overrides = {};
|
|
20
|
+
if (chain_id)
|
|
21
|
+
overrides.chain_id = chain_id;
|
|
22
|
+
if (dex_id)
|
|
23
|
+
overrides.dex_id = dex_id;
|
|
24
|
+
const cfg = (0, env_loader_1.loadEnvConfig)(ENVARGS_KEYS, overrides);
|
|
25
|
+
this.app_name = cfg.app_name || '';
|
|
26
|
+
this.app_base_dir = cfg.app_base_dir || '';
|
|
27
|
+
this.chain_id = (cfg.chain_id || '').toUpperCase();
|
|
28
|
+
this.dex_id = (cfg.dex_id || '').toUpperCase();
|
|
11
29
|
if ((0, __1.isEmpty)(this.chain_id)) {
|
|
12
30
|
throw new Error(`environment: CHAIN_ID is empty!!!`);
|
|
13
31
|
}
|
|
14
|
-
this.
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
this.
|
|
18
|
-
this.
|
|
19
|
-
this.
|
|
20
|
-
this.
|
|
21
|
-
this.
|
|
22
|
-
this.
|
|
23
|
-
this.
|
|
24
|
-
this.
|
|
25
|
-
this.
|
|
26
|
-
this.
|
|
27
|
-
this.
|
|
28
|
-
this.
|
|
29
|
-
this.
|
|
30
|
-
this.
|
|
31
|
-
this.
|
|
32
|
-
this.
|
|
33
|
-
this.trade_pair = process.env.TRADE_PAIR || '';
|
|
34
|
-
this.token_price_cache_seconds = parseInt(process.env.TOKEN_PRICE_CACHE_SECONDS || '1200');
|
|
32
|
+
this.redis_host = cfg.redis_host || '127.0.0.1';
|
|
33
|
+
this.redis_port = ((_a = cfg.redis_port) === null || _a === void 0 ? void 0 : _a.toString()) || '6379';
|
|
34
|
+
this.config_center_host = cfg.config_center_host || '';
|
|
35
|
+
this.trade_analyze_url = cfg.trade_analyze_url || '';
|
|
36
|
+
this.rpc_endpoint = cfg.rpc_endpoint || '';
|
|
37
|
+
this.ws_endpoint = cfg.ws_endpoint || '';
|
|
38
|
+
this.grpc_endpoint = cfg.grpc_endpoint || '';
|
|
39
|
+
this.grpc_token = cfg.grpc_token || '';
|
|
40
|
+
this.auto_quote_enable = cfg.auto_quote_enable || false;
|
|
41
|
+
this.auto_quote_interval_mills = cfg.auto_quote_interval_mills || 60000;
|
|
42
|
+
this.quote_pair = cfg.quote_pair || '';
|
|
43
|
+
this.quote_amount_usd = cfg.quote_amount_usd || 100;
|
|
44
|
+
this.quote_pool_address = cfg.quote_pool_address || '';
|
|
45
|
+
this.quote_pool_name = cfg.quote_pool_name || '';
|
|
46
|
+
this.quote_pool_fee_rate = cfg.quote_pool_fee_rate || 0;
|
|
47
|
+
this.trade_group_id = cfg.trade_group_id || '';
|
|
48
|
+
this.trade_pair = cfg.trade_pair || '';
|
|
49
|
+
this.token_price_cache_seconds = cfg.token_price_cache_seconds || 1200;
|
|
50
|
+
this.wallet_dir = cfg.wallet_dir || '';
|
|
35
51
|
}
|
|
36
|
-
print() {
|
|
37
|
-
|
|
52
|
+
print(moduleName) {
|
|
53
|
+
const cfg = {};
|
|
54
|
+
for (const key of ENVARGS_KEYS) {
|
|
55
|
+
cfg[key] = this[key];
|
|
56
|
+
}
|
|
57
|
+
(0, env_loader_1.printEnvConfig)(moduleName || this.app_name || 'EnvArgs', cfg, ENVARGS_KEYS);
|
|
38
58
|
}
|
|
39
59
|
}
|
|
40
60
|
exports.EnvArgs = EnvArgs;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export declare function loadEnvConfig(keys: string[], overrides?: Record<string, any>): Record<string, any>;
|
|
2
|
+
export declare function validateEnvConfig(keys: string[]): string[];
|
|
3
|
+
export declare function printEnvConfig(moduleName: string, config: Record<string, any>, keys: string[]): void;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.loadEnvConfig = loadEnvConfig;
|
|
4
|
+
exports.validateEnvConfig = validateEnvConfig;
|
|
5
|
+
exports.printEnvConfig = printEnvConfig;
|
|
6
|
+
const env_registry_1 = require("./env_registry");
|
|
7
|
+
function parseValue(raw, def) {
|
|
8
|
+
var _a;
|
|
9
|
+
const str = raw !== undefined ? raw : undefined;
|
|
10
|
+
if (str === undefined || str === '') {
|
|
11
|
+
return def.default !== undefined ? def.default : getTypeDefault(def.type);
|
|
12
|
+
}
|
|
13
|
+
switch (def.type) {
|
|
14
|
+
case 'number':
|
|
15
|
+
const n = Number(str);
|
|
16
|
+
return isNaN(n) ? ((_a = def.default) !== null && _a !== void 0 ? _a : 0) : n;
|
|
17
|
+
case 'boolean':
|
|
18
|
+
return str === 'true' || str === '1';
|
|
19
|
+
case 'string[]':
|
|
20
|
+
return str.split(',').map(s => s.trim()).filter(Boolean);
|
|
21
|
+
default:
|
|
22
|
+
return str;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
function getTypeDefault(type) {
|
|
26
|
+
switch (type) {
|
|
27
|
+
case 'number': return 0;
|
|
28
|
+
case 'boolean': return false;
|
|
29
|
+
case 'string[]': return [];
|
|
30
|
+
default: return '';
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function loadEnvConfig(keys, overrides) {
|
|
34
|
+
var _a, _b;
|
|
35
|
+
const registry = (0, env_registry_1.getEnvRegistry)();
|
|
36
|
+
const config = {};
|
|
37
|
+
for (const key of keys) {
|
|
38
|
+
const def = registry[key];
|
|
39
|
+
if (!def) {
|
|
40
|
+
console.warn(`[EnvConfig] unknown key: ${key} (not registered)`);
|
|
41
|
+
config[key] = (_b = (_a = overrides === null || overrides === void 0 ? void 0 : overrides[key]) !== null && _a !== void 0 ? _a : process.env[key.toUpperCase()]) !== null && _b !== void 0 ? _b : '';
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (overrides && key in overrides && overrides[key] !== undefined) {
|
|
45
|
+
config[key] = overrides[key];
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
config[key] = parseValue(process.env[def.env], def);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return config;
|
|
52
|
+
}
|
|
53
|
+
function validateEnvConfig(keys) {
|
|
54
|
+
const registry = (0, env_registry_1.getEnvRegistry)();
|
|
55
|
+
const missing = [];
|
|
56
|
+
for (const key of keys) {
|
|
57
|
+
const def = registry[key];
|
|
58
|
+
if (!def)
|
|
59
|
+
continue;
|
|
60
|
+
if (!def.required)
|
|
61
|
+
continue;
|
|
62
|
+
const val = process.env[def.env];
|
|
63
|
+
if (val === undefined || val === '') {
|
|
64
|
+
missing.push(`${def.env} (${def.desc || key})`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return missing;
|
|
68
|
+
}
|
|
69
|
+
const SENSITIVE_RE = /key|token|secret|password|private|auth/i;
|
|
70
|
+
function maskValue(key, value, def) {
|
|
71
|
+
if (value === undefined || value === null || value === '')
|
|
72
|
+
return '(empty)';
|
|
73
|
+
const str = String(value);
|
|
74
|
+
const isSensitive = (def === null || def === void 0 ? void 0 : def.sensitive) || SENSITIVE_RE.test(key);
|
|
75
|
+
if (isSensitive && str.length > 4) {
|
|
76
|
+
return str.slice(0, 4) + '****';
|
|
77
|
+
}
|
|
78
|
+
return str;
|
|
79
|
+
}
|
|
80
|
+
function printEnvConfig(moduleName, config, keys) {
|
|
81
|
+
const registry = (0, env_registry_1.getEnvRegistry)();
|
|
82
|
+
const divider = '═'.repeat(70);
|
|
83
|
+
const lines = [];
|
|
84
|
+
lines.push('');
|
|
85
|
+
lines.push(divider);
|
|
86
|
+
lines.push(` ${moduleName.toUpperCase()} — Configuration`);
|
|
87
|
+
lines.push(divider);
|
|
88
|
+
const maxKeyLen = Math.max(...keys.map(k => {
|
|
89
|
+
const def = registry[k];
|
|
90
|
+
return def ? def.env.length : k.length;
|
|
91
|
+
}), 20);
|
|
92
|
+
for (const key of keys) {
|
|
93
|
+
const def = registry[key];
|
|
94
|
+
const envName = def ? def.env : key.toUpperCase();
|
|
95
|
+
const val = maskValue(key, config[key], def);
|
|
96
|
+
const desc = (def === null || def === void 0 ? void 0 : def.desc) ? ` # ${def.desc}` : '';
|
|
97
|
+
lines.push(` ${envName.padEnd(maxKeyLen)} ${val}${desc}`);
|
|
98
|
+
}
|
|
99
|
+
lines.push(divider);
|
|
100
|
+
lines.push('');
|
|
101
|
+
console.log(lines.join('\n'));
|
|
102
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export type EnvVarType = 'string' | 'number' | 'boolean' | 'string[]';
|
|
2
|
+
export interface EnvVarDef {
|
|
3
|
+
env: string;
|
|
4
|
+
type: EnvVarType;
|
|
5
|
+
default?: any;
|
|
6
|
+
required?: boolean;
|
|
7
|
+
sensitive?: boolean;
|
|
8
|
+
desc?: string;
|
|
9
|
+
}
|
|
10
|
+
export declare function registerEnvVars(defs: Record<string, EnvVarDef>): void;
|
|
11
|
+
export declare function getEnvRegistry(): Readonly<Record<string, EnvVarDef>>;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.registerEnvVars = registerEnvVars;
|
|
4
|
+
exports.getEnvRegistry = getEnvRegistry;
|
|
5
|
+
const _registry = {};
|
|
6
|
+
function registerEnvVars(defs) {
|
|
7
|
+
Object.assign(_registry, defs);
|
|
8
|
+
}
|
|
9
|
+
function getEnvRegistry() {
|
|
10
|
+
return _registry;
|
|
11
|
+
}
|
|
12
|
+
registerEnvVars({
|
|
13
|
+
app_name: { env: 'APP_NAME', type: 'string', desc: '应用名称' },
|
|
14
|
+
app_base_dir: { env: 'APP_BASE_DIR', type: 'string', desc: '应用根目录' },
|
|
15
|
+
chain_id: { env: 'CHAIN_ID', type: 'string', required: true, desc: '链标识(BSC/SOLANA/TRON)' },
|
|
16
|
+
dex_id: { env: 'DEX_ID', type: 'string', desc: 'DEX 标识' },
|
|
17
|
+
redis_host: { env: 'REDIS_HOST', type: 'string', default: '127.0.0.1', desc: 'Redis 地址' },
|
|
18
|
+
redis_port: { env: 'REDIS_PORT', type: 'number', default: 6379, desc: 'Redis 端口' },
|
|
19
|
+
config_center_host: { env: 'CONFIG_CENTER_HOST', type: 'string', default: '', desc: 'config-center 地址' },
|
|
20
|
+
trade_analyze_host: { env: 'TRADE_ANALYZE_HOST', type: 'string', default: '', desc: 'analyze 地址' },
|
|
21
|
+
trade_analyze_port: { env: 'TRADE_ANALYZE_PORT', type: 'number', default: 8004, desc: 'analyze 端口' },
|
|
22
|
+
trade_analyze_url: { env: 'TRADE_ANALYZE_URL', type: 'string', default: '', desc: 'analyze URL(旧)' },
|
|
23
|
+
rpc_endpoint: { env: 'RPC_ENDPOINT', type: 'string', default: '', desc: 'RPC HTTP 端点' },
|
|
24
|
+
ws_endpoint: { env: 'WS_ENDPOINT', type: 'string', default: '', desc: 'RPC WebSocket 端点' },
|
|
25
|
+
grpc_endpoint: { env: 'GRPC_ENDPOINT', type: 'string', default: '', desc: 'gRPC 端点' },
|
|
26
|
+
grpc_token: { env: 'GRPC_TOKEN', type: 'string', default: '', sensitive: true, desc: 'gRPC 认证 token' },
|
|
27
|
+
auto_quote_enable: { env: 'AUTO_QUOTE_ENABLE', type: 'boolean', default: false, desc: '自动报价开关' },
|
|
28
|
+
auto_quote_interval_mills: { env: 'AUTO_QUOTE_INTERVAL_MILLS', type: 'number', default: 60000, desc: '自动报价间隔(ms)' },
|
|
29
|
+
quote_pair: { env: 'QUOTE_PAIR', type: 'string', default: '', desc: '报价币对' },
|
|
30
|
+
quote_amount_usd: { env: 'QUOTE_AMOUNT_USD', type: 'number', default: 100, desc: '报价金额(USD)' },
|
|
31
|
+
quote_pool_address: { env: 'QUOTE_POOL_ADDRESS', type: 'string', default: '', desc: '指定池子地址' },
|
|
32
|
+
quote_pool_name: { env: 'QUOTE_POOL_NAME', type: 'string', default: '', desc: '指定池子名称' },
|
|
33
|
+
quote_pool_fee_rate: { env: 'QUOTE_POOL_FEE_RATE', type: 'number', default: 0, desc: '指定池子费率' },
|
|
34
|
+
trade_group_id: { env: 'TRADE_GROUP_ID', type: 'string', default: '', desc: '交易组 ID' },
|
|
35
|
+
trade_pair: { env: 'TRADE_PAIR', type: 'string', default: '', desc: '交易币对' },
|
|
36
|
+
token_price_cache_seconds: { env: 'TOKEN_PRICE_CACHE_SECONDS', type: 'number', default: 1200, desc: 'token 价格缓存时间(s)' },
|
|
37
|
+
wallet_dir: { env: 'WALLET_DIR', type: 'string', default: '', desc: '钱包目录' },
|
|
38
|
+
});
|
package/dist/app_config/index.js
CHANGED
|
@@ -18,6 +18,8 @@ exports.getGlobalAppConfig = void 0;
|
|
|
18
18
|
__exportStar(require("./EnvArgs"), exports);
|
|
19
19
|
__exportStar(require("./AppConfig"), exports);
|
|
20
20
|
__exportStar(require("./AbstractTradeAppConfig"), exports);
|
|
21
|
+
__exportStar(require("./env_registry"), exports);
|
|
22
|
+
__exportStar(require("./env_loader"), exports);
|
|
21
23
|
const getGlobalAppConfig = () => {
|
|
22
24
|
let app_config = global.app_config;
|
|
23
25
|
if (!app_config) {
|
|
@@ -10,7 +10,7 @@ var SERVICE_PORT;
|
|
|
10
10
|
SERVICE_PORT[SERVICE_PORT["TRADE_PROXY_WS"] = 8002] = "TRADE_PROXY_WS";
|
|
11
11
|
SERVICE_PORT[SERVICE_PORT["TRADE_MGT_HTTP"] = 8003] = "TRADE_MGT_HTTP";
|
|
12
12
|
SERVICE_PORT[SERVICE_PORT["TRADE_ANALYZE_HTTP"] = 8004] = "TRADE_ANALYZE_HTTP";
|
|
13
|
-
SERVICE_PORT[SERVICE_PORT["
|
|
13
|
+
SERVICE_PORT[SERVICE_PORT["MARKET_DATA_HTTP"] = 8005] = "MARKET_DATA_HTTP";
|
|
14
14
|
SERVICE_PORT[SERVICE_PORT["WALLET_ASSETS_HTTP"] = 8006] = "WALLET_ASSETS_HTTP";
|
|
15
15
|
SERVICE_PORT[SERVICE_PORT["STREAM_QUOTE_WS"] = 8011] = "STREAM_QUOTE_WS";
|
|
16
16
|
SERVICE_PORT[SERVICE_PORT["SEND_TX_WS"] = 8012] = "SEND_TX_WS";
|