@yuants/exchange 0.8.12 → 0.8.14
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/exchange.d.ts +24 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/interest_rate.js +4 -4
- package/dist/interest_rate.js.map +1 -1
- package/dist/trade_history.js +103 -0
- package/dist/trade_history.js.map +1 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/interest_rate.d.ts.map +1 -1
- package/lib/interest_rate.js +3 -3
- package/lib/interest_rate.js.map +1 -1
- package/lib/trade_history.d.ts +24 -0
- package/lib/trade_history.d.ts.map +1 -0
- package/lib/trade_history.js +107 -0
- package/lib/trade_history.js.map +1 -0
- package/package.json +6 -5
- package/temp/build/typescript/ts_ji-pb6h_.json +1 -1
- package/temp/exchange.api.json +118 -0
- package/temp/exchange.api.md +12 -0
- package/temp/package-deps.json +10 -8
- package/temp/test/jest/haste-map-f50e7cc8db739b6ac2b07d506eb6fdee-b0f299de6a65bcc22988084da157f170-c4011bd9f6a16ae7503ec572fe00f1e3 +0 -0
- package/temp/test/jest/perf-cache-f50e7cc8db739b6ac2b07d506eb6fdee-da39a3ee5e6b4b0d3255bfef95601890 +1 -1
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { IServiceOptions, Terminal } from '@yuants/protocol';
|
|
2
|
+
import { ITradeHistory } from '@yuants/data-trade';
|
|
3
|
+
interface IExchangeCredential {
|
|
4
|
+
type: string;
|
|
5
|
+
payload: any;
|
|
6
|
+
}
|
|
7
|
+
interface IIngestInterestLedgerRequest {
|
|
8
|
+
credential: IExchangeCredential;
|
|
9
|
+
account_id: string;
|
|
10
|
+
time: number;
|
|
11
|
+
trade_type: string;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* @public
|
|
15
|
+
*/
|
|
16
|
+
export declare const provideTradeHistoryService: (terminal: Terminal, metadata: {
|
|
17
|
+
direction: string;
|
|
18
|
+
type: string;
|
|
19
|
+
trade_type: string[];
|
|
20
|
+
}, fetchPage: (request: IIngestInterestLedgerRequest) => Promise<ITradeHistory[]>, serviceOptions?: IServiceOptions) => {
|
|
21
|
+
dispose: () => void;
|
|
22
|
+
};
|
|
23
|
+
export {};
|
|
24
|
+
//# sourceMappingURL=trade_history.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trade_history.d.ts","sourceRoot":"","sources":["../src/trade_history.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC7D,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAInD,UAAU,mBAAmB;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,GAAG,CAAC;CACd;AAED,UAAU,4BAA4B;IACpC,UAAU,EAAE,mBAAmB,CAAC;IAChC,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,CAAC;CACpB;AAwCD;;GAEG;AACH,eAAO,MAAM,0BAA0B,GACrC,UAAU,QAAQ,EAClB,UAAU;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,EAAE,CAAA;CAAE,EACnE,WAAW,CAAC,OAAO,EAAE,4BAA4B,KAAK,OAAO,CAAC,aAAa,EAAE,CAAC,EAC9E,iBAAiB,eAAe;;CAkFjC,CAAC"}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.provideTradeHistoryService = void 0;
|
|
4
|
+
const sql_1 = require("@yuants/sql");
|
|
5
|
+
const computeInterestRatePageRange = (items) => {
|
|
6
|
+
if (items.length === 0)
|
|
7
|
+
return undefined;
|
|
8
|
+
let start = items[0];
|
|
9
|
+
let startMs = Date.parse(start.created_at);
|
|
10
|
+
let end = items[0];
|
|
11
|
+
let endMs = Date.parse(end.created_at);
|
|
12
|
+
for (const item of items) {
|
|
13
|
+
const createdAtMs = Date.parse(item.created_at);
|
|
14
|
+
if (!isNaN(createdAtMs) && (isNaN(startMs) || createdAtMs < startMs)) {
|
|
15
|
+
start = item;
|
|
16
|
+
startMs = createdAtMs;
|
|
17
|
+
}
|
|
18
|
+
if (!isNaN(createdAtMs) && (isNaN(endMs) || createdAtMs > endMs)) {
|
|
19
|
+
end = item;
|
|
20
|
+
endMs = createdAtMs;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return { start_time: start.created_at, end_time: end.created_at };
|
|
24
|
+
};
|
|
25
|
+
const TRADE_HISTORY_INSERT_COLUMNS = [
|
|
26
|
+
'id',
|
|
27
|
+
'product_id',
|
|
28
|
+
'account_id',
|
|
29
|
+
'direction',
|
|
30
|
+
'size',
|
|
31
|
+
'price',
|
|
32
|
+
'fee',
|
|
33
|
+
'fee_currency',
|
|
34
|
+
'pnl',
|
|
35
|
+
'created_at',
|
|
36
|
+
];
|
|
37
|
+
/**
|
|
38
|
+
* @public
|
|
39
|
+
*/
|
|
40
|
+
const provideTradeHistoryService = (terminal, metadata, fetchPage, serviceOptions) => {
|
|
41
|
+
return terminal.server.provideService('IngestTradeHistory', {
|
|
42
|
+
type: 'object',
|
|
43
|
+
required: ['account_id', 'direction', 'time', 'credential', 'trade_type'],
|
|
44
|
+
properties: {
|
|
45
|
+
account_id: { type: 'string', pattern: `^${metadata.type}` },
|
|
46
|
+
direction: { const: metadata.direction },
|
|
47
|
+
time: { type: 'number' },
|
|
48
|
+
trade_type: { type: 'string', enum: metadata.trade_type },
|
|
49
|
+
credential: {
|
|
50
|
+
type: 'object',
|
|
51
|
+
required: ['type', 'payload'],
|
|
52
|
+
properties: {
|
|
53
|
+
type: { type: 'string', const: metadata.type },
|
|
54
|
+
payload: { type: 'object' },
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
}, async (msg) => {
|
|
59
|
+
try {
|
|
60
|
+
const tradeHistory = await fetchPage(Object.assign({}, msg.req));
|
|
61
|
+
const range = computeInterestRatePageRange(tradeHistory);
|
|
62
|
+
// Atomic write: data rows + series_data_range in the same statement.
|
|
63
|
+
if (tradeHistory.length > 0 && range) {
|
|
64
|
+
const writeInterestRate = `${(0, sql_1.buildInsertManyIntoTableSQL)(tradeHistory, 'trade_history', {
|
|
65
|
+
columns: TRADE_HISTORY_INSERT_COLUMNS,
|
|
66
|
+
conflictKeys: ['id', 'account_id'],
|
|
67
|
+
})} RETURNING 1`;
|
|
68
|
+
const writeRange = `${(0, sql_1.buildInsertManyIntoTableSQL)([
|
|
69
|
+
{
|
|
70
|
+
series_id: msg.req.account_id,
|
|
71
|
+
table_name: 'trade_history',
|
|
72
|
+
start_time: range.start_time,
|
|
73
|
+
end_time: range.end_time,
|
|
74
|
+
},
|
|
75
|
+
], 'series_data_range', {
|
|
76
|
+
columns: ['series_id', 'table_name', 'start_time', 'end_time'],
|
|
77
|
+
ignoreConflict: true,
|
|
78
|
+
})} RETURNING 1`;
|
|
79
|
+
await (0, sql_1.requestSQL)(terminal, `
|
|
80
|
+
WITH
|
|
81
|
+
write_trade_history AS (
|
|
82
|
+
${writeInterestRate}
|
|
83
|
+
),
|
|
84
|
+
write_range AS (
|
|
85
|
+
${writeRange}
|
|
86
|
+
)
|
|
87
|
+
SELECT 1 as ok;
|
|
88
|
+
`);
|
|
89
|
+
}
|
|
90
|
+
else if (tradeHistory.length > 0) {
|
|
91
|
+
await (0, sql_1.requestSQL)(terminal, (0, sql_1.buildInsertManyIntoTableSQL)(tradeHistory, 'trade_history', {
|
|
92
|
+
columns: TRADE_HISTORY_INSERT_COLUMNS,
|
|
93
|
+
conflictKeys: ['id', 'account_id'],
|
|
94
|
+
}));
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
res: { code: 0, message: 'OK', data: { wrote_count: tradeHistory.length, range } },
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
const message = error instanceof Error ? error.message : `${error}`;
|
|
102
|
+
return { res: { code: 1, message } };
|
|
103
|
+
}
|
|
104
|
+
}, serviceOptions);
|
|
105
|
+
};
|
|
106
|
+
exports.provideTradeHistoryService = provideTradeHistoryService;
|
|
107
|
+
//# sourceMappingURL=trade_history.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trade_history.js","sourceRoot":"","sources":["../src/trade_history.ts"],"names":[],"mappings":";;;AAGA,qCAAsE;AActE,MAAM,4BAA4B,GAAG,CACnC,KAAsB,EACgC,EAAE;IACxD,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,SAAS,CAAC;IAEzC,IAAI,KAAK,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;IACrB,IAAI,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;IAC3C,IAAI,GAAG,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;IACnB,IAAI,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IAEvC,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,MAAM,WAAW,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QAChD,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,WAAW,GAAG,OAAO,CAAC,EAAE,CAAC;YACrE,KAAK,GAAG,IAAI,CAAC;YACb,OAAO,GAAG,WAAW,CAAC;QACxB,CAAC;QACD,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,WAAW,GAAG,KAAK,CAAC,EAAE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EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,WAAW,EAAE,YAAY,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE;aACnF,CAAC;QACJ,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,OAAO,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,KAAK,EAAE,CAAC;YACpE,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE,CAAC;QACvC,CAAC;IACH,CAAC,EACD,cAAc,CACf,CAAC;AACJ,CAAC,CAAC;AAtFW,QAAA,0BAA0B,8BAsFrC","sourcesContent":["import { IServiceOptions, Terminal } from '@yuants/protocol';\nimport { ITradeHistory } from '@yuants/data-trade';\nimport { ISeriesIngestResult } from './types';\nimport { buildInsertManyIntoTableSQL, requestSQL } from '@yuants/sql';\n\ninterface IExchangeCredential {\n type: string;\n payload: any;\n}\n\ninterface IIngestInterestLedgerRequest {\n credential: IExchangeCredential;\n account_id: string;\n time: number;\n trade_type: string;\n}\n\nconst computeInterestRatePageRange = (\n items: ITradeHistory[],\n): { start_time: string; end_time: string } | undefined => {\n if (items.length === 0) return undefined;\n\n let start = items[0];\n let startMs = Date.parse(start.created_at);\n let end = items[0];\n let endMs = Date.parse(end.created_at);\n\n for (const item of items) {\n const createdAtMs = Date.parse(item.created_at);\n if (!isNaN(createdAtMs) && (isNaN(startMs) || createdAtMs < startMs)) {\n start = item;\n startMs = createdAtMs;\n }\n if (!isNaN(createdAtMs) && (isNaN(endMs) || createdAtMs > endMs)) {\n end = item;\n endMs = createdAtMs;\n }\n }\n\n return { start_time: start.created_at, end_time: end.created_at };\n};\n\nconst TRADE_HISTORY_INSERT_COLUMNS: Array<keyof ITradeHistory> = [\n 'id',\n 'product_id',\n 'account_id',\n 'direction',\n 'size',\n 'price',\n 'fee',\n 'fee_currency',\n 'pnl',\n 'created_at',\n];\n\n/**\n * @public\n */\nexport const provideTradeHistoryService = (\n terminal: Terminal,\n metadata: { direction: string; type: string; trade_type: string[] },\n fetchPage: (request: IIngestInterestLedgerRequest) => Promise<ITradeHistory[]>,\n serviceOptions?: IServiceOptions,\n) => {\n return terminal.server.provideService<IIngestInterestLedgerRequest, ISeriesIngestResult>(\n 'IngestTradeHistory',\n {\n type: 'object',\n required: ['account_id', 'direction', 'time', 'credential', 'trade_type'],\n properties: {\n account_id: { type: 'string', pattern: `^${metadata.type}` },\n direction: { const: metadata.direction },\n time: { type: 'number' },\n trade_type: { type: 'string', enum: metadata.trade_type },\n credential: {\n type: 'object',\n required: ['type', 'payload'],\n properties: {\n type: { type: 'string', const: metadata.type },\n payload: { type: 'object' },\n },\n },\n },\n },\n async (msg) => {\n try {\n const tradeHistory = await fetchPage({ ...msg.req });\n const range = computeInterestRatePageRange(tradeHistory);\n // Atomic write: data rows + series_data_range in the same statement.\n if (tradeHistory.length > 0 && range) {\n const writeInterestRate = `${buildInsertManyIntoTableSQL(tradeHistory, 'trade_history', {\n columns: TRADE_HISTORY_INSERT_COLUMNS,\n conflictKeys: ['id', 'account_id'],\n })} RETURNING 1`;\n\n const writeRange = `${buildInsertManyIntoTableSQL(\n [\n {\n series_id: msg.req.account_id,\n table_name: 'trade_history',\n start_time: range.start_time,\n end_time: range.end_time,\n },\n ],\n 'series_data_range',\n {\n columns: ['series_id', 'table_name', 'start_time', 'end_time'],\n ignoreConflict: true,\n },\n )} RETURNING 1`;\n\n await requestSQL(\n terminal,\n `\n WITH\n write_trade_history AS (\n ${writeInterestRate}\n ),\n write_range AS (\n ${writeRange}\n )\n SELECT 1 as ok;\n `,\n );\n } else if (tradeHistory.length > 0) {\n await requestSQL(\n terminal,\n buildInsertManyIntoTableSQL(tradeHistory, 'trade_history', {\n columns: TRADE_HISTORY_INSERT_COLUMNS,\n conflictKeys: ['id', 'account_id'],\n }),\n );\n }\n\n return {\n res: { code: 0, message: 'OK', data: { wrote_count: tradeHistory.length, range } },\n };\n } catch (error) {\n const message = error instanceof Error ? error.message : `${error}`;\n return { res: { code: 1, message } };\n }\n },\n serviceOptions,\n );\n};\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yuants/exchange",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.14",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"module": "dist/index.js",
|
|
6
6
|
"files": [
|
|
@@ -26,15 +26,16 @@
|
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"rxjs": "~7.5.6",
|
|
28
28
|
"@yuants/protocol": "0.54.1",
|
|
29
|
+
"@yuants/data-trade": "0.1.31",
|
|
29
30
|
"@yuants/data-account": "0.11.8",
|
|
31
|
+
"@yuants/data-order": "0.7.9",
|
|
30
32
|
"@yuants/data-product": "0.5.9",
|
|
31
33
|
"@yuants/data-quote": "0.4.8",
|
|
32
|
-
"@yuants/data-order": "0.7.9",
|
|
33
34
|
"@yuants/data-ohlc": "0.6.4",
|
|
34
|
-
"@yuants/data-interest-rate": "0.2.
|
|
35
|
-
"@yuants/cache": "0.3.12",
|
|
35
|
+
"@yuants/data-interest-rate": "0.2.9",
|
|
36
36
|
"@yuants/sql": "0.9.39",
|
|
37
|
-
"@yuants/utils": "0.19.4"
|
|
37
|
+
"@yuants/utils": "0.19.4",
|
|
38
|
+
"@yuants/cache": "0.3.12"
|
|
38
39
|
},
|
|
39
40
|
"scripts": {
|
|
40
41
|
"build": "heft test --clean && api-extractor run --local --config ./config/api-extractor.json && yuan-toolkit post-build"
|