@yuants/vendor-okx 0.19.1 → 0.19.2

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
@@ -4,4 +4,5 @@ import './ohlc';
4
4
  import './quote';
5
5
  import './account';
6
6
  import './order';
7
+ import './trade';
7
8
  //# 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,iBAAiB,CAAC;AACzB,OAAO,gBAAgB,CAAC;AACxB,OAAO,QAAQ,CAAC;AAChB,OAAO,SAAS,CAAC;AACjB,OAAO,WAAW,CAAC;AACnB,OAAO,SAAS,CAAC","sourcesContent":["import './interest_rate';\nimport './legacy_index';\nimport './ohlc';\nimport './quote';\nimport './account';\nimport './order';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,iBAAiB,CAAC;AACzB,OAAO,gBAAgB,CAAC;AACxB,OAAO,QAAQ,CAAC;AAChB,OAAO,SAAS,CAAC;AACjB,OAAO,WAAW,CAAC;AACnB,OAAO,SAAS,CAAC;AACjB,OAAO,SAAS,CAAC","sourcesContent":["import './interest_rate';\nimport './legacy_index';\nimport './ohlc';\nimport './quote';\nimport './account';\nimport './order';\nimport './trade';\n"]}
package/dist/trade.js CHANGED
@@ -29,6 +29,7 @@ const tradeParser = async (accountId, params) => {
29
29
  v.forEach((bill) => {
30
30
  trade.created_at = Math.max(Number(trade.created_at), Number(bill.ts)).toString();
31
31
  trade.product_id = bill.instId;
32
+ trade.traded_price = bill.px;
32
33
  if (bill.instType === 'SWAP') {
33
34
  if (bill.subType === '3')
34
35
  trade.direction = 'OPEN_LONG';
package/dist/trade.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"trade.js","sourceRoot":"","sources":["../src/trade.ts"],"names":[],"mappings":"AAAA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;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"]}
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,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,eAAe;YACf,cAAc;YACd,cAAc;YACd,WAAW;YACX,KAAK;YACL,cAAc;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 trade.traded_price = bill.px;\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/index.d.ts CHANGED
@@ -4,4 +4,5 @@ import './ohlc';
4
4
  import './quote';
5
5
  import './account';
6
6
  import './order';
7
+ import './trade';
7
8
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,iBAAiB,CAAC;AACzB,OAAO,gBAAgB,CAAC;AACxB,OAAO,QAAQ,CAAC;AAChB,OAAO,SAAS,CAAC;AACjB,OAAO,WAAW,CAAC;AACnB,OAAO,SAAS,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,iBAAiB,CAAC;AACzB,OAAO,gBAAgB,CAAC;AACxB,OAAO,QAAQ,CAAC;AAChB,OAAO,SAAS,CAAC;AACjB,OAAO,WAAW,CAAC;AACnB,OAAO,SAAS,CAAC;AACjB,OAAO,SAAS,CAAC"}
package/lib/index.js CHANGED
@@ -6,4 +6,5 @@ require("./ohlc");
6
6
  require("./quote");
7
7
  require("./account");
8
8
  require("./order");
9
+ require("./trade");
9
10
  //# 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,2BAAyB;AACzB,0BAAwB;AACxB,kBAAgB;AAChB,mBAAiB;AACjB,qBAAmB;AACnB,mBAAiB","sourcesContent":["import './interest_rate';\nimport './legacy_index';\nimport './ohlc';\nimport './quote';\nimport './account';\nimport './order';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA,2BAAyB;AACzB,0BAAwB;AACxB,kBAAgB;AAChB,mBAAiB;AACjB,qBAAmB;AACnB,mBAAiB;AACjB,mBAAiB","sourcesContent":["import './interest_rate';\nimport './legacy_index';\nimport './ohlc';\nimport './quote';\nimport './account';\nimport './order';\nimport './trade';\n"]}
package/lib/trade.js CHANGED
@@ -31,6 +31,7 @@ const tradeParser = async (accountId, params) => {
31
31
  v.forEach((bill) => {
32
32
  trade.created_at = Math.max(Number(trade.created_at), Number(bill.ts)).toString();
33
33
  trade.product_id = bill.instId;
34
+ trade.traded_price = bill.px;
34
35
  if (bill.instType === 'SWAP') {
35
36
  if (bill.subType === '3')
36
37
  trade.direction = 'OPEN_LONG';
package/lib/trade.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"trade.js","sourceRoot":"","sources":["../src/trade.ts"],"names":[],"mappings":";;AAAA,+BAAsG;AACtG,+BAA+B;AAE/B,uCAAwC;AACxC,yCAA2C;AAC3C,qCAAiF;AACjF,+CAA4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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"]}
1
+ {"version":3,"file":"trade.js","sourceRoot":"","sources":["../src/trade.ts"],"names":[],"mappings":";;AAAA,+BAAsG;AACtG,+BAA+B;AAE/B,uCAAwC;AACxC,yCAA2C;AAC3C,qCAAiF;AACjF,+CAA4C;AAC5C,MAAM,WAAW,GAAG,KAAK,EAAE,SAAiB,EAAE,MAA8B,EAAqB,EAAE;IACjG,MAAM,SAAS,GAAa,EAAE,CAAC;IAC/B,MAAM,MAAM,GAAG,MAAM,YAAM,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,IAAI,CAAC,OAAO,EAAE,CAAC,GAAG,CAAC,MAAA,oBAAoB,CAAC,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,mCAAI,EAAE,CAAC,EAAE,IAAI,CAAC,CAAC,CAAA,EAAA,CAClG,CAAC;QACF,oBAAoB,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,EAAE;YAC1C,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,KAAK,MAAM,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,KAAK,MAAM,CAAC;gBAAE,OAAO;YAExF,MAAM,KAAK,GAAW;gBACpB,EAAE,EAAE,OAAO;gBACX,UAAU,EAAE,SAAS;gBACrB,UAAU,EAAE,EAAE;gBACd,SAAS,EAAE,EAAE;gBACb,aAAa,EAAE,EAAE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,UAAU,CAAC,CAAC;SAClD,IAAI;IACH,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 trade.traded_price = bill.px;\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.1",
3
+ "version": "0.19.2",
4
4
  "bin": "lib/cli.js",
5
5
  "main": "lib/index.js",
6
6
  "files": [
package/temp/image-tag CHANGED
@@ -1 +1 @@
1
- sha-d5d1dfc7384ae6b85d29cffa3d67d4ba14bb5cd8
1
+ sha-a36cec00b4c05de133fac06920ee6b3c6c385e70
@@ -1,6 +1,6 @@
1
1
  {
2
- "apps/vendor-okx/CHANGELOG.json": "f5a3131d83abf9512050b37afe500ac72512c2a9",
3
- "apps/vendor-okx/CHANGELOG.md": "43be34ff4efdfcb8dc020069083de86036965701",
2
+ "apps/vendor-okx/CHANGELOG.json": "eee9ae69009fcdb8d889b595ea9150f1a994e5c1",
3
+ "apps/vendor-okx/CHANGELOG.md": "f7978c2a250753210877c69bd2316ee4a4983765",
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,13 +8,13 @@
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": "94caa46fc91e7f5d700448a38d294f4c7ae746d2",
11
+ "apps/vendor-okx/package.json": "23eaab38ab3c8bfc14073ea8b6ddc60e509bb51e",
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",
15
15
  "apps/vendor-okx/src/cluster.ts": "9971d8d5e6af16e09d2d1fe5e9f05ccee87281e8",
16
16
  "apps/vendor-okx/src/extension.ts": "e5c9bc791d3c4beade4c8a1854d1351ba3ddd3aa",
17
- "apps/vendor-okx/src/index.ts": "6c9f8e299d58c3acdcf99d75f3173abba085f385",
17
+ "apps/vendor-okx/src/index.ts": "79d05bc51df41c385401f2b2390551ea43c04722",
18
18
  "apps/vendor-okx/src/interest_rate.ts": "f5ee44bf6b92a484dccc57863c47a7101a78e793",
19
19
  "apps/vendor-okx/src/legacy_index.ts": "1bf031b9f8f870a099d74867292b257346344e4d",
20
20
  "apps/vendor-okx/src/logger.ts": "d25d427e74f46819a601e0cb17d5358b22c8db7b",
@@ -22,7 +22,7 @@
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
+ "apps/vendor-okx/src/trade.ts": "37deab9dc9ff2924ccf464124005822d776aa6fc",
26
26
  "apps/vendor-okx/src/websocket.ts": "dbfbe9785d9ce06ceae0092750f1db5518be8151",
27
27
  "apps/vendor-okx/tsconfig.json": "81da8f78196974b5d15da0edb6b2d9f48641063c",
28
28
  "apps/vendor-okx/.rush/temp/shrinkwrap-deps.json": "0a6205550d411d42b6d36324572e5e2a0ba166b2",