@yuants/vendor-okx 0.30.6 → 0.31.0

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/index.js CHANGED
@@ -15,4 +15,6 @@ import './trade';
15
15
  import './transfer';
16
16
  import './public-data/new-quote';
17
17
  import './services/quotes';
18
+ import './services/ohlc-service';
19
+ import './services/interest-rate-service';
18
20
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,WAAW,CAAC;AACnB,OAAO,mCAAmC,CAAC;AAC3C,OAAO,eAAe,CAAC;AACvB,OAAO,yBAAyB,CAAC;AACjC,OAAO,SAAS,CAAC;AACjB,OAAO,iBAAiB,CAAC;AACzB,OAAO,iCAAiC,CAAC;AACzC,OAAO,6BAA6B,CAAC;AACrC,OAAO,4BAA4B,CAAC;AACpC,OAAO,oBAAoB,CAAC;AAC5B,OAAO,qBAAqB,CAAC;AAC7B,OAAO,YAAY,CAAC;AACpB,OAAO,oBAAoB,CAAC;AAC5B,OAAO,SAAS,CAAC;AACjB,OAAO,YAAY,CAAC;AACpB,OAAO,yBAAyB,CAAC;AACjC,OAAO,mBAAmB,CAAC","sourcesContent":["import './account';\nimport './account-actions-with-credential';\nimport './api-service';\nimport './experimental/exchange';\nimport './order';\nimport './order-actions';\nimport './order-actions-with-credential';\nimport './public-data/interest_rate';\nimport './public-data/market-order';\nimport './public-data/ohlc';\nimport './public-data/quote';\nimport './services';\nimport './strategy-account';\nimport './trade';\nimport './transfer';\nimport './public-data/new-quote';\nimport './services/quotes';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,WAAW,CAAC;AACnB,OAAO,mCAAmC,CAAC;AAC3C,OAAO,eAAe,CAAC;AACvB,OAAO,yBAAyB,CAAC;AACjC,OAAO,SAAS,CAAC;AACjB,OAAO,iBAAiB,CAAC;AACzB,OAAO,iCAAiC,CAAC;AACzC,OAAO,6BAA6B,CAAC;AACrC,OAAO,4BAA4B,CAAC;AACpC,OAAO,oBAAoB,CAAC;AAC5B,OAAO,qBAAqB,CAAC;AAC7B,OAAO,YAAY,CAAC;AACpB,OAAO,oBAAoB,CAAC;AAC5B,OAAO,SAAS,CAAC;AACjB,OAAO,YAAY,CAAC;AACpB,OAAO,yBAAyB,CAAC;AACjC,OAAO,mBAAmB,CAAC;AAC3B,OAAO,yBAAyB,CAAC;AACjC,OAAO,kCAAkC,CAAC","sourcesContent":["import './account';\nimport './account-actions-with-credential';\nimport './api-service';\nimport './experimental/exchange';\nimport './order';\nimport './order-actions';\nimport './order-actions-with-credential';\nimport './public-data/interest_rate';\nimport './public-data/market-order';\nimport './public-data/ohlc';\nimport './public-data/quote';\nimport './services';\nimport './strategy-account';\nimport './trade';\nimport './transfer';\nimport './public-data/new-quote';\nimport './services/quotes';\nimport './services/ohlc-service';\nimport './services/interest-rate-service';\n"]}
@@ -0,0 +1,76 @@
1
+ import { provideInterestRateService } from '@yuants/exchange';
2
+ import { Terminal } from '@yuants/protocol';
3
+ import { decodePath, formatTime } from '@yuants/utils';
4
+ import { getFundingRateHistory, getLendingRateHistory } from '../api/public-api';
5
+ const terminal = Terminal.fromNodeEnv();
6
+ const fetchSwapFundingRateBackward = async (req) => {
7
+ const [, instType, instId] = decodePath(req.product_id);
8
+ if (instType !== 'SWAP' || !instId)
9
+ throw new Error(`Unsupported product_id: ${req.product_id}`);
10
+ const endedAtMs = req.time;
11
+ const res = await getFundingRateHistory({ instId, after: `${endedAtMs}` });
12
+ if (res.code !== '0') {
13
+ throw new Error(`OKX getFundingRateHistory failed: ${res.code} ${res.msg}`);
14
+ }
15
+ return res.data
16
+ .map((v) => {
17
+ const ms = Number(v.fundingTime);
18
+ const rate = Number(v.fundingRate);
19
+ return {
20
+ series_id: req.series_id,
21
+ product_id: req.product_id,
22
+ datasource_id: 'OKX',
23
+ created_at: formatTime(ms),
24
+ long_rate: `${-rate}`,
25
+ short_rate: `${rate}`,
26
+ settlement_price: '',
27
+ };
28
+ })
29
+ .filter((x) => Date.parse(x.created_at) < endedAtMs);
30
+ };
31
+ const fetchMarginInterestRateBackward = async (req) => {
32
+ const [, instType, instId] = decodePath(req.product_id);
33
+ if (instType !== 'MARGIN' || !instId)
34
+ throw new Error(`Unsupported product_id: ${req.product_id}`);
35
+ const [base, quote] = instId.split('-');
36
+ if (!base || !quote)
37
+ throw new Error(`Invalid MARGIN instId: ${instId}`);
38
+ const endedAtMs = req.time;
39
+ const [resBase, resQuote] = await Promise.all([
40
+ getLendingRateHistory({ ccy: base, after: `${endedAtMs}` }),
41
+ getLendingRateHistory({ ccy: quote, after: `${endedAtMs}` }),
42
+ ]);
43
+ if (resBase.code !== '0')
44
+ throw new Error(`OKX getLendingRateHistory failed: ${resBase.code} ${resBase.msg}`);
45
+ if (resQuote.code !== '0')
46
+ throw new Error(`OKX getLendingRateHistory failed: ${resQuote.code} ${resQuote.msg}`);
47
+ const mapTsToBaseRate = new Map();
48
+ resBase.data.forEach((v) => mapTsToBaseRate.set(v.ts, v.rate));
49
+ const data = [];
50
+ resQuote.data.forEach((v) => {
51
+ const baseRate = mapTsToBaseRate.get(v.ts);
52
+ if (!baseRate)
53
+ return;
54
+ const longRate = Number(v.rate) / 365 / 24;
55
+ const shortRate = Number(baseRate) / 365 / 24;
56
+ data.push({
57
+ series_id: req.series_id,
58
+ product_id: req.product_id,
59
+ datasource_id: 'OKX',
60
+ created_at: formatTime(+v.ts),
61
+ long_rate: `${-longRate}`,
62
+ short_rate: `${-shortRate}`,
63
+ settlement_price: '',
64
+ });
65
+ });
66
+ return data.filter((x) => Date.parse(x.created_at) < endedAtMs);
67
+ };
68
+ provideInterestRateService(terminal, {
69
+ product_id_prefix: 'OKX/SWAP/',
70
+ direction: 'backward',
71
+ }, fetchSwapFundingRateBackward);
72
+ provideInterestRateService(terminal, {
73
+ product_id_prefix: 'OKX/MARGIN/',
74
+ direction: 'backward',
75
+ }, fetchMarginInterestRateBackward);
76
+ //# sourceMappingURL=interest-rate-service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interest-rate-service.js","sourceRoot":"","sources":["../../src/services/interest-rate-service.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,0BAA0B,EAAE,MAAM,kBAAkB,CAAC;AAC9D,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AACvD,OAAO,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAEjF,MAAM,QAAQ,GAAG,QAAQ,CAAC,WAAW,EAAE,CAAC;AAExC,MAAM,4BAA4B,GAAG,KAAK,EAAE,GAI3C,EAA4B,EAAE;IAC7B,MAAM,CAAC,EAAE,QAAQ,EAAE,MAAM,CAAC,GAAG,UAAU,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IACxD,IAAI,QAAQ,KAAK,MAAM,IAAI,CAAC,MAAM;QAAE,MAAM,IAAI,KAAK,CAAC,2BAA2B,GAAG,CAAC,UAAU,EAAE,CAAC,CAAC;IAEjG,MAAM,SAAS,GAAG,GAAG,CAAC,IAAI,CAAC;IAC3B,MAAM,GAAG,GAAG,MAAM,qBAAqB,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,SAAS,EAAE,EAAE,CAAC,CAAC;IAC3E,IAAI,GAAG,CAAC,IAAI,KAAK,GAAG,EAAE;QACpB,MAAM,IAAI,KAAK,CAAC,qCAAqC,GAAG,CAAC,IAAI,IAAI,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC;KAC7E;IAED,OAAO,GAAG,CAAC,IAAI;SACZ,GAAG,CAAC,CAAC,CAAC,EAAiB,EAAE;QACxB,MAAM,EAAE,GAAG,MAAM,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC;QACjC,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AC3B,gBAAgB,EAAE,EAAE;SACrB,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,UAAU,CAAC,GAAG,SAAS,CAAC,CAAC;AAClE,CAAC,CAAC;AAEF,0BAA0B,CACxB,QAAQ,EACR;IACE,iBAAiB,EAAE,WAAW;IAC9B,SAAS,EAAE,UAAU;CACtB,EACD,4BAA4B,CAC7B,CAAC;AAEF,0BAA0B,CACxB,QAAQ,EACR;IACE,iBAAiB,EAAE,aAAa;IAChC,SAAS,EAAE,UAAU;CACtB,EACD,+BAA+B,CAChC,CAAC","sourcesContent":["import { IInterestRate } from '@yuants/data-interest-rate';\nimport { provideInterestRateService } from '@yuants/exchange';\nimport { Terminal } from '@yuants/protocol';\nimport { decodePath, formatTime } from '@yuants/utils';\nimport { getFundingRateHistory, getLendingRateHistory } from '../api/public-api';\n\nconst terminal = Terminal.fromNodeEnv();\n\nconst fetchSwapFundingRateBackward = async (req: {\n product_id: string;\n time: number;\n series_id: string;\n}): Promise<IInterestRate[]> => {\n const [, instType, instId] = decodePath(req.product_id);\n if (instType !== 'SWAP' || !instId) throw new Error(`Unsupported product_id: ${req.product_id}`);\n\n const endedAtMs = req.time;\n const res = await getFundingRateHistory({ instId, after: `${endedAtMs}` });\n if (res.code !== '0') {\n throw new Error(`OKX getFundingRateHistory failed: ${res.code} ${res.msg}`);\n }\n\n return res.data\n .map((v): IInterestRate => {\n const ms = Number(v.fundingTime);\n const rate = Number(v.fundingRate);\n return {\n series_id: req.series_id,\n product_id: req.product_id,\n datasource_id: 'OKX',\n created_at: formatTime(ms),\n long_rate: `${-rate}`,\n short_rate: `${rate}`,\n settlement_price: '',\n };\n })\n .filter((x) => Date.parse(x.created_at) < endedAtMs);\n};\n\nconst fetchMarginInterestRateBackward = async (req: {\n product_id: string;\n time: number;\n series_id: string;\n}): Promise<IInterestRate[]> => {\n const [, instType, instId] = decodePath(req.product_id);\n if (instType !== 'MARGIN' || !instId) throw new Error(`Unsupported product_id: ${req.product_id}`);\n\n const [base, quote] = instId.split('-');\n if (!base || !quote) throw new Error(`Invalid MARGIN instId: ${instId}`);\n\n const endedAtMs = req.time;\n\n const [resBase, resQuote] = await Promise.all([\n getLendingRateHistory({ ccy: base, after: `${endedAtMs}` }),\n getLendingRateHistory({ ccy: quote, after: `${endedAtMs}` }),\n ]);\n\n if (resBase.code !== '0')\n throw new Error(`OKX getLendingRateHistory failed: ${resBase.code} ${resBase.msg}`);\n if (resQuote.code !== '0')\n throw new Error(`OKX getLendingRateHistory failed: ${resQuote.code} ${resQuote.msg}`);\n\n const mapTsToBaseRate = new Map<string, string>();\n resBase.data.forEach((v) => mapTsToBaseRate.set(v.ts, v.rate));\n\n const data: IInterestRate[] = [];\n resQuote.data.forEach((v) => {\n const baseRate = mapTsToBaseRate.get(v.ts);\n if (!baseRate) return;\n\n const longRate = Number(v.rate) / 365 / 24;\n const shortRate = Number(baseRate) / 365 / 24;\n\n data.push({\n series_id: req.series_id,\n product_id: req.product_id,\n datasource_id: 'OKX',\n created_at: formatTime(+v.ts),\n long_rate: `${-longRate}`,\n short_rate: `${-shortRate}`,\n settlement_price: '',\n });\n });\n\n return data.filter((x) => Date.parse(x.created_at) < endedAtMs);\n};\n\nprovideInterestRateService(\n terminal,\n {\n product_id_prefix: 'OKX/SWAP/',\n direction: 'backward',\n },\n fetchSwapFundingRateBackward,\n);\n\nprovideInterestRateService(\n terminal,\n {\n product_id_prefix: 'OKX/MARGIN/',\n direction: 'backward',\n },\n fetchMarginInterestRateBackward,\n);\n"]}
@@ -0,0 +1,71 @@
1
+ import { provideOHLCService } from '@yuants/exchange';
2
+ import { Terminal } from '@yuants/protocol';
3
+ import { convertDurationToOffset, decodePath, formatTime } from '@yuants/utils';
4
+ import { getHistoryCandles } from '../api/public-api';
5
+ const terminal = Terminal.fromNodeEnv();
6
+ const DURATION_TO_OKX_BAR_TYPE = {
7
+ PT1M: '1m',
8
+ PT3M: '3m',
9
+ PT5M: '5m',
10
+ PT15M: '15m',
11
+ PT30M: '30m',
12
+ PT1H: '1H',
13
+ PT2H: '2H',
14
+ PT4H: '4H',
15
+ PT6H: '6H',
16
+ PT12H: '12H',
17
+ P1D: '1D',
18
+ P1W: '1W',
19
+ P1M: '1M',
20
+ };
21
+ const fetchOHLCPageBackward = async (req) => {
22
+ const [, instType, instId] = decodePath(req.product_id);
23
+ if (!instType || !instId)
24
+ throw new Error(`Invalid product_id: ${req.product_id}`);
25
+ const bar = DURATION_TO_OKX_BAR_TYPE[req.duration];
26
+ if (!bar)
27
+ throw new Error(`Unsupported duration: ${req.duration}`);
28
+ const offset = convertDurationToOffset(req.duration);
29
+ const endedAtMs = req.time;
30
+ const res = await getHistoryCandles({
31
+ instId,
32
+ bar,
33
+ after: `${endedAtMs}`,
34
+ limit: '300',
35
+ });
36
+ if (res.code !== '0') {
37
+ throw new Error(`OKX getHistoryCandles failed: ${res.code} ${res.msg}`);
38
+ }
39
+ return res.data
40
+ .map((x) => ({
41
+ series_id: req.series_id,
42
+ datasource_id: 'OKX',
43
+ product_id: req.product_id,
44
+ duration: req.duration,
45
+ created_at: formatTime(+x[0]),
46
+ closed_at: formatTime(+x[0] + offset),
47
+ open: x[1],
48
+ high: x[2],
49
+ low: x[3],
50
+ close: x[4],
51
+ volume: x[5],
52
+ open_interest: '0',
53
+ }))
54
+ .filter((x) => Date.parse(x.created_at) < endedAtMs);
55
+ };
56
+ provideOHLCService(terminal, {
57
+ product_id_prefix: 'OKX/SWAP/',
58
+ duration_list: Object.keys(DURATION_TO_OKX_BAR_TYPE),
59
+ direction: 'backward',
60
+ }, fetchOHLCPageBackward);
61
+ provideOHLCService(terminal, {
62
+ product_id_prefix: 'OKX/SPOT/',
63
+ duration_list: Object.keys(DURATION_TO_OKX_BAR_TYPE),
64
+ direction: 'backward',
65
+ }, fetchOHLCPageBackward);
66
+ provideOHLCService(terminal, {
67
+ product_id_prefix: 'OKX/MARGIN/',
68
+ duration_list: Object.keys(DURATION_TO_OKX_BAR_TYPE),
69
+ direction: 'backward',
70
+ }, fetchOHLCPageBackward);
71
+ //# sourceMappingURL=ohlc-service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ohlc-service.js","sourceRoot":"","sources":["../../src/services/ohlc-service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAEtD,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,uBAAuB,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAChF,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAEtD,MAAM,QAAQ,GAAG,QAAQ,CAAC,WAAW,EAAE,CAAC;AAExC,MAAM,wBAAwB,GAA2B;IACvD,IAAI,EAAE,IAAI;IACV,IAAI,EAAE,IAAI;IACV,IAAI,EAAE,IAAI;IACV,KAAK,EAAE,KAAK;IACZ,KAAK,EAAE,KAAK;IAEZ,IAAI,EAAE,IAAI;IACV,IAAI,EAAE,IAAI;IACV,IAAI,EAAE,IAAI;IACV,IAAI,EAAE,IAAI;IACV,KAAK,EAAE,KAAK;IAEZ,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;CACV,CAAC;AAEF,MAAM,qBAAqB,GAAG,KAAK,EAAE,GAKpC,EAAoB,EAAE;IACrB,MAAM,CAAC,EAAE,QAAQ,EAAE,MAAM,CAAC,GAAG,UAAU,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IACxD,IAAI,CAAC,QAAQ,IAAI,CAAC,MAAM;QAAE,MAAM,IAAI,KAAK,CAAC,uBAAuB,GAAG,CAAC,UAAU,EAAE,CAAC,CAAC;IAEnF,MAAM,GAAG,GAAG,wBAAwB,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IACn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,CAAC;AACzD,CAAC,CAAC;AAEF,kBAAkB,CAChB,QAAQ,EACR;IACE,iBAAiB,EAAE,WAAW;IAC9B,aAAa,EAAE,MAAM,CAAC,IAAI,CAAC,wBAAwB,CAAC;IACpD,SAAS,EAAE,UAAU;CACtB,EACD,qBAAqB,CACtB,CAAC;AAEF,kBAAkB,CAChB,QAAQ,EACR;IACE,iBAAiB,EAAE,WAAW;IAC9B,aAAa,EAAE,MAAM,CAAC,IAAI,CAAC,wBAAwB,CAAC;IACpD,SAAS,EAAE,UAAU;CACtB,EACD,qBAAqB,CACtB,CAAC;AAEF,kBAAkB,CAChB,QAAQ,EACR;IACE,iBAAiB,EAAE,aAAa;IAChC,aAAa,EAAE,MAAM,CAAC,IAAI,CAAC,wBAAwB,CAAC;IACpD,SAAS,EAAE,UAAU;CACtB,EACD,qBAAqB,CACtB,CAAC","sourcesContent":["import { provideOHLCService } from '@yuants/exchange';\nimport { IOHLC } from '@yuants/data-ohlc';\nimport { Terminal } from '@yuants/protocol';\nimport { convertDurationToOffset, decodePath, formatTime } from '@yuants/utils';\nimport { getHistoryCandles } from '../api/public-api';\n\nconst terminal = Terminal.fromNodeEnv();\n\nconst DURATION_TO_OKX_BAR_TYPE: Record<string, string> = {\n PT1M: '1m',\n PT3M: '3m',\n PT5M: '5m',\n PT15M: '15m',\n PT30M: '30m',\n\n PT1H: '1H',\n PT2H: '2H',\n PT4H: '4H',\n PT6H: '6H',\n PT12H: '12H',\n\n P1D: '1D',\n P1W: '1W',\n P1M: '1M',\n};\n\nconst fetchOHLCPageBackward = async (req: {\n product_id: string;\n duration: string;\n time: number;\n series_id: string;\n}): Promise<IOHLC[]> => {\n const [, instType, instId] = decodePath(req.product_id);\n if (!instType || !instId) throw new Error(`Invalid product_id: ${req.product_id}`);\n\n const bar = DURATION_TO_OKX_BAR_TYPE[req.duration];\n if (!bar) throw new Error(`Unsupported duration: ${req.duration}`);\n\n const offset = convertDurationToOffset(req.duration);\n const endedAtMs = req.time;\n\n const res = await getHistoryCandles({\n instId,\n bar,\n after: `${endedAtMs}`,\n limit: '300',\n });\n if (res.code !== '0') {\n throw new Error(`OKX getHistoryCandles failed: ${res.code} ${res.msg}`);\n }\n\n return res.data\n .map(\n (x): IOHLC => ({\n series_id: req.series_id,\n datasource_id: 'OKX',\n product_id: req.product_id,\n duration: req.duration,\n created_at: formatTime(+x[0]),\n closed_at: formatTime(+x[0] + offset),\n open: x[1],\n high: x[2],\n low: x[3],\n close: x[4],\n volume: x[5],\n open_interest: '0',\n }),\n )\n .filter((x) => Date.parse(x.created_at) < endedAtMs);\n};\n\nprovideOHLCService(\n terminal,\n {\n product_id_prefix: 'OKX/SWAP/',\n duration_list: Object.keys(DURATION_TO_OKX_BAR_TYPE),\n direction: 'backward',\n },\n fetchOHLCPageBackward,\n);\n\nprovideOHLCService(\n terminal,\n {\n product_id_prefix: 'OKX/SPOT/',\n duration_list: Object.keys(DURATION_TO_OKX_BAR_TYPE),\n direction: 'backward',\n },\n fetchOHLCPageBackward,\n);\n\nprovideOHLCService(\n terminal,\n {\n product_id_prefix: 'OKX/MARGIN/',\n duration_list: Object.keys(DURATION_TO_OKX_BAR_TYPE),\n direction: 'backward',\n },\n fetchOHLCPageBackward,\n);\n"]}
package/lib/index.d.ts CHANGED
@@ -15,4 +15,6 @@ import './trade';
15
15
  import './transfer';
16
16
  import './public-data/new-quote';
17
17
  import './services/quotes';
18
+ import './services/ohlc-service';
19
+ import './services/interest-rate-service';
18
20
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,WAAW,CAAC;AACnB,OAAO,mCAAmC,CAAC;AAC3C,OAAO,eAAe,CAAC;AACvB,OAAO,yBAAyB,CAAC;AACjC,OAAO,SAAS,CAAC;AACjB,OAAO,iBAAiB,CAAC;AACzB,OAAO,iCAAiC,CAAC;AACzC,OAAO,6BAA6B,CAAC;AACrC,OAAO,4BAA4B,CAAC;AACpC,OAAO,oBAAoB,CAAC;AAC5B,OAAO,qBAAqB,CAAC;AAC7B,OAAO,YAAY,CAAC;AACpB,OAAO,oBAAoB,CAAC;AAC5B,OAAO,SAAS,CAAC;AACjB,OAAO,YAAY,CAAC;AACpB,OAAO,yBAAyB,CAAC;AACjC,OAAO,mBAAmB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,WAAW,CAAC;AACnB,OAAO,mCAAmC,CAAC;AAC3C,OAAO,eAAe,CAAC;AACvB,OAAO,yBAAyB,CAAC;AACjC,OAAO,SAAS,CAAC;AACjB,OAAO,iBAAiB,CAAC;AACzB,OAAO,iCAAiC,CAAC;AACzC,OAAO,6BAA6B,CAAC;AACrC,OAAO,4BAA4B,CAAC;AACpC,OAAO,oBAAoB,CAAC;AAC5B,OAAO,qBAAqB,CAAC;AAC7B,OAAO,YAAY,CAAC;AACpB,OAAO,oBAAoB,CAAC;AAC5B,OAAO,SAAS,CAAC;AACjB,OAAO,YAAY,CAAC;AACpB,OAAO,yBAAyB,CAAC;AACjC,OAAO,mBAAmB,CAAC;AAC3B,OAAO,yBAAyB,CAAC;AACjC,OAAO,kCAAkC,CAAC"}
package/lib/index.js CHANGED
@@ -17,4 +17,6 @@ require("./trade");
17
17
  require("./transfer");
18
18
  require("./public-data/new-quote");
19
19
  require("./services/quotes");
20
+ require("./services/ohlc-service");
21
+ require("./services/interest-rate-service");
20
22
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA,qBAAmB;AACnB,6CAA2C;AAC3C,yBAAuB;AACvB,mCAAiC;AACjC,mBAAiB;AACjB,2BAAyB;AACzB,2CAAyC;AACzC,uCAAqC;AACrC,sCAAoC;AACpC,8BAA4B;AAC5B,+BAA6B;AAC7B,sBAAoB;AACpB,8BAA4B;AAC5B,mBAAiB;AACjB,sBAAoB;AACpB,mCAAiC;AACjC,6BAA2B","sourcesContent":["import './account';\nimport './account-actions-with-credential';\nimport './api-service';\nimport './experimental/exchange';\nimport './order';\nimport './order-actions';\nimport './order-actions-with-credential';\nimport './public-data/interest_rate';\nimport './public-data/market-order';\nimport './public-data/ohlc';\nimport './public-data/quote';\nimport './services';\nimport './strategy-account';\nimport './trade';\nimport './transfer';\nimport './public-data/new-quote';\nimport './services/quotes';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA,qBAAmB;AACnB,6CAA2C;AAC3C,yBAAuB;AACvB,mCAAiC;AACjC,mBAAiB;AACjB,2BAAyB;AACzB,2CAAyC;AACzC,uCAAqC;AACrC,sCAAoC;AACpC,8BAA4B;AAC5B,+BAA6B;AAC7B,sBAAoB;AACpB,8BAA4B;AAC5B,mBAAiB;AACjB,sBAAoB;AACpB,mCAAiC;AACjC,6BAA2B;AAC3B,mCAAiC;AACjC,4CAA0C","sourcesContent":["import './account';\nimport './account-actions-with-credential';\nimport './api-service';\nimport './experimental/exchange';\nimport './order';\nimport './order-actions';\nimport './order-actions-with-credential';\nimport './public-data/interest_rate';\nimport './public-data/market-order';\nimport './public-data/ohlc';\nimport './public-data/quote';\nimport './services';\nimport './strategy-account';\nimport './trade';\nimport './transfer';\nimport './public-data/new-quote';\nimport './services/quotes';\nimport './services/ohlc-service';\nimport './services/interest-rate-service';\n"]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=interest-rate-service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interest-rate-service.d.ts","sourceRoot":"","sources":["../../src/services/interest-rate-service.ts"],"names":[],"mappings":""}
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const exchange_1 = require("@yuants/exchange");
4
+ const protocol_1 = require("@yuants/protocol");
5
+ const utils_1 = require("@yuants/utils");
6
+ const public_api_1 = require("../api/public-api");
7
+ const terminal = protocol_1.Terminal.fromNodeEnv();
8
+ const fetchSwapFundingRateBackward = async (req) => {
9
+ const [, instType, instId] = (0, utils_1.decodePath)(req.product_id);
10
+ if (instType !== 'SWAP' || !instId)
11
+ throw new Error(`Unsupported product_id: ${req.product_id}`);
12
+ const endedAtMs = req.time;
13
+ const res = await (0, public_api_1.getFundingRateHistory)({ instId, after: `${endedAtMs}` });
14
+ if (res.code !== '0') {
15
+ throw new Error(`OKX getFundingRateHistory failed: ${res.code} ${res.msg}`);
16
+ }
17
+ return res.data
18
+ .map((v) => {
19
+ const ms = Number(v.fundingTime);
20
+ const rate = Number(v.fundingRate);
21
+ return {
22
+ series_id: req.series_id,
23
+ product_id: req.product_id,
24
+ datasource_id: 'OKX',
25
+ created_at: (0, utils_1.formatTime)(ms),
26
+ long_rate: `${-rate}`,
27
+ short_rate: `${rate}`,
28
+ settlement_price: '',
29
+ };
30
+ })
31
+ .filter((x) => Date.parse(x.created_at) < endedAtMs);
32
+ };
33
+ const fetchMarginInterestRateBackward = async (req) => {
34
+ const [, instType, instId] = (0, utils_1.decodePath)(req.product_id);
35
+ if (instType !== 'MARGIN' || !instId)
36
+ throw new Error(`Unsupported product_id: ${req.product_id}`);
37
+ const [base, quote] = instId.split('-');
38
+ if (!base || !quote)
39
+ throw new Error(`Invalid MARGIN instId: ${instId}`);
40
+ const endedAtMs = req.time;
41
+ const [resBase, resQuote] = await Promise.all([
42
+ (0, public_api_1.getLendingRateHistory)({ ccy: base, after: `${endedAtMs}` }),
43
+ (0, public_api_1.getLendingRateHistory)({ ccy: quote, after: `${endedAtMs}` }),
44
+ ]);
45
+ if (resBase.code !== '0')
46
+ throw new Error(`OKX getLendingRateHistory failed: ${resBase.code} ${resBase.msg}`);
47
+ if (resQuote.code !== '0')
48
+ throw new Error(`OKX getLendingRateHistory failed: ${resQuote.code} ${resQuote.msg}`);
49
+ const mapTsToBaseRate = new Map();
50
+ resBase.data.forEach((v) => mapTsToBaseRate.set(v.ts, v.rate));
51
+ const data = [];
52
+ resQuote.data.forEach((v) => {
53
+ const baseRate = mapTsToBaseRate.get(v.ts);
54
+ if (!baseRate)
55
+ return;
56
+ const longRate = Number(v.rate) / 365 / 24;
57
+ const shortRate = Number(baseRate) / 365 / 24;
58
+ data.push({
59
+ series_id: req.series_id,
60
+ product_id: req.product_id,
61
+ datasource_id: 'OKX',
62
+ created_at: (0, utils_1.formatTime)(+v.ts),
63
+ long_rate: `${-longRate}`,
64
+ short_rate: `${-shortRate}`,
65
+ settlement_price: '',
66
+ });
67
+ });
68
+ return data.filter((x) => Date.parse(x.created_at) < endedAtMs);
69
+ };
70
+ (0, exchange_1.provideInterestRateService)(terminal, {
71
+ product_id_prefix: 'OKX/SWAP/',
72
+ direction: 'backward',
73
+ }, fetchSwapFundingRateBackward);
74
+ (0, exchange_1.provideInterestRateService)(terminal, {
75
+ product_id_prefix: 'OKX/MARGIN/',
76
+ direction: 'backward',
77
+ }, fetchMarginInterestRateBackward);
78
+ //# sourceMappingURL=interest-rate-service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interest-rate-service.js","sourceRoot":"","sources":["../../src/services/interest-rate-service.ts"],"names":[],"mappings":";;AACA,+CAA8D;AAC9D,+CAA4C;AAC5C,yCAAuD;AACvD,kDAAiF;AAEjF,MAAM,QAAQ,GAAG,mBAAQ,CAAC,WAAW,EAAE,CAAC;AAExC,MAAM,4BAA4B,GAAG,KAAK,EAAE,GAI3C,EAA4B,EAAE;IAC7B,MAAM,CAAC,EAAE,QAAQ,EAAE,MAAM,CAAC,GAAG,IAAA,kBAAU,EAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IACxD,IAAI,QAAQ,KAAK,MAAM,IAAI,CAAC,MAAM;QAAE,MAAM,IAAI,KAAK,CAAC,2BAA2B,GAAG,CAAC,UAAU,EAAE,CAAC,CAAC;IAEjG,MAAM,SAAS,GAAG,GAAG,CAAC,IAAI,CAAC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tB,MAAM,QAAQ,GAAG,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,GAAG,GAAG,EAAE,CAAC;QAC3C,MAAM,SAAS,GAAG,MAAM,CAAC,QAAQ,CAAC,GAAG,GAAG,GAAG,EAAE,CAAC;QAE9C,IAAI,CAAC,IAAI,CAAC;YACR,SAAS,EAAE,GAAG,CAAC,SAAS;YACxB,UAAU,EAAE,GAAG,CAAC,UAAU;YAC1B,aAAa,EAAE,KAAK;YACpB,UAAU,EAAE,IAAA,kBAAU,EAAC,CAAC,CAAC,CAAC,EAAE,CAAC;YAC7B,SAAS,EAAE,GAAG,CAAC,QAAQ,EAAE;YACzB,UAAU,EAAE,GAAG,CAAC,SAAS,EAAE;YAC3B,gBAAgB,EAAE,EAAE;SACrB,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,UAAU,CAAC,GAAG,SAAS,CAAC,CAAC;AAClE,CAAC,CAAC;AAEF,IAAA,qCAA0B,EACxB,QAAQ,EACR;IACE,iBAAiB,EAAE,WAAW;IAC9B,SAAS,EAAE,UAAU;CACtB,EACD,4BAA4B,CAC7B,CAAC;AAEF,IAAA,qCAA0B,EACxB,QAAQ,EACR;IACE,iBAAiB,EAAE,aAAa;IAChC,SAAS,EAAE,UAAU;CACtB,EACD,+BAA+B,CAChC,CAAC","sourcesContent":["import { IInterestRate } from '@yuants/data-interest-rate';\nimport { provideInterestRateService } from '@yuants/exchange';\nimport { Terminal } from '@yuants/protocol';\nimport { decodePath, formatTime } from '@yuants/utils';\nimport { getFundingRateHistory, getLendingRateHistory } from '../api/public-api';\n\nconst terminal = Terminal.fromNodeEnv();\n\nconst fetchSwapFundingRateBackward = async (req: {\n product_id: string;\n time: number;\n series_id: string;\n}): Promise<IInterestRate[]> => {\n const [, instType, instId] = decodePath(req.product_id);\n if (instType !== 'SWAP' || !instId) throw new Error(`Unsupported product_id: ${req.product_id}`);\n\n const endedAtMs = req.time;\n const res = await getFundingRateHistory({ instId, after: `${endedAtMs}` });\n if (res.code !== '0') {\n throw new Error(`OKX getFundingRateHistory failed: ${res.code} ${res.msg}`);\n }\n\n return res.data\n .map((v): IInterestRate => {\n const ms = Number(v.fundingTime);\n const rate = Number(v.fundingRate);\n return {\n series_id: req.series_id,\n product_id: req.product_id,\n datasource_id: 'OKX',\n created_at: formatTime(ms),\n long_rate: `${-rate}`,\n short_rate: `${rate}`,\n settlement_price: '',\n };\n })\n .filter((x) => Date.parse(x.created_at) < endedAtMs);\n};\n\nconst fetchMarginInterestRateBackward = async (req: {\n product_id: string;\n time: number;\n series_id: string;\n}): Promise<IInterestRate[]> => {\n const [, instType, instId] = decodePath(req.product_id);\n if (instType !== 'MARGIN' || !instId) throw new Error(`Unsupported product_id: ${req.product_id}`);\n\n const [base, quote] = instId.split('-');\n if (!base || !quote) throw new Error(`Invalid MARGIN instId: ${instId}`);\n\n const endedAtMs = req.time;\n\n const [resBase, resQuote] = await Promise.all([\n getLendingRateHistory({ ccy: base, after: `${endedAtMs}` }),\n getLendingRateHistory({ ccy: quote, after: `${endedAtMs}` }),\n ]);\n\n if (resBase.code !== '0')\n throw new Error(`OKX getLendingRateHistory failed: ${resBase.code} ${resBase.msg}`);\n if (resQuote.code !== '0')\n throw new Error(`OKX getLendingRateHistory failed: ${resQuote.code} ${resQuote.msg}`);\n\n const mapTsToBaseRate = new Map<string, string>();\n resBase.data.forEach((v) => mapTsToBaseRate.set(v.ts, v.rate));\n\n const data: IInterestRate[] = [];\n resQuote.data.forEach((v) => {\n const baseRate = mapTsToBaseRate.get(v.ts);\n if (!baseRate) return;\n\n const longRate = Number(v.rate) / 365 / 24;\n const shortRate = Number(baseRate) / 365 / 24;\n\n data.push({\n series_id: req.series_id,\n product_id: req.product_id,\n datasource_id: 'OKX',\n created_at: formatTime(+v.ts),\n long_rate: `${-longRate}`,\n short_rate: `${-shortRate}`,\n settlement_price: '',\n });\n });\n\n return data.filter((x) => Date.parse(x.created_at) < endedAtMs);\n};\n\nprovideInterestRateService(\n terminal,\n {\n product_id_prefix: 'OKX/SWAP/',\n direction: 'backward',\n },\n fetchSwapFundingRateBackward,\n);\n\nprovideInterestRateService(\n terminal,\n {\n product_id_prefix: 'OKX/MARGIN/',\n direction: 'backward',\n },\n fetchMarginInterestRateBackward,\n);\n"]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=ohlc-service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ohlc-service.d.ts","sourceRoot":"","sources":["../../src/services/ohlc-service.ts"],"names":[],"mappings":""}
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const exchange_1 = require("@yuants/exchange");
4
+ const protocol_1 = require("@yuants/protocol");
5
+ const utils_1 = require("@yuants/utils");
6
+ const public_api_1 = require("../api/public-api");
7
+ const terminal = protocol_1.Terminal.fromNodeEnv();
8
+ const DURATION_TO_OKX_BAR_TYPE = {
9
+ PT1M: '1m',
10
+ PT3M: '3m',
11
+ PT5M: '5m',
12
+ PT15M: '15m',
13
+ PT30M: '30m',
14
+ PT1H: '1H',
15
+ PT2H: '2H',
16
+ PT4H: '4H',
17
+ PT6H: '6H',
18
+ PT12H: '12H',
19
+ P1D: '1D',
20
+ P1W: '1W',
21
+ P1M: '1M',
22
+ };
23
+ const fetchOHLCPageBackward = async (req) => {
24
+ const [, instType, instId] = (0, utils_1.decodePath)(req.product_id);
25
+ if (!instType || !instId)
26
+ throw new Error(`Invalid product_id: ${req.product_id}`);
27
+ const bar = DURATION_TO_OKX_BAR_TYPE[req.duration];
28
+ if (!bar)
29
+ throw new Error(`Unsupported duration: ${req.duration}`);
30
+ const offset = (0, utils_1.convertDurationToOffset)(req.duration);
31
+ const endedAtMs = req.time;
32
+ const res = await (0, public_api_1.getHistoryCandles)({
33
+ instId,
34
+ bar,
35
+ after: `${endedAtMs}`,
36
+ limit: '300',
37
+ });
38
+ if (res.code !== '0') {
39
+ throw new Error(`OKX getHistoryCandles failed: ${res.code} ${res.msg}`);
40
+ }
41
+ return res.data
42
+ .map((x) => ({
43
+ series_id: req.series_id,
44
+ datasource_id: 'OKX',
45
+ product_id: req.product_id,
46
+ duration: req.duration,
47
+ created_at: (0, utils_1.formatTime)(+x[0]),
48
+ closed_at: (0, utils_1.formatTime)(+x[0] + offset),
49
+ open: x[1],
50
+ high: x[2],
51
+ low: x[3],
52
+ close: x[4],
53
+ volume: x[5],
54
+ open_interest: '0',
55
+ }))
56
+ .filter((x) => Date.parse(x.created_at) < endedAtMs);
57
+ };
58
+ (0, exchange_1.provideOHLCService)(terminal, {
59
+ product_id_prefix: 'OKX/SWAP/',
60
+ duration_list: Object.keys(DURATION_TO_OKX_BAR_TYPE),
61
+ direction: 'backward',
62
+ }, fetchOHLCPageBackward);
63
+ (0, exchange_1.provideOHLCService)(terminal, {
64
+ product_id_prefix: 'OKX/SPOT/',
65
+ duration_list: Object.keys(DURATION_TO_OKX_BAR_TYPE),
66
+ direction: 'backward',
67
+ }, fetchOHLCPageBackward);
68
+ (0, exchange_1.provideOHLCService)(terminal, {
69
+ product_id_prefix: 'OKX/MARGIN/',
70
+ duration_list: Object.keys(DURATION_TO_OKX_BAR_TYPE),
71
+ direction: 'backward',
72
+ }, fetchOHLCPageBackward);
73
+ //# sourceMappingURL=ohlc-service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ohlc-service.js","sourceRoot":"","sources":["../../src/services/ohlc-service.ts"],"names":[],"mappings":";;AAAA,+CAAsD;AAEtD,+CAA4C;AAC5C,yCAAgF;AAChF,kDAAsD;AAEtD,MAAM,QAAQ,GAAG,mBAAQ,CAAC,WAAW,EAAE,CAAC;AAExC,MAAM,wBAAwB,GAA2B;IACvD,IAAI,EAAE,IAAI;IACV,IAAI,EAAE,IAAI;IACV,IAAI,EAAE,IAAI;IACV,KAAK,EAAE,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nimport { Terminal } from '@yuants/protocol';\nimport { convertDurationToOffset, decodePath, formatTime } from '@yuants/utils';\nimport { getHistoryCandles } from '../api/public-api';\n\nconst terminal = Terminal.fromNodeEnv();\n\nconst DURATION_TO_OKX_BAR_TYPE: Record<string, string> = {\n PT1M: '1m',\n PT3M: '3m',\n PT5M: '5m',\n PT15M: '15m',\n PT30M: '30m',\n\n PT1H: '1H',\n PT2H: '2H',\n PT4H: '4H',\n PT6H: '6H',\n PT12H: '12H',\n\n P1D: '1D',\n P1W: '1W',\n P1M: '1M',\n};\n\nconst fetchOHLCPageBackward = async (req: {\n product_id: string;\n duration: string;\n time: number;\n series_id: string;\n}): Promise<IOHLC[]> => {\n const [, instType, instId] = decodePath(req.product_id);\n if (!instType || !instId) throw new Error(`Invalid product_id: ${req.product_id}`);\n\n const bar = DURATION_TO_OKX_BAR_TYPE[req.duration];\n if (!bar) throw new Error(`Unsupported duration: ${req.duration}`);\n\n const offset = convertDurationToOffset(req.duration);\n const endedAtMs = req.time;\n\n const res = await getHistoryCandles({\n instId,\n bar,\n after: `${endedAtMs}`,\n limit: '300',\n });\n if (res.code !== '0') {\n throw new Error(`OKX getHistoryCandles failed: ${res.code} ${res.msg}`);\n }\n\n return res.data\n .map(\n (x): IOHLC => ({\n series_id: req.series_id,\n datasource_id: 'OKX',\n product_id: req.product_id,\n duration: req.duration,\n created_at: formatTime(+x[0]),\n closed_at: formatTime(+x[0] + offset),\n open: x[1],\n high: x[2],\n low: x[3],\n close: x[4],\n volume: x[5],\n open_interest: '0',\n }),\n )\n .filter((x) => Date.parse(x.created_at) < endedAtMs);\n};\n\nprovideOHLCService(\n terminal,\n {\n product_id_prefix: 'OKX/SWAP/',\n duration_list: Object.keys(DURATION_TO_OKX_BAR_TYPE),\n direction: 'backward',\n },\n fetchOHLCPageBackward,\n);\n\nprovideOHLCService(\n terminal,\n {\n product_id_prefix: 'OKX/SPOT/',\n duration_list: Object.keys(DURATION_TO_OKX_BAR_TYPE),\n direction: 'backward',\n },\n fetchOHLCPageBackward,\n);\n\nprovideOHLCService(\n terminal,\n {\n product_id_prefix: 'OKX/MARGIN/',\n duration_list: Object.keys(DURATION_TO_OKX_BAR_TYPE),\n direction: 'backward',\n },\n fetchOHLCPageBackward,\n);\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yuants/vendor-okx",
3
- "version": "0.30.6",
3
+ "version": "0.31.0",
4
4
  "main": "lib/index.js",
5
5
  "files": [
6
6
  "dist",
@@ -21,7 +21,7 @@
21
21
  "@yuants/data-interest-rate": "0.1.49",
22
22
  "@yuants/data-quote": "0.4.0",
23
23
  "@yuants/data-trade": "0.1.22",
24
- "@yuants/exchange": "0.8.0",
24
+ "@yuants/exchange": "0.8.1",
25
25
  "rxjs": "~7.5.6",
26
26
  "ws": "~8.8.1"
27
27
  },
@@ -1,13 +1,13 @@
1
1
  {
2
- "apps/vendor-okx/CHANGELOG.json": "79fa6827cc5fe6f053c1a6c18b14b264940f12c7",
3
- "apps/vendor-okx/CHANGELOG.md": "ad79f2f5b961858d1eb5b6bd68363bb5a1afb57c",
2
+ "apps/vendor-okx/CHANGELOG.json": "16b5b139d0f1697c5afe150d2c001465d146ba8e",
3
+ "apps/vendor-okx/CHANGELOG.md": "3df9a6bc407ca1192e0b5ae9b3781cb5bc1d27a8",
4
4
  "apps/vendor-okx/README.md": "68caeb8e383d77ece973cd70f4b167b86abb54c3",
5
5
  "apps/vendor-okx/api-extractor.json": "62f4fd324425b9a235f0c117975967aab09ced0c",
6
6
  "apps/vendor-okx/config/jest.config.json": "4bb17bde3ee911163a3edb36a6eb71491d80b1bd",
7
7
  "apps/vendor-okx/config/rig.json": "f6c7b5537dc77a3170ba9f008bae3b6c3ee11956",
8
8
  "apps/vendor-okx/config/typescript.json": "854907e8a821f2050f6533368db160c649c25348",
9
9
  "apps/vendor-okx/etc/vendor-okx.api.md": "92c54ccd547d2ad81ca6088158ca193e85c208df",
10
- "apps/vendor-okx/package.json": "4f6db5f3cfed52876e3c4a9a81a051c5ce9ecbfb",
10
+ "apps/vendor-okx/package.json": "a0499c56919700d3e96b9f1d33ff0958c19001d6",
11
11
  "apps/vendor-okx/src/account-actions-with-credential.ts": "1a2de787b588e98ceef60b3ffb951f298504bf12",
12
12
  "apps/vendor-okx/src/account.ts": "672014c974457b438cf026f763d6eecfe585c82b",
13
13
  "apps/vendor-okx/src/accountInfos/earning.ts": "62aaeb3b62fa9197d07ee14155625530cdb9b1cd",
@@ -25,7 +25,7 @@
25
25
  "apps/vendor-okx/src/experimental/getOrders.ts": "f89b8334fa55163a78f91cf675cea8c7c869d4a7",
26
26
  "apps/vendor-okx/src/experimental/getPositions.ts": "5fe0bd5ebc3db151be7ae897aaad256dea878122",
27
27
  "apps/vendor-okx/src/experimental/product.ts": "808bb037c1559ead2260a6b1eb2552f246da45c6",
28
- "apps/vendor-okx/src/index.ts": "f59f1ff5f6c97ae517621d46c0ef5a46bcef3070",
28
+ "apps/vendor-okx/src/index.ts": "59cc7955ae001a71f0c1b61b858536e40587d6a0",
29
29
  "apps/vendor-okx/src/order-actions-with-credential.ts": "f58b164c57a776db0fd0ad3a9f7197603ab2f66e",
30
30
  "apps/vendor-okx/src/order-actions.ts": "18907924c81ab679bf8a58142fcbc3addd160a94",
31
31
  "apps/vendor-okx/src/order.ts": "342f08dbc4a760e5f29bdaa1fe840872219f9ab5",
@@ -40,6 +40,8 @@
40
40
  "apps/vendor-okx/src/public-data/product.ts": "9caa1b0d9fa621800870ef47086767f6f5810ad9",
41
41
  "apps/vendor-okx/src/public-data/quote.ts": "da471098f6c59376f7221bf6fc3d2625eeacecee",
42
42
  "apps/vendor-okx/src/services.ts": "aeaf5f9d0a87347be6150b28961c5a7c0886b1d8",
43
+ "apps/vendor-okx/src/services/interest-rate-service.ts": "d4efc6db95aae1526ad202837b03f6cc4a88dbb7",
44
+ "apps/vendor-okx/src/services/ohlc-service.ts": "fa686c562a50e2abf05a9c60677bebb28d326ee5",
43
45
  "apps/vendor-okx/src/services/quotes.ts": "2f2278acf1884c8c8b29f9bb79221955b2eb8016",
44
46
  "apps/vendor-okx/src/strategy-account.ts": "2d8f913e19c4402eab249291aaab0b77bc4d9ff8",
45
47
  "apps/vendor-okx/src/trade.ts": "17a2afcdbf2ec69944a73d3d097660675dbe09a4",
@@ -62,7 +64,7 @@
62
64
  "libraries/data-interest-rate/temp/package-deps.json": "5a1011b097b18b96c2d8c932d874f69472621efd",
63
65
  "libraries/data-quote/temp/package-deps.json": "c2f009f818a503342bef588da21c9abd52beea00",
64
66
  "libraries/data-trade/temp/package-deps.json": "95dcb14d4678f4a4165dd214ae5286bb8fd94eaf",
65
- "libraries/exchange/temp/package-deps.json": "98f6e1318166102a07cf64b69399b35cb4575222",
67
+ "libraries/exchange/temp/package-deps.json": "0f0549ddf97292ceadb8c3cbcc8799e31e7a0901",
66
68
  "libraries/extension/temp/package-deps.json": "3e0a81b0c863c2daf342dfaf284b8fd7d18c8562",
67
69
  "tools/toolkit/temp/package-deps.json": "23e053490eb8feade23e4d45de4e54883e322711"
68
70
  }