@yuants/vendor-okx 0.19.0 → 0.19.1

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/trade.js ADDED
@@ -0,0 +1,118 @@
1
+ import { catchError, defer, first, of, repeat, retry, timeout } from 'rxjs';
2
+ import { client } from './api';
3
+ import { accountUid$ } from './account';
4
+ import { formatTime } from '@yuants/utils';
5
+ import { buildInsertManyIntoTableSQL, escapeSQL, requestSQL } from '@yuants/sql';
6
+ import { Terminal } from '@yuants/protocol';
7
+ const tradeParser = async (accountId, params) => {
8
+ const tradeList = [];
9
+ const result = await client.getAccountBillsArchive(params);
10
+ if (result.code === '0' && result.data) {
11
+ const data = result.data;
12
+ const mapTradeIdToBillList = new Map();
13
+ data.forEach((item) => { var _a; return mapTradeIdToBillList.set(item.tradeId, [...((_a = mapTradeIdToBillList.get(item.tradeId)) !== null && _a !== void 0 ? _a : []), item]); });
14
+ mapTradeIdToBillList.forEach((v, tradeId) => {
15
+ if (!((v[0].instType === 'SPOT' && v.length === 2) || v[0].instType === 'SWAP'))
16
+ return;
17
+ const trade = {
18
+ id: tradeId,
19
+ account_id: accountId,
20
+ product_id: '',
21
+ direction: '',
22
+ traded_volume: '',
23
+ traded_price: '',
24
+ traded_value: '',
25
+ fee: '',
26
+ fee_currency: '',
27
+ created_at: '0',
28
+ };
29
+ v.forEach((bill) => {
30
+ trade.created_at = Math.max(Number(trade.created_at), Number(bill.ts)).toString();
31
+ trade.product_id = bill.instId;
32
+ if (bill.instType === 'SWAP') {
33
+ if (bill.subType === '3')
34
+ trade.direction = 'OPEN_LONG';
35
+ if (bill.subType === '4')
36
+ trade.direction = 'OPEN_SHORT';
37
+ if (bill.subType === '5')
38
+ trade.direction = 'CLOSE_LONG';
39
+ if (bill.subType === '6')
40
+ trade.direction = 'CLOSE_SHORT';
41
+ trade.traded_volume = bill.sz;
42
+ trade.traded_value = (Number(bill.sz) * Number(bill.px)).toString();
43
+ }
44
+ if (bill.instType === 'SPOT') {
45
+ if (bill.subType === '1') {
46
+ if (bill.ccy !== 'USDT') {
47
+ trade.direction = 'OPEN_LONG';
48
+ }
49
+ else {
50
+ trade.direction = 'CLOSE_LONG';
51
+ }
52
+ }
53
+ if (bill.ccy !== 'USDT') {
54
+ trade.traded_volume = bill.sz;
55
+ }
56
+ if (bill.ccy === 'USDT') {
57
+ trade.traded_value = bill.sz;
58
+ }
59
+ }
60
+ // fee
61
+ if (bill.fee !== '0') {
62
+ trade.fee = Math.abs(Number(bill.fee)).toString();
63
+ trade.fee_currency = bill.ccy;
64
+ }
65
+ });
66
+ trade.created_at = formatTime(Number(trade.created_at));
67
+ tradeList.push(trade);
68
+ });
69
+ }
70
+ return tradeList;
71
+ };
72
+ const getAccountTradeWithAccountId = async (accountId) => {
73
+ var _a;
74
+ const currentTrade = await requestSQL(Terminal.fromNodeEnv(), `
75
+ select * from trade where account_id=${escapeSQL(accountId)} order by created_at desc limit 1;
76
+ `);
77
+ const params = {
78
+ type: '2',
79
+ };
80
+ if (currentTrade.length === 1) {
81
+ params['begin'] = new Date((_a = currentTrade[0].created_at) !== null && _a !== void 0 ? _a : 0).getTime().toString();
82
+ params['end'] = Date.now().toString();
83
+ }
84
+ console.log(formatTime(Date.now()), 'getAccountTrade', `params: ${JSON.stringify(params)}`);
85
+ const tradeList = await tradeParser(accountId, params);
86
+ console.log(formatTime(Date.now()), 'getAccountTrade', `tradeList: ${JSON.stringify(tradeList)}`);
87
+ await requestSQL(Terminal.fromNodeEnv(), buildInsertManyIntoTableSQL(tradeList, 'trade', {
88
+ columns: [
89
+ 'id',
90
+ 'account_id',
91
+ 'product_id',
92
+ 'traded_volume',
93
+ 'traded_value',
94
+ 'traded_price',
95
+ 'direction',
96
+ 'fee',
97
+ 'fee_currency',
98
+ 'created_at',
99
+ ],
100
+ conflictKeys: ['id'],
101
+ }));
102
+ };
103
+ defer(() => accountUid$)
104
+ .pipe(first())
105
+ .subscribe((uid) => {
106
+ const account_id = `okx/${uid}/trading`;
107
+ console.log(formatTime(Date.now()), 'getAccountTrade', `AccountID: ${account_id}`);
108
+ defer(() => getAccountTradeWithAccountId(account_id))
109
+ .pipe(
110
+ //
111
+ repeat({ delay: 30000 }), retry({ delay: 5000 }), timeout(10000), // 超时设定:10 秒
112
+ catchError((err) => {
113
+ console.error(formatTime(Date.now()), 'getAccountTradeError', err);
114
+ return of([]);
115
+ }))
116
+ .subscribe();
117
+ });
118
+ //# sourceMappingURL=trade.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trade.js","sourceRoot":"","sources":["../src/trade.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,KAAK,EAA4B,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AACtG,OAAO,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAE/B,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AACxC,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,2BAA2B,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACjF,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,MAAM,WAAW,GAAG,KAAK,EAAE,SAAiB,EAAE,MAA8B,EAAqB,EAAE;IACjG,MAAM,SAAS,GAAa,EAAE,CAAC;IAC/B,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,sBAAsB,CAAC,MAAM,CAAC,CAAC;IAE3D,IAAI,MAAM,CAAC,IAAI,KAAK,GAAG,IAAI,MAAM,CAAC,IAAI,EAAE;QACtC,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC;QACzB,MAAM,oBAAoB,GAAG,IAAI,GAAG,EAAuB,CAAC;QAC5D,IAAI,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE,WACpB,OAAA,oBAAoB,CAAC,GAAG,CAAC,IA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Ac;YACd,YAAY;SACb;QACD,YAAY,EAAE,CAAC,IAAI,CAAC;KACrB,CAAC,CACH,CAAC;AACJ,CAAC,CAAC;AAEF,KAAK,CAAC,GAAG,EAAE,CAAC,WAAW,CAAC;KACrB,IAAI,CAAC,KAAK,EAAE,CAAC;KACb,SAAS,CAAC,CAAC,GAAG,EAAE,EAAE;IACjB,MAAM,UAAU,GAAG,OAAO,GAAG,UAAU,CAAC;IACxC,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,EAAE,iBAAiB,EAAE,cAAc,UAAU,EAAE,CAAC,CAAC;IACnF,KAAK,CAAC,GAAG,EAAE,CAAC,4BAA4B,CAAC,UAAU,CAAC,CAAC;SAClD,IAAI;IACH,EAAE;IACF,MAAM,CAAC,EAAE,KAAK,EAAE,KAAM,EAAE,CAAC,EACzB,KAAK,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,EACtB,OAAO,CAAC,KAAM,CAAC,EAAE,aAAa;IAC9B,UAAU,CAAC,CAAC,GAAG,EAAE,EAAE;QACjB,OAAO,CAAC,KAAK,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,EAAE,sBAAsB,EAAE,GAAG,CAAC,CAAC;QACnE,OAAO,EAAE,CAAC,EAAE,CAAC,CAAC;IAChB,CAAC,CAAC,CACH;SACA,SAAS,EAAE,CAAC;AACjB,CAAC,CAAC,CAAC","sourcesContent":["import { catchError, defer, first, firstValueFrom, interval, of, repeat, retry, timeout } from 'rxjs';\nimport { client } from './api';\nimport { ITrade } from '@yuants/data-trade';\nimport { accountUid$ } from './account';\nimport { formatTime } from '@yuants/utils';\nimport { buildInsertManyIntoTableSQL, escapeSQL, requestSQL } from '@yuants/sql';\nimport { Terminal } from '@yuants/protocol';\nconst tradeParser = async (accountId: string, params: Record<string, string>): Promise<ITrade[]> => {\n const tradeList: ITrade[] = [];\n const result = await client.getAccountBillsArchive(params);\n\n if (result.code === '0' && result.data) {\n const data = result.data;\n const mapTradeIdToBillList = new Map<string, typeof data>();\n data.forEach((item) =>\n mapTradeIdToBillList.set(item.tradeId, [...(mapTradeIdToBillList.get(item.tradeId) ?? []), item]),\n );\n mapTradeIdToBillList.forEach((v, tradeId) => {\n if (!((v[0].instType === 'SPOT' && v.length === 2) || v[0].instType === 'SWAP')) return;\n\n const trade: ITrade = {\n id: tradeId,\n account_id: accountId,\n product_id: '',\n direction: '',\n traded_volume: '',\n traded_price: '',\n traded_value: '',\n fee: '',\n fee_currency: '',\n created_at: '0',\n };\n\n v.forEach((bill) => {\n trade.created_at = Math.max(Number(trade.created_at), Number(bill.ts)).toString();\n trade.product_id = bill.instId;\n if (bill.instType === 'SWAP') {\n if (bill.subType === '3') trade.direction = 'OPEN_LONG';\n if (bill.subType === '4') trade.direction = 'OPEN_SHORT';\n if (bill.subType === '5') trade.direction = 'CLOSE_LONG';\n if (bill.subType === '6') trade.direction = 'CLOSE_SHORT';\n trade.traded_volume = bill.sz;\n trade.traded_value = (Number(bill.sz) * Number(bill.px)).toString();\n }\n if (bill.instType === 'SPOT') {\n if (bill.subType === '1') {\n if (bill.ccy !== 'USDT') {\n trade.direction = 'OPEN_LONG';\n } else {\n trade.direction = 'CLOSE_LONG';\n }\n }\n\n if (bill.ccy !== 'USDT') {\n trade.traded_volume = bill.sz;\n }\n if (bill.ccy === 'USDT') {\n trade.traded_value = bill.sz;\n }\n }\n // fee\n if (bill.fee !== '0') {\n trade.fee = Math.abs(Number(bill.fee)).toString();\n trade.fee_currency = bill.ccy;\n }\n });\n trade.created_at = formatTime(Number(trade.created_at));\n tradeList.push(trade);\n });\n }\n return tradeList;\n};\n\nconst getAccountTradeWithAccountId = async (accountId: string) => {\n const currentTrade = await requestSQL<ITrade[]>(\n Terminal.fromNodeEnv(),\n `\n select * from trade where account_id=${escapeSQL(accountId)} order by created_at desc limit 1;\n `,\n );\n const params: Record<string, string> = {\n type: '2',\n };\n if (currentTrade.length === 1) {\n params['begin'] = new Date(currentTrade[0].created_at ?? 0).getTime().toString();\n params['end'] = Date.now().toString();\n }\n console.log(formatTime(Date.now()), 'getAccountTrade', `params: ${JSON.stringify(params)}`);\n const tradeList = await tradeParser(accountId, params);\n console.log(formatTime(Date.now()), 'getAccountTrade', `tradeList: ${JSON.stringify(tradeList)}`);\n await requestSQL(\n Terminal.fromNodeEnv(),\n buildInsertManyIntoTableSQL(tradeList, 'trade', {\n columns: [\n 'id',\n 'account_id',\n 'product_id',\n 'traded_volume',\n 'traded_value',\n 'traded_price',\n 'direction',\n 'fee',\n 'fee_currency',\n 'created_at',\n ],\n conflictKeys: ['id'],\n }),\n );\n};\n\ndefer(() => accountUid$)\n .pipe(first())\n .subscribe((uid) => {\n const account_id = `okx/${uid}/trading`;\n console.log(formatTime(Date.now()), 'getAccountTrade', `AccountID: ${account_id}`);\n defer(() => getAccountTradeWithAccountId(account_id))\n .pipe(\n //\n repeat({ delay: 30_000 }),\n retry({ delay: 5000 }),\n timeout(10_000), // 超时设定:10 秒\n catchError((err) => {\n console.error(formatTime(Date.now()), 'getAccountTradeError', err);\n return of([]);\n }),\n )\n .subscribe();\n });\n"]}
package/lib/trade.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=trade.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trade.d.ts","sourceRoot":"","sources":["../src/trade.ts"],"names":[],"mappings":""}
package/lib/trade.js ADDED
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const rxjs_1 = require("rxjs");
4
+ const api_1 = require("./api");
5
+ const account_1 = require("./account");
6
+ const utils_1 = require("@yuants/utils");
7
+ const sql_1 = require("@yuants/sql");
8
+ const protocol_1 = require("@yuants/protocol");
9
+ const tradeParser = async (accountId, params) => {
10
+ const tradeList = [];
11
+ const result = await api_1.client.getAccountBillsArchive(params);
12
+ if (result.code === '0' && result.data) {
13
+ const data = result.data;
14
+ const mapTradeIdToBillList = new Map();
15
+ data.forEach((item) => { var _a; return mapTradeIdToBillList.set(item.tradeId, [...((_a = mapTradeIdToBillList.get(item.tradeId)) !== null && _a !== void 0 ? _a : []), item]); });
16
+ mapTradeIdToBillList.forEach((v, tradeId) => {
17
+ if (!((v[0].instType === 'SPOT' && v.length === 2) || v[0].instType === 'SWAP'))
18
+ return;
19
+ const trade = {
20
+ id: tradeId,
21
+ account_id: accountId,
22
+ product_id: '',
23
+ direction: '',
24
+ traded_volume: '',
25
+ traded_price: '',
26
+ traded_value: '',
27
+ fee: '',
28
+ fee_currency: '',
29
+ created_at: '0',
30
+ };
31
+ v.forEach((bill) => {
32
+ trade.created_at = Math.max(Number(trade.created_at), Number(bill.ts)).toString();
33
+ trade.product_id = bill.instId;
34
+ if (bill.instType === 'SWAP') {
35
+ if (bill.subType === '3')
36
+ trade.direction = 'OPEN_LONG';
37
+ if (bill.subType === '4')
38
+ trade.direction = 'OPEN_SHORT';
39
+ if (bill.subType === '5')
40
+ trade.direction = 'CLOSE_LONG';
41
+ if (bill.subType === '6')
42
+ trade.direction = 'CLOSE_SHORT';
43
+ trade.traded_volume = bill.sz;
44
+ trade.traded_value = (Number(bill.sz) * Number(bill.px)).toString();
45
+ }
46
+ if (bill.instType === 'SPOT') {
47
+ if (bill.subType === '1') {
48
+ if (bill.ccy !== 'USDT') {
49
+ trade.direction = 'OPEN_LONG';
50
+ }
51
+ else {
52
+ trade.direction = 'CLOSE_LONG';
53
+ }
54
+ }
55
+ if (bill.ccy !== 'USDT') {
56
+ trade.traded_volume = bill.sz;
57
+ }
58
+ if (bill.ccy === 'USDT') {
59
+ trade.traded_value = bill.sz;
60
+ }
61
+ }
62
+ // fee
63
+ if (bill.fee !== '0') {
64
+ trade.fee = Math.abs(Number(bill.fee)).toString();
65
+ trade.fee_currency = bill.ccy;
66
+ }
67
+ });
68
+ trade.created_at = (0, utils_1.formatTime)(Number(trade.created_at));
69
+ tradeList.push(trade);
70
+ });
71
+ }
72
+ return tradeList;
73
+ };
74
+ const getAccountTradeWithAccountId = async (accountId) => {
75
+ var _a;
76
+ const currentTrade = await (0, sql_1.requestSQL)(protocol_1.Terminal.fromNodeEnv(), `
77
+ select * from trade where account_id=${(0, sql_1.escapeSQL)(accountId)} order by created_at desc limit 1;
78
+ `);
79
+ const params = {
80
+ type: '2',
81
+ };
82
+ if (currentTrade.length === 1) {
83
+ params['begin'] = new Date((_a = currentTrade[0].created_at) !== null && _a !== void 0 ? _a : 0).getTime().toString();
84
+ params['end'] = Date.now().toString();
85
+ }
86
+ console.log((0, utils_1.formatTime)(Date.now()), 'getAccountTrade', `params: ${JSON.stringify(params)}`);
87
+ const tradeList = await tradeParser(accountId, params);
88
+ console.log((0, utils_1.formatTime)(Date.now()), 'getAccountTrade', `tradeList: ${JSON.stringify(tradeList)}`);
89
+ await (0, sql_1.requestSQL)(protocol_1.Terminal.fromNodeEnv(), (0, sql_1.buildInsertManyIntoTableSQL)(tradeList, 'trade', {
90
+ columns: [
91
+ 'id',
92
+ 'account_id',
93
+ 'product_id',
94
+ 'traded_volume',
95
+ 'traded_value',
96
+ 'traded_price',
97
+ 'direction',
98
+ 'fee',
99
+ 'fee_currency',
100
+ 'created_at',
101
+ ],
102
+ conflictKeys: ['id'],
103
+ }));
104
+ };
105
+ (0, rxjs_1.defer)(() => account_1.accountUid$)
106
+ .pipe((0, rxjs_1.first)())
107
+ .subscribe((uid) => {
108
+ const account_id = `okx/${uid}/trading`;
109
+ console.log((0, utils_1.formatTime)(Date.now()), 'getAccountTrade', `AccountID: ${account_id}`);
110
+ (0, rxjs_1.defer)(() => getAccountTradeWithAccountId(account_id))
111
+ .pipe(
112
+ //
113
+ (0, rxjs_1.repeat)({ delay: 30000 }), (0, rxjs_1.retry)({ delay: 5000 }), (0, rxjs_1.timeout)(10000), // 超时设定:10 秒
114
+ (0, rxjs_1.catchError)((err) => {
115
+ console.error((0, utils_1.formatTime)(Date.now()), 'getAccountTradeError', err);
116
+ return (0, rxjs_1.of)([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ACH,EAAE;IACF,IAAA,aAAM,EAAC,EAAE,KAAK,EAAE,KAAM,EAAE,CAAC,EACzB,IAAA,YAAK,EAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,EACtB,IAAA,cAAO,EAAC,KAAM,CAAC,EAAE,aAAa;IAC9B,IAAA,iBAAU,EAAC,CAAC,GAAG,EAAE,EAAE;QACjB,OAAO,CAAC,KAAK,CAAC,IAAA,kBAAU,EAAC,IAAI,CAAC,GAAG,EAAE,CAAC,EAAE,sBAAsB,EAAE,GAAG,CAAC,CAAC;QACnE,OAAO,IAAA,SAAE,EAAC,EAAE,CAAC,CAAC;IAChB,CAAC,CAAC,CACH;SACA,SAAS,EAAE,CAAC;AACjB,CAAC,CAAC,CAAC","sourcesContent":["import { catchError, defer, first, firstValueFrom, interval, of, repeat, retry, timeout } from 'rxjs';\nimport { client } from './api';\nimport { ITrade } from '@yuants/data-trade';\nimport { accountUid$ } from './account';\nimport { formatTime } from '@yuants/utils';\nimport { buildInsertManyIntoTableSQL, escapeSQL, requestSQL } from '@yuants/sql';\nimport { Terminal } from '@yuants/protocol';\nconst tradeParser = async (accountId: string, params: Record<string, string>): Promise<ITrade[]> => {\n const tradeList: ITrade[] = [];\n const result = await client.getAccountBillsArchive(params);\n\n if (result.code === '0' && result.data) {\n const data = result.data;\n const mapTradeIdToBillList = new Map<string, typeof data>();\n data.forEach((item) =>\n mapTradeIdToBillList.set(item.tradeId, [...(mapTradeIdToBillList.get(item.tradeId) ?? []), item]),\n );\n mapTradeIdToBillList.forEach((v, tradeId) => {\n if (!((v[0].instType === 'SPOT' && v.length === 2) || v[0].instType === 'SWAP')) return;\n\n const trade: ITrade = {\n id: tradeId,\n account_id: accountId,\n product_id: '',\n direction: '',\n traded_volume: '',\n traded_price: '',\n traded_value: '',\n fee: '',\n fee_currency: '',\n created_at: '0',\n };\n\n v.forEach((bill) => {\n trade.created_at = Math.max(Number(trade.created_at), Number(bill.ts)).toString();\n trade.product_id = bill.instId;\n if (bill.instType === 'SWAP') {\n if (bill.subType === '3') trade.direction = 'OPEN_LONG';\n if (bill.subType === '4') trade.direction = 'OPEN_SHORT';\n if (bill.subType === '5') trade.direction = 'CLOSE_LONG';\n if (bill.subType === '6') trade.direction = 'CLOSE_SHORT';\n trade.traded_volume = bill.sz;\n trade.traded_value = (Number(bill.sz) * Number(bill.px)).toString();\n }\n if (bill.instType === 'SPOT') {\n if (bill.subType === '1') {\n if (bill.ccy !== 'USDT') {\n trade.direction = 'OPEN_LONG';\n } else {\n trade.direction = 'CLOSE_LONG';\n }\n }\n\n if (bill.ccy !== 'USDT') {\n trade.traded_volume = bill.sz;\n }\n if (bill.ccy === 'USDT') {\n trade.traded_value = bill.sz;\n }\n }\n // fee\n if (bill.fee !== '0') {\n trade.fee = Math.abs(Number(bill.fee)).toString();\n trade.fee_currency = bill.ccy;\n }\n });\n trade.created_at = formatTime(Number(trade.created_at));\n tradeList.push(trade);\n });\n }\n return tradeList;\n};\n\nconst getAccountTradeWithAccountId = async (accountId: string) => {\n const currentTrade = await requestSQL<ITrade[]>(\n Terminal.fromNodeEnv(),\n `\n select * from trade where account_id=${escapeSQL(accountId)} order by created_at desc limit 1;\n `,\n );\n const params: Record<string, string> = {\n type: '2',\n };\n if (currentTrade.length === 1) {\n params['begin'] = new Date(currentTrade[0].created_at ?? 0).getTime().toString();\n params['end'] = Date.now().toString();\n }\n console.log(formatTime(Date.now()), 'getAccountTrade', `params: ${JSON.stringify(params)}`);\n const tradeList = await tradeParser(accountId, params);\n console.log(formatTime(Date.now()), 'getAccountTrade', `tradeList: ${JSON.stringify(tradeList)}`);\n await requestSQL(\n Terminal.fromNodeEnv(),\n buildInsertManyIntoTableSQL(tradeList, 'trade', {\n columns: [\n 'id',\n 'account_id',\n 'product_id',\n 'traded_volume',\n 'traded_value',\n 'traded_price',\n 'direction',\n 'fee',\n 'fee_currency',\n 'created_at',\n ],\n conflictKeys: ['id'],\n }),\n );\n};\n\ndefer(() => accountUid$)\n .pipe(first())\n .subscribe((uid) => {\n const account_id = `okx/${uid}/trading`;\n console.log(formatTime(Date.now()), 'getAccountTrade', `AccountID: ${account_id}`);\n defer(() => getAccountTradeWithAccountId(account_id))\n .pipe(\n //\n repeat({ delay: 30_000 }),\n retry({ delay: 5000 }),\n timeout(10_000), // 超时设定:10 秒\n catchError((err) => {\n console.error(formatTime(Date.now()), 'getAccountTradeError', err);\n return of([]);\n }),\n )\n .subscribe();\n });\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yuants/vendor-okx",
3
- "version": "0.19.0",
3
+ "version": "0.19.1",
4
4
  "bin": "lib/cli.js",
5
5
  "main": "lib/index.js",
6
6
  "files": [
@@ -20,7 +20,8 @@
20
20
  "@yuants/data-ohlc": "0.3.13",
21
21
  "@yuants/data-interest-rate": "0.1.14",
22
22
  "@yuants/data-quote": "0.2.9",
23
- "@yuants/secret": "0.1.9",
23
+ "@yuants/data-trade": "0.0.2",
24
+ "@yuants/secret": "0.2.0",
24
25
  "rxjs": "~7.5.6",
25
26
  "crypto-js": "^4.2.0",
26
27
  "ws": "~8.8.1"
package/temp/image-tag CHANGED
@@ -1 +1 @@
1
- sha-3bd901d2f7f304fa3d8df2b7688ffb08b3aba573
1
+ sha-d5d1dfc7384ae6b85d29cffa3d67d4ba14bb5cd8
@@ -1,6 +1,6 @@
1
1
  {
2
- "apps/vendor-okx/CHANGELOG.json": "905665d05dec62b8a1a25fb29e7be0d69293738e",
3
- "apps/vendor-okx/CHANGELOG.md": "e7f29324dee99b52530c8f0f89be8973f416d16c",
2
+ "apps/vendor-okx/CHANGELOG.json": "f5a3131d83abf9512050b37afe500ac72512c2a9",
3
+ "apps/vendor-okx/CHANGELOG.md": "43be34ff4efdfcb8dc020069083de86036965701",
4
4
  "apps/vendor-okx/README.md": "ac3d1f6c4c8d73066664699b0bb1b01db02e67a2",
5
5
  "apps/vendor-okx/api-extractor.json": "62f4fd324425b9a235f0c117975967aab09ced0c",
6
6
  "apps/vendor-okx/build/Dockerfile": "e625b9593e496c343297c9ce133d6cc4be9617a7",
@@ -8,7 +8,7 @@
8
8
  "apps/vendor-okx/config/rig.json": "f6c7b5537dc77a3170ba9f008bae3b6c3ee11956",
9
9
  "apps/vendor-okx/config/typescript.json": "854907e8a821f2050f6533368db160c649c25348",
10
10
  "apps/vendor-okx/etc/vendor-okx.api.md": "92c54ccd547d2ad81ca6088158ca193e85c208df",
11
- "apps/vendor-okx/package.json": "85195061015636ffff411f5c11d5952465b53519",
11
+ "apps/vendor-okx/package.json": "94caa46fc91e7f5d700448a38d294f4c7ae746d2",
12
12
  "apps/vendor-okx/src/account.ts": "7e8bb44845269a401a850aff2e5bf170b0811bd8",
13
13
  "apps/vendor-okx/src/api.ts": "24d7484abffc5a06c67c70c624aa7e401b3be8f2",
14
14
  "apps/vendor-okx/src/cli.ts": "9bf6b5559a6c6f33da20e74cc6c5d702c60ec891",
@@ -22,9 +22,10 @@
22
22
  "apps/vendor-okx/src/order.ts": "09509ae5af4b212cc1c99c55c436b3b0ce8bda33",
23
23
  "apps/vendor-okx/src/product.ts": "a2e7feb86bc01fe3b1af6dcb9a9ebfba6be91434",
24
24
  "apps/vendor-okx/src/quote.ts": "b8dd019e6cceb50e486a9d41e54385bda4e21ab0",
25
+ "apps/vendor-okx/src/trade.ts": "07c59c0923e3e7114bd7820311bd533f5b60d79b",
25
26
  "apps/vendor-okx/src/websocket.ts": "dbfbe9785d9ce06ceae0092750f1db5518be8151",
26
27
  "apps/vendor-okx/tsconfig.json": "81da8f78196974b5d15da0edb6b2d9f48641063c",
27
- "apps/vendor-okx/.rush/temp/shrinkwrap-deps.json": "87df045a10e829fd62d50013da30fd929442d54c",
28
+ "apps/vendor-okx/.rush/temp/shrinkwrap-deps.json": "0a6205550d411d42b6d36324572e5e2a0ba166b2",
28
29
  "libraries/protocol/temp/package-deps.json": "43291b31649f44821b64235587e30fe4663fc305",
29
30
  "libraries/transfer/temp/package-deps.json": "a662579703128c1770f30f8ffaab3f5a233f9d48",
30
31
  "libraries/data-account/temp/package-deps.json": "6271fcb95ec701464eb1e36e3efad6d674644484",
@@ -36,7 +37,8 @@
36
37
  "libraries/data-ohlc/temp/package-deps.json": "d8812a8cdf4b60e7d3a66ff723f6d9ec82f27259",
37
38
  "libraries/data-interest-rate/temp/package-deps.json": "27c147717413bf219a8aef339c14411290cd7d93",
38
39
  "libraries/data-quote/temp/package-deps.json": "06ec122f1a8d33627668e30ea2a7ecbe7c04e12f",
39
- "libraries/secret/temp/package-deps.json": "9ab6e32f718f74f5358a3950ca4bf1fdb04642ca",
40
+ "libraries/data-trade/temp/package-deps.json": "58c4c30b6f194b1d98c8b904908e472312978a65",
41
+ "libraries/secret/temp/package-deps.json": "59e8d11b06181dec675c2ec58437023bf8f49a30",
40
42
  "libraries/extension/temp/package-deps.json": "f22ec5c30214f7aeeda63f46bd920290be555f57",
41
43
  "tools/toolkit/temp/package-deps.json": "eb0447c467da3a90db0913ed01e42ec0de840234"
42
44
  }