@yuants/exchange 0.8.11 → 0.8.13

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.
@@ -1 +1 @@
1
- {"version":3,"file":"interest_rate.js","sourceRoot":"","sources":["../src/interest_rate.ts"],"names":[],"mappings":";;;AAAA,mEAAwG;AAExG,wDAA8D;AAC9D,qCAAsE;AACtE,yCAA2C;AAuB3C,MAAM,eAAe,GAAG,IAAA,wBAAe,EAAC;IACtC,IAAI,EAAE,QAAQ;IACd,QAAQ,EAAE,CAAC,MAAM,EAAE,YAAY,CAAC;IAChC,UAAU,EAAE;QACV,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,QAAQ,EAAE;QACzC,QAAQ,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,YAAY,EAAE,WAAW,EAAE,MAAM,CAAC,EAAE;QACvE,UAAU,EAAE;YACV,IAAI,EAAE,QAAQ;YACd,QAAQ,EAAE,CAAC,YAAY,EAAE,WAAW,EAAE,MAAM,CAAC;YAC7C,UAAU,EAAE;gBACV,UAAU,EAAE;oBACV,IAAI,EAAE,QAAQ;oBACd,QAAQ,EAAE,CAAC,MAAM,EAAE,SAAS,CAAC;oBAC7B,UAAU,EAAE;wBACV,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,QAAQ,EAAE;wBACzC,OAAO,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE;qBAC7C;iBACF;gBACD,SAAS,EAAE;oBACT,IAAI,EAAE,QAAQ;oBACd,QAAQ,EAAE,CAAC,OAAO,CAAC;oBACnB,UAAU,EAAE;wBACV,KAAK,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,UAAU,EAAE,SAAS,CAAC,EAAE;qBACzD;iBACF;gBACD,IAAI,EAAE;oBACJ,IAAI,EAAE,QAAQ;oB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,GAAG,CAAC,EAAE,CAAC;gBAC7C,MAAM,IAAA,gBAAU,EACd,QAAQ,EACR,IAAA,iCAA2B,EAAC,sBAAsB,EAAE,yBAAyB,EAAE;oBAC7E,OAAO,EAAE,sCAAsC;oBAC/C,YAAY,EAAE,CAAC,IAAI,CAAC;iBACrB,CAAC,CACH,CAAC;YACJ,CAAC;YAED,OAAO;gBACL,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,WAAW,EAAE,sBAAsB,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE;aAC7F,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;AA3FW,QAAA,4BAA4B,gCA2FvC","sourcesContent":["import { encodeInterestRateSeriesId, IInterestRate, IInterestLedger } from '@yuants/data-interest-rate';\nimport { IServiceOptions, Terminal } from '@yuants/protocol';\nimport { createValidator } from '@yuants/protocol/lib/schema';\nimport { buildInsertManyIntoTableSQL, requestSQL } from '@yuants/sql';\nimport { newError } from '../../utils/lib';\nimport { ISeriesIngestResult, SeriesFetchDirection } from './types';\nimport { IExchange } from '.';\n\n/**\n * Interest Rate Service Metadata\n * @public\n */\nexport interface IInterestRateServiceMetadata {\n product_id_prefix: string;\n direction: SeriesFetchDirection;\n}\n\n/**\n * Interest Rate Service Request from VEX to Vendor\n * @public\n */\nexport interface IIngestInterestRateRequest {\n product_id: string;\n direction: SeriesFetchDirection;\n time: number;\n}\n\nconst schemaValidator = createValidator({\n type: 'object',\n required: ['type', 'properties'],\n properties: {\n type: { type: 'string', const: 'object' },\n required: { type: 'array', const: ['product_id', 'direction', 'time'] },\n properties: {\n type: 'object',\n required: ['product_id', 'direction', 'time'],\n properties: {\n product_id: {\n type: 'object',\n required: ['type', 'pattern'],\n properties: {\n type: { type: 'string', const: 'string' },\n pattern: { type: 'string', pattern: '^\\\\^' },\n },\n },\n direction: {\n type: 'object',\n required: ['const'],\n properties: {\n const: { type: 'string', enum: ['backward', 'forward'] },\n },\n },\n time: {\n type: 'object',\n required: ['type'],\n properties: {\n type: { type: 'string', const: 'number' },\n },\n },\n },\n },\n },\n});\n\n/**\n * @public\n */\nexport const parseInterestRateServiceMetadataFromSchema = (schema: any): IInterestRateServiceMetadata => {\n if (!schema) throw newError('INTEREST_RATE_SERVICE_SCHEMA_MISSING', { schema });\n if (!schemaValidator(schema)) throw newError('INTEREST_RATE_SERVICE_SCHEMA_INVALID', { schema });\n return {\n product_id_prefix: schema.properties.product_id.pattern.slice(1),\n direction: schema.properties.direction.const,\n };\n};\n\nconst INTEREST_RATE_INSERT_COLUMNS: Array<keyof IInterestRate> = [\n 'series_id',\n 'created_at',\n 'datasource_id',\n 'product_id',\n 'long_rate',\n 'short_rate',\n 'settlement_price',\n];\n\nconst computeInterestRatePageRange = (\n items: (IInterestRate | IInterestLedger)[],\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\n/**\n * @public\n */\nexport const provideInterestRateService = (\n terminal: Terminal,\n metadata: IInterestRateServiceMetadata,\n fetchPage: (request: IIngestInterestRateRequest & { series_id: string }) => Promise<IInterestRate[]>,\n serviceOptions?: IServiceOptions,\n) => {\n return terminal.server.provideService<IIngestInterestRateRequest, ISeriesIngestResult>(\n 'IngestInterestRate',\n {\n type: 'object',\n required: ['product_id', 'direction', 'time'],\n properties: {\n product_id: { type: 'string', pattern: `^${metadata.product_id_prefix}` },\n direction: { const: metadata.direction },\n time: { type: 'number' },\n },\n },\n async (msg) => {\n try {\n const series_id = encodeInterestRateSeriesId(msg.req.product_id);\n\n const items = await fetchPage({ ...msg.req, series_id });\n\n const normalized: IInterestRate[] = items.map((x) => ({\n ...x,\n series_id,\n datasource_id: '',\n product_id: msg.req.product_id,\n }));\n\n const range = computeInterestRatePageRange(normalized);\n\n // Atomic write: data rows + series_data_range in the same statement.\n if (normalized.length > 0 && range) {\n const writeInterestRate = `${buildInsertManyIntoTableSQL(normalized, 'interest_rate', {\n columns: INTEREST_RATE_INSERT_COLUMNS,\n conflictKeys: ['series_id', 'created_at'],\n })} RETURNING 1`;\n\n const writeRange = `${buildInsertManyIntoTableSQL(\n [\n {\n series_id,\n table_name: 'interest_rate',\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_interest_rate AS (\n ${writeInterestRate}\n ),\n write_range AS (\n ${writeRange}\n )\n SELECT 1 as ok;\n `,\n );\n } else if (normalized.length > 0) {\n await requestSQL(\n terminal,\n buildInsertManyIntoTableSQL(normalized, 'interest_rate', {\n columns: INTEREST_RATE_INSERT_COLUMNS,\n conflictKeys: ['series_id', 'created_at'],\n }),\n );\n }\n\n return { res: { code: 0, message: 'OK', data: { wrote_count: normalized.length, range } } };\n } catch (error) {\n const message = error instanceof Error ? error.message : `${error}`;\n return { res: { code: 1, message } };\n }\n },\n serviceOptions,\n );\n};\ninterface IExchangeCredential {\n type: string;\n payload: any;\n}\n\ninterface IIngestInterestLedgerRequest {\n credential: IExchangeCredential;\n account_id: string;\n time: number;\n ledger_type: string;\n}\n\nconst ACCOUNT_INTEREST_LEDGER_INSERT_COLUMNS: Array<keyof IInterestLedger> = [\n 'created_at',\n 'product_id',\n 'account_id',\n 'amount',\n 'currency',\n 'id',\n];\n\n/**\n * @public\n */\nexport const provideInterestLedgerService = (\n terminal: Terminal,\n metadata: { direction: string; type: string },\n fetchPage: (request: IIngestInterestLedgerRequest) => Promise<IInterestLedger[]>,\n serviceOptions?: IServiceOptions,\n) => {\n return terminal.server.provideService<IIngestInterestLedgerRequest, ISeriesIngestResult>(\n 'IngestInterestLedger',\n {\n type: 'object',\n required: ['account_id', 'direction', 'time', 'credential', 'ledger_type'],\n properties: {\n account_id: { type: 'string', pattern: `^${metadata.type}` },\n direction: { const: metadata.direction },\n time: { type: 'number' },\n ledger_type: { type: 'string' },\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 accountInterestLedgers = await fetchPage({ ...msg.req });\n\n const range = computeInterestRatePageRange(accountInterestLedgers);\n // Atomic write: data rows + series_data_range in the same statement.\n if (accountInterestLedgers.length > 0 && range) {\n const writeInterestRate = `${buildInsertManyIntoTableSQL(\n accountInterestLedgers,\n 'account_interest_ledger',\n {\n columns: ACCOUNT_INTEREST_LEDGER_INSERT_COLUMNS,\n conflictKeys: ['id'],\n },\n )} RETURNING 1`;\n\n const writeRange = `${buildInsertManyIntoTableSQL(\n [\n {\n series_id: msg.req.account_id,\n table_name: 'account_interest_ledger',\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_account_interest_ledger AS (\n ${writeInterestRate}\n ),\n write_range AS (\n ${writeRange}\n )\n SELECT 1 as ok;\n `,\n );\n } else if (accountInterestLedgers.length > 0) {\n await requestSQL(\n terminal,\n buildInsertManyIntoTableSQL(accountInterestLedgers, 'account_interest_ledger', {\n columns: ACCOUNT_INTEREST_LEDGER_INSERT_COLUMNS,\n conflictKeys: ['id'],\n }),\n );\n }\n\n return {\n res: { code: 0, message: 'OK', data: { wrote_count: accountInterestLedgers.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"]}
1
+ {"version":3,"file":"interest_rate.js","sourceRoot":"","sources":["../src/interest_rate.ts"],"names":[],"mappings":";;;AAAA,mEAAwG;AAExG,wDAA8D;AAC9D,qCAAsE;AACtE,yCAA2C;AAuB3C,MAAM,eAAe,GAAG,IAAA,wBAAe,EAAC;IACtC,IAAI,EAAE,QAAQ;IACd,QAAQ,EAAE,CAAC,MAAM,EAAE,YAAY,CAAC;IAChC,UAAU,EAAE;QACV,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,QAAQ,EAAE;QACzC,QAAQ,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,YAAY,EAAE,WAAW,EAAE,MAAM,CAAC,EAAE;QACvE,UAAU,EAAE;YACV,IAAI,EAAE,QAAQ;YACd,QAAQ,EAAE,CAAC,YAAY,EAAE,WAAW,EAAE,MAAM,CAAC;YAC7C,UAAU,EAAE;gBACV,UAAU,EAAE;oBACV,IAAI,EAAE,QAAQ;oBACd,QAAQ,EAAE,CAAC,MAAM,EAAE,SAAS,CAAC;oBAC7B,UAAU,EAAE;wBACV,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,QAAQ,EAAE;wBACzC,OAAO,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE;qBAC7C;iBACF;gBACD,SAAS,EAAE;oBACT,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,mBAAmB,EACnB;oBACE,OAAO,EAAE,CAAC,WAAW,EAAE,YAAY,EAAE,YAAY,EAAE,UAAU,CAAC;oBAC9D,cAAc,EAAE,IAAI;iBACrB,CACF,cAAc,CAAC;gBAEhB,MAAM,IAAA,gBAAU,EACd,QAAQ,EACR;;;kBAGM,iBAAiB;;;kBAGjB,UAAU;;;aAGf,CACF,CAAC;YACJ,CAAC;iBAAM,IAAI,sBAAsB,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBAC7C,MAAM,IAAA,gBAAU,EACd,QAAQ,EACR,IAAA,iCAA2B,EAAC,sBAAsB,EAAE,yBAAyB,EAAE;oBAC7E,OAAO,EAAE,sCAAsC;oBAC/C,YAAY,EAAE,CAAC,IAAI,EAAE,YAAY,CAAC;iBACnC,CAAC,CACH,CAAC;YACJ,CAAC;YAED,OAAO;gBACL,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,WAAW,EAAE,sBAAsB,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE;aAC7F,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;AA3FW,QAAA,4BAA4B,gCA2FvC","sourcesContent":["import { encodeInterestRateSeriesId, IInterestRate, IInterestLedger } from '@yuants/data-interest-rate';\nimport { IServiceOptions, Terminal } from '@yuants/protocol';\nimport { createValidator } from '@yuants/protocol/lib/schema';\nimport { buildInsertManyIntoTableSQL, requestSQL } from '@yuants/sql';\nimport { newError } from '../../utils/lib';\nimport { ISeriesIngestResult, SeriesFetchDirection } from './types';\nimport { IExchange } from '.';\n\n/**\n * Interest Rate Service Metadata\n * @public\n */\nexport interface IInterestRateServiceMetadata {\n product_id_prefix: string;\n direction: SeriesFetchDirection;\n}\n\n/**\n * Interest Rate Service Request from VEX to Vendor\n * @public\n */\nexport interface IIngestInterestRateRequest {\n product_id: string;\n direction: SeriesFetchDirection;\n time: number;\n}\n\nconst schemaValidator = createValidator({\n type: 'object',\n required: ['type', 'properties'],\n properties: {\n type: { type: 'string', const: 'object' },\n required: { type: 'array', const: ['product_id', 'direction', 'time'] },\n properties: {\n type: 'object',\n required: ['product_id', 'direction', 'time'],\n properties: {\n product_id: {\n type: 'object',\n required: ['type', 'pattern'],\n properties: {\n type: { type: 'string', const: 'string' },\n pattern: { type: 'string', pattern: '^\\\\^' },\n },\n },\n direction: {\n type: 'object',\n required: ['const'],\n properties: {\n const: { type: 'string', enum: ['backward', 'forward'] },\n },\n },\n time: {\n type: 'object',\n required: ['type'],\n properties: {\n type: { type: 'string', const: 'number' },\n },\n },\n },\n },\n },\n});\n\n/**\n * @public\n */\nexport const parseInterestRateServiceMetadataFromSchema = (schema: any): IInterestRateServiceMetadata => {\n if (!schema) throw newError('INTEREST_RATE_SERVICE_SCHEMA_MISSING', { schema });\n if (!schemaValidator(schema)) throw newError('INTEREST_RATE_SERVICE_SCHEMA_INVALID', { schema });\n return {\n product_id_prefix: schema.properties.product_id.pattern.slice(1),\n direction: schema.properties.direction.const,\n };\n};\n\nconst INTEREST_RATE_INSERT_COLUMNS: Array<keyof IInterestRate> = [\n 'series_id',\n 'created_at',\n 'datasource_id',\n 'product_id',\n 'long_rate',\n 'short_rate',\n 'settlement_price',\n];\n\nconst computeInterestRatePageRange = (\n items: (IInterestRate | IInterestLedger)[],\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\n/**\n * @public\n */\nexport const provideInterestRateService = (\n terminal: Terminal,\n metadata: IInterestRateServiceMetadata,\n fetchPage: (request: IIngestInterestRateRequest & { series_id: string }) => Promise<IInterestRate[]>,\n serviceOptions?: IServiceOptions,\n) => {\n return terminal.server.provideService<IIngestInterestRateRequest, ISeriesIngestResult>(\n 'IngestInterestRate',\n {\n type: 'object',\n required: ['product_id', 'direction', 'time'],\n properties: {\n product_id: { type: 'string', pattern: `^${metadata.product_id_prefix}` },\n direction: { const: metadata.direction },\n time: { type: 'number' },\n },\n },\n async (msg) => {\n try {\n const series_id = encodeInterestRateSeriesId(msg.req.product_id);\n\n const items = await fetchPage({ ...msg.req, series_id });\n\n const normalized: IInterestRate[] = items.map((x) => ({\n ...x,\n series_id,\n datasource_id: '',\n product_id: msg.req.product_id,\n }));\n\n const range = computeInterestRatePageRange(normalized);\n\n // Atomic write: data rows + series_data_range in the same statement.\n if (normalized.length > 0 && range) {\n const writeInterestRate = `${buildInsertManyIntoTableSQL(normalized, 'interest_rate', {\n columns: INTEREST_RATE_INSERT_COLUMNS,\n conflictKeys: ['series_id', 'created_at'],\n })} RETURNING 1`;\n\n const writeRange = `${buildInsertManyIntoTableSQL(\n [\n {\n series_id,\n table_name: 'interest_rate',\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_interest_rate AS (\n ${writeInterestRate}\n ),\n write_range AS (\n ${writeRange}\n )\n SELECT 1 as ok;\n `,\n );\n } else if (normalized.length > 0) {\n await requestSQL(\n terminal,\n buildInsertManyIntoTableSQL(normalized, 'interest_rate', {\n columns: INTEREST_RATE_INSERT_COLUMNS,\n conflictKeys: ['series_id', 'created_at'],\n }),\n );\n }\n\n return { res: { code: 0, message: 'OK', data: { wrote_count: normalized.length, range } } };\n } catch (error) {\n const message = error instanceof Error ? error.message : `${error}`;\n return { res: { code: 1, message } };\n }\n },\n serviceOptions,\n );\n};\ninterface IExchangeCredential {\n type: string;\n payload: any;\n}\n\ninterface IIngestInterestLedgerRequest {\n credential: IExchangeCredential;\n account_id: string;\n time: number;\n ledger_type: string;\n}\n\nconst ACCOUNT_INTEREST_LEDGER_INSERT_COLUMNS: Array<keyof IInterestLedger> = [\n 'created_at',\n 'product_id',\n 'account_id',\n 'amount',\n 'currency',\n 'id',\n];\n\n/**\n * @public\n */\nexport const provideInterestLedgerService = (\n terminal: Terminal,\n metadata: { direction: string; type: string; ledger_type: string[] },\n fetchPage: (request: IIngestInterestLedgerRequest) => Promise<IInterestLedger[]>,\n serviceOptions?: IServiceOptions,\n) => {\n return terminal.server.provideService<IIngestInterestLedgerRequest, ISeriesIngestResult>(\n 'IngestInterestLedger',\n {\n type: 'object',\n required: ['account_id', 'direction', 'time', 'credential', 'ledger_type'],\n properties: {\n account_id: { type: 'string', pattern: `^${metadata.type}` },\n direction: { const: metadata.direction },\n time: { type: 'number' },\n ledger_type: { type: 'string', enum: metadata.ledger_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 accountInterestLedgers = await fetchPage({ ...msg.req });\n\n const range = computeInterestRatePageRange(accountInterestLedgers);\n // Atomic write: data rows + series_data_range in the same statement.\n if (accountInterestLedgers.length > 0 && range) {\n const writeInterestRate = `${buildInsertManyIntoTableSQL(\n accountInterestLedgers,\n 'account_interest_ledger',\n {\n columns: ACCOUNT_INTEREST_LEDGER_INSERT_COLUMNS,\n conflictKeys: ['id', 'account_id'],\n },\n )} RETURNING 1`;\n\n const writeRange = `${buildInsertManyIntoTableSQL(\n [\n {\n series_id: msg.req.account_id,\n table_name: 'account_interest_ledger',\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_account_interest_ledger AS (\n ${writeInterestRate}\n ),\n write_range AS (\n ${writeRange}\n )\n SELECT 1 as ok;\n `,\n );\n } else if (accountInterestLedgers.length > 0) {\n await requestSQL(\n terminal,\n buildInsertManyIntoTableSQL(accountInterestLedgers, 'account_interest_ledger', {\n columns: ACCOUNT_INTEREST_LEDGER_INSERT_COLUMNS,\n conflictKeys: ['id', 'account_id'],\n }),\n );\n }\n\n return {\n res: { code: 0, message: 'OK', data: { wrote_count: accountInterestLedgers.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"]}
@@ -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;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AAY,CAAC;iBACnC,CAAC,CACH,CAAC;YACJ,CAAC;YAED,OAAO;gBACL,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,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.11",
3
+ "version": "0.8.13",
4
4
  "main": "lib/index.js",
5
5
  "module": "dist/index.js",
6
6
  "files": [
@@ -25,14 +25,15 @@
25
25
  },
26
26
  "dependencies": {
27
27
  "rxjs": "~7.5.6",
28
- "@yuants/data-order": "0.7.9",
29
- "@yuants/data-account": "0.11.8",
30
28
  "@yuants/protocol": "0.54.1",
29
+ "@yuants/data-account": "0.11.8",
30
+ "@yuants/data-trade": "0.1.31",
31
31
  "@yuants/data-product": "0.5.9",
32
+ "@yuants/data-order": "0.7.9",
32
33
  "@yuants/data-quote": "0.4.8",
33
34
  "@yuants/data-ohlc": "0.6.4",
34
- "@yuants/cache": "0.3.12",
35
35
  "@yuants/data-interest-rate": "0.2.8",
36
+ "@yuants/cache": "0.3.12",
36
37
  "@yuants/sql": "0.9.39",
37
38
  "@yuants/utils": "0.19.4"
38
39
  },