@yuants/vendor-huobi 0.15.4 → 0.15.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.
Files changed (40) hide show
  1. package/dist/api/public-api.js +7 -0
  2. package/dist/api/public-api.js.map +1 -1
  3. package/dist/services/accounts/spot.js +17 -11
  4. package/dist/services/accounts/spot.js.map +1 -1
  5. package/dist/services/accounts/super-margin.js +18 -44
  6. package/dist/services/accounts/super-margin.js.map +1 -1
  7. package/dist/services/accounts/swap.js +20 -11
  8. package/dist/services/accounts/swap.js.map +1 -1
  9. package/dist/services/market-data/quote.js +21 -3
  10. package/dist/services/market-data/quote.js.map +1 -1
  11. package/dist/services/orders/submitOrder.js +5 -7
  12. package/dist/services/orders/submitOrder.js.map +1 -1
  13. package/dist/services/product.js +2 -4
  14. package/dist/services/product.js.map +1 -1
  15. package/lib/api/public-api.d.ts +24 -0
  16. package/lib/api/public-api.d.ts.map +1 -1
  17. package/lib/api/public-api.js +9 -1
  18. package/lib/api/public-api.js.map +1 -1
  19. package/lib/services/accounts/spot.d.ts.map +1 -1
  20. package/lib/services/accounts/spot.js +17 -11
  21. package/lib/services/accounts/spot.js.map +1 -1
  22. package/lib/services/accounts/super-margin.d.ts.map +1 -1
  23. package/lib/services/accounts/super-margin.js +17 -43
  24. package/lib/services/accounts/super-margin.js.map +1 -1
  25. package/lib/services/accounts/swap.d.ts.map +1 -1
  26. package/lib/services/accounts/swap.js +19 -10
  27. package/lib/services/accounts/swap.js.map +1 -1
  28. package/lib/services/market-data/quote.d.ts +2 -1
  29. package/lib/services/market-data/quote.d.ts.map +1 -1
  30. package/lib/services/market-data/quote.js +20 -1
  31. package/lib/services/market-data/quote.js.map +1 -1
  32. package/lib/services/orders/submitOrder.d.ts.map +1 -1
  33. package/lib/services/orders/submitOrder.js +4 -6
  34. package/lib/services/orders/submitOrder.js.map +1 -1
  35. package/lib/services/product.d.ts +1 -1
  36. package/lib/services/product.d.ts.map +1 -1
  37. package/lib/services/product.js +2 -4
  38. package/lib/services/product.js.map +1 -1
  39. package/package.json +3 -3
  40. package/temp/package-deps.json +12 -12
@@ -142,4 +142,11 @@ export function getSwapCrossLadderMargin(params) {
142
142
  export function getV2ReferenceCurrencies(params) {
143
143
  return publicRequest('GET', '/v2/reference/currencies', SPOT_API_ROOT, params);
144
144
  }
145
+ /**
146
+ * 【现货】获取市场所有交易对的最新行情
147
+ *
148
+ * https://www.htx.com/zh-cn/opend/newApiPages/?id=7ec40374-7773-11ed-9966-0242ac110003
149
+ *
150
+ */
151
+ export const getSpotMarketTickers = () => publicRequest('GET', '/market/tickers', SPOT_API_ROOT);
145
152
  //# sourceMappingURL=public-api.js.map
@@ -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,EAAE;YACjD,GAAG,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;SACvC;KACF;IAED,MAAM,IAAI,GAAG,MAAM,KAAK,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;IAE5D,OAAO,CAAC,IAAI,CAAC,UAAU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;;;;;;;;;GAUG;AACH,MAAM,UAAU,uBAAuB,CAAC,MAAkC;IAcxE,OAAO,aAAa,CAAC,KAAK,EAAE,6CAA6C,EAAE,aAAa,EAAE,MAAM,CAAC,CAAC;AACpG,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,kBAAkB,CAAC,MAA0D;IAqB3F,OAAO,aAAa,CAAC,KAAK,EAAE,8BAA8B,EAAE,aAAa,EAAE,MAAM,CAAC,CAAC;AACrF,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,mBAAmB,CAAC,MAKnC;IAiBC,OAAO,aAAa,CAAC,KAAK,EAAE,wCAAwC,EAAE,aAAa,EAAE,MAAM,CAAC,CAAC;AAC/F,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,gBAAgB,CAAC,MAAkC;IAajE,OAAO,aAAa,CAAC,KAAK,EAAE,4BAA4B,EAAE,aAAa,EAAE,MAAM,CAAC,CAAC;AACnF,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,4BAA4B,CAAC,MAI5C;IAkBC,OAAO,aAAa,CAAC,KAAK,EAAE,kDAAkD,EAAE,aAAa,EAAE,MAAM,CAAC,CAAC;AACzG,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","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 * 【通用】批量获取合约资金费率\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 * 【全仓】获取平台阶梯保证金\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"]}
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,EAAE;YACjD,GAAG,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;SACvC;KACF;IAED,MAAM,IAAI,GAAG,MAAM,KAAK,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;IAE5D,OAAO,CAAC,IAAI,C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ils';\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 * 【通用】批量获取合约资金费率\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 * 【全仓】获取平台阶梯保证金\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"]}
@@ -2,23 +2,29 @@ import { makeSpotPosition } from '@yuants/data-account';
2
2
  import { getSpotAccountBalance } from '../../api/private-api';
3
3
  import { spotAccountUidCache } from '../uid';
4
4
  import { encodePath } from '@yuants/utils';
5
+ import { quoteCache } from '../market-data/quote';
5
6
  export const getSpotAccountInfo = async (credential) => {
7
+ var _a, _b, _c;
6
8
  const spotAccountUid = await spotAccountUidCache.query(JSON.stringify(credential));
7
9
  if (!spotAccountUid)
8
10
  throw new Error('Failed to get Spot Account UID');
9
11
  const spotBalance = await getSpotAccountBalance(credential, spotAccountUid);
10
- return spotBalance.data.list
11
- .map((v) => {
12
- var _a, _b;
13
- return makeSpotPosition({
12
+ const positions = [];
13
+ for (const v of spotBalance.data.list) {
14
+ if (v.balance === '0')
15
+ continue;
16
+ const product_id = encodePath('HTX', 'SPOT', v.currency + 'usdt');
17
+ const quote = await quoteCache.query(product_id);
18
+ const closable_price = v.currency === 'usdt' ? 1 : +((_a = quote === null || quote === void 0 ? void 0 : quote.ask_price) !== null && _a !== void 0 ? _a : 0);
19
+ positions.push(makeSpotPosition({
14
20
  position_id: `${v.currency}`,
15
21
  datasource_id: 'HTX',
16
- product_id: encodePath('HTX', 'SPOT', v.currency + 'usdt'),
17
- volume: +((_a = v.balance) !== null && _a !== void 0 ? _a : 0),
18
- free_volume: +((_b = v.balance) !== null && _b !== void 0 ? _b : 0),
19
- closable_price: 1, // TODO: 获取现货币对价格
20
- });
21
- })
22
- .filter((x) => x.volume > 0);
22
+ product_id: product_id,
23
+ volume: +((_b = v.balance) !== null && _b !== void 0 ? _b : 0),
24
+ free_volume: +((_c = v.balance) !== null && _c !== void 0 ? _c : 0),
25
+ closable_price: closable_price,
26
+ }));
27
+ }
28
+ return positions;
23
29
  };
24
30
  //# sourceMappingURL=spot.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"spot.js","sourceRoot":"","sources":["../../../src/services/accounts/spot.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkC,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AACxF,OAAO,EAAE,qBAAqB,EAAe,MAAM,uBAAuB,CAAC;AAC3E,OAAO,EAAE,mBAAmB,EAAE,MAAM,QAAQ,CAAC;AAC7C,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAE3C,MAAM,CAAC,MAAM,kBAAkB,GAAgD,KAAK,EAAE,UAAU,EAAE,EAAE;IAClG,MAAM,cAAc,GAAG,MAAM,mBAAmB,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC,CAAC;IACnF,IAAI,CAAC,cAAc;QAAE,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAC;IACvE,MAAM,WAAW,GAAG,MAAM,qBAAqB,CAAC,UAAU,EAAE,cAAc,CAAC,CAAC;IAE5E,OAAO,WAAW,CAAC,IAAI,CAAC,IAAI;SACzB,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE;;QACT,OAAO,gBAAgB,CAAC;YACtB,WAAW,EAAE,GAAG,CAAC,CAAC,QAAQ,EAAE;YAC5B,aAAa,EAAE,KAAK;YACpB,UAAU,EAAE,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,CAAC,CAAC,QAAQ,GAAG,MAAM,CAAC;YAC1D,MAAM,EAAE,CAAC,CAAC,MAAA,CAAC,CAAC,OAAO,mCAAI,CAAC,CAAC;YACzB,WAAW,EAAE,CAAC,CAAC,MAAA,CAAC,CAAC,OAAO,mCAAI,CAAC,CAAC;YAC9B,cAAc,EAAE,CAAC,EAAE,iBAAiB;SACrC,CAAC,CAAC;IACL,CAAC,CAAC;SACD,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;AACjC,CAAC,CAAC","sourcesContent":["import { IActionHandlerOfGetAccountInfo, makeSpotPosition } from '@yuants/data-account';\nimport { getSpotAccountBalance, ICredential } from '../../api/private-api';\nimport { spotAccountUidCache } from '../uid';\nimport { encodePath } from '@yuants/utils';\n\nexport const getSpotAccountInfo: IActionHandlerOfGetAccountInfo<ICredential> = async (credential) => {\n const spotAccountUid = await spotAccountUidCache.query(JSON.stringify(credential));\n if (!spotAccountUid) throw new Error('Failed to get Spot Account UID');\n const spotBalance = await getSpotAccountBalance(credential, spotAccountUid);\n\n return spotBalance.data.list\n .map((v) => {\n return makeSpotPosition({\n position_id: `${v.currency}`,\n datasource_id: 'HTX',\n product_id: encodePath('HTX', 'SPOT', v.currency + 'usdt'),\n volume: +(v.balance ?? 0),\n free_volume: +(v.balance ?? 0),\n closable_price: 1, // TODO: 获取现货币对价格\n });\n })\n .filter((x) => x.volume > 0);\n};\n"]}
1
+ {"version":3,"file":"spot.js","sourceRoot":"","sources":["../../../src/services/accounts/spot.ts"],"names":[],"mappings":"AAAA,OAAO,EAA6C,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AACnG,OAAO,EAAE,qBAAqB,EAAe,MAAM,uBAAuB,CAAC;AAC3E,OAAO,EAAE,mBAAmB,EAAE,MAAM,QAAQ,CAAC;AAC7C,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAElD,MAAM,CAAC,MAAM,kBAAkB,GAAgD,KAAK,EAAE,UAAU,EAAE,EAAE;;IAClG,MAAM,cAAc,GAAG,MAAM,mBAAmB,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC,CAAC;IACnF,IAAI,CAAC,cAAc;QAAE,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAC;IACvE,MAAM,WAAW,GAAG,MAAM,qBAAqB,CAAC,UAAU,EAAE,cAAc,CAAC,CAAC;IAE5E,MAAM,SAAS,GAAgB,EAAE,CAAC;IAElC,KAAK,MAAM,CAAC,IAAI,WAAW,CAAC,IAAI,CAAC,IAAI,EAAE;QACrC,IAAI,CAAC,CAAC,OAAO,KAAK,GAAG;YAAE,SAAS;QAChC,MAAM,UAAU,GAAG,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,CAAC,CAAC,QAAQ,GAAG,MAAM,CAAC,CAAC;QAClE,MAAM,KAAK,GAAG,MAAM,UAAU,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;QACjD,MAAM,cAAc,GAAG,CAAC,CAAC,QAAQ,KAAK,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAA,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,SAAS,mCAAI,CAAC,CAAC,CAAC;QAC5E,SAAS,CAAC,IAAI,CACZ,gBAAgB,CAAC;YACf,WAAW,EAAE,GAAG,CAAC,CAAC,QAAQ,EAAE;YAC5B,aAAa,EAAE,KAAK;YACpB,UAAU,EAAE,UAAU;YACtB,MAAM,EAAE,CAAC,CAAC,MAAA,CAAC,CAAC,OAAO,mCAAI,CAAC,CAAC;YACzB,WAAW,EAAE,CAAC,CAAC,MAAA,CAAC,CAAC,OAAO,mCAAI,CAAC,CAAC;YAC9B,cAAc,EAAE,cAAc;SAC/B,CAAC,CACH,CAAC;KACH;IACD,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC","sourcesContent":["import { IActionHandlerOfGetAccountInfo, IPosition, makeSpotPosition } from '@yuants/data-account';\nimport { getSpotAccountBalance, ICredential } from '../../api/private-api';\nimport { spotAccountUidCache } from '../uid';\nimport { encodePath } from '@yuants/utils';\nimport { quoteCache } from '../market-data/quote';\n\nexport const getSpotAccountInfo: IActionHandlerOfGetAccountInfo<ICredential> = async (credential) => {\n const spotAccountUid = await spotAccountUidCache.query(JSON.stringify(credential));\n if (!spotAccountUid) throw new Error('Failed to get Spot Account UID');\n const spotBalance = await getSpotAccountBalance(credential, spotAccountUid);\n\n const positions: IPosition[] = [];\n\n for (const v of spotBalance.data.list) {\n if (v.balance === '0') continue;\n const product_id = encodePath('HTX', 'SPOT', v.currency + 'usdt');\n const quote = await quoteCache.query(product_id);\n const closable_price = v.currency === 'usdt' ? 1 : +(quote?.ask_price ?? 0);\n positions.push(\n makeSpotPosition({\n position_id: `${v.currency}`,\n datasource_id: 'HTX',\n product_id: product_id,\n volume: +(v.balance ?? 0),\n free_volume: +(v.balance ?? 0),\n closable_price: closable_price,\n }),\n );\n }\n return positions;\n};\n"]}
@@ -1,60 +1,34 @@
1
- import { createCache } from '@yuants/cache';
2
- import { formatTime } from '@yuants/utils';
1
+ import { makeSpotPosition } from '@yuants/data-account';
2
+ import { encodePath } from '@yuants/utils';
3
3
  import { getSpotAccountBalance } from '../../api/private-api';
4
- import { getSpotTick } from '../../api/public-api';
4
+ import { quoteCache } from '../market-data/quote';
5
5
  import { superMarginAccountUidCache } from '../uid';
6
- const spotTickCache = createCache((currency) => getSpotTick({ symbol: `${currency}usdt` }), {
7
- expire: 300000,
8
- swrAfter: 10000, // 10 seconds
9
- });
10
6
  /**
11
7
  * 全仓杠杆账户 (Super Margin Account)
12
8
  */
13
9
  export const getSuperMarginAccountInfo = async (credential) => {
14
- var _a;
10
+ var _a, _b;
11
+ const positions = [];
15
12
  // get account balance
16
13
  const superMarginAccountUid = await superMarginAccountUidCache.query(JSON.stringify(credential));
17
14
  if (!superMarginAccountUid)
18
15
  throw new Error('Failed to get Super Margin Account UID');
19
16
  const accountBalance = await getSpotAccountBalance(credential, superMarginAccountUid);
20
17
  const balanceList = ((_a = accountBalance.data) === null || _a === void 0 ? void 0 : _a.list) || [];
21
- // get positions (non-usdt currencies)
22
- const positions = [];
23
- const nonUsdtCurrencies = balanceList
24
- .filter((v) => v.currency !== 'usdt')
25
- .reduce((acc, cur) => {
26
- const existing = acc.find((item) => item.currency === cur.currency);
27
- if (existing) {
28
- existing.balance += +cur.balance;
29
- }
30
- else {
31
- acc.push({ currency: cur.currency, balance: +cur.balance });
32
- }
33
- return acc;
34
- }, []);
35
18
  // get prices and create positions
36
- for (const currencyData of nonUsdtCurrencies) {
37
- if (currencyData.balance > 0) {
38
- const tickerRes = await spotTickCache.query(currencyData.currency);
39
- const price = (tickerRes === null || tickerRes === void 0 ? void 0 : tickerRes.tick.close) || 0;
40
- try {
41
- // get current price from websocket or fallback to REST API
42
- positions.push({
43
- position_id: `${currencyData.currency}/usdt/spot`,
44
- product_id: `${currencyData.currency}usdt`,
45
- direction: 'LONG',
46
- volume: currencyData.balance,
47
- free_volume: currencyData.balance,
48
- position_price: price,
49
- closable_price: price,
50
- floating_profit: 0,
51
- valuation: currencyData.balance * price,
52
- });
53
- }
54
- catch (error) {
55
- console.warn(formatTime(Date.now()), `Failed to get price for ${currencyData.currency}:`, error);
56
- }
57
- }
19
+ for (const currencyData of balanceList) {
20
+ if (currencyData.balance === '0')
21
+ continue;
22
+ const product_id = encodePath('HTX', 'SPOT', currencyData.currency + 'usdt');
23
+ const quote = await quoteCache.query(currencyData.currency);
24
+ const closable_price = currencyData.currency === 'usdt' ? 1 : +((_b = quote === null || quote === void 0 ? void 0 : quote.ask_price) !== null && _b !== void 0 ? _b : 0);
25
+ positions.push(makeSpotPosition({
26
+ position_id: `${currencyData.currency}/usdt/spot`,
27
+ product_id: product_id,
28
+ volume: +currencyData.balance,
29
+ free_volume: +currencyData.balance,
30
+ closable_price: closable_price,
31
+ }));
58
32
  }
59
33
  return positions;
60
34
  };
@@ -1 +1 @@
1
- {"version":3,"file":"super-margin.js","sourceRoot":"","sources":["../../../src/services/accounts/super-margin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,qBAAqB,EAAe,MAAM,uBAAuB,CAAC;AAC3E,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACnD,OAAO,EAAE,0BAA0B,EAAE,MAAM,QAAQ,CAAC;AAEpD,MAAM,aAAa,GAAG,WAAW,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,WAAW,CAAC,EAAE,MAAM,EAAE,GAAG,QAAQ,MAAM,EAAE,CAAC,EAAE;IAC1F,MAAM,EAAE,MAAO;IACf,QAAQ,EAAE,KAAM,EAAE,aAAa;CAChC,CAAC,CAAC;AAEH;;GAEG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAgD,KAAK,EAAE,UAAU,EAAE,EAAE;;IACzG,sBAAsB;IACtB,MAAM,qBAAqB,GAAG,MAAM,0BAA0B,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC,CAAC;IACjG,IA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AAI,CAAC;oBACb,WAAW,EAAE,GAAG,YAAY,CAAC,QAAQ,YAAY;oBACjD,UAAU,EAAE,GAAG,YAAY,CAAC,QAAQ,MAAM;oBAC1C,SAAS,EAAE,MAAM;oBACjB,MAAM,EAAE,YAAY,CAAC,OAAO;oBAC5B,WAAW,EAAE,YAAY,CAAC,OAAO;oBACjC,cAAc,EAAE,KAAK;oBACrB,cAAc,EAAE,KAAK;oBACrB,eAAe,EAAE,CAAC;oBAClB,SAAS,EAAE,YAAY,CAAC,OAAO,GAAG,KAAK;iBACxC,CAAC,CAAC;aACJ;YAAC,OAAO,KAAK,EAAE;gBACd,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,EAAE,2BAA2B,YAAY,CAAC,QAAQ,GAAG,EAAE,KAAK,CAAC,CAAC;aAClG;SACF;KACF;IAED,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC","sourcesContent":["import { createCache } from '@yuants/cache';\nimport { IActionHandlerOfGetAccountInfo, IPosition } from '@yuants/data-account';\nimport { formatTime } from '@yuants/utils';\nimport { getSpotAccountBalance, ICredential } from '../../api/private-api';\nimport { getSpotTick } from '../../api/public-api';\nimport { superMarginAccountUidCache } from '../uid';\n\nconst spotTickCache = createCache((currency) => getSpotTick({ symbol: `${currency}usdt` }), {\n expire: 300_000, // 5 minutes\n swrAfter: 10_000, // 10 seconds\n});\n\n/**\n * 全仓杠杆账户 (Super Margin Account)\n */\nexport const getSuperMarginAccountInfo: IActionHandlerOfGetAccountInfo<ICredential> = async (credential) => {\n // get account balance\n const superMarginAccountUid = await superMarginAccountUidCache.query(JSON.stringify(credential));\n if (!superMarginAccountUid) throw new Error('Failed to get Super Margin Account UID');\n const accountBalance = await getSpotAccountBalance(credential, superMarginAccountUid);\n const balanceList = accountBalance.data?.list || [];\n\n // get positions (non-usdt currencies)\n const positions: IPosition[] = [];\n const nonUsdtCurrencies = balanceList\n .filter((v) => v.currency !== 'usdt')\n .reduce((acc, cur) => {\n const existing = acc.find((item) => item.currency === cur.currency);\n if (existing) {\n existing.balance += +cur.balance;\n } else {\n acc.push({ currency: cur.currency, balance: +cur.balance });\n }\n return acc;\n }, [] as { currency: string; balance: number }[]);\n\n // get prices and create positions\n for (const currencyData of nonUsdtCurrencies) {\n if (currencyData.balance > 0) {\n const tickerRes = await spotTickCache.query(currencyData.currency);\n const price = tickerRes?.tick.close || 0;\n try {\n // get current price from websocket or fallback to REST API\n\n positions.push({\n position_id: `${currencyData.currency}/usdt/spot`,\n product_id: `${currencyData.currency}usdt`,\n direction: 'LONG',\n volume: currencyData.balance,\n free_volume: currencyData.balance,\n position_price: price,\n closable_price: price,\n floating_profit: 0,\n valuation: currencyData.balance * price,\n });\n } catch (error) {\n console.warn(formatTime(Date.now()), `Failed to get price for ${currencyData.currency}:`, error);\n }\n }\n }\n\n return positions;\n};\n"]}
1
+ {"version":3,"file":"super-margin.js","sourceRoot":"","sources":["../../../src/services/accounts/super-margin.ts"],"names":[],"mappings":"AAAA,OAAO,EAA6C,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AACnG,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,qBAAqB,EAAe,MAAM,uBAAuB,CAAC;AAC3E,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAClD,OAAO,EAAE,0BAA0B,EAAE,MAAM,QAAQ,CAAC;AAEpD;;GAEG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAgD,KAAK,EAAE,UAAU,EAAE,EAAE;;IACzG,MAAM,SAAS,GAAgB,EAAE,CAAC;IAClC,sBAAsB;IACtB,MAAM,qBAAqB,GAAG,MAAM,0BAA0B,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC,CAAC;IACjG,IAAI,CAAC,qBAAqB;QAAE,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAC;IACtF,MAAM,cAAc,GAAG,MAAM,qBAAqB,CAAC,UAAU,EAAE,qBAAqB,CAAC,CAAC;IACtF,MAAM,WAAW,GAAG,CAAA,MAAA,cAAc,CAAC,IAAI,0CAAE,IAAI,KAAI,EAAE,CAAC;IAEpD,kCAAkC;IAClC,KAAK,MAAM,YAAY,IAAI,WAAW,EAAE;QACtC,IAAI,YAAY,CAAC,OAAO,KAAK,GAAG;YAAE,SAAS;QAC3C,MAAM,UAAU,GAAG,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,YAAY,CAAC,QAAQ,GAAG,MAAM,CAAC,CAAC;QAC7E,MAAM,KAAK,GAAG,MAAM,UAAU,CAAC,KAAK,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC;QAC5D,MAAM,cAAc,GAAG,YAAY,CAAC,QAAQ,KAAK,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAA,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,SAAS,mCAAI,CAAC,CAAC,CAAC;QAEvF,SAAS,CAAC,IAAI,CACZ,gBAAgB,CAAC;YACf,WAAW,EAAE,GAAG,YAAY,CAAC,QAAQ,YAAY;YACjD,UAAU,EAAE,UAAU;YACtB,MAAM,EAAE,CAAC,YAAY,CAAC,OAAO;YAC7B,WAAW,EAAE,CAAC,YAAY,CAAC,OAAO;YAClC,cAAc,EAAE,cAAc;SAC/B,CAAC,CACH,CAAC;KACH;IAED,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC","sourcesContent":["import { IActionHandlerOfGetAccountInfo, IPosition, makeSpotPosition } from '@yuants/data-account';\nimport { encodePath } from '@yuants/utils';\nimport { getSpotAccountBalance, ICredential } from '../../api/private-api';\nimport { quoteCache } from '../market-data/quote';\nimport { superMarginAccountUidCache } from '../uid';\n\n/**\n * 全仓杠杆账户 (Super Margin Account)\n */\nexport const getSuperMarginAccountInfo: IActionHandlerOfGetAccountInfo<ICredential> = async (credential) => {\n const positions: IPosition[] = [];\n // get account balance\n const superMarginAccountUid = await superMarginAccountUidCache.query(JSON.stringify(credential));\n if (!superMarginAccountUid) throw new Error('Failed to get Super Margin Account UID');\n const accountBalance = await getSpotAccountBalance(credential, superMarginAccountUid);\n const balanceList = accountBalance.data?.list || [];\n\n // get prices and create positions\n for (const currencyData of balanceList) {\n if (currencyData.balance === '0') continue;\n const product_id = encodePath('HTX', 'SPOT', currencyData.currency + 'usdt');\n const quote = await quoteCache.query(currencyData.currency);\n const closable_price = currencyData.currency === 'usdt' ? 1 : +(quote?.ask_price ?? 0);\n\n positions.push(\n makeSpotPosition({\n position_id: `${currencyData.currency}/usdt/spot`,\n product_id: product_id,\n volume: +currencyData.balance,\n free_volume: +currencyData.balance,\n closable_price: closable_price,\n }),\n );\n }\n\n return positions;\n};\n"]}
@@ -1,25 +1,34 @@
1
+ import { makeSpotPosition } from '@yuants/data-account';
1
2
  import { encodePath } from '@yuants/utils';
2
- import { firstValueFrom } from 'rxjs';
3
3
  import { getSwapCrossPositionInfo, getUnifiedAccountInfo } from '../../api/private-api';
4
- import { productService } from '../product';
4
+ import { productCache } from '../product';
5
+ const usdAssets = new Set(['USDT', 'USDC', 'USDD']);
5
6
  export const getSwapAccountInfo = async (credential) => {
7
+ const positions = [];
6
8
  // balance
7
9
  const balance = await getUnifiedAccountInfo(credential);
8
10
  if (!balance.data) {
9
11
  throw new Error('Failed to get unified account info');
10
12
  }
11
- const balanceData = balance.data.find((v) => v.margin_asset === 'USDT');
12
- if (!balanceData) {
13
- throw new Error('No USDT balance found in unified account');
13
+ for (const v of balance.data || []) {
14
+ if (v.margin_balance === 0)
15
+ continue;
16
+ positions.push(makeSpotPosition({
17
+ position_id: encodePath('HTX', 'SWAP-MARGIN', v.margin_asset),
18
+ datasource_id: 'HTX',
19
+ product_id: encodePath('HTX', 'SWAP-MARGIN', v.margin_asset),
20
+ volume: +v.margin_balance,
21
+ free_volume: +v.withdraw_available,
22
+ closable_price: usdAssets.has(v.margin_asset) ? 1 : 0,
23
+ }));
14
24
  }
15
25
  // positions
16
26
  const positionsRes = await getSwapCrossPositionInfo(credential);
17
- const mapProductIdToPerpetualProduct = await firstValueFrom(productService.mapProductIdToProduct$);
18
- const positions = (positionsRes.data || []).map((v) => {
27
+ for (const v of positionsRes.data || []) {
19
28
  const product_id = encodePath('HTX', 'SWAP', v.contract_code);
20
- const theProduct = mapProductIdToPerpetualProduct === null || mapProductIdToPerpetualProduct === void 0 ? void 0 : mapProductIdToPerpetualProduct.get(product_id);
29
+ const theProduct = await productCache.query(product_id);
21
30
  const valuation = v.volume * v.last_price * ((theProduct === null || theProduct === void 0 ? void 0 : theProduct.value_scale) || 1);
22
- return {
31
+ positions.push({
23
32
  position_id: `${product_id}/${v.contract_type}/${v.direction}/${v.margin_mode}`,
24
33
  datasource_id: 'HTX',
25
34
  product_id,
@@ -30,8 +39,8 @@ export const getSwapAccountInfo = async (credential) => {
30
39
  closable_price: v.last_price,
31
40
  floating_profit: v.profit_unreal,
32
41
  valuation,
33
- };
34
- });
42
+ });
43
+ }
35
44
  return positions;
36
45
  };
37
46
  //# sourceMappingURL=swap.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"swap.js","sourceRoot":"","sources":["../../../src/services/accounts/swap.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,cAAc,EAAE,MAAM,MAAM,CAAC;AACtC,OAAO,EAAE,wBAAwB,EAAE,qBAAqB,EAAe,MAAM,uBAAuB,CAAC;AACrG,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAE5C,MAAM,CAAC,MAAM,kBAAkB,GAAG,KAAK,EAAE,UAAuB,EAAwB,EAAE;IACxF,UAAU;IACV,MAAM,OAAO,GAAG,MAAM,qBAAqB,CAAC,UAAU,CAAC,CAAC;IACxD,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE;QACjB,MAAM,IAAI,KAAK,CAAC,oCAAoC,CAAC,CAAC;KACvD;IACD,MAAM,WA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C,CAAC,aAAa;YAChC,SAAS;SACV,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC","sourcesContent":["import { IPosition } from '@yuants/data-account';\nimport { encodePath } from '@yuants/utils';\nimport { firstValueFrom } from 'rxjs';\nimport { getSwapCrossPositionInfo, getUnifiedAccountInfo, ICredential } from '../../api/private-api';\nimport { productService } from '../product';\n\nexport const getSwapAccountInfo = async (credential: ICredential): Promise<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 const balanceData = balance.data.find((v) => v.margin_asset === 'USDT');\n if (!balanceData) {\n throw new Error('No USDT balance found in unified account');\n }\n\n // positions\n const positionsRes = await getSwapCrossPositionInfo(credential);\n const mapProductIdToPerpetualProduct = await firstValueFrom(productService.mapProductIdToProduct$);\n const positions: IPosition[] = (positionsRes.data || []).map((v): IPosition => {\n const product_id = encodePath('HTX', 'SWAP', v.contract_code);\n const theProduct = mapProductIdToPerpetualProduct?.get(product_id);\n const valuation = v.volume * v.last_price * (theProduct?.value_scale || 1);\n return {\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 valuation,\n };\n });\n\n return positions;\n};\n"]}
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,MA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U,CAAC,CAAC;QACxD,MAAM,SAAS,GAAG,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,UAAU,GAAG,CAAC,CAAA,UAAU,aAAV,UAAU,uBAAV,UAAU,CAAE,WAAW,KAAI,CAAC,CAAC,CAAC;QAC3E,SAAS,CAAC,IAAI,CAAC;YACb,WAAW,EAAE,GAAG,UAAU,IAAI,CAAC,CAAC,aAAa,IAAI,CAAC,CAAC,SAAS,IAAI,CAAC,CAAC,WAAW,EAAE;YAC/E,aAAa,EAAE,KAAK;YACpB,UAAU;YACV,SAAS,EAAE,CAAC,CAAC,SAAS,KAAK,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO;YACnD,MAAM,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,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-MARGIN', v.margin_asset),\n datasource_id: 'HTX',\n product_id: encodePath('HTX', 'SWAP-MARGIN', v.margin_asset),\n volume: +v.margin_balance,\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 valuation,\n });\n }\n\n return positions;\n};\n"]}
@@ -1,8 +1,9 @@
1
+ import { createCache } from '@yuants/cache';
1
2
  import { Terminal } from '@yuants/protocol';
2
- import { writeToSQL } from '@yuants/sql';
3
+ import { escapeSQL, requestSQL, writeToSQL } from '@yuants/sql';
3
4
  import { encodePath, formatTime } from '@yuants/utils';
4
5
  import { defer, from, groupBy, map, merge, mergeMap, repeat, retry, scan, shareReplay, toArray } from 'rxjs';
5
- import { getSwapBatchFundingRate, getSwapMarketBbo, getSwapMarketTrade, getSwapOpenInterest, } from '../../api/public-api';
6
+ import { getSpotMarketTickers, getSwapBatchFundingRate, getSwapMarketBbo, getSwapMarketTrade, getSwapOpenInterest, } from '../../api/public-api';
6
7
  const swapBboTick$ = defer(() => getSwapMarketBbo({})).pipe(repeat({ delay: 1000 }), retry({ delay: 1000 }), shareReplay(1));
7
8
  const quote1$ = swapBboTick$.pipe(mergeMap((res) => res.ticks || []), map((tick) => {
8
9
  const [ask_price = '', ask_volume = ''] = tick.ask || [];
@@ -23,6 +24,15 @@ const quote2$ = swapTradeTick$.pipe(mergeMap((res) => { var _a; return ((_a = re
23
24
  product_id: encodePath('HTX', 'SWAP', tick.contract_code),
24
25
  last_price: `${tick.price}`,
25
26
  })));
27
+ const quote5$ = defer(() => getSpotMarketTickers()).pipe(mergeMap((res) => res.data || []), map((tick) => ({
28
+ datasource_id: 'HTX',
29
+ product_id: encodePath('HTX', 'SPOT', tick.symbol),
30
+ ask_price: `${tick.ask}`,
31
+ bid_price: `${tick.bid}`,
32
+ ask_volume: `${tick.askSize}`,
33
+ bid_volume: `${tick.bidSize}`,
34
+ last_price: `${tick.close}`,
35
+ })), repeat({ delay: 1000 }), retry({ delay: 1000 }), shareReplay(1));
26
36
  const mapSwapContractCodeToTradeTick$ = defer(() => swapTradeTick$).pipe(mergeMap((res) => from(res.tick.data).pipe(map((tick) => [tick.contract_code, tick]), toArray(), map((ticks) => Object.fromEntries(ticks)))), repeat({ delay: 1000 }), retry({ delay: 1000 }), shareReplay(1));
27
37
  const swapFundingRateTick$ = defer(() => getSwapBatchFundingRate({})).pipe(repeat({ delay: 1000 }), retry({ delay: 1000 }), shareReplay(1));
28
38
  const quote3$ = swapFundingRateTick$.pipe(mergeMap((res) => res.data || []), map((tick) => ({
@@ -41,7 +51,7 @@ const quote4$ = swapOpenInterest$.pipe(mergeMap((res) => res.data || []), map((t
41
51
  })));
42
52
  if (process.env.WRITE_QUOTE_TO_SQL === 'true') {
43
53
  // 合并不同来源的数据并进行合并,避免死锁
44
- merge(quote1$, quote2$, quote3$, quote4$)
54
+ merge(quote1$, quote2$, quote3$, quote4$, quote5$)
45
55
  .pipe(groupBy((x) => encodePath(x.datasource_id, x.product_id)), mergeMap((group$) => {
46
56
  return group$.pipe(scan((acc, cur) => Object.assign(acc, cur), {}));
47
57
  }))
@@ -54,6 +64,14 @@ if (process.env.WRITE_QUOTE_TO_SQL === 'true') {
54
64
  .subscribe();
55
65
  }
56
66
  const mapSwapContractCodeToOpenInterest$ = defer(() => swapOpenInterest$).pipe(mergeMap((res) => from(res.data).pipe(map((tick) => [tick.contract_code, tick]), toArray(), map((ticks) => Object.fromEntries(ticks)))), repeat({ delay: 1000 }), retry({ delay: 1000 }), shareReplay(1));
67
+ const terminal = Terminal.fromNodeEnv();
68
+ export const quoteCache = createCache(async (product_id) => {
69
+ const sql = `select * from quote where product_id = ${escapeSQL(product_id)}`;
70
+ console.info('QuoteSQL', sql);
71
+ const [quote] = await requestSQL(terminal, sql);
72
+ console.info('QuoteFetched', product_id, JSON.stringify(quote));
73
+ return quote;
74
+ }, { expire: 10000 });
57
75
  // provideTicks(Terminal.fromNodeEnv(), 'HUOBI-SWAP', (product_id) => {
58
76
  // return defer(async () => {
59
77
  // const products = await firstValueFrom(perpetualContractProducts$);
@@ -1 +1 @@
1
- {"version":3,"file":"quote.js","sourceRoot":"","sources":["../../../src/services/market-data/quote.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AACvD,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAC7G,OAAO,EACL,uBAAuB,EACvB,gBAAgB,EAChB,kBAAkB,EAClB,mBAAmB,GACpB,MAAM,sBAAsB,CAAC;AAE9B,MAAM,YAAY,GAAG,KAAK,CAAC,GAAG,EAAE,CAAC,gBAAgB,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CACzD,MAAM,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,EACvB,KAAK,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,EACtB,WAAW,CAAC,CAAC,CAAC,CACf,CAAC;AAEF,MAAM,OAAO,GAAG,YAAY,CAAC,IAAI,CAC/B,QAAQ,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,KAAK,IAAI,EAAE,CAAC,EAClC,GAAG,CAAC,CAAC,IAAI,EAAmB,EAAE;IAC5B,MAAM,CAAC,SAA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AC,IAAI,CAC5E,QAAQ,CAAC,CAAC,GAAG,EAAE,EAAE,CACf,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,IAAI,CACjB,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,aAAa,EAAE,IAAI,CAAU,CAAC,EAClD,OAAO,EAAE,EACT,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,MAAM,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC,CAC1C,CACF,EAED,MAAM,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,EACvB,KAAK,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,EACtB,WAAW,CAAC,CAAC,CAAC,CACf,CAAC;AAEF,uEAAuE;AACvE,+BAA+B;AAC/B,yEAAyE;AACzE,4EAA4E;AAC5E,iEAAiE;AAEjE,eAAe;AACf,wBAAwB;AACxB,uCAAuC;AACvC,yCAAyC;AACzC,+CAA+C;AAC/C,4CAA4C;AAC5C,kBAAkB;AAClB,aAAa;AACb,+BAA+B;AAC/B,sBAAsB;AACtB,+BAA+B;AAC/B,4FAA4F;AAC5F,qBAAqB;AACrB,2CAA2C;AAC3C,0BAA0B;AAC1B,sCAAsC;AACtC,kFAAkF;AAClF,mDAAmD;AACnD,8DAA8D;AAC9D,8DAA8D;AAC9D,qDAAqD;AACrD,kFAAkF;AAClF,kFAAkF;AAClF,gEAAgE;AAChE,eAAe;AACf,cAAc;AACd,WAAW;AACX,SAAS;AACT,OAAO;AACP,MAAM","sourcesContent":["import { IQuote } from '@yuants/data-quote';\nimport { Terminal } from '@yuants/protocol';\nimport { writeToSQL } from '@yuants/sql';\nimport { encodePath, formatTime } from '@yuants/utils';\nimport { defer, from, groupBy, map, merge, mergeMap, repeat, retry, scan, shareReplay, toArray } from 'rxjs';\nimport {\n getSwapBatchFundingRate,\n getSwapMarketBbo,\n getSwapMarketTrade,\n getSwapOpenInterest,\n} from '../../api/public-api';\n\nconst swapBboTick$ = defer(() => getSwapMarketBbo({})).pipe(\n repeat({ delay: 1000 }),\n retry({ delay: 1000 }),\n shareReplay(1),\n);\n\nconst quote1$ = swapBboTick$.pipe(\n mergeMap((res) => res.ticks || []),\n map((tick): Partial<IQuote> => {\n const [ask_price = '', ask_volume = ''] = tick.ask || [];\n const [bid_price = '', bid_volume = ''] = tick.bid || [];\n return {\n datasource_id: 'HTX',\n product_id: encodePath('HTX', 'SWAP', tick.contract_code),\n ask_price: `${ask_price}`,\n bid_price: `${bid_price}`,\n ask_volume: `${ask_volume}`,\n bid_volume: `${bid_volume}`,\n };\n }),\n);\n\nconst mapSwapContractCodeToBboTick$ = defer(() => swapBboTick$).pipe(\n mergeMap((res) =>\n from(res.ticks).pipe(\n map((tick) => [tick.contract_code, tick] as const),\n toArray(),\n map((ticks) => Object.fromEntries(ticks)),\n ),\n ),\n\n repeat({ delay: 1000 }),\n retry({ delay: 1000 }),\n shareReplay(1),\n);\n\nconst swapTradeTick$ = defer(() => getSwapMarketTrade({})).pipe(\n repeat({ delay: 1000 }),\n retry({ delay: 1000 }),\n shareReplay(1),\n);\n\nconst quote2$ = swapTradeTick$.pipe(\n mergeMap((res) => res.tick?.data || []),\n map(\n (tick): Partial<IQuote> => ({\n datasource_id: 'HTX',\n product_id: encodePath('HTX', 'SWAP', tick.contract_code),\n last_price: `${tick.price}`,\n }),\n ),\n);\n\nconst mapSwapContractCodeToTradeTick$ = defer(() => swapTradeTick$).pipe(\n mergeMap((res) =>\n from(res.tick.data).pipe(\n map((tick) => [tick.contract_code, tick] as const),\n toArray(),\n map((ticks) => Object.fromEntries(ticks)),\n ),\n ),\n\n repeat({ delay: 1000 }),\n retry({ delay: 1000 }),\n shareReplay(1),\n);\nconst swapFundingRateTick$ = defer(() => getSwapBatchFundingRate({})).pipe(\n repeat({ delay: 1000 }),\n retry({ delay: 1000 }),\n shareReplay(1),\n);\n\nconst quote3$ = swapFundingRateTick$.pipe(\n mergeMap((res) => res.data || []),\n map(\n (tick): Partial<IQuote> => ({\n datasource_id: 'HTX',\n product_id: encodePath('HTX', 'SWAP', tick.contract_code),\n interest_rate_long: `${-tick.funding_rate}`,\n interest_rate_short: `${tick.funding_rate}`,\n interest_rate_next_settled_at: formatTime(+tick.funding_time),\n }),\n ),\n);\n\nconst mapSwapContractCodeToFundingRateTick$ = defer(() => swapFundingRateTick$).pipe(\n mergeMap((res) =>\n from(res.data).pipe(\n map((tick) => [tick.contract_code, tick] as const),\n toArray(),\n map((ticks) => Object.fromEntries(ticks)),\n ),\n ),\n\n repeat({ delay: 1000 }),\n retry({ delay: 1000 }),\n shareReplay(1),\n);\n\nconst swapOpenInterest$ = defer(() => getSwapOpenInterest({})).pipe(\n repeat({ delay: 1000 }),\n retry({ delay: 1000 }),\n shareReplay(1),\n);\n\nconst quote4$ = swapOpenInterest$.pipe(\n mergeMap((res) => res.data || []),\n map(\n (tick): Partial<IQuote> => ({\n datasource_id: 'HTX',\n product_id: encodePath('HTX', 'SWAP', tick.contract_code),\n open_interest: `${tick.volume}`,\n }),\n ),\n);\n\nif (process.env.WRITE_QUOTE_TO_SQL === 'true') {\n // 合并不同来源的数据并进行合并,避免死锁\n merge(quote1$, quote2$, quote3$, quote4$)\n .pipe(\n groupBy((x) => encodePath(x.datasource_id, x.product_id)),\n mergeMap((group$) => {\n return group$.pipe(scan((acc, cur) => Object.assign(acc, cur), {} as Partial<IQuote>));\n }),\n )\n .pipe(\n writeToSQL({\n terminal: Terminal.fromNodeEnv(),\n tableName: 'quote',\n writeInterval: 1000,\n conflictKeys: ['datasource_id', 'product_id'],\n }),\n )\n .subscribe();\n}\n\nconst mapSwapContractCodeToOpenInterest$ = defer(() => swapOpenInterest$).pipe(\n mergeMap((res) =>\n from(res.data).pipe(\n map((tick) => [tick.contract_code, tick] as const),\n toArray(),\n map((ticks) => Object.fromEntries(ticks)),\n ),\n ),\n\n repeat({ delay: 1000 }),\n retry({ delay: 1000 }),\n shareReplay(1),\n);\n\n// provideTicks(Terminal.fromNodeEnv(), 'HUOBI-SWAP', (product_id) => {\n// return defer(async () => {\n// const products = await firstValueFrom(perpetualContractProducts$);\n// const theProduct = products.find((x) => x.product_id === product_id);\n// if (!theProduct) throw `No Found ProductID ${product_id}`;\n\n// return [\n// of(theProduct),\n// mapSwapContractCodeToBboTick$,\n// mapSwapContractCodeToTradeTick$,\n// mapSwapContractCodeToFundingRateTick$,\n// mapSwapContractCodeToOpenInterest$,\n// ] as const;\n// }).pipe(\n// catchError(() => EMPTY),\n// mergeMap((x) =>\n// combineLatest(x).pipe(\n// map(([theProduct, bboTick, tradeTick, fundingRateTick, openInterest]): ITick => {\n// return {\n// datasource_id: 'HUOBI-SWAP',\n// product_id,\n// updated_at: Date.now(),\n// settlement_scheduled_at: +fundingRateTick[product_id].funding_time,\n// price: +tradeTick[product_id].price,\n// ask: bboTick[product_id].ask?.[0] ?? undefined,\n// bid: bboTick[product_id].bid?.[0] ?? undefined,\n// volume: +tradeTick[product_id].amount,\n// interest_rate_for_long: -+fundingRateTick[product_id].funding_rate,\n// interest_rate_for_short: +fundingRateTick[product_id].funding_rate,\n// open_interest: +openInterest[product_id]?.volume,\n// };\n// }),\n// ),\n// ),\n// );\n// });\n"]}
1
+ {"version":3,"file":"quote.js","sourceRoot":"","sources":["../../../src/services/market-data/quote.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAChE,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AACvD,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;A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4CAA4C;AAC5C,kBAAkB;AAClB,aAAa;AACb,+BAA+B;AAC/B,sBAAsB;AACtB,+BAA+B;AAC/B,4FAA4F;AAC5F,qBAAqB;AACrB,2CAA2C;AAC3C,0BAA0B;AAC1B,sCAAsC;AACtC,kFAAkF;AAClF,mDAAmD;AACnD,8DAA8D;AAC9D,8DAA8D;AAC9D,qDAAqD;AACrD,kFAAkF;AAClF,kFAAkF;AAClF,gEAAgE;AAChE,eAAe;AACf,cAAc;AACd,WAAW;AACX,SAAS;AACT,OAAO;AACP,MAAM","sourcesContent":["import { createCache } from '@yuants/cache';\nimport { IQuote } from '@yuants/data-quote';\nimport { Terminal } from '@yuants/protocol';\nimport { escapeSQL, requestSQL, writeToSQL } from '@yuants/sql';\nimport { encodePath, formatTime } from '@yuants/utils';\nimport { defer, from, groupBy, map, merge, mergeMap, repeat, retry, scan, shareReplay, toArray } from 'rxjs';\nimport {\n getSpotMarketTickers,\n getSwapBatchFundingRate,\n getSwapMarketBbo,\n getSwapMarketTrade,\n getSwapOpenInterest,\n} from '../../api/public-api';\n\nconst swapBboTick$ = defer(() => getSwapMarketBbo({})).pipe(\n repeat({ delay: 1000 }),\n retry({ delay: 1000 }),\n shareReplay(1),\n);\n\nconst quote1$ = swapBboTick$.pipe(\n mergeMap((res) => res.ticks || []),\n map((tick): Partial<IQuote> => {\n const [ask_price = '', ask_volume = ''] = tick.ask || [];\n const [bid_price = '', bid_volume = ''] = tick.bid || [];\n return {\n datasource_id: 'HTX',\n product_id: encodePath('HTX', 'SWAP', tick.contract_code),\n ask_price: `${ask_price}`,\n bid_price: `${bid_price}`,\n ask_volume: `${ask_volume}`,\n bid_volume: `${bid_volume}`,\n };\n }),\n);\n\nconst mapSwapContractCodeToBboTick$ = defer(() => swapBboTick$).pipe(\n mergeMap((res) =>\n from(res.ticks).pipe(\n map((tick) => [tick.contract_code, tick] as const),\n toArray(),\n map((ticks) => Object.fromEntries(ticks)),\n ),\n ),\n\n repeat({ delay: 1000 }),\n retry({ delay: 1000 }),\n shareReplay(1),\n);\n\nconst swapTradeTick$ = defer(() => getSwapMarketTrade({})).pipe(\n repeat({ delay: 1000 }),\n retry({ delay: 1000 }),\n shareReplay(1),\n);\n\nconst quote2$ = swapTradeTick$.pipe(\n mergeMap((res) => res.tick?.data || []),\n map(\n (tick): Partial<IQuote> => ({\n datasource_id: 'HTX',\n product_id: encodePath('HTX', 'SWAP', tick.contract_code),\n last_price: `${tick.price}`,\n }),\n ),\n);\n\nconst quote5$ = defer(() => getSpotMarketTickers()).pipe(\n mergeMap((res) => res.data || []),\n map(\n (tick): Partial<IQuote> => ({\n datasource_id: 'HTX',\n product_id: encodePath('HTX', 'SPOT', tick.symbol),\n ask_price: `${tick.ask}`,\n bid_price: `${tick.bid}`,\n ask_volume: `${tick.askSize}`,\n bid_volume: `${tick.bidSize}`,\n last_price: `${tick.close}`,\n }),\n ),\n repeat({ delay: 1000 }),\n retry({ delay: 1000 }),\n shareReplay(1),\n);\n\nconst mapSwapContractCodeToTradeTick$ = defer(() => swapTradeTick$).pipe(\n mergeMap((res) =>\n from(res.tick.data).pipe(\n map((tick) => [tick.contract_code, tick] as const),\n toArray(),\n map((ticks) => Object.fromEntries(ticks)),\n ),\n ),\n\n repeat({ delay: 1000 }),\n retry({ delay: 1000 }),\n shareReplay(1),\n);\nconst swapFundingRateTick$ = defer(() => getSwapBatchFundingRate({})).pipe(\n repeat({ delay: 1000 }),\n retry({ delay: 1000 }),\n shareReplay(1),\n);\n\nconst quote3$ = swapFundingRateTick$.pipe(\n mergeMap((res) => res.data || []),\n map(\n (tick): Partial<IQuote> => ({\n datasource_id: 'HTX',\n product_id: encodePath('HTX', 'SWAP', tick.contract_code),\n interest_rate_long: `${-tick.funding_rate}`,\n interest_rate_short: `${tick.funding_rate}`,\n interest_rate_next_settled_at: formatTime(+tick.funding_time),\n }),\n ),\n);\n\nconst mapSwapContractCodeToFundingRateTick$ = defer(() => swapFundingRateTick$).pipe(\n mergeMap((res) =>\n from(res.data).pipe(\n map((tick) => [tick.contract_code, tick] as const),\n toArray(),\n map((ticks) => Object.fromEntries(ticks)),\n ),\n ),\n\n repeat({ delay: 1000 }),\n retry({ delay: 1000 }),\n shareReplay(1),\n);\n\nconst swapOpenInterest$ = defer(() => getSwapOpenInterest({})).pipe(\n repeat({ delay: 1000 }),\n retry({ delay: 1000 }),\n shareReplay(1),\n);\n\nconst quote4$ = swapOpenInterest$.pipe(\n mergeMap((res) => res.data || []),\n map(\n (tick): Partial<IQuote> => ({\n datasource_id: 'HTX',\n product_id: encodePath('HTX', 'SWAP', tick.contract_code),\n open_interest: `${tick.volume}`,\n }),\n ),\n);\n\nif (process.env.WRITE_QUOTE_TO_SQL === 'true') {\n // 合并不同来源的数据并进行合并,避免死锁\n merge(quote1$, quote2$, quote3$, quote4$, quote5$)\n .pipe(\n groupBy((x) => encodePath(x.datasource_id, x.product_id)),\n mergeMap((group$) => {\n return group$.pipe(scan((acc, cur) => Object.assign(acc, cur), {} as Partial<IQuote>));\n }),\n )\n .pipe(\n writeToSQL({\n terminal: Terminal.fromNodeEnv(),\n tableName: 'quote',\n writeInterval: 1000,\n conflictKeys: ['datasource_id', 'product_id'],\n }),\n )\n .subscribe();\n}\n\nconst mapSwapContractCodeToOpenInterest$ = defer(() => swapOpenInterest$).pipe(\n mergeMap((res) =>\n from(res.data).pipe(\n map((tick) => [tick.contract_code, tick] as const),\n toArray(),\n map((ticks) => Object.fromEntries(ticks)),\n ),\n ),\n\n repeat({ delay: 1000 }),\n retry({ delay: 1000 }),\n shareReplay(1),\n);\n\nconst terminal = Terminal.fromNodeEnv();\n\nexport const quoteCache = createCache<IQuote>(\n async (product_id) => {\n const sql = `select * from quote where product_id = ${escapeSQL(product_id)}`;\n console.info('QuoteSQL', sql);\n const [quote] = await requestSQL<IQuote[]>(terminal, sql);\n console.info('QuoteFetched', product_id, JSON.stringify(quote));\n return quote;\n },\n { expire: 10_000 },\n);\n\n// provideTicks(Terminal.fromNodeEnv(), 'HUOBI-SWAP', (product_id) => {\n// return defer(async () => {\n// const products = await firstValueFrom(perpetualContractProducts$);\n// const theProduct = products.find((x) => x.product_id === product_id);\n// if (!theProduct) throw `No Found ProductID ${product_id}`;\n\n// return [\n// of(theProduct),\n// mapSwapContractCodeToBboTick$,\n// mapSwapContractCodeToTradeTick$,\n// mapSwapContractCodeToFundingRateTick$,\n// mapSwapContractCodeToOpenInterest$,\n// ] as const;\n// }).pipe(\n// catchError(() => EMPTY),\n// mergeMap((x) =>\n// combineLatest(x).pipe(\n// map(([theProduct, bboTick, tradeTick, fundingRateTick, openInterest]): ITick => {\n// return {\n// datasource_id: 'HUOBI-SWAP',\n// product_id,\n// updated_at: Date.now(),\n// settlement_scheduled_at: +fundingRateTick[product_id].funding_time,\n// price: +tradeTick[product_id].price,\n// ask: bboTick[product_id].ask?.[0] ?? undefined,\n// bid: bboTick[product_id].bid?.[0] ?? undefined,\n// volume: +tradeTick[product_id].amount,\n// interest_rate_for_long: -+fundingRateTick[product_id].funding_rate,\n// interest_rate_for_short: +fundingRateTick[product_id].funding_rate,\n// open_interest: +openInterest[product_id]?.volume,\n// };\n// }),\n// ),\n// ),\n// );\n// });\n"]}
@@ -1,8 +1,7 @@
1
1
  import { decodePath, formatTime, newError, roundToStep } from '@yuants/utils';
2
- import { firstValueFrom } from 'rxjs';
3
2
  import { getCrossMarginLoanInfo, getSpotAccountBalance, getSwapCrossPositionInfo, postSpotOrder, postSwapOrder, } from '../../api/private-api';
4
3
  import { getSpotTick } from '../../api/public-api';
5
- import { productService } from '../product';
4
+ import { productCache } from '../product';
6
5
  import { superMarginAccountUidCache } from '../uid';
7
6
  /**
8
7
  * 处理 swap 账户订单提交
@@ -45,17 +44,16 @@ async function handleSuperMarginOrder(order, credential) {
45
44
  throw new Error('USDT loanable amount not found');
46
45
  const loanable = +usdtLoanable['loanable-amt'];
47
46
  // 获取账户余额, 产品信息和价格
48
- const [balanceRes, mapProductIdToProduct, priceRes] = await Promise.all([
47
+ const [balanceRes, theProduct, priceRes] = await Promise.all([
49
48
  getSpotAccountBalance(credential, superMarginAccountUid),
50
- firstValueFrom(productService.mapProductIdToProduct$),
49
+ productCache.query(order.product_id),
51
50
  getSpotTick({ symbol: order.product_id }),
52
51
  ]);
53
52
  const balance = balanceRes.data.list
54
53
  .filter((v) => v.currency === 'usdt' && v.type === 'trade')
55
54
  .reduce((acc, cur) => acc + +cur.balance, 0);
56
- if (!mapProductIdToProduct)
57
- throw new Error('Product map not found');
58
- const theProduct = mapProductIdToProduct.get(order.product_id);
55
+ if (!theProduct)
56
+ throw newError('HUOBI_SUBMIT_ORDER_PRODUCT_NOT_FOUND', { product_id: order.product_id });
59
57
  const price = priceRes.tick.close;
60
58
  const borrow_amount = order.order_direction === 'OPEN_LONG' || order.order_direction === 'CLOSE_SHORT'
61
59
  ? Math.max(Math.min(loanable, order.volume * price - balance), 0)
@@ -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,EAAE,cAAc,EAAE,MAAM,MAAM,CAAC;AACtC,OAAO,EAEL,sBAAsB,EACtB,qBAAqB,EACrB,wBAAwB,EACxB,aAAa,EACb,aAAa,GACd,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACnD,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAC5C,OAAO,EAAE,0BAA0B,EAAE,MAAM,QAAQ,CAAC;AAEpD;;GAEG;AACH,KAAK,UAAU,eAAe,CAAC,KAAa,EAAE,UAAuB;;IACnE,SAAS;IACT,MAAM,YAAY,GAAG,MAAM,wBAAwB,CAAC,UAAU,CAAC,CAAC;IAChE,MAAM,qBAAqB,GAAG,MAAM,CAAC,WAAW,CAC9C,YAAY,CAAC,IAAI,CAAC,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C,UAAU,KAAK,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,GAAG,KAAK,CAAC,KAAK;QACnE,MAAM,EAAE,kBAAkB;KAC3B,CAAC;IAEF,MAAM,MAAM,GAAG,MAAM,aAAa,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;IACvD,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,EAAE,aAAa,EAAE,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC;IAEpG,IAAI,MAAM,CAAC,OAAO,KAAK,KAAK,EAAE;QAC5B,MAAM,IAAI,KAAK,CAAC,6CAA6C,MAAM,CAAC,IAAI,YAAY,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC;KACvG;IAED,OAAO,EAAE,QAAQ,EAAE,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC;AACtD,CAAC;AAED,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,UAAuB,EAAE,KAAa,EAAiC,EAAE;IACnG,MAAM,CAAC,EAAE,QAAQ,CAAC,GAAG,UAAU,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;IAClD,IAAI,QAAQ,KAAK,MAAM,EAAE;QACvB,OAAO,eAAe,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;KAC3C;IACD,IAAI,QAAQ,KAAK,cAAc,EAAE;QAC/B,OAAO,sBAAsB,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;KAClD;IACD,MAAM,QAAQ,CAAC,uBAAuB,EAAE,EAAE,UAAU,EAAE,QAAQ,EAAE,CAAC,CAAC;AACpE,CAAC,CAAC","sourcesContent":["import { IOrder } from '@yuants/data-order';\nimport { decodePath, formatTime, newError, roundToStep } from '@yuants/utils';\nimport { firstValueFrom } from 'rxjs';\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 { productService } 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 lever_rate = mapContractCodeToRate[order.product_id] ?? 20;\n const params = {\n contract_code: order.product_id,\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 };\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 new Error(`Failed to submit swap order: status=${result.status}`);\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 // 获取账户余额, 产品信息和价格\n const [balanceRes, mapProductIdToProduct, priceRes] = await Promise.all([\n getSpotAccountBalance(credential, superMarginAccountUid),\n firstValueFrom(productService.mapProductIdToProduct$),\n getSpotTick({ symbol: order.product_id }),\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 (!mapProductIdToProduct) throw new Error('Product map not found');\n\n const theProduct = mapProductIdToProduct.get(order.product_id);\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: order.product_id,\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 };\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"]}
1
+ {"version":3,"file":"submitOrder.js","sourceRoot":"","sources":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AW,GAAG,CAAC,UAAuB,EAAE,KAAa,EAAiC,EAAE;IACnG,MAAM,CAAC,EAAE,QAAQ,CAAC,GAAG,UAAU,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;IAClD,IAAI,QAAQ,KAAK,MAAM,EAAE;QACvB,OAAO,eAAe,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;KAC3C;IACD,IAAI,QAAQ,KAAK,cAAc,EAAE;QAC/B,OAAO,sBAAsB,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;KAClD;IACD,MAAM,QAAQ,CAAC,uBAAuB,EAAE,EAAE,UAAU,EAAE,QAAQ,EAAE,CAAC,CAAC;AACpE,CAAC,CAAC","sourcesContent":["import { IOrder } from '@yuants/data-order';\nimport { decodePath, formatTime, newError, roundToStep } from '@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 lever_rate = mapContractCodeToRate[order.product_id] ?? 20;\n const params = {\n contract_code: order.product_id,\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 };\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 new Error(`Failed to submit swap order: status=${result.status}`);\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 // 获取账户余额, 产品信息和价格\n const [balanceRes, theProduct, priceRes] = await Promise.all([\n getSpotAccountBalance(credential, superMarginAccountUid),\n productCache.query(order.product_id),\n getSpotTick({ symbol: order.product_id }),\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: order.product_id,\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 };\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"]}
@@ -1,4 +1,4 @@
1
- import { provideQueryProductsService } from '@yuants/data-product';
1
+ import { createProductCache } from '@yuants/data-product';
2
2
  import { Terminal } from '@yuants/protocol';
3
3
  import { encodePath } from '@yuants/utils';
4
4
  import { getPerpetualContractSymbols, getSpotSymbols, getSwapCrossLadderMargin } from '../api/public-api';
@@ -74,7 +74,5 @@ export const listProducts = async () => {
74
74
  const [swapProducts, spotProducts] = await Promise.all([listSwapProducts(), listSpotProducts()]);
75
75
  return [...swapProducts, ...spotProducts];
76
76
  };
77
- export const productService = provideQueryProductsService(terminal, 'HTX', listProducts, {
78
- auto_refresh_interval: 3600000,
79
- });
77
+ export const productCache = createProductCache(terminal);
80
78
  //# sourceMappingURL=product.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"product.js","sourceRoot":"","sources":["../../src/services/product.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,2BAA2B,EAAE,MAAM,sBAAsB,CAAC;AAC7E,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,2BAA2B,EAAE,cAAc,EAAE,wBAAwB,EAAE,MAAM,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AED,OAAO,QAAQ,CAAC;AAClB,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,YAAY,GAAG,KAAK,IAAyB,EAAE;IAC1D,MAAM,CAAC,YAAY,EAAE,YAAY,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC,gBAAgB,EAAE,EAAE,gBAAgB,EAAE,CAAC,CAAC,CAAC;IACjG,OAAO,CAAC,GAAG,YAAY,EAAE,GAAG,YAAY,CAAC,CAAC;AAC5C,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,cAAc,GAAG,2BAA2B,CAAC,QAAQ,EAAE,KAAK,EAAE,YAAY,EAAE;IACvF,qBAAqB,EAAE,OAAQ;CAChC,CAAC,CAAC","sourcesContent":["import { IProduct, provideQueryProductsService } from '@yuants/data-product';\nimport { Terminal } from '@yuants/protocol';\nimport { encodePath } from '@yuants/utils';\nimport { getPerpetualContractSymbols, getSpotSymbols, getSwapCrossLadderMargin } from '../api/public-api';\n\nconst terminal = Terminal.fromNodeEnv();\n\n// Provide QueryProducts service for swap products\nconst listSwapProducts = async (): Promise<IProduct[]> => {\n const products: IProduct[] = [];\n\n // Get perpetual contract products and cross leverage data in parallel\n const [swapSymbols, crossLeverage] = await Promise.all([\n getPerpetualContractSymbols(),\n getSwapCrossLadderMargin(),\n ]);\n\n for (const symbol of swapSymbols?.data || []) {\n if (symbol.contract_status !== 1) continue; // Only include active contracts\n\n const maxLeverage = crossLeverage?.data\n .find((x) => x.contract_code === symbol.contract_code)\n ?.list.reduce((acc, cur) => Math.max(acc, cur.lever_rate), 1);\n\n products.push({\n datasource_id: 'HTX',\n product_id: encodePath('HTX', 'SWAP', symbol.contract_code),\n base_currency: symbol.symbol,\n quote_currency: 'USDT',\n value_scale: symbol.contract_size,\n price_step: symbol.price_tick,\n volume_step: 1,\n name: '',\n value_scale_unit: '',\n margin_rate: 1 / (maxLeverage || 1),\n value_based_cost: 0,\n volume_based_cost: 0,\n max_position: 0,\n max_volume: 0,\n allow_long: true,\n allow_short: true,\n market_id: 'HTX',\n no_interest_rate: false,\n });\n }\n\n return products;\n};\n\n// Provide QueryProducts service for spot products\nconst listSpotProducts = async (): Promise<IProduct[]> => {\n const products: IProduct[] = [];\n\n // Get spot products\n const spotSymbols = await getSpotSymbols();\n\n for (const symbol of spotSymbols?.data || []) {\n if (symbol.state !== 'online') continue; // Only include online symbols\n\n products.push({\n datasource_id: 'HTX',\n product_id: encodePath('HTX', 'SPOT', symbol.sc),\n base_currency: symbol.bc,\n quote_currency: symbol.qc,\n value_scale: 1,\n price_step: 1 / 10 ** symbol.tpp,\n volume_step: 1 / 10 ** symbol.tap,\n name: '',\n value_scale_unit: '',\n margin_rate: 1,\n value_based_cost: 0,\n volume_based_cost: 0,\n max_position: 0,\n max_volume: 0,\n allow_long: true,\n allow_short: false,\n market_id: 'HUOBI/SPOT',\n no_interest_rate: true,\n });\n }\n\n return products;\n};\n\nexport const listProducts = async (): Promise<IProduct[]> => {\n const [swapProducts, spotProducts] = await Promise.all([listSwapProducts(), listSpotProducts()]);\n return [...swapProducts, ...spotProducts];\n};\n\nexport const productService = provideQueryProductsService(terminal, 'HTX', listProducts, {\n auto_refresh_interval: 3600_000,\n});\n"]}
1
+ {"version":3,"file":"product.js","sourceRoot":"","sources":["../../src/services/product.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAY,MAAM,sBAAsB,CAAC;AACpE,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,2BAA2B,EAAE,cAAc,EAAE,wBAAwB,EAAE,MAAM,mBAAmB,CAAC;AAE1G,MAAM,QAAQ,GAAG,QAAQ,CAAC,WAAW,EAAE,CAAC;AAExC,kDAAkD;AAClD,MAAM,gBAAgB,GAAG,KAAK,IAAyB,EAAE;;IACvD,MAAM,QAAQ,GAAe,EAAE,CAAC;IAEhC,sEAAsE;IACtE,MAAM,C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E,gBAAgB,EAAE,CAAC,CAAC,CAAC;IACjG,OAAO,CAAC,GAAG,YAAY,EAAE,GAAG,YAAY,CAAC,CAAC;AAC5C,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,YAAY,GAAG,kBAAkB,CAAC,QAAQ,CAAC,CAAC","sourcesContent":["import { createProductCache, IProduct } from '@yuants/data-product';\nimport { Terminal } from '@yuants/protocol';\nimport { encodePath } from '@yuants/utils';\nimport { getPerpetualContractSymbols, getSpotSymbols, getSwapCrossLadderMargin } from '../api/public-api';\n\nconst terminal = Terminal.fromNodeEnv();\n\n// Provide QueryProducts service for swap products\nconst listSwapProducts = async (): Promise<IProduct[]> => {\n const products: IProduct[] = [];\n\n // Get perpetual contract products and cross leverage data in parallel\n const [swapSymbols, crossLeverage] = await Promise.all([\n getPerpetualContractSymbols(),\n getSwapCrossLadderMargin(),\n ]);\n\n for (const symbol of swapSymbols?.data || []) {\n if (symbol.contract_status !== 1) continue; // Only include active contracts\n\n const maxLeverage = crossLeverage?.data\n .find((x) => x.contract_code === symbol.contract_code)\n ?.list.reduce((acc, cur) => Math.max(acc, cur.lever_rate), 1);\n\n products.push({\n datasource_id: 'HTX',\n product_id: encodePath('HTX', 'SWAP', symbol.contract_code),\n base_currency: symbol.symbol,\n quote_currency: 'USDT',\n value_scale: symbol.contract_size,\n price_step: symbol.price_tick,\n volume_step: 1,\n name: '',\n value_scale_unit: '',\n margin_rate: 1 / (maxLeverage || 1),\n value_based_cost: 0,\n volume_based_cost: 0,\n max_position: 0,\n max_volume: 0,\n allow_long: true,\n allow_short: true,\n market_id: 'HTX',\n no_interest_rate: false,\n });\n }\n\n return products;\n};\n\n// Provide QueryProducts service for spot products\nconst listSpotProducts = async (): Promise<IProduct[]> => {\n const products: IProduct[] = [];\n\n // Get spot products\n const spotSymbols = await getSpotSymbols();\n\n for (const symbol of spotSymbols?.data || []) {\n if (symbol.state !== 'online') continue; // Only include online symbols\n\n products.push({\n datasource_id: 'HTX',\n product_id: encodePath('HTX', 'SPOT', symbol.sc),\n base_currency: symbol.bc,\n quote_currency: symbol.qc,\n value_scale: 1,\n price_step: 1 / 10 ** symbol.tpp,\n volume_step: 1 / 10 ** symbol.tap,\n name: '',\n value_scale_unit: '',\n margin_rate: 1,\n value_based_cost: 0,\n volume_based_cost: 0,\n max_position: 0,\n max_volume: 0,\n allow_long: true,\n allow_short: false,\n market_id: 'HUOBI/SPOT',\n no_interest_rate: true,\n });\n }\n\n return products;\n};\n\nexport const listProducts = async (): Promise<IProduct[]> => {\n const [swapProducts, spotProducts] = await Promise.all([listSwapProducts(), listSpotProducts()]);\n return [...swapProducts, ...spotProducts];\n};\n\nexport const productCache = createProductCache(terminal);\n"]}