refactor(exchanges): fetchKlines 改为 params 对象签名,新增 type/exchange 参数
- 新增 FetchKlinesParams 接口统一 fetchKlines 参数(exchange/type/symbol/startTime/limit/endTime) - BaseRestClient 抽象方法 + MarketDataFeed 接口改为 params 对象签名 - BinanceRestClient / BinanceFuturesRestClient 由调用方传入 type,不再硬编码 spot/um - index.ts 新增静态 fetchKlines(params) 便捷函数,按 exchange+type 自动路由 - exchange.ts / bnkline.ts 调用方适配新签名 - 初始化 SQL 补充 BNBUSDT/SOLUSDT 合约交易对
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { BaseRestClient } from "./base";
|
||||
import { BinanceRestClient, BinanceFuturesRestClient } from "./binance/rest";
|
||||
import type { FetchKlinesParams, Kline } from "../types";
|
||||
|
||||
/** 交易所 ID 到 RestClient 构造器的注册表 */
|
||||
const registry: Record<string, () => BaseRestClient> = {
|
||||
@@ -28,6 +29,37 @@ export function createRestClient(exchangeId: string): BaseRestClient {
|
||||
return factory();
|
||||
}
|
||||
|
||||
/**
|
||||
* 拉取历史 K 线(静态便捷方法)。
|
||||
*
|
||||
* 根据 exchange + type 自动路由到正确的交易所客户端,
|
||||
* 调用方无需手动创建 client 或选择 spot/futures 子类。
|
||||
*
|
||||
* 路由规则:
|
||||
* - type = "spot" → exchange(如 "binance")
|
||||
* - type = "um"/"cm" → `${exchange}_futures`(如 "binance_futures")
|
||||
*
|
||||
* @param params - fetchKlines 统一参数对象
|
||||
* @returns 标准化 K 线数组,按时间升序
|
||||
*
|
||||
* @example
|
||||
* const klines = await fetchKlines({
|
||||
* exchange: 'binance',
|
||||
* type: 'um',
|
||||
* symbol: 'BTCUSDT',
|
||||
* startTime: 1700000000000,
|
||||
* limit: 500,
|
||||
* });
|
||||
*/
|
||||
export async function fetchKlines(params: FetchKlinesParams): Promise<Kline[]> {
|
||||
const exchangeId =
|
||||
params.type === "spot"
|
||||
? params.exchange
|
||||
: `${params.exchange}_futures`;
|
||||
const client = createRestClient(exchangeId);
|
||||
return client.fetchKlines(params);
|
||||
}
|
||||
|
||||
export { BaseRestClient } from "./base";
|
||||
export { BinanceRestClient, BinanceFuturesRestClient } from "./binance/rest";
|
||||
export { KLINE_INTERVAL_MS } from "./constants";
|
||||
|
||||
Reference in New Issue
Block a user