@yuants/vendor-huobi 0.18.2 → 0.18.4
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/api/private-api.js +50 -25
- package/dist/api/private-api.js.map +1 -1
- package/dist/api/public-api.js +86 -16
- package/dist/api/public-api.js.map +1 -1
- package/dist/services/accounts/swap.js +3 -3
- package/dist/services/accounts/swap.js.map +1 -1
- package/dist/services/orders/submitOrder.js +2 -2
- package/dist/services/orders/submitOrder.js.map +1 -1
- package/lib/api/private-api.d.ts +1 -1
- package/lib/api/private-api.d.ts.map +1 -1
- package/lib/api/private-api.js +50 -26
- package/lib/api/private-api.js.map +1 -1
- package/lib/api/public-api.d.ts +1 -1
- package/lib/api/public-api.d.ts.map +1 -1
- package/lib/api/public-api.js +85 -15
- package/lib/api/public-api.js.map +1 -1
- package/lib/services/accounts/swap.js +3 -3
- package/lib/services/accounts/swap.js.map +1 -1
- package/lib/services/orders/submitOrder.js +2 -2
- package/lib/services/orders/submitOrder.js.map +1 -1
- package/package.json +15 -15
- package/temp/package-deps.json +21 -21
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"public-api.js","sourceRoot":"","sources":["../../src/api/public-api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAE3C,gBAAgB;AAChB,MAAM,aAAa,GAAG,cAAc,CAAC;AACrC,MAAM,aAAa,GAAG,eAAe,CAAC;AAEtC;;GAEG;AACH,KAAK,UAAU,aAAa,CAAC,MAAc,EAAE,IAAY,EAAE,QAAgB,EAAE,MAAY;IACvF,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,WAAW,QAAQ,GAAG,IAAI,EAAE,CAAC,CAAC;IAElD,IAAI,MAAM,KAAK,KAAK,EAAE;QACpB,KAAK,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,IAAI,EAAE,CAAC,EAA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AMnC;IAeC,OAAO,aAAa,CAAC,KAAK,EAAE,sCAAsC,EAAE,aAAa,EAAE,MAAM,CAAC,CAAC;AAC7F,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,wBAAwB,CAAC,MAKxC;IAIC,OAAO,aAAa,CAAC,KAAK,EAAE,8CAA8C,EAAE,aAAa,EAAE,MAAM,CAAC,CAAC;AACrG,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,wBAAwB,CAAC,MAAsD;IAkC7F,OAAO,aAAa,CAAC,KAAK,EAAE,0BAA0B,EAAE,aAAa,EAAE,MAAM,CAAC,CAAC;AACjF,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG,GAiBjC,EAAE,CAAC,aAAa,CAAC,KAAK,EAAE,iBAAiB,EAAE,aAAa,CAAC,CAAC","sourcesContent":["import { formatTime } from '@yuants/utils';\n\n// Huobi API 根域名\nconst SWAP_API_ROOT = 'api.hbdm.com';\nconst SPOT_API_ROOT = 'api.huobi.pro';\n\n/**\n * 公共 API 请求方法\n */\nasync function publicRequest(method: string, path: string, api_root: string, params?: any) {\n const url = new URL(`https://${api_root}${path}`);\n\n if (method === 'GET') {\n for (const [k, v] of Object.entries(params || {})) {\n url.searchParams.append(k, String(v));\n }\n }\n\n const body = method === 'GET' ? '' : JSON.stringify(params);\n\n console.info(formatTime(Date.now()), method, url.href, body);\n\n const res = await fetch(url.href, {\n method,\n headers: { 'Content-Type': 'application/json' },\n body: body || undefined,\n });\n\n console.info(formatTime(Date.now()), 'PublicResponse', url.toString(), res.status);\n\n const retStr = await res.text();\n try {\n return JSON.parse(retStr);\n } catch (e) {\n console.error(formatTime(Date.now()), 'huobiRequestFailed', path, JSON.stringify(params), retStr);\n throw e;\n }\n}\n\n// ==================== 公共 API 方法 ====================\n\n/**\n * 获取永续合约产品信息\n *\n * https://www.htx.com/zh-cn/opend/newApiPages/?id=8cb72f34-77b5-11ed-9966-0242ac110003\n */\nexport function getPerpetualContractSymbols(params?: {\n contract_code?: string;\n support_margin_mode?: string;\n pair?: string;\n contract_type?: string;\n business_type?: string;\n}): Promise<{\n status: string;\n data: {\n symbol: string;\n contract_code: string;\n contract_size: number;\n price_tick: number;\n settlement_date: string;\n delivery_time: string;\n create_date: string;\n contract_status: number;\n support_margin_mode: string;\n contract_type: string;\n pair: string;\n business_type: string;\n delivery_date: string;\n }[];\n ts: string;\n}> {\n return publicRequest('GET', '/linear-swap-api/v1/swap_contract_info', SWAP_API_ROOT, params);\n}\n\n/**\n * 获取现货产品信息\n *\n * https://www.htx.com/zh-cn/opend/newApiPages/?id=7ec47f16-7773-11ed-9966-0242ac110003\n */\nexport function getSpotSymbols(): Promise<{\n status: string;\n data: {\n si: string;\n scr: string;\n sc: string;\n dn: string;\n bc: string;\n bcdn: string;\n qc: string;\n qcdn: string;\n state: string;\n whe: boolean;\n cd: boolean;\n te: boolean;\n toa: number;\n sp: string;\n w: number;\n ttp: number;\n tap: number;\n tpp: number;\n fp: number;\n suspend_desc: string;\n transfer_board_desc: string;\n tags: string;\n lr: number;\n smlr: number;\n flr: string;\n wr: string;\n d: number;\n elr: number;\n p: any;\n castate: string;\n ca1oa: number;\n ca2oa: number;\n }[];\n ts: string;\n full: number;\n err_code: string;\n err_msg: string;\n}> {\n return publicRequest('GET', '/v2/settings/common/symbols', SPOT_API_ROOT);\n}\n\n/**\n * 获取现货行情\n *\n * https://www.htx.com/zh-cn/opend/newApiPages/?id=7ec3fc25-7773-11ed-9966-0242ac110003\n */\nexport function getSpotTick(params: { symbol: string }): Promise<{\n status: string;\n tick: {\n close: number;\n };\n}> {\n return publicRequest('GET', `/market/detail/merged`, SPOT_API_ROOT, params);\n}\n\n/**\n * 【现货】获取K线数据\n *\n * https://www.htx.com/zh-cn/opend/newApiPages/?id=7ec3ff18-7773-11ed-9966-0242ac110003\n *\n * Note: HTX spot KLine uses unix seconds in `data[].id`\n */\nexport function getSpotHistoryKline(params: {\n symbol: string;\n period: string;\n size?: number;\n from?: number;\n to?: number;\n}): Promise<{\n status: string;\n ch?: string;\n ts: number;\n data: {\n id: number;\n open: number;\n close: number;\n low: number;\n high: number;\n vol: number;\n amount?: number;\n count?: number;\n }[];\n}> {\n return publicRequest('GET', `/market/history/kline`, SPOT_API_ROOT, params);\n}\n\n/**\n * 【通用】批量获取合约资金费率\n *\n * 接口权限: 读取\n *\n * 限频: 其他非行情类的公开接口,比如获取指数信息,限价信息,交割结算、平台持仓信息等,所有用户都是每个IP3秒最多240次请求(所有该IP的非行情类的公开接口请求共享3秒240次的额度)\n *\n * 接口描述: 该接口支持全仓模式和逐仓模式\n *\n * https://www.htx.com/zh-cn/opend/newApiPages/?id=8cb71b45-77b5-11ed-9966-0242ac110003\n */\nexport function getSwapBatchFundingRate(params: { contract_code?: string }): Promise<{\n status: string;\n ts: number;\n data: {\n estimated_rate: null;\n funding_rate: string;\n contract_code: string;\n symbol: string;\n fee_asset: string;\n funding_time: string;\n next_funding_time: null;\n trade_partition: string;\n }[];\n}> {\n return publicRequest('GET', `/linear-swap-api/v1/swap_batch_funding_rate`, SWAP_API_ROOT, params);\n}\n\n/**\n * 【通用】获取市场最近成交记录\n *\n * 接口权限: 读取\n *\n * 限频: 行情类的公开接口,比如:获取K线数据、获取聚合行情、市场行情、获取行情深度数据、获取溢价指数K线、获取实时预测资金费率k线,获取基差数据、获取市场最近成交记录:\n *\n * (1) restful接口:同一个IP, 所有业务(交割合约、币本位永续合约和U本位合约)总共1秒最多800个请求\n *\n * 接口描述: 该接口支持全仓模式和逐仓模式\n *\n * 请求参数contract_code支持交割合约代码,格式为BTC-USDT-210625;同时支持合约标识,格式为 BTC-USDT(永续)、BTC-USDT-CW(当周)、BTC-USDT-NW(次周)、BTC-USDT-CQ(当季)、BTC-USDT-NQ(次季)。\n *\n * business_type 在查询交割合约数据时为必填参数。且参数值要传:futures 或 all 。\n *\n * https://www.htx.com/zh-cn/opend/newApiPages/?id=8cb73c34-77b5-11ed-9966-0242ac110003\n */\nexport function getSwapMarketTrade(params: { contract_code?: string; business_type?: string }): Promise<{\n ch: string;\n status: string;\n tick: {\n data: {\n amount: string;\n quantity: string;\n trade_turnover: string;\n ts: number;\n id: number;\n price: string;\n direction: string;\n contract_code: string;\n business_type: string;\n trade_partition: string;\n }[];\n id: number;\n ts: number;\n };\n ts: number;\n}> {\n return publicRequest('GET', `/linear-swap-ex/market/trade`, SWAP_API_ROOT, params);\n}\n\n/**\n * 获得当前合约的总持仓量\n *\n * https://huobiapi.github.io/docs/usdt_swap/v1/cn/#3218e7531a\n */\nexport function getSwapOpenInterest(params: {\n contract_code?: string;\n pair?: string;\n contract_type?: string;\n business_type?: string;\n}): Promise<{\n status: string;\n data: {\n volume: number;\n amount: number;\n symbol: string;\n value: number;\n contract_code: string;\n trade_amount: number;\n trade_volume: number;\n trade_turnover: number;\n business_type: string;\n pair: string;\n contract_type: string;\n }[];\n ts: number;\n}> {\n return publicRequest('GET', '/linear-swap-api/v1/swap_open_interest', SWAP_API_ROOT, params);\n}\n\n/**\n * 【通用】获取市场最优挂单\n *\n * 接口权限: 读取\n *\n * 限频: 行情类的公开接口,比如:获取K线数据、获取聚合行情、市场行情、获取行情深度数据、获取溢价指数K线、获取实时预测资金费率k线,获取基差数据、获取市场最近成交记录:\n *\n * (1) restful接口:同一个IP, 所有业务(交割合约、币本位永续合约和U本位合约)总共1秒最多800个请求\n *\n * 接口描述: 该接口支持全仓模式和逐仓模式\n *\n * 请求参数contract_code支持交割合约代码,格式为BTC-USDT-210625;同时支持合约标识,格式为 BTC-USDT(永续)、BTC-USDT-CW(当周)、BTC-USDT-NW(次周)、BTC-USDT-CQ(当季)、BTC-USDT-NQ(次季)。\n *\n * business_type 在查询交割合约数据时为必填参数。且参数值要传:futures 或 all 。\n *\n * https://www.htx.com/zh-cn/opend/newApiPages/?id=8cb735e0-77b5-11ed-9966-0242ac110003\n */\nexport function getSwapMarketBbo(params: { contract_code?: string }): Promise<{\n status: string;\n ticks: {\n trade_partition: string;\n business_type: string;\n contract_code: string;\n ask: number[] | null;\n bid: number[] | null;\n mrid: number;\n ts: number;\n }[];\n ts: number;\n}> {\n return publicRequest('GET', `/linear-swap-ex/market/bbo`, SWAP_API_ROOT, params);\n}\n\n/**\n * 获取合约的历史资金费率\n *\n * 接口权限: 读取\n *\n * https://www.htx.com/zh-cn/opend/newApiPages/?id=5d51f1f9-77b6-11ed-9966-0242ac110003\n */\nexport function getSwapHistoricalFundingRate(params: {\n contract_code: string;\n page_index?: number;\n page_size?: number;\n}): Promise<{\n status: string;\n ts: number;\n data: {\n data: {\n symbol: string;\n contract_code: string;\n fee_asset: string;\n funding_time: string;\n funding_rate: string;\n realized_rate: string;\n avg_premium_index: string;\n }[];\n total_page: number;\n current_page: number;\n total_size: number;\n };\n}> {\n return publicRequest('GET', `/linear-swap-api/v1/swap_historical_funding_rate`, SWAP_API_ROOT, params);\n}\n\n/**\n * 【通用】获取K线数据\n *\n * https://www.htx.com/zh-cn/opend/newApiPages/?id=8cb73746-77b5-11ed-9966-0242ac110003\n */\nexport function getSwapHistoryKline(params: {\n contract_code: string;\n period: string;\n size?: number;\n from?: number;\n to?: number;\n}): Promise<{\n status: string;\n ch?: string;\n ts: number;\n data: {\n id: number;\n open: number;\n close: number;\n low: number;\n high: number;\n vol: number;\n amount?: number;\n count?: number;\n }[];\n}> {\n return publicRequest('GET', `/linear-swap-ex/market/history/kline`, SWAP_API_ROOT, params);\n}\n\n/**\n * 【全仓】获取平台阶梯保证金\n *\n * https://www.htx.com/zh-cn/opend/newApiPages/?id=8cb72290-77b5-11ed-9966-0242ac110003\n */\nexport function getSwapCrossLadderMargin(params?: {\n contract_code?: string;\n pair?: string;\n contract_type?: string;\n business_type?: string;\n}): Promise<{\n status: string;\n data: Array<{ contract_code: string; pair: string; list: Array<{ lever_rate: number }> }>;\n}> {\n return publicRequest('GET', '/linear-swap-api/v1/swap_cross_ladder_margin', SWAP_API_ROOT, params);\n}\n\n/**\n * APIv2币链参考信息\n *\n * https://www.htx.com/zh-cn/opend/newApiPages/?id=7ec478f0-7773-11ed-9966-0242ac110003\n */\nexport function getV2ReferenceCurrencies(params: { currency?: string; authorizedUser?: string }): Promise<{\n code: number;\n data: {\n currency: string;\n assetType: number;\n chains: {\n chain: string;\n displayName: string;\n fullName: string;\n baseChain?: string;\n baseChainProtocol?: string;\n isDynamic?: boolean;\n numOfConfirmations: number;\n numOfFastConfirmations: number;\n depositStatus: string;\n minDepositAmt: string;\n withdrawStatus: string;\n minWithdrawAmt: string;\n withdrawPrecision: number;\n maxWithdrawAmt: string;\n withdrawQuotaPerDay: string;\n withdrawQuotaPerYear: null;\n withdrawQuotaTotal: null;\n withdrawFeeType: string;\n transactFeeWithdraw?: string;\n addrWithTag: boolean;\n addrDepositTag: boolean;\n minTransactFeeWithdraw?: string;\n transactFeeRateWithdraw?: string;\n maxTransactFeeWithdraw?: string;\n }[];\n instStatus: string;\n }[];\n}> {\n return publicRequest('GET', '/v2/reference/currencies', SPOT_API_ROOT, params);\n}\n\n/**\n * 【现货】获取市场所有交易对的最新行情\n *\n * https://www.htx.com/zh-cn/opend/newApiPages/?id=7ec40374-7773-11ed-9966-0242ac110003\n *\n */\nexport const getSpotMarketTickers = (): Promise<{\n data: {\n symbol: string;\n open: number;\n high: number;\n low: number;\n close: number;\n amount: number;\n vol: number;\n count: number;\n bid: number;\n bidSize: number;\n ask: number;\n askSize: number;\n }[];\n status: string;\n ts: number;\n}> => publicRequest('GET', '/market/tickers', SPOT_API_ROOT);\n"]}
|
|
1
|
+
{"version":3,"file":"public-api.js","sourceRoot":"","sources":["../../src/api/public-api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAEpE,gBAAgB;AAChB,MAAM,aAAa,GAAG,cAAc,CAAC;AACrC,MAAM,aAAa,GAAG,eAAe,CAAC;AAKtC,MAAM,OAAO,GAAG,CAAC,QAAgB,EAAE,IAA6B,EAAE,EAAE;IAClE,MAAM,MA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M,CAAC,CAAC;AAC9E,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,4BAA4B,CAAC,MAI5C;IAkBC,OAAO,0BAA0B,CAAC,KAAK,EAAE,kDAAkD,EAAE,MAAM,CAAC,CAAC;AACvG,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,mBAAmB,CAAC,MAMnC;IAeC,OAAO,uBAAuB,CAAC,KAAK,EAAE,sCAAsC,EAAE,MAAM,CAAC,CAAC;AACxF,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,wBAAwB,CAAC,MAKxC;IAIC,OAAO,0BAA0B,CAAC,KAAK,EAAE,8CAA8C,EAAE,MAAM,CAAC,CAAC;AACnG,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,wBAAwB,CAAC,MAAsD;IAkC7F,OAAO,oBAAoB,CAAC,KAAK,EAAE,0BAA0B,EAAE,MAAM,CAAC,CAAC;AACzE,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG,GAiBjC,EAAE,CAAC,iBAAiB,CAAC,KAAK,EAAE,iBAAiB,CAAC,CAAC","sourcesContent":["import { formatTime, scopeError, tokenBucket } from '@yuants/utils';\n\n// Huobi API 根域名\nconst SWAP_API_ROOT = 'api.hbdm.com';\nconst SPOT_API_ROOT = 'api.huobi.pro';\n\ntype HuobiBusiness = 'spot' | 'linear-swap';\ntype HuobiPublicInterfaceType = 'market' | 'non-market';\n\nconst acquire = (bucketId: string, meta: Record<string, unknown>) => {\n const bucket = tokenBucket(bucketId);\n scopeError('HUOBI_API_RATE_LIMIT', { ...meta, bucketId }, () => bucket.acquireSync(1));\n};\n\n// https://www.htx.com/zh-cn/opend/newApiPages/?id=474\n// 行情类:同一个 IP,总共 1s 最多 800 个请求(合约业务共享总额度)\nconst marketDataIPAllBucketId = 'HUOBI_PUBLIC_MARKET_IP_1S_ALL';\ntokenBucket(marketDataIPAllBucketId, {\n capacity: 800,\n refillInterval: 1000,\n refillAmount: 800,\n});\n\n// 行情类:按业务线拆分(用于交割/币本位永续/U本位分开限频)\nconst marketDataSpotBucketId = 'HUOBI_PUBLIC_MARKET_IP_1S_SPOT';\ntokenBucket(marketDataSpotBucketId, { capacity: 800, refillInterval: 1000, refillAmount: 800 });\n\nconst marketDataLinearSwapBucketId = 'HUOBI_PUBLIC_MARKET_IP_1S_LINEAR_SWAP';\ntokenBucket(marketDataLinearSwapBucketId, { capacity: 800, refillInterval: 1000, refillAmount: 800 });\n\n// 非行情类公开接口:同一个 IP,3s 最多 120 次请求\nconst nonMarketDataIPBucketId = 'HUOBI_PUBLIC_NON_MARKET_IP_3S_ALL';\ntokenBucket(nonMarketDataIPBucketId, {\n capacity: 120,\n refillInterval: 3000,\n refillAmount: 120,\n});\n\n/**\n * 公共 API 请求方法(不负责限流;限流由调用点选择对应 helper)\n */\nasync function publicRequest(method: string, path: string, api_root: string, params?: any) {\n const url = new URL(`https://${api_root}${path}`);\n\n if (method === 'GET') {\n for (const [k, v] of Object.entries(params || {})) {\n url.searchParams.append(k, String(v));\n }\n }\n\n const body = method === 'GET' ? '' : JSON.stringify(params);\n\n console.info(formatTime(Date.now()), method, url.href, body);\n\n const res = await fetch(url.href, {\n method,\n headers: { 'Content-Type': 'application/json' },\n body: body || undefined,\n });\n\n console.info(formatTime(Date.now()), 'PublicResponse', url.toString(), res.status);\n\n const retStr = await res.text();\n try {\n return JSON.parse(retStr);\n } catch (e) {\n console.error(formatTime(Date.now()), 'huobiRequestFailed', path, JSON.stringify(params), retStr);\n throw e;\n }\n}\n\nconst spotMarketRequest = async (method: string, path: string, params?: any) => {\n const meta = {\n method,\n api_root: SPOT_API_ROOT,\n path,\n business: 'spot' as HuobiBusiness,\n interfaceType: 'market' as HuobiPublicInterfaceType,\n };\n acquire(marketDataIPAllBucketId, meta);\n acquire(marketDataSpotBucketId, meta);\n return publicRequest(method, path, SPOT_API_ROOT, params);\n};\n\nconst spotNonMarketRequest = async (method: string, path: string, params?: any) => {\n const meta = {\n method,\n api_root: SPOT_API_ROOT,\n path,\n business: 'spot' as HuobiBusiness,\n interfaceType: 'non-market' as HuobiPublicInterfaceType,\n };\n acquire(nonMarketDataIPBucketId, meta);\n return publicRequest(method, path, SPOT_API_ROOT, params);\n};\n\nconst linearSwapMarketRequest = async (method: string, path: string, params?: any) => {\n const meta = {\n method,\n api_root: SWAP_API_ROOT,\n path,\n business: 'linear-swap' as HuobiBusiness,\n interfaceType: 'market' as HuobiPublicInterfaceType,\n };\n acquire(marketDataIPAllBucketId, meta);\n acquire(marketDataLinearSwapBucketId, meta);\n return publicRequest(method, path, SWAP_API_ROOT, params);\n};\n\nconst linearSwapNonMarketRequest = async (method: string, path: string, params?: any) => {\n const meta = {\n method,\n api_root: SWAP_API_ROOT,\n path,\n business: 'linear-swap' as HuobiBusiness,\n interfaceType: 'non-market' as HuobiPublicInterfaceType,\n };\n acquire(nonMarketDataIPBucketId, meta);\n return publicRequest(method, path, SWAP_API_ROOT, params);\n};\n\n// ==================== 公共 API 方法 ====================\n\n/**\n * 获取永续合约产品信息\n *\n * https://www.htx.com/zh-cn/opend/newApiPages/?id=8cb72f34-77b5-11ed-9966-0242ac110003\n */\nexport function getPerpetualContractSymbols(params?: {\n contract_code?: string;\n support_margin_mode?: string;\n pair?: string;\n contract_type?: string;\n business_type?: string;\n}): Promise<{\n status: string;\n data: {\n symbol: string;\n contract_code: string;\n contract_size: number;\n price_tick: number;\n settlement_date: string;\n delivery_time: string;\n create_date: string;\n contract_status: number;\n support_margin_mode: string;\n contract_type: string;\n pair: string;\n business_type: string;\n delivery_date: string;\n }[];\n ts: string;\n}> {\n return linearSwapNonMarketRequest('GET', '/linear-swap-api/v1/swap_contract_info', params);\n}\n\n/**\n * 获取现货产品信息\n *\n * https://www.htx.com/zh-cn/opend/newApiPages/?id=7ec47f16-7773-11ed-9966-0242ac110003\n */\nexport function getSpotSymbols(): Promise<{\n status: string;\n data: {\n si: string;\n scr: string;\n sc: string;\n dn: string;\n bc: string;\n bcdn: string;\n qc: string;\n qcdn: string;\n state: string;\n whe: boolean;\n cd: boolean;\n te: boolean;\n toa: number;\n sp: string;\n w: number;\n ttp: number;\n tap: number;\n tpp: number;\n fp: number;\n suspend_desc: string;\n transfer_board_desc: string;\n tags: string;\n lr: number;\n smlr: number;\n flr: string;\n wr: string;\n d: number;\n elr: number;\n p: any;\n castate: string;\n ca1oa: number;\n ca2oa: number;\n }[];\n ts: string;\n full: number;\n err_code: string;\n err_msg: string;\n}> {\n return spotNonMarketRequest('GET', '/v2/settings/common/symbols');\n}\n\n/**\n * 获取现货行情\n *\n * https://www.htx.com/zh-cn/opend/newApiPages/?id=7ec3fc25-7773-11ed-9966-0242ac110003\n */\nexport function getSpotTick(params: { symbol: string }): Promise<{\n status: string;\n tick: {\n close: number;\n };\n}> {\n return spotMarketRequest('GET', `/market/detail/merged`, params);\n}\n\n/**\n * 【现货】获取K线数据\n *\n * https://www.htx.com/zh-cn/opend/newApiPages/?id=7ec3ff18-7773-11ed-9966-0242ac110003\n *\n * Note: HTX spot KLine uses unix seconds in `data[].id`\n */\nexport function getSpotHistoryKline(params: {\n symbol: string;\n period: string;\n size?: number;\n from?: number;\n to?: number;\n}): Promise<{\n status: string;\n ch?: string;\n ts: number;\n data: {\n id: number;\n open: number;\n close: number;\n low: number;\n high: number;\n vol: number;\n amount?: number;\n count?: number;\n }[];\n}> {\n return spotMarketRequest('GET', `/market/history/kline`, params);\n}\n\n/**\n * 【通用】批量获取合约资金费率\n *\n * 接口权限: 读取\n *\n * 限频: 其他非行情类的公开接口,比如获取指数信息,限价信息,交割结算、平台持仓信息等,所有用户都是每个IP3秒最多120次请求(所有该IP的非行情类的公开接口请求共享3秒120次的额度)\n *\n * 接口描述: 该接口支持全仓模式和逐仓模式\n *\n * https://www.htx.com/zh-cn/opend/newApiPages/?id=8cb71b45-77b5-11ed-9966-0242ac110003\n */\nexport function getSwapBatchFundingRate(params: { contract_code?: string }): Promise<{\n status: string;\n ts: number;\n data: {\n estimated_rate: null;\n funding_rate: string;\n contract_code: string;\n symbol: string;\n fee_asset: string;\n funding_time: string;\n next_funding_time: null;\n trade_partition: string;\n }[];\n}> {\n return linearSwapNonMarketRequest('GET', `/linear-swap-api/v1/swap_batch_funding_rate`, params);\n}\n\n/**\n * 【通用】获取市场最近成交记录\n *\n * 接口权限: 读取\n *\n * 限频: 行情类的公开接口,比如:获取K线数据、获取聚合行情、市场行情、获取行情深度数据、获取溢价指数K线、获取实时预测资金费率k线,获取基差数据、获取市场最近成交记录:\n *\n * (1) restful接口:同一个IP, 所有业务(交割合约、币本位永续合约和U本位合约)总共1秒最多800个请求\n *\n * 接口描述: 该接口支持全仓模式和逐仓模式\n *\n * 请求参数contract_code支持交割合约代码,格式为BTC-USDT-210625;同时支持合约标识,格式为 BTC-USDT(永续)、BTC-USDT-CW(当周)、BTC-USDT-NW(次周)、BTC-USDT-CQ(当季)、BTC-USDT-NQ(次季)。\n *\n * business_type 在查询交割合约数据时为必填参数。且参数值要传:futures 或 all 。\n *\n * https://www.htx.com/zh-cn/opend/newApiPages/?id=8cb73c34-77b5-11ed-9966-0242ac110003\n */\nexport function getSwapMarketTrade(params: { contract_code?: string; business_type?: string }): Promise<{\n ch: string;\n status: string;\n tick: {\n data: {\n amount: string;\n quantity: string;\n trade_turnover: string;\n ts: number;\n id: number;\n price: string;\n direction: string;\n contract_code: string;\n business_type: string;\n trade_partition: string;\n }[];\n id: number;\n ts: number;\n };\n ts: number;\n}> {\n return linearSwapMarketRequest('GET', `/linear-swap-ex/market/trade`, params);\n}\n\n/**\n * 获得当前合约的总持仓量\n *\n * https://huobiapi.github.io/docs/usdt_swap/v1/cn/#3218e7531a\n */\nexport function getSwapOpenInterest(params: {\n contract_code?: string;\n pair?: string;\n contract_type?: string;\n business_type?: string;\n}): Promise<{\n status: string;\n data: {\n volume: number;\n amount: number;\n symbol: string;\n value: number;\n contract_code: string;\n trade_amount: number;\n trade_volume: number;\n trade_turnover: number;\n business_type: string;\n pair: string;\n contract_type: string;\n }[];\n ts: number;\n}> {\n return linearSwapMarketRequest('GET', '/linear-swap-api/v1/swap_open_interest', params);\n}\n\n/**\n * 【通用】获取市场最优挂单\n *\n * 接口权限: 读取\n *\n * 限频: 行情类的公开接口,比如:获取K线数据、获取聚合行情、市场行情、获取行情深度数据、获取溢价指数K线、获取实时预测资金费率k线,获取基差数据、获取市场最近成交记录:\n *\n * (1) restful接口:同一个IP, 所有业务(交割合约、币本位永续合约和U本位合约)总共1秒最多800个请求\n *\n * 接口描述: 该接口支持全仓模式和逐仓模式\n *\n * 请求参数contract_code支持交割合约代码,格式为BTC-USDT-210625;同时支持合约标识,格式为 BTC-USDT(永续)、BTC-USDT-CW(当周)、BTC-USDT-NW(次周)、BTC-USDT-CQ(当季)、BTC-USDT-NQ(次季)。\n *\n * business_type 在查询交割合约数据时为必填参数。且参数值要传:futures 或 all 。\n *\n * https://www.htx.com/zh-cn/opend/newApiPages/?id=8cb735e0-77b5-11ed-9966-0242ac110003\n */\nexport function getSwapMarketBbo(params: { contract_code?: string }): Promise<{\n status: string;\n ticks: {\n trade_partition: string;\n business_type: string;\n contract_code: string;\n ask: number[] | null;\n bid: number[] | null;\n mrid: number;\n ts: number;\n }[];\n ts: number;\n}> {\n return linearSwapMarketRequest('GET', `/linear-swap-ex/market/bbo`, params);\n}\n\n/**\n * 获取合约的历史资金费率\n *\n * 接口权限: 读取\n *\n * https://www.htx.com/zh-cn/opend/newApiPages/?id=5d51f1f9-77b6-11ed-9966-0242ac110003\n */\nexport function getSwapHistoricalFundingRate(params: {\n contract_code: string;\n page_index?: number;\n page_size?: number;\n}): Promise<{\n status: string;\n ts: number;\n data: {\n data: {\n symbol: string;\n contract_code: string;\n fee_asset: string;\n funding_time: string;\n funding_rate: string;\n realized_rate: string;\n avg_premium_index: string;\n }[];\n total_page: number;\n current_page: number;\n total_size: number;\n };\n}> {\n return linearSwapNonMarketRequest('GET', `/linear-swap-api/v1/swap_historical_funding_rate`, params);\n}\n\n/**\n * 【通用】获取K线数据\n *\n * https://www.htx.com/zh-cn/opend/newApiPages/?id=8cb73746-77b5-11ed-9966-0242ac110003\n */\nexport function getSwapHistoryKline(params: {\n contract_code: string;\n period: string;\n size?: number;\n from?: number;\n to?: number;\n}): Promise<{\n status: string;\n ch?: string;\n ts: number;\n data: {\n id: number;\n open: number;\n close: number;\n low: number;\n high: number;\n vol: number;\n amount?: number;\n count?: number;\n }[];\n}> {\n return linearSwapMarketRequest('GET', `/linear-swap-ex/market/history/kline`, params);\n}\n\n/**\n * 【全仓】获取平台阶梯保证金\n *\n * https://www.htx.com/zh-cn/opend/newApiPages/?id=8cb72290-77b5-11ed-9966-0242ac110003\n */\nexport function getSwapCrossLadderMargin(params?: {\n contract_code?: string;\n pair?: string;\n contract_type?: string;\n business_type?: string;\n}): Promise<{\n status: string;\n data: Array<{ contract_code: string; pair: string; list: Array<{ lever_rate: number }> }>;\n}> {\n return linearSwapNonMarketRequest('GET', '/linear-swap-api/v1/swap_cross_ladder_margin', params);\n}\n\n/**\n * APIv2币链参考信息\n *\n * https://www.htx.com/zh-cn/opend/newApiPages/?id=7ec478f0-7773-11ed-9966-0242ac110003\n */\nexport function getV2ReferenceCurrencies(params: { currency?: string; authorizedUser?: string }): Promise<{\n code: number;\n data: {\n currency: string;\n assetType: number;\n chains: {\n chain: string;\n displayName: string;\n fullName: string;\n baseChain?: string;\n baseChainProtocol?: string;\n isDynamic?: boolean;\n numOfConfirmations: number;\n numOfFastConfirmations: number;\n depositStatus: string;\n minDepositAmt: string;\n withdrawStatus: string;\n minWithdrawAmt: string;\n withdrawPrecision: number;\n maxWithdrawAmt: string;\n withdrawQuotaPerDay: string;\n withdrawQuotaPerYear: null;\n withdrawQuotaTotal: null;\n withdrawFeeType: string;\n transactFeeWithdraw?: string;\n addrWithTag: boolean;\n addrDepositTag: boolean;\n minTransactFeeWithdraw?: string;\n transactFeeRateWithdraw?: string;\n maxTransactFeeWithdraw?: string;\n }[];\n instStatus: string;\n }[];\n}> {\n return spotNonMarketRequest('GET', '/v2/reference/currencies', params);\n}\n\n/**\n * 【现货】获取市场所有交易对的最新行情\n *\n * https://www.htx.com/zh-cn/opend/newApiPages/?id=7ec40374-7773-11ed-9966-0242ac110003\n *\n */\nexport const getSpotMarketTickers = (): Promise<{\n data: {\n symbol: string;\n open: number;\n high: number;\n low: number;\n close: number;\n amount: number;\n vol: number;\n count: number;\n bid: number;\n bidSize: number;\n ask: number;\n askSize: number;\n }[];\n status: string;\n ts: number;\n}> => spotMarketRequest('GET', '/market/tickers');\n"]}
|
|
@@ -14,10 +14,10 @@ export const getSwapAccountInfo = async (credential) => {
|
|
|
14
14
|
if (v.margin_balance === 0)
|
|
15
15
|
continue;
|
|
16
16
|
positions.push(makeSpotPosition({
|
|
17
|
-
position_id: encodePath('HTX', 'SWAP-
|
|
17
|
+
position_id: encodePath('HTX', 'SWAP-ASSET', v.margin_asset),
|
|
18
18
|
datasource_id: 'HTX',
|
|
19
|
-
product_id: encodePath('HTX', 'SWAP-
|
|
20
|
-
volume: +v.
|
|
19
|
+
product_id: encodePath('HTX', 'SWAP-ASSET', v.margin_asset),
|
|
20
|
+
volume: +v.margin_static,
|
|
21
21
|
free_volume: +v.withdraw_available,
|
|
22
22
|
closable_price: usdAssets.has(v.margin_asset) ? 1 : 0,
|
|
23
23
|
}));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"swap.js","sourceRoot":"","sources":["../../../src/services/accounts/swap.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AACnE,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,wBAAwB,EAAE,qBAAqB,EAAe,MAAM,uBAAuB,CAAC;AACrG,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE1C,MAAM,SAAS,GAAG,IAAI,GAAG,CAAC,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;AAEpD,MAA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,EAAE,CAAC,CAAC,MAAM;YAChB,WAAW,EAAE,CAAC,CAAC,SAAS;YACxB,cAAc,EAAE,CAAC,CAAC,SAAS;YAC3B,cAAc,EAAE,CAAC,CAAC,UAAU;YAC5B,eAAe,EAAE,CAAC,CAAC,aAAa;YAChC,iBAAiB,EAAE,CAAC,CAAC,iBAAiB,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,iBAAiB,EAAE,CAAC,CAAC,CAAC,SAAS;YAC7E,SAAS;SACV,CAAC,CAAC;KACJ;IAED,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC","sourcesContent":["import { IPosition, makeSpotPosition } from '@yuants/data-account';\nimport { encodePath } from '@yuants/utils';\nimport { getSwapCrossPositionInfo, getUnifiedAccountInfo, ICredential } from '../../api/private-api';\nimport { productCache } from '../product';\n\nconst usdAssets = new Set(['USDT', 'USDC', 'USDD']);\n\nexport const getSwapAccountInfo = async (credential: ICredential): Promise<IPosition[]> => {\n const positions: IPosition[] = [];\n // balance\n const balance = await getUnifiedAccountInfo(credential);\n if (!balance.data) {\n throw new Error('Failed to get unified account info');\n }\n\n for (const v of balance.data || []) {\n if (v.margin_balance === 0) continue;\n positions.push(\n makeSpotPosition({\n position_id: encodePath('HTX', 'SWAP-ASSET', v.margin_asset),\n datasource_id: 'HTX',\n product_id: encodePath('HTX', 'SWAP-ASSET', v.margin_asset),\n volume: +v.margin_static,\n free_volume: +v.withdraw_available,\n closable_price: usdAssets.has(v.margin_asset) ? 1 : 0,\n }),\n );\n }\n\n // positions\n const positionsRes = await getSwapCrossPositionInfo(credential);\n for (const v of positionsRes.data || []) {\n const product_id = encodePath('HTX', 'SWAP', v.contract_code);\n const theProduct = await productCache.query(product_id);\n const valuation = v.volume * v.last_price * (theProduct?.value_scale || 1);\n positions.push({\n position_id: `${product_id}/${v.contract_type}/${v.direction}/${v.margin_mode}`,\n datasource_id: 'HTX',\n product_id,\n direction: v.direction === 'buy' ? 'LONG' : 'SHORT',\n volume: v.volume,\n free_volume: v.available,\n position_price: v.cost_hold,\n closable_price: v.last_price,\n floating_profit: v.profit_unreal,\n liquidation_price: v.liquidation_price ? `${v.liquidation_price}` : undefined,\n valuation,\n });\n }\n\n return positions;\n};\n"]}
|
|
@@ -11,8 +11,8 @@ async function handleSwapOrder(order, credential) {
|
|
|
11
11
|
// 获取仓位信息
|
|
12
12
|
const positionInfo = await getSwapCrossPositionInfo(credential);
|
|
13
13
|
const mapContractCodeToRate = Object.fromEntries(positionInfo.data.map((v) => [v.contract_code, v.lever_rate]));
|
|
14
|
-
const lever_rate = (_a = mapContractCodeToRate[order.product_id]) !== null && _a !== void 0 ? _a : 20;
|
|
15
14
|
const [, instType, contractCode] = decodePath(order.product_id);
|
|
15
|
+
const lever_rate = (_a = mapContractCodeToRate[contractCode]) !== null && _a !== void 0 ? _a : 20;
|
|
16
16
|
const params = {
|
|
17
17
|
contract_code: contractCode,
|
|
18
18
|
contract_type: 'swap',
|
|
@@ -28,7 +28,7 @@ async function handleSwapOrder(order, credential) {
|
|
|
28
28
|
const result = await postSwapOrder(credential, params);
|
|
29
29
|
console.info(formatTime(Date.now()), 'SubmitOrder', JSON.stringify(result), JSON.stringify(params));
|
|
30
30
|
if (result.status !== 'ok') {
|
|
31
|
-
throw
|
|
31
|
+
throw newError(`HTX_SUBMIT_ORDER_FAILED`, { result });
|
|
32
32
|
}
|
|
33
33
|
return { order_id: result.data.order_id_str };
|
|
34
34
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"submitOrder.js","sourceRoot":"","sources":["../../../src/services/orders/submitOrder.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC9E,OAAO,EAEL,sBAAsB,EACtB,qBAAqB,EACrB,wBAAwB,EACxB,aAAa,EACb,aAAa,GACd,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,C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@yuants/utils';\nimport {\n ICredential,\n getCrossMarginLoanInfo,\n getSpotAccountBalance,\n getSwapCrossPositionInfo,\n postSpotOrder,\n postSwapOrder,\n} from '../../api/private-api';\nimport { getSpotTick } from '../../api/public-api';\nimport { productCache } from '../product';\nimport { superMarginAccountUidCache } from '../uid';\n\n/**\n * 处理 swap 账户订单提交\n */\nasync function handleSwapOrder(order: IOrder, credential: ICredential): Promise<{ order_id: string }> {\n // 获取仓位信息\n const positionInfo = await getSwapCrossPositionInfo(credential);\n const mapContractCodeToRate = Object.fromEntries(\n positionInfo.data.map((v) => [v.contract_code, v.lever_rate]),\n );\n\n const [, instType, contractCode] = decodePath(order.product_id);\n const lever_rate = mapContractCodeToRate[contractCode] ?? 20;\n const params = {\n contract_code: contractCode,\n contract_type: 'swap',\n price: order.price,\n volume: order.volume,\n offset:\n order.order_direction === 'OPEN_LONG' || order.order_direction === 'OPEN_SHORT' ? 'open' : 'close',\n direction:\n order.order_direction === 'OPEN_LONG' || order.order_direction === 'CLOSE_SHORT' ? 'buy' : 'sell',\n // dynamically adjust the leverage\n lever_rate,\n order_price_type: order.order_type === 'MARKET' ? 'market' : 'limit',\n channel_code: process.env.BROKER_ID,\n };\n\n const result = await postSwapOrder(credential, params);\n console.info(formatTime(Date.now()), 'SubmitOrder', JSON.stringify(result), JSON.stringify(params));\n\n if (result.status !== 'ok') {\n throw newError(`HTX_SUBMIT_ORDER_FAILED`, { result });\n }\n return { order_id: result.data.order_id_str };\n}\n\n/**\n * 处理 super-margin 账户订单提交\n */\nasync function handleSuperMarginOrder(order: IOrder, credential: ICredential): Promise<{ order_id: string }> {\n // 获取可贷款金额\n const superMarginAccountUid = await superMarginAccountUidCache.query(JSON.stringify(credential));\n if (!superMarginAccountUid) throw new Error('Super margin account UID not found');\n const loanInfo = await getCrossMarginLoanInfo(credential);\n const usdtLoanable = loanInfo.data.find((v) => v.currency === 'usdt');\n if (!usdtLoanable) throw new Error('USDT loanable amount not found');\n const loanable = +usdtLoanable['loanable-amt'];\n\n const [, instType, contractCode] = decodePath(order.product_id);\n\n // 获取账户余额, 产品信息和价格\n const [balanceRes, theProduct, priceRes] = await Promise.all([\n getSpotAccountBalance(credential, superMarginAccountUid),\n productCache.query(order.product_id),\n getSpotTick({ symbol: contractCode }),\n ]);\n\n const balance = balanceRes.data.list\n .filter((v) => v.currency === 'usdt' && v.type === 'trade')\n .reduce((acc, cur) => acc + +cur.balance, 0);\n\n if (!theProduct) throw newError('HUOBI_SUBMIT_ORDER_PRODUCT_NOT_FOUND', { product_id: order.product_id });\n\n const price = priceRes.tick.close;\n const borrow_amount =\n order.order_direction === 'OPEN_LONG' || order.order_direction === 'CLOSE_SHORT'\n ? Math.max(Math.min(loanable, order.volume * price - balance), 0)\n : undefined;\n\n const params = {\n symbol: contractCode,\n 'account-id': '' + superMarginAccountUid,\n amount:\n '' +\n (order.order_direction === 'OPEN_LONG' || order.order_direction === 'CLOSE_SHORT'\n ? roundToStep(order.volume * price, theProduct?.volume_step!)\n : order.volume),\n 'borrow-amount': '' + borrow_amount,\n type: `${\n order.order_direction === 'OPEN_LONG' || order.order_direction === 'CLOSE_SHORT' ? 'buy' : 'sell'\n }-${'LIMIT' === order.order_type ? 'limit' : 'market'}`,\n 'trade-purpose':\n order.order_direction === 'OPEN_LONG' || order.order_direction === 'CLOSE_SHORT'\n ? '1' // auto borrow\n : '2', // auto repay\n price: order.order_type === 'MARKET' ? undefined : '' + order.price,\n source: 'super-margin-api',\n 'client-order-id': process.env.BROKER_ID,\n };\n\n const result = await postSpotOrder(credential, params);\n console.info(formatTime(Date.now()), 'SubmitOrder', JSON.stringify(result), JSON.stringify(params));\n\n if (result.success === false) {\n throw new Error(`Failed to submit super margin order: code=${result.code} message=${result.message}`);\n }\n\n return { order_id: result.data.orderId.toString() };\n}\n\nexport const submitOrder = (credential: ICredential, order: IOrder): Promise<{ order_id: string }> => {\n const [, instType] = decodePath(order.product_id);\n if (instType === 'SWAP') {\n return handleSwapOrder(order, credential);\n }\n if (instType === 'SUPER-MARGIN') {\n return handleSuperMarginOrder(order, credential);\n }\n throw newError('UNSUPPORTED_INST_TYPE', { order_type: instType });\n};\n"]}
|
package/lib/api/private-api.d.ts
CHANGED
|
@@ -3,7 +3,6 @@ export interface ICredential {
|
|
|
3
3
|
secret_key: string;
|
|
4
4
|
}
|
|
5
5
|
export declare const getDefaultCredential: () => ICredential;
|
|
6
|
-
export declare const privateRequest: (credential: ICredential, method: string, path: string, api_root: string, params?: any) => Promise<any>;
|
|
7
6
|
/**
|
|
8
7
|
* 获取账户信息
|
|
9
8
|
*
|
|
@@ -39,6 +38,7 @@ export declare const getUnifiedAccountInfo: (credential: ICredential) => Promise
|
|
|
39
38
|
data: {
|
|
40
39
|
margin_asset: string;
|
|
41
40
|
margin_balance: number;
|
|
41
|
+
margin_static: number;
|
|
42
42
|
cross_margin_static: number;
|
|
43
43
|
cross_profit_unreal: number;
|
|
44
44
|
withdraw_available: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"private-api.d.ts","sourceRoot":"","sources":["../../src/api/private-api.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"private-api.d.ts","sourceRoot":"","sources":["../../src/api/private-api.ts"],"names":[],"mappings":"AAKA,MAAM,WAAW,WAAW;IAC1B,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,eAAO,MAAM,oBAAoB,QAAO,WAKvC,CAAC;AA8FF;;;;GAIG;AACH,eAAO,MAAM,UAAU,eACT,WAAW,KACtB,QAAQ;IACT,QAAQ,MAAM,CAAC;IACf,IAAI,EAAE;QACJ,EAAE,EAAE,MAAM,CAAC;QACX,IAAI,EAAE,MAAM,CAAC;QACb,KAAK,EAAE,MAAM,CAAC;QACd,OAAO,EAAE,MAAM,CAAC;KACjB,EAAE,CAAC;CACL,CAEA,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,MAAM,eAAgB,WAAW,KAAG,QAAQ;IAAE,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,CAAC;QACJ,UAAU,EAAE,MAAM,CAAC;KACpB,EAAE,CAAC;CACL,CAEA,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,eAChB,WAAW,UACf;IACN,YAAY,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,IAAI,CAAC,EAAE,KAAK,GAAG,MAAM,CAAC;IACtB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,KACA,QAAQ;IAET,cAAc,EAAE,MAAM,CAAC;IACvB,eAAe,EAAE,MAAM,CAAC;IACxB,cAAc,EAAE,MAAM,CAAC;IACvB,eAAe,EAAE,MAAM,CAAC;IACxB,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,MAAM,CAAC;IACf,IAAI,EAAE;QACJ,YAAY,EAAE,MAAM,CAAC;QACrB,QAAQ,EAAE,MAAM,CAAC;QACjB,WAAW,EAAE,MAAM,CAAC;QACpB,cAAc,EAAE,MAAM,CAAC;QACvB,eAAe,EAAE,MAAM,CAAC;QACxB,eAAe,EAAE,MAAM,CAAC;QACxB,cAAc,EAAE,MAAM,CAAC;QACvB,eAAe,EAAE,MAAM,CAAC;KACzB,EAAE,CAAC;CACL,CAEA,CAAC"}
|
package/lib/api/private-api.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getAccountHistory = exports.getV2ReferenceCurrencies = exports.postSwapSwitchAccountType = exports.getSwapUnifiedAccountType = exports.getSubUserList = exports.postSubUserTransfer = exports.getDepositWithdrawHistory = exports.postWithdraw = exports.postBorrow = exports.postSpotAccountTransfer = exports.postSuperMarginAccountTransferIn = exports.postSuperMarginAccountTransferOut = exports.getAccountLedger = exports.getSpotAccountDepositAddresses = exports.postSwapOrder = exports.postSpotOrder = exports.getCrossMarginLoanInfo = exports.getSpotAccountBalance = exports.getSwapOpenOrders = exports.getSwapCrossPositionInfo = exports.getUnifiedAccountInfo = exports.getUid = exports.getAccount = exports.
|
|
3
|
+
exports.getAccountHistory = exports.getV2ReferenceCurrencies = exports.postSwapSwitchAccountType = exports.getSwapUnifiedAccountType = exports.getSubUserList = exports.postSubUserTransfer = exports.getDepositWithdrawHistory = exports.postWithdraw = exports.postBorrow = exports.postSpotAccountTransfer = exports.postSuperMarginAccountTransferIn = exports.postSuperMarginAccountTransferOut = exports.getAccountLedger = exports.getSpotAccountDepositAddresses = exports.postSwapOrder = exports.postSpotOrder = exports.getCrossMarginLoanInfo = exports.getSpotAccountBalance = exports.getSwapOpenOrders = exports.getSwapCrossPositionInfo = exports.getUnifiedAccountInfo = exports.getUid = exports.getAccount = exports.getDefaultCredential = void 0;
|
|
4
4
|
const utils_1 = require("@yuants/utils");
|
|
5
|
+
const SPOT_API_ROOT = 'api.huobi.pro';
|
|
6
|
+
const LINEAR_SWAP_API_ROOT = 'api.hbdm.com';
|
|
5
7
|
const getDefaultCredential = () => {
|
|
6
8
|
return {
|
|
7
9
|
access_key: process.env.ACCESS_KEY,
|
|
@@ -9,7 +11,26 @@ const getDefaultCredential = () => {
|
|
|
9
11
|
};
|
|
10
12
|
};
|
|
11
13
|
exports.getDefaultCredential = getDefaultCredential;
|
|
12
|
-
const
|
|
14
|
+
const createdBuckets = new Set();
|
|
15
|
+
const getOrCreatePrivateBucket = (bucketId) => {
|
|
16
|
+
if (!createdBuckets.has(bucketId)) {
|
|
17
|
+
createdBuckets.add(bucketId);
|
|
18
|
+
(0, utils_1.tokenBucket)(bucketId, { capacity: 36, refillAmount: 36, refillInterval: 3000 });
|
|
19
|
+
}
|
|
20
|
+
return (0, utils_1.tokenBucket)(bucketId);
|
|
21
|
+
};
|
|
22
|
+
const acquirePrivate = (bucketId, meta) => {
|
|
23
|
+
const bucket = getOrCreatePrivateBucket(bucketId);
|
|
24
|
+
(0, utils_1.scopeError)('HUOBI_API_RATE_LIMIT', Object.assign(Object.assign({}, meta), { bucketId }), () => bucket.acquireSync(1));
|
|
25
|
+
};
|
|
26
|
+
const privateRequestWithRateLimit = async (credential, interfaceType, business, method, path, api_root, params) => {
|
|
27
|
+
const meta = { method, api_root, path, business, interfaceType, access_key: credential.access_key };
|
|
28
|
+
const interfaceTypeUpper = interfaceType.toUpperCase();
|
|
29
|
+
const businessUpper = business.toUpperCase();
|
|
30
|
+
const globalBucketId = `HUOBI_PRIVATE_${interfaceTypeUpper}_UID_3S_ALL:${credential.access_key}`;
|
|
31
|
+
const businessBucketId = `HUOBI_PRIVATE_${interfaceTypeUpper}_UID_3S_${businessUpper}:${credential.access_key}`;
|
|
32
|
+
acquirePrivate(globalBucketId, meta);
|
|
33
|
+
acquirePrivate(businessBucketId, meta);
|
|
13
34
|
const requestParams = `AccessKeyId=${credential.access_key}&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=${encodeURIComponent(new Date().toISOString().split('.')[0])}${method === 'GET' && params !== undefined
|
|
14
35
|
? `&${Object.entries(params)
|
|
15
36
|
.filter(([, v]) => v !== undefined)
|
|
@@ -38,14 +59,17 @@ const privateRequest = async (credential, method, path, api_root, params) => {
|
|
|
38
59
|
throw e;
|
|
39
60
|
}
|
|
40
61
|
};
|
|
41
|
-
|
|
62
|
+
const spotPrivateQueryRequest = (credential, method, path, params) => privateRequestWithRateLimit(credential, 'query', 'spot', method, path, SPOT_API_ROOT, params);
|
|
63
|
+
const spotPrivateTradeRequest = (credential, method, path, params) => privateRequestWithRateLimit(credential, 'trade', 'spot', method, path, SPOT_API_ROOT, params);
|
|
64
|
+
const linearSwapPrivateQueryRequest = (credential, method, path, params) => privateRequestWithRateLimit(credential, 'query', 'linear-swap', method, path, LINEAR_SWAP_API_ROOT, params);
|
|
65
|
+
const linearSwapPrivateTradeRequest = (credential, method, path, params) => privateRequestWithRateLimit(credential, 'trade', 'linear-swap', method, path, LINEAR_SWAP_API_ROOT, params);
|
|
42
66
|
/**
|
|
43
67
|
* 获取账户信息
|
|
44
68
|
*
|
|
45
69
|
* https://www.htx.com/zh-cn/opend/newApiPages/?id=7ec40743-7773-11ed-9966-0242ac110003
|
|
46
70
|
*/
|
|
47
71
|
const getAccount = (credential) => {
|
|
48
|
-
return (
|
|
72
|
+
return spotPrivateQueryRequest(credential, 'GET', '/v1/account/accounts');
|
|
49
73
|
};
|
|
50
74
|
exports.getAccount = getAccount;
|
|
51
75
|
/**
|
|
@@ -54,7 +78,7 @@ exports.getAccount = getAccount;
|
|
|
54
78
|
* https://www.htx.com/zh-cn/opend/newApiPages/?id=7ec491c9-7773-11ed-9966-0242ac110003
|
|
55
79
|
*/
|
|
56
80
|
const getUid = (credential) => {
|
|
57
|
-
return (
|
|
81
|
+
return spotPrivateQueryRequest(credential, 'GET', '/v2/user/uid');
|
|
58
82
|
};
|
|
59
83
|
exports.getUid = getUid;
|
|
60
84
|
/**
|
|
@@ -63,7 +87,7 @@ exports.getUid = getUid;
|
|
|
63
87
|
* https://www.htx.com/zh-cn/opend/newApiPages/?id=10000073-77b7-11ed-9966-0242ac110003
|
|
64
88
|
*/
|
|
65
89
|
const getUnifiedAccountInfo = (credential) => {
|
|
66
|
-
return (
|
|
90
|
+
return linearSwapPrivateQueryRequest(credential, 'GET', '/linear-swap-api/v3/unified_account_info');
|
|
67
91
|
};
|
|
68
92
|
exports.getUnifiedAccountInfo = getUnifiedAccountInfo;
|
|
69
93
|
/**
|
|
@@ -72,7 +96,7 @@ exports.getUnifiedAccountInfo = getUnifiedAccountInfo;
|
|
|
72
96
|
* https://www.htx.com/zh-cn/opend/newApiPages/?id=8cb74963-77b5-11ed-9966-0242ac110003
|
|
73
97
|
*/
|
|
74
98
|
const getSwapCrossPositionInfo = (credential, params) => {
|
|
75
|
-
return (
|
|
99
|
+
return linearSwapPrivateQueryRequest(credential, 'POST', '/linear-swap-api/v1/swap_cross_position_info', params);
|
|
76
100
|
};
|
|
77
101
|
exports.getSwapCrossPositionInfo = getSwapCrossPositionInfo;
|
|
78
102
|
/**
|
|
@@ -81,7 +105,7 @@ exports.getSwapCrossPositionInfo = getSwapCrossPositionInfo;
|
|
|
81
105
|
* https://www.htx.com/zh-cn/opend/newApiPages/?id=8cb784d4-77b5-11ed-9966-0242ac110003
|
|
82
106
|
*/
|
|
83
107
|
const getSwapOpenOrders = (credential, params) => {
|
|
84
|
-
return (
|
|
108
|
+
return linearSwapPrivateQueryRequest(credential, 'POST', '/linear-swap-api/v1/swap_cross_openorders', params);
|
|
85
109
|
};
|
|
86
110
|
exports.getSwapOpenOrders = getSwapOpenOrders;
|
|
87
111
|
/**
|
|
@@ -90,7 +114,7 @@ exports.getSwapOpenOrders = getSwapOpenOrders;
|
|
|
90
114
|
* https://www.htx.com/zh-cn/opend/newApiPages/?id=7ec40922-7773-11ed-9966-0242ac110003
|
|
91
115
|
*/
|
|
92
116
|
const getSpotAccountBalance = (credential, account_uid) => {
|
|
93
|
-
return (
|
|
117
|
+
return spotPrivateQueryRequest(credential, 'GET', `/v1/account/accounts/${account_uid}/balance`);
|
|
94
118
|
};
|
|
95
119
|
exports.getSpotAccountBalance = getSpotAccountBalance;
|
|
96
120
|
/**
|
|
@@ -99,7 +123,7 @@ exports.getSpotAccountBalance = getSpotAccountBalance;
|
|
|
99
123
|
* https://www.htx.com/zh-cn/opend/newApiPages/?id=7ec41863-7773-11ed-9966-0242ac110003
|
|
100
124
|
*/
|
|
101
125
|
const getCrossMarginLoanInfo = (credential) => {
|
|
102
|
-
return (
|
|
126
|
+
return spotPrivateQueryRequest(credential, 'GET', '/v1/cross-margin/loan-info');
|
|
103
127
|
};
|
|
104
128
|
exports.getCrossMarginLoanInfo = getCrossMarginLoanInfo;
|
|
105
129
|
/**
|
|
@@ -108,7 +132,7 @@ exports.getCrossMarginLoanInfo = getCrossMarginLoanInfo;
|
|
|
108
132
|
* https://www.htx.com/zh-cn/opend/newApiPages/?id=10000065-77b7-11ed-9966-0242ac110003
|
|
109
133
|
*/
|
|
110
134
|
const postSpotOrder = (credential, params) => {
|
|
111
|
-
return (
|
|
135
|
+
return spotPrivateTradeRequest(credential, 'POST', `/v1/order/auto/place`, params);
|
|
112
136
|
};
|
|
113
137
|
exports.postSpotOrder = postSpotOrder;
|
|
114
138
|
/**
|
|
@@ -117,7 +141,7 @@ exports.postSpotOrder = postSpotOrder;
|
|
|
117
141
|
* https://www.htx.com/zh-cn/opend/newApiPages/?id=8cb77159-77b5-11ed-9966-0242ac110003
|
|
118
142
|
*/
|
|
119
143
|
const postSwapOrder = (credential, params) => {
|
|
120
|
-
return (
|
|
144
|
+
return linearSwapPrivateTradeRequest(credential, 'POST', '/linear-swap-api/v1/swap_cross_order', params);
|
|
121
145
|
};
|
|
122
146
|
exports.postSwapOrder = postSwapOrder;
|
|
123
147
|
/**
|
|
@@ -126,7 +150,7 @@ exports.postSwapOrder = postSwapOrder;
|
|
|
126
150
|
* https://www.htx.com/zh-cn/opend/newApiPages/?id=7ec45fb7-7773-11ed-9966-0242ac110003
|
|
127
151
|
*/
|
|
128
152
|
const getSpotAccountDepositAddresses = (credential, params) => {
|
|
129
|
-
return (
|
|
153
|
+
return spotPrivateQueryRequest(credential, 'GET', `/v2/account/deposit/address`, params);
|
|
130
154
|
};
|
|
131
155
|
exports.getSpotAccountDepositAddresses = getSpotAccountDepositAddresses;
|
|
132
156
|
/**
|
|
@@ -135,7 +159,7 @@ exports.getSpotAccountDepositAddresses = getSpotAccountDepositAddresses;
|
|
|
135
159
|
* https://www.htx.com/zh-cn/opend/newApiPages/?id=7ec4610b-7773-11ed-9966-0242ac110003
|
|
136
160
|
*/
|
|
137
161
|
const getAccountLedger = (credential, params) => {
|
|
138
|
-
return (
|
|
162
|
+
return spotPrivateQueryRequest(credential, 'GET', `/v2/account/ledger`, params);
|
|
139
163
|
};
|
|
140
164
|
exports.getAccountLedger = getAccountLedger;
|
|
141
165
|
/**
|
|
@@ -144,7 +168,7 @@ exports.getAccountLedger = getAccountLedger;
|
|
|
144
168
|
* https://www.htx.com/zh-cn/opend/newApiPages/?id=7ec41ff0-7773-11ed-9966-0242ac110003
|
|
145
169
|
*/
|
|
146
170
|
const postSuperMarginAccountTransferOut = (credential, params) => {
|
|
147
|
-
return (
|
|
171
|
+
return spotPrivateTradeRequest(credential, 'POST', `/v1/cross-margin/transfer-out`, params);
|
|
148
172
|
};
|
|
149
173
|
exports.postSuperMarginAccountTransferOut = postSuperMarginAccountTransferOut;
|
|
150
174
|
/**
|
|
@@ -153,7 +177,7 @@ exports.postSuperMarginAccountTransferOut = postSuperMarginAccountTransferOut;
|
|
|
153
177
|
* https://www.htx.com/zh-cn/opend/newApiPages/?id=7ec41f0e-7773-11ed-9966-0242ac110003
|
|
154
178
|
*/
|
|
155
179
|
const postSuperMarginAccountTransferIn = (credential, params) => {
|
|
156
|
-
return (
|
|
180
|
+
return spotPrivateTradeRequest(credential, 'POST', `/v1/cross-margin/transfer-in`, params);
|
|
157
181
|
};
|
|
158
182
|
exports.postSuperMarginAccountTransferIn = postSuperMarginAccountTransferIn;
|
|
159
183
|
/**
|
|
@@ -162,7 +186,7 @@ exports.postSuperMarginAccountTransferIn = postSuperMarginAccountTransferIn;
|
|
|
162
186
|
* https://www.htx.com/zh-cn/opend/newApiPages/?id=10000095-77b7-11ed-9966-0242ac110003
|
|
163
187
|
*/
|
|
164
188
|
const postSpotAccountTransfer = (credential, params) => {
|
|
165
|
-
return (
|
|
189
|
+
return spotPrivateTradeRequest(credential, 'POST', `/v2/account/transfer`, params);
|
|
166
190
|
};
|
|
167
191
|
exports.postSpotAccountTransfer = postSpotAccountTransfer;
|
|
168
192
|
/**
|
|
@@ -171,7 +195,7 @@ exports.postSpotAccountTransfer = postSpotAccountTransfer;
|
|
|
171
195
|
* https://www.htx.com/zh-cn/opend/newApiPages/?id=7ec41b5d-7773-11ed-9966-0242ac110003
|
|
172
196
|
*/
|
|
173
197
|
const postBorrow = (credential, params) => {
|
|
174
|
-
return (
|
|
198
|
+
return spotPrivateTradeRequest(credential, 'POST', `/v1/cross-margin/orders`, params);
|
|
175
199
|
};
|
|
176
200
|
exports.postBorrow = postBorrow;
|
|
177
201
|
/**
|
|
@@ -180,7 +204,7 @@ exports.postBorrow = postBorrow;
|
|
|
180
204
|
* https://www.htx.com/zh-cn/opend/newApiPages/?id=7ec42cfd-7773-11ed-9966-0242ac110003
|
|
181
205
|
*/
|
|
182
206
|
const postWithdraw = (credential, params) => {
|
|
183
|
-
return (
|
|
207
|
+
return spotPrivateTradeRequest(credential, 'POST', `/v1/dw/withdraw/api/create`, params);
|
|
184
208
|
};
|
|
185
209
|
exports.postWithdraw = postWithdraw;
|
|
186
210
|
/**
|
|
@@ -189,7 +213,7 @@ exports.postWithdraw = postWithdraw;
|
|
|
189
213
|
* https://www.htx.com/zh-cn/opend/newApiPages/?id=7ec44f99-7773-11ed-9966-0242ac110003
|
|
190
214
|
*/
|
|
191
215
|
const getDepositWithdrawHistory = (credential, params) => {
|
|
192
|
-
return (
|
|
216
|
+
return spotPrivateQueryRequest(credential, 'GET', `/v1/query/deposit-withdraw`, params);
|
|
193
217
|
};
|
|
194
218
|
exports.getDepositWithdrawHistory = getDepositWithdrawHistory;
|
|
195
219
|
/**
|
|
@@ -202,7 +226,7 @@ exports.getDepositWithdrawHistory = getDepositWithdrawHistory;
|
|
|
202
226
|
* https://www.htx.com/zh-cn/opend/newApiPages/?id=7ec45d8e-7773-11ed-9966-0242ac110003
|
|
203
227
|
*/
|
|
204
228
|
const postSubUserTransfer = (credential, params) => {
|
|
205
|
-
return (
|
|
229
|
+
return spotPrivateTradeRequest(credential, 'POST', '/v1/subuser/transfer', params);
|
|
206
230
|
};
|
|
207
231
|
exports.postSubUserTransfer = postSubUserTransfer;
|
|
208
232
|
/**
|
|
@@ -213,7 +237,7 @@ exports.postSubUserTransfer = postSubUserTransfer;
|
|
|
213
237
|
* https://www.htx.com/zh-cn/opend/newApiPages/?id=7ec48f09-7773-11ed-9966-0242ac110003
|
|
214
238
|
*/
|
|
215
239
|
const getSubUserList = (credential, params) => {
|
|
216
|
-
return (
|
|
240
|
+
return spotPrivateQueryRequest(credential, 'GET', `/v2/sub-user/user-list`, params);
|
|
217
241
|
};
|
|
218
242
|
exports.getSubUserList = getSubUserList;
|
|
219
243
|
/**
|
|
@@ -231,7 +255,7 @@ exports.getSubUserList = getSubUserList;
|
|
|
231
255
|
* https://www.htx.com/zh-cn/opend/newApiPages/?id=8cb71825-77b5-11ed-9966-0242ac110003
|
|
232
256
|
*/
|
|
233
257
|
const getSwapUnifiedAccountType = (credential) => {
|
|
234
|
-
return (
|
|
258
|
+
return linearSwapPrivateQueryRequest(credential, 'GET', '/linear-swap-api/v3/swap_unified_account_type');
|
|
235
259
|
};
|
|
236
260
|
exports.getSwapUnifiedAccountType = getSwapUnifiedAccountType;
|
|
237
261
|
/**
|
|
@@ -248,7 +272,7 @@ exports.getSwapUnifiedAccountType = getSwapUnifiedAccountType;
|
|
|
248
272
|
* https://www.htx.com/zh-cn/opend/newApiPages/?id=8cb7196b-77b5-11ed-9966-0242ac110003
|
|
249
273
|
*/
|
|
250
274
|
const postSwapSwitchAccountType = (credential, params) => {
|
|
251
|
-
return (
|
|
275
|
+
return linearSwapPrivateTradeRequest(credential, 'POST', '/linear-swap-api/v3/swap_switch_account_type', params);
|
|
252
276
|
};
|
|
253
277
|
exports.postSwapSwitchAccountType = postSwapSwitchAccountType;
|
|
254
278
|
/**
|
|
@@ -257,7 +281,7 @@ exports.postSwapSwitchAccountType = postSwapSwitchAccountType;
|
|
|
257
281
|
* https://www.htx.com/zh-cn/opend/newApiPages/?id=7ec478f0-7773-11ed-9966-0242ac110003
|
|
258
282
|
*/
|
|
259
283
|
const getV2ReferenceCurrencies = (credential, params) => {
|
|
260
|
-
return (
|
|
284
|
+
return spotPrivateQueryRequest(credential, 'GET', '/v2/reference/currencies', params);
|
|
261
285
|
};
|
|
262
286
|
exports.getV2ReferenceCurrencies = getV2ReferenceCurrencies;
|
|
263
287
|
/**
|
|
@@ -266,7 +290,7 @@ exports.getV2ReferenceCurrencies = getV2ReferenceCurrencies;
|
|
|
266
290
|
* https://www.htx.com/zh-cn/opend/newApiPages/?id=7ec41049-7773-11ed-9966-0242ac110003
|
|
267
291
|
*/
|
|
268
292
|
const getAccountHistory = (credential, params) => {
|
|
269
|
-
return (
|
|
293
|
+
return spotPrivateQueryRequest(credential, 'GET', '/v1/account/history', params);
|
|
270
294
|
};
|
|
271
295
|
exports.getAccountHistory = getAccountHistory;
|
|
272
296
|
//# sourceMappingURL=private-api.js.map
|