@yuants/exchange 0.8.10 → 0.8.11

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.
@@ -308,10 +308,10 @@ export declare const provideOHLCService: (terminal: Terminal, metadata: IOHLCSer
308
308
  * Provide Quote Service
309
309
  * @public
310
310
  */
311
- export declare const provideQuoteService: <K extends IQuoteField>(terminal: Terminal, metadata: IQuoteServiceMetadata<K>, requestFunc: (request: IQuoteServiceRequestByVEX) => Promise<(Pick<IQuote, K> & {
311
+ export declare const provideQuoteService: <K extends IQuoteField>(terminal: Terminal, metadata: IQuoteServiceMetadata<K>, requestFunc: (request: IQuoteServiceRequestByVEX) => Promise<Array<Pick<IQuote, K> & {
312
312
  product_id: string;
313
313
  updated_at: number;
314
- })[]>, serviceOptions?: IServiceOptions) => {
314
+ }>>, serviceOptions?: IServiceOptions) => {
315
315
  dispose: () => void;
316
316
  };
317
317
 
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA,cAAc,SAAS,CAAC;AACxB,cAAc,QAAQ,CAAC;AACvB,cAAc,iBAAiB,CAAC;AAChC,cAAc,SAAS,CAAC;AAqFxB,MAAM,oBAAoB,GAAG,CAAC,IAAY,EAAE,aAA0B,EAAe,EAAE;IACrF,OAAO;QACL,IAAI,EAAE,QAAQ;QACd,QAAQ,EAAE,CAAC,MAAM,EAAE,SAAS,CAAC;QAC7B,UAAU,EAAE;YACV,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,IAAI,EAAE;YACrC,OAAO,EAAE,aAAa;SACvB;KACF,CAAC;AACJ,CAAC,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAI,QAAkB,EAAE,QAAsB,EAAE,EAAE;IACvF,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,gBAAgB,EAAE,GAAG,Q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,EAC/B,QAAkB,EAClB,UAAwC,EACxC,UAAmB,EACc,EAAE;IACnC,OAAO,QAAQ,CAAC,MAAM,CAAC,kBAAkB,CAAC,cAAc,EAAE,EAAE,UAAU,EAAE,UAAU,EAAE,CAAC,CAAC;AACxF,CAAC,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,KAAK,EAC5B,QAAkB,EAClB,UAAwC,EACxC,UAAmB,EACW,EAAE;IAChC,OAAO,QAAQ,CAAC,MAAM,CAAC,kBAAkB,CAAC,WAAW,EAAE,EAAE,UAAU,EAAE,UAAU,EAAE,CAAC,CAAC;AACrF,CAAC,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,KAAK,EAC9B,QAAkB,EAClB,UAAwC,EACxC,KAAa,EAC6B,EAAE;IAC5C,OAAO,QAAQ,CAAC,MAAM,CAAC,kBAAkB,CAAC,aAAa,EAAE,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC,CAAC;AAClF,CAAC,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,KAAK,EAC9B,QAAkB,EAClB,UAAwC,EACxC,KAAa,EACa,EAAE;IAC5B,OAAO,QAAQ,CAAC,MAAM,CAAC,kBAAkB,CAAC,aAAa,EAAE,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC,CAAC;AAClF,CAAC,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,KAAK,EAC9B,QAAkB,EAClB,UAAwC,EACxC,KAAa,EACa,EAAE;IAC5B,OAAO,QAAQ,CAAC,MAAM,CAAC,kBAAkB,CAAC,aAAa,EAAE,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC,CAAC;AAClF,CAAC,CAAC","sourcesContent":["import { IPosition } from '@yuants/data-account';\nimport { IOrder } from '@yuants/data-order';\nimport { IProduct } from '@yuants/data-product';\nimport { IResponse, Terminal } from '@yuants/protocol';\nimport { JSONSchema7 } from 'json-schema';\nexport * from './quote';\nexport * from './ohlc';\nexport * from './interest_rate';\nexport * from './types';\n\n/**\n * Exchange Interface\n *\n * @public\n */\nexport interface IExchange<T = any> {\n /**\n * Exchange name / type (e.g. 'Binance', 'OKX')\n */\n name: string;\n\n /**\n * JSON Schema for the credential payload\n */\n credentialSchema: JSONSchema7;\n\n /**\n * Get credential ID from credential\n *\n * @param credential - The credential object\n */\n getCredentialId(credential: T): Promise<string>;\n\n /**\n * List all products\n */\n listProducts(): Promise<IProduct[]>;\n\n /**\n * Get all positions\n *\n * @param credential - The credential object\n */\n getPositions(credential: T): Promise<IPosition[]>;\n\n /**\n * Get all orders\n *\n * @param credential - The credential object\n */\n getOrders(credential: T): Promise<IOrder[]>;\n\n /**\n * Get positions by product_id\n *\n * @param credential - The credential object\n * @param product_id - The product ID\n */\n getPositionsByProductId(credential: T, product_id: string): Promise<IPosition[]>;\n\n /**\n * Get orders by product_id\n *\n * @param credential - The credential object\n * @param product_id - The product ID\n */\n getOrdersByProductId(credential: T, product_id: string): Promise<IOrder[]>;\n\n /**\n * Submit an order\n *\n * @param credential - The credential object\n * @param order - The order object\n */\n submitOrder(credential: T, order: IOrder): Promise<{ order_id: string }>;\n\n /**\n * Modify an order\n *\n * @param credential - The credential object\n * @param order - The order object\n */\n modifyOrder(credential: T, order: IOrder): Promise<void>;\n\n /**\n * Cancel an order\n *\n * @param credential - The credential object\n * @param order - The order object\n */\n cancelOrder(credential: T, order: IOrder): Promise<void>;\n}\n\nconst makeCredentialSchema = (type: string, payloadSchema: JSONSchema7): JSONSchema7 => {\n return {\n type: 'object',\n required: ['type', 'payload'],\n properties: {\n type: { type: 'string', const: type },\n payload: payloadSchema,\n },\n };\n};\n\n/**\n * Provide exchange services\n *\n * @public\n */\nexport const provideExchangeServices = <T>(terminal: Terminal, exchange: IExchange<T>) => {\n const { name: type, credentialSchema } = exchange;\n\n // GetCredentialId\n terminal.server.provideService<{ credential: { type: string; payload: T } }, string>(\n 'GetCredentialId',\n {\n type: 'object',\n required: ['credential'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n },\n },\n async (msg) => {\n const credentialId = await exchange.getCredentialId(msg.req.credential.payload);\n return { res: { code: 0, message: 'OK', data: credentialId } };\n },\n );\n\n // ListProducts\n terminal.server.provideService<void, IProduct[]>(\n 'ListProducts',\n {\n type: 'object',\n required: ['type'],\n properties: {\n type: { const: exchange.name },\n },\n },\n async () => {\n const products = await exchange.listProducts();\n return { res: { code: 0, message: 'OK', data: products } };\n },\n );\n\n // GetPositions\n terminal.server.provideService<\n { credential: { type: string; payload: T }; product_id?: string },\n IPosition[]\n >(\n 'GetPositions',\n {\n type: 'object',\n required: ['credential'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n product_id: { type: 'string' },\n },\n },\n async (msg) => {\n if (msg.req.product_id) {\n const positions = await exchange.getPositionsByProductId(\n msg.req.credential.payload,\n msg.req.product_id,\n );\n return { res: { code: 0, message: 'OK', data: positions } };\n }\n const positions = await exchange.getPositions(msg.req.credential.payload);\n return { res: { code: 0, message: 'OK', data: positions } };\n },\n );\n\n // GetOrders\n terminal.server.provideService<{ credential: { type: string; payload: T }; product_id?: string }, IOrder[]>(\n 'GetOrders',\n {\n type: 'object',\n required: ['credential'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n product_id: { type: 'string' },\n },\n },\n async (msg) => {\n if (msg.req.product_id) {\n const orders = await exchange.getOrdersByProductId(msg.req.credential.payload, msg.req.product_id);\n return { res: { code: 0, message: 'OK', data: orders } };\n }\n const orders = await exchange.getOrders(msg.req.credential.payload);\n return { res: { code: 0, message: 'OK', data: orders } };\n },\n );\n\n // SubmitOrder\n terminal.server.provideService<\n { credential: { type: string; payload: T }; order: IOrder },\n { order_id: string }\n >(\n 'SubmitOrder',\n {\n type: 'object',\n required: ['credential', 'order'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n order: { type: 'object' },\n },\n },\n async (msg) => {\n const result = await exchange.submitOrder(msg.req.credential.payload, msg.req.order);\n return { res: { code: 0, message: 'OK', data: result } };\n },\n );\n\n // ModifyOrder\n terminal.server.provideService<{ credential: { type: string; payload: T }; order: IOrder }, void>(\n 'ModifyOrder',\n {\n type: 'object',\n required: ['credential', 'order'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n order: { type: 'object' },\n },\n },\n async (msg) => {\n await exchange.modifyOrder(msg.req.credential.payload, msg.req.order);\n return { res: { code: 0, message: 'OK' } };\n },\n );\n\n // CancelOrder\n terminal.server.provideService<{ credential: { type: string; payload: T }; order: IOrder }, void>(\n 'CancelOrder',\n {\n type: 'object',\n required: ['credential', 'order'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n order: { type: 'object' },\n },\n },\n async (msg) => {\n await exchange.cancelOrder(msg.req.credential.payload, msg.req.order);\n return { res: { code: 0, message: 'OK' } };\n },\n );\n};\n\n/**\n * Get credential ID\n *\n * @public\n */\nexport const getCredentialId = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n): Promise<IResponse<string>> => {\n return terminal.client.requestForResponse('GetCredentialId', { credential });\n};\n\n/**\n * List products\n *\n * @public\n */\nexport const listProducts = async (terminal: Terminal, type: string): Promise<IResponse<IProduct[]>> => {\n return terminal.client.requestForResponse('ListProducts', { type });\n};\n\n/**\n * Get positions\n *\n * @public\n */\nexport const getPositions = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n product_id?: string,\n): Promise<IResponse<IPosition[]>> => {\n return terminal.client.requestForResponse('GetPositions', { credential, product_id });\n};\n\n/**\n * Get orders\n *\n * @public\n */\nexport const getOrders = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n product_id?: string,\n): Promise<IResponse<IOrder[]>> => {\n return terminal.client.requestForResponse('GetOrders', { credential, product_id });\n};\n\n/**\n * Submit order\n *\n * @public\n */\nexport const submitOrder = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n order: IOrder,\n): Promise<IResponse<{ order_id: string }>> => {\n return terminal.client.requestForResponse('SubmitOrder', { credential, order });\n};\n\n/**\n * Modify order\n *\n * @public\n */\nexport const modifyOrder = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n order: IOrder,\n): Promise<IResponse<void>> => {\n return terminal.client.requestForResponse('ModifyOrder', { credential, order });\n};\n\n/**\n * Cancel order\n *\n * @public\n */\nexport const cancelOrder = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n order: IOrder,\n): Promise<IResponse<void>> => {\n return terminal.client.requestForResponse('CancelOrder', { credential, order });\n};\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA,cAAc,SAAS,CAAC;AACxB,cAAc,QAAQ,CAAC;AACvB,cAAc,iBAAiB,CAAC;AAChC,cAAc,SAAS,CAAC;AAqFxB,MAAM,oBAAoB,GAAG,CAAC,IAAY,EAAE,aAA0B,EAAe,EAAE;IACrF,OAAO;QACL,IAAI,EAAE,QAAQ;QACd,QAAQ,EAAE,CAAC,MAAM,EAAE,SAAS,CAAC;QAC7B,UAAU,EAAE;YACV,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,IAAI,EAAE;YACrC,OAAO,EAAE,aAAa;SACvB;KACF,CAAC;AACJ,CAAC,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAI,QAAkB,EAAE,QAAsB,EAAE,EAAE;IACvF,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,gBAAgB,EAAE,GAAG,QAAQ,CAAC;IAElD,kBAAkB;IAClB,QAAQ,CAAC,MAAM,CAAC,cAAc,CAC5B,iBAAiB,EACjB;QACE,IAAI,EAAE,QAAQ;QACd,QAAQ,EAAE,CAAC,YAAY,CAAC;QACxB,UAAU,EAAE;YACV,UAAU,EAAE,oBAAoB,CAAC,IAAI,EAAE,gBAAgB,CAAC;SACzD;KACF,EACD,KAAK,EAAE,GAAG,EAAE,E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B,QAAkB,EAClB,UAAwC,EACxC,UAAmB,EACW,EAAE;IAChC,OAAO,QAAQ,CAAC,MAAM,CAAC,kBAAkB,CAAC,WAAW,EAAE,EAAE,UAAU,EAAE,UAAU,EAAE,CAAC,CAAC;AACrF,CAAC,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,KAAK,EAC9B,QAAkB,EAClB,UAAwC,EACxC,KAAa,EAC6B,EAAE;IAC5C,OAAO,QAAQ,CAAC,MAAM,CAAC,kBAAkB,CAAC,aAAa,EAAE,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC,CAAC;AAClF,CAAC,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,KAAK,EAC9B,QAAkB,EAClB,UAAwC,EACxC,KAAa,EACa,EAAE;IAC5B,OAAO,QAAQ,CAAC,MAAM,CAAC,kBAAkB,CAAC,aAAa,EAAE,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC,CAAC;AAClF,CAAC,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,KAAK,EAC9B,QAAkB,EAClB,UAAwC,EACxC,KAAa,EACa,EAAE;IAC5B,OAAO,QAAQ,CAAC,MAAM,CAAC,kBAAkB,CAAC,aAAa,EAAE,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC,CAAC;AAClF,CAAC,CAAC","sourcesContent":["import { IPosition } from '@yuants/data-account';\nimport { IOrder } from '@yuants/data-order';\nimport { IProduct } from '@yuants/data-product';\nimport { IResponse, Terminal } from '@yuants/protocol';\nimport { JSONSchema7 } from 'json-schema';\nexport * from './quote';\nexport * from './ohlc';\nexport * from './interest_rate';\nexport * from './types';\n\n/**\n * Exchange Interface\n *\n * @public\n */\nexport interface IExchange<T = any> {\n /**\n * Exchange name / type (e.g. 'Binance', 'OKX')\n */\n name: string;\n\n /**\n * JSON Schema for the credential payload\n */\n credentialSchema: JSONSchema7;\n\n /**\n * Get credential ID from credential\n *\n * @param credential - The credential object\n */\n getCredentialId(credential: T): Promise<string>;\n\n /**\n * List all products\n */\n listProducts(): Promise<IProduct[]>;\n\n /**\n * Get all positions\n *\n * @param credential - The credential object\n */\n getPositions(credential: T): Promise<IPosition[]>;\n\n /**\n * Get all orders\n *\n * @param credential - The credential object\n */\n getOrders(credential: T): Promise<IOrder[]>;\n\n /**\n * Get positions by product_id\n *\n * @param credential - The credential object\n * @param product_id - The product ID\n */\n getPositionsByProductId(credential: T, product_id: string): Promise<IPosition[]>;\n\n /**\n * Get orders by product_id\n *\n * @param credential - The credential object\n * @param product_id - The product ID\n */\n getOrdersByProductId(credential: T, product_id: string): Promise<IOrder[]>;\n\n /**\n * Submit an order\n *\n * @param credential - The credential object\n * @param order - The order object\n */\n submitOrder(credential: T, order: IOrder): Promise<{ order_id: string }>;\n\n /**\n * Modify an order\n *\n * @param credential - The credential object\n * @param order - The order object\n */\n modifyOrder(credential: T, order: IOrder): Promise<void>;\n\n /**\n * Cancel an order\n *\n * @param credential - The credential object\n * @param order - The order object\n */\n cancelOrder(credential: T, order: IOrder): Promise<void>;\n}\n\nconst makeCredentialSchema = (type: string, payloadSchema: JSONSchema7): JSONSchema7 => {\n return {\n type: 'object',\n required: ['type', 'payload'],\n properties: {\n type: { type: 'string', const: type },\n payload: payloadSchema,\n },\n };\n};\n\n/**\n * Provide exchange services\n *\n * @public\n */\nexport const provideExchangeServices = <T>(terminal: Terminal, exchange: IExchange<T>) => {\n const { name: type, credentialSchema } = exchange;\n\n // GetCredentialId\n terminal.server.provideService<{ credential: { type: string; payload: T } }, string>(\n 'GetCredentialId',\n {\n type: 'object',\n required: ['credential'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n },\n },\n async (msg) => {\n const credentialId = await exchange.getCredentialId(msg.req.credential.payload);\n return { res: { code: 0, message: 'OK', data: credentialId } };\n },\n );\n\n // ListProducts\n terminal.server.provideService<void, IProduct[]>(\n 'ListProducts',\n {\n type: 'object',\n required: ['type'],\n properties: {\n type: { const: exchange.name },\n },\n },\n async () => {\n const products = await exchange.listProducts();\n return { res: { code: 0, message: 'OK', data: products } };\n },\n );\n\n // GetPositions\n terminal.server.provideService<\n { credential: { type: string; payload: T }; product_id?: string },\n IPosition[]\n >(\n 'GetPositions',\n {\n type: 'object',\n required: ['credential'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n product_id: { type: 'string' },\n },\n },\n async (msg) => {\n if (msg.req.product_id) {\n const positions = await exchange.getPositionsByProductId(\n msg.req.credential.payload,\n msg.req.product_id,\n );\n return { res: { code: 0, message: 'OK', data: positions } };\n }\n const positions = await exchange.getPositions(msg.req.credential.payload);\n return { res: { code: 0, message: 'OK', data: positions } };\n },\n );\n\n // GetOrders\n terminal.server.provideService<{ credential: { type: string; payload: T }; product_id?: string }, IOrder[]>(\n 'GetOrders',\n {\n type: 'object',\n required: ['credential'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n product_id: { type: 'string' },\n },\n },\n async (msg) => {\n if (msg.req.product_id) {\n const orders = await exchange.getOrdersByProductId(msg.req.credential.payload, msg.req.product_id);\n return { res: { code: 0, message: 'OK', data: orders } };\n }\n const orders = await exchange.getOrders(msg.req.credential.payload);\n return { res: { code: 0, message: 'OK', data: orders } };\n },\n );\n\n // SubmitOrder\n terminal.server.provideService<\n { credential: { type: string; payload: T }; order: IOrder },\n { order_id: string }\n >(\n 'SubmitOrder',\n {\n type: 'object',\n required: ['credential', 'order'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n order: { type: 'object' },\n },\n },\n async (msg) => {\n const result = await exchange.submitOrder(msg.req.credential.payload, msg.req.order);\n return { res: { code: 0, message: 'OK', data: result } };\n },\n );\n\n // ModifyOrder\n terminal.server.provideService<{ credential: { type: string; payload: T }; order: IOrder }, void>(\n 'ModifyOrder',\n {\n type: 'object',\n required: ['credential', 'order'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n order: { type: 'object' },\n },\n },\n async (msg) => {\n await exchange.modifyOrder(msg.req.credential.payload, msg.req.order);\n return { res: { code: 0, message: 'OK' } };\n },\n );\n\n // CancelOrder\n terminal.server.provideService<{ credential: { type: string; payload: T }; order: IOrder }, void>(\n 'CancelOrder',\n {\n type: 'object',\n required: ['credential', 'order'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n order: { type: 'object' },\n },\n },\n async (msg) => {\n await exchange.cancelOrder(msg.req.credential.payload, msg.req.order);\n return { res: { code: 0, message: 'OK' } };\n },\n );\n};\n\n/**\n * Get credential ID\n *\n * @public\n */\nexport const getCredentialId = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n): Promise<IResponse<string>> => {\n return terminal.client.requestForResponse('GetCredentialId', { credential });\n};\n\n/**\n * List products\n *\n * @public\n */\nexport const listProducts = async (terminal: Terminal, type: string): Promise<IResponse<IProduct[]>> => {\n return terminal.client.requestForResponse('ListProducts', { type });\n};\n\n/**\n * Get positions\n *\n * @public\n */\nexport const getPositions = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n product_id?: string,\n): Promise<IResponse<IPosition[]>> => {\n return terminal.client.requestForResponse('GetPositions', { credential, product_id });\n};\n\n/**\n * Get orders\n *\n * @public\n */\nexport const getOrders = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n product_id?: string,\n): Promise<IResponse<IOrder[]>> => {\n return terminal.client.requestForResponse('GetOrders', { credential, product_id });\n};\n\n/**\n * Submit order\n *\n * @public\n */\nexport const submitOrder = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n order: IOrder,\n): Promise<IResponse<{ order_id: string }>> => {\n return terminal.client.requestForResponse('SubmitOrder', { credential, order });\n};\n\n/**\n * Modify order\n *\n * @public\n */\nexport const modifyOrder = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n order: IOrder,\n): Promise<IResponse<void>> => {\n return terminal.client.requestForResponse('ModifyOrder', { credential, order });\n};\n\n/**\n * Cancel order\n *\n * @public\n */\nexport const cancelOrder = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n order: IOrder,\n): Promise<IResponse<void>> => {\n return terminal.client.requestForResponse('CancelOrder', { credential, order });\n};\n"]}
@@ -140,7 +140,7 @@ export const provideInterestRateService = (terminal, metadata, fetchPage, servic
140
140
  }
141
141
  }, serviceOptions);
142
142
  };
143
- const INTEREST_RATE_LEDGER_INSERT_COLUMNS = [
143
+ const ACCOUNT_INTEREST_LEDGER_INSERT_COLUMNS = [
144
144
  'created_at',
145
145
  'product_id',
146
146
  'account_id',
@@ -171,18 +171,18 @@ export const provideInterestLedgerService = (terminal, metadata, fetchPage, serv
171
171
  },
172
172
  }, async (msg) => {
173
173
  try {
174
- const interestRateLedgers = await fetchPage(Object.assign({}, msg.req));
175
- const range = computeInterestRatePageRange(interestRateLedgers);
174
+ const accountInterestLedgers = await fetchPage(Object.assign({}, msg.req));
175
+ const range = computeInterestRatePageRange(accountInterestLedgers);
176
176
  // Atomic write: data rows + series_data_range in the same statement.
177
- if (interestRateLedgers.length > 0 && range) {
178
- const writeInterestRate = `${buildInsertManyIntoTableSQL(interestRateLedgers, 'interest_rate_ledger', {
179
- columns: INTEREST_RATE_LEDGER_INSERT_COLUMNS,
177
+ if (accountInterestLedgers.length > 0 && range) {
178
+ const writeInterestRate = `${buildInsertManyIntoTableSQL(accountInterestLedgers, 'account_interest_ledger', {
179
+ columns: ACCOUNT_INTEREST_LEDGER_INSERT_COLUMNS,
180
180
  conflictKeys: ['id'],
181
181
  })} RETURNING 1`;
182
182
  const writeRange = `${buildInsertManyIntoTableSQL([
183
183
  {
184
184
  series_id: msg.req.account_id,
185
- table_name: 'interest_rate_ledger',
185
+ table_name: 'account_interest_ledger',
186
186
  start_time: range.start_time,
187
187
  end_time: range.end_time,
188
188
  },
@@ -192,7 +192,7 @@ export const provideInterestLedgerService = (terminal, metadata, fetchPage, serv
192
192
  })} RETURNING 1`;
193
193
  await requestSQL(terminal, `
194
194
  WITH
195
- write_interest_rate_ledger AS (
195
+ write_account_interest_ledger AS (
196
196
  ${writeInterestRate}
197
197
  ),
198
198
  write_range AS (
@@ -201,13 +201,15 @@ export const provideInterestLedgerService = (terminal, metadata, fetchPage, serv
201
201
  SELECT 1 as ok;
202
202
  `);
203
203
  }
204
- else if (interestRateLedgers.length > 0) {
205
- await requestSQL(terminal, buildInsertManyIntoTableSQL(interestRateLedgers, 'interest_rate_ledger', {
206
- columns: INTEREST_RATE_LEDGER_INSERT_COLUMNS,
204
+ else if (accountInterestLedgers.length > 0) {
205
+ await requestSQL(terminal, buildInsertManyIntoTableSQL(accountInterestLedgers, 'account_interest_ledger', {
206
+ columns: ACCOUNT_INTEREST_LEDGER_INSERT_COLUMNS,
207
207
  conflictKeys: ['id'],
208
208
  }));
209
209
  }
210
- return { res: { code: 0, message: 'OK', data: { wrote_count: interestRateLedgers.length, range } } };
210
+ return {
211
+ res: { code: 0, message: 'OK', data: { wrote_count: accountInterestLedgers.length, range } },
212
+ };
211
213
  }
212
214
  catch (error) {
213
215
  const message = error instanceof Error ? error.message : `${error}`;
@@ -1 +1 @@
1
- {"version":3,"file":"interest_rate.js","sourceRoot":"","sources":["../src/interest_rate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,0BAA0B,EAAkC,MAAM,4BAA4B,CAAC;AAExG,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAC9D,OAAO,EAAE,2BAA2B,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACtE,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAuB3C,MAAM,eAAe,GAAG,eAAe,CAAC;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,EAA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hB,MAAM,UAAU,GAAG,GAAG,2BAA2B,CAC/C;oBACE;wBACE,SAAS,EAAE,GAAG,CAAC,GAAG,CAAC,UAAU;wBAC7B,UAAU,EAAE,sBAAsB;wBAClC,UAAU,EAAE,KAAK,CAAC,UAAU;wBAC5B,QAAQ,EAAE,KAAK,CAAC,QAAQ;qBACzB;iBACF,EACD,mBAAmB,EACnB;oBACE,OAAO,EAAE,CAAC,WAAW,EAAE,YAAY,EAAE,YAAY,EAAE,UAAU,CAAC;oBAC9D,cAAc,EAAE,IAAI;iBACrB,CACF,cAAc,CAAC;gBAEhB,MAAM,UAAU,CACd,QAAQ,EACR;;;kBAGM,iBAAiB;;;kBAGjB,UAAU;;;aAGf,CACF,CAAC;aACH;iBAAM,IAAI,mBAAmB,CAAC,MAAM,GAAG,CAAC,EAAE;gBACzC,MAAM,UAAU,CACd,QAAQ,EACR,2BAA2B,CAAC,mBAAmB,EAAE,sBAAsB,EAAE;oBACvE,OAAO,EAAE,mCAAmC;oBAC5C,YAAY,EAAE,CAAC,IAAI,CAAC;iBACrB,CAAC,CACH,CAAC;aACH;YAED,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,WAAW,EAAE,mBAAmB,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC;SACtG;QAAC,OAAO,KAAK,EAAE;YACd,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;SACtC;IACH,CAAC,EACD,cAAc,CACf,CAAC;AACJ,CAAC,CAAC","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 INTEREST_RATE_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 interestRateLedgers = await fetchPage({ ...msg.req });\n const range = computeInterestRatePageRange(interestRateLedgers);\n\n // Atomic write: data rows + series_data_range in the same statement.\n if (interestRateLedgers.length > 0 && range) {\n const writeInterestRate = `${buildInsertManyIntoTableSQL(\n interestRateLedgers,\n 'interest_rate_ledger',\n {\n columns: INTEREST_RATE_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: 'interest_rate_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_interest_rate_ledger AS (\n ${writeInterestRate}\n ),\n write_range AS (\n ${writeRange}\n )\n SELECT 1 as ok;\n `,\n );\n } else if (interestRateLedgers.length > 0) {\n await requestSQL(\n terminal,\n buildInsertManyIntoTableSQL(interestRateLedgers, 'interest_rate_ledger', {\n columns: INTEREST_RATE_LEDGER_INSERT_COLUMNS,\n conflictKeys: ['id'],\n }),\n );\n }\n\n return { res: { code: 0, message: 'OK', data: { wrote_count: interestRateLedgers.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};\n"]}
1
+ {"version":3,"file":"interest_rate.js","sourceRoot":"","sources":["../src/interest_rate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,0BAA0B,EAAkC,MAAM,4BAA4B,CAAC;AAExG,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAC9D,OAAO,EAAE,2BAA2B,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACtE,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAuB3C,MAAM,eAAe,GAAG,eAAe,CAAC;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,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AAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE,CAAC;QACvC,CAAC;IACH,CAAC,EACD,cAAc,CACf,CAAC;AACJ,CAAC,CAAC","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"]}
package/dist/ohlc.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"ohlc.js","sourceRoot":"","sources":["../src/ohlc.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAS,MAAM,mBAAmB,CAAC;AAE9D,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAC9D,OAAO,EAAE,2BAA2B,EAAE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AAO,CAAC,KAAK,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,EAAE,mBAAmB,EAAE,KAAK,CAAC,CAAC;YAClE,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE,CAAC;SACtC;IACH,CAAC,EACD,cAAc,CACf,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import { encodeOHLCSeriesId, IOHLC } from '@yuants/data-ohlc';\nimport { IServiceOptions, Terminal } from '@yuants/protocol';\nimport { createValidator } from '@yuants/protocol/lib/schema';\nimport { buildInsertManyIntoTableSQL, requestSQL } from '@yuants/sql';\nimport { formatTime, newError } from '@yuants/utils';\nimport { ISeriesIngestResult, SeriesFetchDirection } from './types';\n\n/**\n * OHLC Service Metadata\n * @public\n */\nexport interface IOHLCServiceMetadata {\n product_id_prefix: string;\n duration_list: string[];\n direction: SeriesFetchDirection;\n}\n\n/**\n * OHLC Service Request from VEX to Vendor\n * @public\n */\nexport interface IIngestOHLCRequest {\n product_id: string;\n duration: 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', 'duration', 'direction', 'time'] },\n properties: {\n type: 'object',\n required: ['product_id', 'duration', '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 duration: {\n type: 'object',\n required: ['type', 'enum'],\n properties: {\n type: { type: 'string', const: 'string' },\n enum: { type: 'array', items: { type: 'string' } },\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 parseOHLCServiceMetadataFromSchema = (schema: any): IOHLCServiceMetadata => {\n if (!schema) throw newError('OHLC_SERVICE_SCHEMA_MISSING', { schema });\n if (!schemaValidator(schema)) throw newError('OHLC_SERVICE_SCHEMA_INVALID', { schema });\n return {\n product_id_prefix: schema.properties.product_id.pattern.slice(1),\n duration_list: schema.properties.duration.enum,\n direction: schema.properties.direction.const,\n };\n};\n\nconst OHLC_INSERT_COLUMNS: Array<keyof IOHLC> = [\n 'series_id',\n 'created_at',\n 'closed_at',\n 'open',\n 'high',\n 'low',\n 'close',\n 'volume',\n 'open_interest',\n];\n\nconst computeOHLCPageRange = (items: IOHLC[]): { 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.closed_at) || 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 const closedAtMs = Date.parse(item.closed_at);\n const candidateEndMs = !isNaN(closedAtMs) ? closedAtMs : createdAtMs;\n if (!isNaN(candidateEndMs) && (isNaN(endMs) || candidateEndMs > endMs)) {\n end = item;\n endMs = candidateEndMs;\n }\n }\n\n return {\n start_time: start.created_at,\n end_time: end.closed_at || end.created_at,\n };\n};\n\n/**\n * @public\n */\nexport const provideOHLCService = (\n terminal: Terminal,\n metadata: IOHLCServiceMetadata,\n fetchPage: (request: IIngestOHLCRequest & { series_id: string }) => Promise<IOHLC[]>,\n serviceOptions?: IServiceOptions,\n) => {\n return terminal.server.provideService<IIngestOHLCRequest, ISeriesIngestResult>(\n 'IngestOHLC',\n {\n type: 'object',\n required: ['product_id', 'duration', 'direction', 'time'],\n properties: {\n product_id: { type: 'string', pattern: `^${metadata.product_id_prefix}` },\n duration: { type: 'string', enum: metadata.duration_list },\n direction: { const: metadata.direction },\n time: { type: 'number' },\n },\n },\n async (msg) => {\n try {\n const series_id = encodeOHLCSeriesId(msg.req.product_id, msg.req.duration);\n\n const items = await fetchPage({ ...msg.req, series_id });\n\n console.info(\n formatTime(Date.now()),\n 'IngestOHLC',\n msg.req.product_id,\n msg.req.duration,\n 'fetched',\n items.length,\n 'bars',\n );\n\n const normalized: IOHLC[] = items.map((x) => ({\n ...x,\n series_id,\n datasource_id: '',\n product_id: msg.req.product_id,\n duration: msg.req.duration,\n open_interest: x.open_interest ?? '0',\n }));\n\n const range = computeOHLCPageRange(normalized);\n\n // Atomic write: data rows + series_data_range in the same statement.\n if (normalized.length > 0 && range) {\n const writeOHLC = `${buildInsertManyIntoTableSQL(normalized, 'ohlc_v2', {\n columns: OHLC_INSERT_COLUMNS,\n conflictKeys: ['series_id', 'created_at'],\n })} RETURNING 1`;\n\n const writeRange = `${buildInsertManyIntoTableSQL(\n [\n {\n series_id,\n table_name: 'ohlc_v2',\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_ohlc AS (\n ${writeOHLC}\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, 'ohlc_v2', {\n columns: OHLC_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 console.error(formatTime(Date.now()), 'IngestOHLC error:', error);\n return { res: { code: 1, message } };\n }\n },\n serviceOptions,\n );\n};\n"]}
1
+ {"version":3,"file":"ohlc.js","sourceRoot":"","sources":["../src/ohlc.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAS,MAAM,mB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,CAAC;QAC9F,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,CAAC,KAAK,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,EAAE,mBAAmB,EAAE,KAAK,CAAC,CAAC;YAClE,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE,CAAC;QACvC,CAAC;IACH,CAAC,EACD,cAAc,CACf,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import { encodeOHLCSeriesId, IOHLC } from '@yuants/data-ohlc';\nimport { IServiceOptions, Terminal } from '@yuants/protocol';\nimport { createValidator } from '@yuants/protocol/lib/schema';\nimport { buildInsertManyIntoTableSQL, requestSQL } from '@yuants/sql';\nimport { formatTime, newError } from '@yuants/utils';\nimport { ISeriesIngestResult, SeriesFetchDirection } from './types';\n\n/**\n * OHLC Service Metadata\n * @public\n */\nexport interface IOHLCServiceMetadata {\n product_id_prefix: string;\n duration_list: string[];\n direction: SeriesFetchDirection;\n}\n\n/**\n * OHLC Service Request from VEX to Vendor\n * @public\n */\nexport interface IIngestOHLCRequest {\n product_id: string;\n duration: 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', 'duration', 'direction', 'time'] },\n properties: {\n type: 'object',\n required: ['product_id', 'duration', '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 duration: {\n type: 'object',\n required: ['type', 'enum'],\n properties: {\n type: { type: 'string', const: 'string' },\n enum: { type: 'array', items: { type: 'string' } },\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 parseOHLCServiceMetadataFromSchema = (schema: any): IOHLCServiceMetadata => {\n if (!schema) throw newError('OHLC_SERVICE_SCHEMA_MISSING', { schema });\n if (!schemaValidator(schema)) throw newError('OHLC_SERVICE_SCHEMA_INVALID', { schema });\n return {\n product_id_prefix: schema.properties.product_id.pattern.slice(1),\n duration_list: schema.properties.duration.enum,\n direction: schema.properties.direction.const,\n };\n};\n\nconst OHLC_INSERT_COLUMNS: Array<keyof IOHLC> = [\n 'series_id',\n 'created_at',\n 'closed_at',\n 'open',\n 'high',\n 'low',\n 'close',\n 'volume',\n 'open_interest',\n];\n\nconst computeOHLCPageRange = (items: IOHLC[]): { 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.closed_at) || 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 const closedAtMs = Date.parse(item.closed_at);\n const candidateEndMs = !isNaN(closedAtMs) ? closedAtMs : createdAtMs;\n if (!isNaN(candidateEndMs) && (isNaN(endMs) || candidateEndMs > endMs)) {\n end = item;\n endMs = candidateEndMs;\n }\n }\n\n return {\n start_time: start.created_at,\n end_time: end.closed_at || end.created_at,\n };\n};\n\n/**\n * @public\n */\nexport const provideOHLCService = (\n terminal: Terminal,\n metadata: IOHLCServiceMetadata,\n fetchPage: (request: IIngestOHLCRequest & { series_id: string }) => Promise<IOHLC[]>,\n serviceOptions?: IServiceOptions,\n) => {\n return terminal.server.provideService<IIngestOHLCRequest, ISeriesIngestResult>(\n 'IngestOHLC',\n {\n type: 'object',\n required: ['product_id', 'duration', 'direction', 'time'],\n properties: {\n product_id: { type: 'string', pattern: `^${metadata.product_id_prefix}` },\n duration: { type: 'string', enum: metadata.duration_list },\n direction: { const: metadata.direction },\n time: { type: 'number' },\n },\n },\n async (msg) => {\n try {\n const series_id = encodeOHLCSeriesId(msg.req.product_id, msg.req.duration);\n\n const items = await fetchPage({ ...msg.req, series_id });\n\n console.info(\n formatTime(Date.now()),\n 'IngestOHLC',\n msg.req.product_id,\n msg.req.duration,\n 'fetched',\n items.length,\n 'bars',\n );\n\n const normalized: IOHLC[] = items.map((x) => ({\n ...x,\n series_id,\n datasource_id: '',\n product_id: msg.req.product_id,\n duration: msg.req.duration,\n open_interest: x.open_interest ?? '0',\n }));\n\n const range = computeOHLCPageRange(normalized);\n\n // Atomic write: data rows + series_data_range in the same statement.\n if (normalized.length > 0 && range) {\n const writeOHLC = `${buildInsertManyIntoTableSQL(normalized, 'ohlc_v2', {\n columns: OHLC_INSERT_COLUMNS,\n conflictKeys: ['series_id', 'created_at'],\n })} RETURNING 1`;\n\n const writeRange = `${buildInsertManyIntoTableSQL(\n [\n {\n series_id,\n table_name: 'ohlc_v2',\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_ohlc AS (\n ${writeOHLC}\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, 'ohlc_v2', {\n columns: OHLC_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 console.error(formatTime(Date.now()), 'IngestOHLC error:', error);\n return { res: { code: 1, message } };\n }\n },\n serviceOptions,\n );\n};\n"]}
package/dist/quote.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"quote.js","sourceRoot":"","sources":["../src/quote.ts"],"names":[],"mappings":";;;;;;;;;;;AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAC9D,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAG3C,MAAM,eAAe,GAAG,eAAe,CAAC;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,aAAa,EAAE,QAAQ,CAAC,EAAE;QAC7D,UAAU,EAAE;YACV,IAAI,EAAE,QAAQ;YACd,QAAQ,EAAE,CAAC,aAAa,EAAE,QAAQ,CAAC;YACnC,UAAU,EAAE;gBACV,WAAW,EAAE;oBACX,IAAI,EAAE,QAAQ;oBACd,QAAQ,EAAE,CAAC,MAAM,EAAE,OAAO,CAAC;oBAC3B,UAAU,EAAE;wBACV,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,OAAO,EAAE;wBACxC,QAAQ,EAAE,EAAE,IAAI,EAAE,CAAC,QAAQ,EAAE,MAAM,CAAC,EAAE;wBACtC,KAAK,EAAE;4BACL,IAAI,EAAE,QAAQ;4BACd,QAAQ,EAAE,CAAC,MAAM,EAAE,SAAS,CAAC;4BAC7B,UAAU,EAAE;gCACV,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,QAAQ,EAAE;gCACzC,OAAO,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE;6BAC7C;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Q,CAAC,wBAAwB;gBAC3C,KAAK,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,IAAI,QAAQ,CAAC,iBAAiB,EAAE,EAAE;aACrE;YACD,MAAM,EAAE;gBACN,IAAI,EAAE,OAAO;gBACb,KAAK,EAAE,QAAQ,CAAC,MAAM,CAAC,IAAI,EAAE;aAC9B;SACF;KACF,EACD,KAAK,EAAE,GAAG,EAAE,EAAE;;QACZ,MAAM,IAAI,GAAG,MAAM,WAAW,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAExC,MAAM,MAAM,GAAuB,EAAE,CAAC;QACtC,KAAK,MAAM,KAAK,IAAI,IAAI,EAAE;YACxB,MAAM,EAAE,UAAU,EAAE,UAAU,KAAgB,KAAK,EAAhB,MAAM,UAAK,KAAK,EAA7C,4BAAqC,CAAQ,CAAC;YACpD,MAAA,MAAM,CAAC,UAAU,qCAAjB,MAAM,CAAC,UAAU,IAAM,EAAE,EAAC;YAC1B,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,MAAM,CAAQ,EAAE;gBAC5C,MAAM,KAAK,GAAI,MAAc,CAAC,GAAG,CAAC,CAAC;gBACnC,IAAI,KAAK,KAAK,SAAS,EAAE;oBACvB,MAAM,CAAC,UAAU,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;iBAC/C;aACF;SACF;QAED,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,CAAC;IAC3D,CAAC,EACD,cAAc,CACf,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import { IQuote } from '@yuants/data-quote';\nimport { IServiceOptions, Terminal } from '@yuants/protocol';\nimport { createValidator } from '@yuants/protocol/lib/schema';\nimport { newError } from '../../utils/lib';\nimport { IQuoteField, IQuoteServiceMetadata, IQuoteServiceRequestByVEX, IQuoteUpdateAction } from './types';\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_ids', 'fields'] },\n properties: {\n type: 'object',\n required: ['product_ids', 'fields'],\n properties: {\n product_ids: {\n type: 'object',\n required: ['type', 'items'],\n properties: {\n type: { type: 'string', const: 'array' },\n maxItems: { type: ['number', 'null'] },\n items: {\n type: 'object',\n required: ['type', 'pattern'],\n properties: {\n type: { type: 'string', const: 'string' },\n pattern: { type: 'string', pattern: '^\\\\^' },\n },\n },\n },\n },\n fields: {\n type: 'object',\n required: ['type', 'const'],\n properties: {\n type: { type: 'string', const: 'array' },\n const: { type: 'array', items: { type: 'string' } },\n },\n },\n },\n },\n },\n});\n\n/**\n * Extract Quote Service Metadata from JSON Schema\n * @public\n */\nexport const parseQuoteServiceMetadataFromSchema = (schema: any): IQuoteServiceMetadata => {\n if (!schema) throw newError('QUOTE_SERVICE_SCHEMA_MISSING', { schema });\n if (!schemaValidator(schema)) throw newError('QUOTE_SERVICE_SCHEMA_INVALID', { schema });\n\n return {\n product_id_prefix: schema.properties.product_ids.items.pattern.slice(1),\n fields: schema.properties.fields.const,\n max_products_per_request: schema.properties.product_ids.maxItems,\n };\n};\n\n/**\n * Provide Quote Service\n * @public\n */\nexport const provideQuoteService = <K extends IQuoteField>(\n terminal: Terminal,\n metadata: IQuoteServiceMetadata<K>,\n requestFunc: (\n request: IQuoteServiceRequestByVEX,\n ) => Promise<Array<Pick<IQuote, K> & { product_id: string; updated_at: number }>>,\n serviceOptions?: IServiceOptions,\n) => {\n return terminal.server.provideService<IQuoteServiceRequestByVEX, IQuoteUpdateAction>(\n 'GetQuotes',\n {\n type: 'object',\n required: ['product_ids', 'fields'],\n properties: {\n product_ids: {\n type: 'array',\n maxItems: metadata.max_products_per_request,\n items: { type: 'string', pattern: `^${metadata.product_id_prefix}` },\n },\n fields: {\n type: 'array',\n const: metadata.fields.sort(),\n },\n },\n },\n async (msg) => {\n const data = await requestFunc(msg.req);\n\n const action: IQuoteUpdateAction = {};\n for (const quote of data) {\n const { product_id, updated_at, ...fields } = quote;\n action[product_id] ??= {};\n for (const key of Object.keys(fields) as K[]) {\n const value = (fields as any)[key];\n if (value !== undefined) {\n action[product_id][key] = [value, updated_at];\n }\n }\n }\n\n return { res: { code: 0, message: 'OK', data: action } };\n },\n serviceOptions,\n );\n};\n"]}
1
+ {"version":3,"file":"quote.js","sourceRoot":"","sources":["../src/quote.ts"],"names":[],"mappings":";;;;;;;;;;;AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAC9D,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAG3C,MAAM,eAAe,GAAG,eAAe,CAAC;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,aAAa,EAAE,QAAQ,CAAC,EAAE;QAC7D,UAAU,EAAE;YACV,IAAI,EAAE,QAAQ;YACd,QAAQ,EAAE,CAAC,aAAa,EAAE,QAAQ,CAAC;YACnC,UAAU,EAAE;gBACV,WAAW,EAAE;oBACX,IAAI,EAAE,QAAQ;oBACd,QAAQ,EAAE,CAAC,MAAM,EAAE,OAAO,CAAC;oBAC3B,UAAU,EAAE;wBACV,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,OAAO,EAAE;wBACxC,QAAQ,EAAE,EAAE,IAAI,EAAE,CAAC,QAAQ,EAAE,MAAM,CAAC,EAAE;wBACtC,KAAK,EAAE;4BACL,IAAI,EAAE,QAAQ;4BACd,QAAQ,EAAE,CAAC,MAAM,EAAE,SAAS,CAAC;4BAC7B,UAAU,EAAE;gCACV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CAAC,CAAC","sourcesContent":["import { IQuote } from '@yuants/data-quote';\nimport { IServiceOptions, Terminal } from '@yuants/protocol';\nimport { createValidator } from '@yuants/protocol/lib/schema';\nimport { newError } from '../../utils/lib';\nimport { IQuoteField, IQuoteServiceMetadata, IQuoteServiceRequestByVEX, IQuoteUpdateAction } from './types';\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_ids', 'fields'] },\n properties: {\n type: 'object',\n required: ['product_ids', 'fields'],\n properties: {\n product_ids: {\n type: 'object',\n required: ['type', 'items'],\n properties: {\n type: { type: 'string', const: 'array' },\n maxItems: { type: ['number', 'null'] },\n items: {\n type: 'object',\n required: ['type', 'pattern'],\n properties: {\n type: { type: 'string', const: 'string' },\n pattern: { type: 'string', pattern: '^\\\\^' },\n },\n },\n },\n },\n fields: {\n type: 'object',\n required: ['type', 'const'],\n properties: {\n type: { type: 'string', const: 'array' },\n const: { type: 'array', items: { type: 'string' } },\n },\n },\n },\n },\n },\n});\n\n/**\n * Extract Quote Service Metadata from JSON Schema\n * @public\n */\nexport const parseQuoteServiceMetadataFromSchema = (schema: any): IQuoteServiceMetadata => {\n if (!schema) throw newError('QUOTE_SERVICE_SCHEMA_MISSING', { schema });\n if (!schemaValidator(schema)) throw newError('QUOTE_SERVICE_SCHEMA_INVALID', { schema });\n\n return {\n product_id_prefix: schema.properties.product_ids.items.pattern.slice(1),\n fields: schema.properties.fields.const,\n max_products_per_request: schema.properties.product_ids.maxItems,\n };\n};\n\n/**\n * Provide Quote Service\n * @public\n */\nexport const provideQuoteService = <K extends IQuoteField>(\n terminal: Terminal,\n metadata: IQuoteServiceMetadata<K>,\n requestFunc: (\n request: IQuoteServiceRequestByVEX,\n ) => Promise<Array<Pick<IQuote, K> & { product_id: string; updated_at: number }>>,\n serviceOptions?: IServiceOptions,\n) => {\n return terminal.server.provideService<IQuoteServiceRequestByVEX, IQuoteUpdateAction>(\n 'GetQuotes',\n {\n type: 'object',\n required: ['product_ids', 'fields'],\n properties: {\n product_ids: {\n type: 'array',\n maxItems: metadata.max_products_per_request,\n items: { type: 'string', pattern: `^${metadata.product_id_prefix}` },\n },\n fields: {\n type: 'array',\n const: metadata.fields.sort(),\n },\n },\n },\n async (msg) => {\n const data = await requestFunc(msg.req);\n\n const action: IQuoteUpdateAction = {};\n for (const quote of data) {\n const { product_id, updated_at, ...fields } = quote;\n action[product_id] ??= {};\n for (const key of Object.keys(fields) as K[]) {\n const value = (fields as any)[key];\n if (value !== undefined) {\n action[product_id][key] = [value, updated_at];\n }\n }\n }\n\n return { res: { code: 0, message: 'OK', data: action } };\n },\n serviceOptions,\n );\n};\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AACjD,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAChD,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,cAAc,SAAS,CAAC;AACxB,cAAc,QAAQ,CAAC;AACvB,cAAc,iBAAiB,CAAC;AAChC,cAAc,SAAS,CAAC;AAExB;;;;GAIG;AACH,MAAM,WAAW,SAAS,CAAC,CAAC,GAAG,GAAG;IAChC;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,gBAAgB,EAAE,WAAW,CAAC;IAE9B;;;;OAIG;IACH,eAAe,CAAC,UAAU,EAAE,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAEhD;;OAEG;IACH,YAAY,IAAI,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAC;IAEpC;;;;OAIG;IACH,YAAY,CAAC,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,UAAU,EAAE,KAAK,EAAE,CAAC,CAAC;AAClF,CAAC,CAAC;AANW,QAAA,WAAW,eAMtB;AAEF;;;;GAIG;AACI,MAAM,WAAW,GAAG,KAAK,EAC9B,QAAkB,EAClB,UAAwC,EACxC,KAAa,EACa,EAAE;IAC5B,OAAO,QAAQ,CAAC,MAAM,CAAC,kBAAkB,CAAC,aAAa,EAAE,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC,CAAC;AAClF,CAAC,CAAC;AANW,QAAA,WAAW,eAMtB;AAEF;;;;GAIG;AACI,MAAM,WAAW,GAAG,KAAK,EAC9B,QAAkB,EAClB,UAAwC,EACxC,KAAa,EACa,EAAE;IAC5B,OAAO,QAAQ,CAAC,MAAM,CAAC,kBAAkB,CAAC,aAAa,EAAE,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC,CAAC;AAClF,CAAC,CAAC;AANW,QAAA,WAAW,eAMtB","sourcesContent":["import { IPosition } from '@yuants/data-account';\nimport { IOrder } from '@yuants/data-order';\nimport { IProduct } from '@yuants/data-product';\nimport { IResponse, Terminal } from '@yuants/protocol';\nimport { JSONSchema7 } from 'json-schema';\nexport * from './quote';\nexport * from './ohlc';\nexport * from './interest_rate';\nexport * from './types';\n\n/**\n * Exchange Interface\n *\n * @public\n */\nexport interface IExchange<T = any> {\n /**\n * Exchange name / type (e.g. 'Binance', 'OKX')\n */\n name: string;\n\n /**\n * JSON Schema for the credential payload\n */\n credentialSchema: JSONSchema7;\n\n /**\n * Get credential ID from credential\n *\n * @param credential - The credential object\n */\n getCredentialId(credential: T): Promise<string>;\n\n /**\n * List all products\n */\n listProducts(): Promise<IProduct[]>;\n\n /**\n * Get all positions\n *\n * @param credential - The credential object\n */\n getPositions(credential: T): Promise<IPosition[]>;\n\n /**\n * Get all orders\n *\n * @param credential - The credential object\n */\n getOrders(credential: T): Promise<IOrder[]>;\n\n /**\n * Get positions by product_id\n *\n * @param credential - The credential object\n * @param product_id - The product ID\n */\n getPositionsByProductId(credential: T, product_id: string): Promise<IPosition[]>;\n\n /**\n * Get orders by product_id\n *\n * @param credential - The credential object\n * @param product_id - The product ID\n */\n getOrdersByProductId(credential: T, product_id: string): Promise<IOrder[]>;\n\n /**\n * Submit an order\n *\n * @param credential - The credential object\n * @param order - The order object\n */\n submitOrder(credential: T, order: IOrder): Promise<{ order_id: string }>;\n\n /**\n * Modify an order\n *\n * @param credential - The credential object\n * @param order - The order object\n */\n modifyOrder(credential: T, order: IOrder): Promise<void>;\n\n /**\n * Cancel an order\n *\n * @param credential - The credential object\n * @param order - The order object\n */\n cancelOrder(credential: T, order: IOrder): Promise<void>;\n}\n\nconst makeCredentialSchema = (type: string, payloadSchema: JSONSchema7): JSONSchema7 => {\n return {\n type: 'object',\n required: ['type', 'payload'],\n properties: {\n type: { type: 'string', const: type },\n payload: payloadSchema,\n },\n };\n};\n\n/**\n * Provide exchange services\n *\n * @public\n */\nexport const provideExchangeServices = <T>(terminal: Terminal, exchange: IExchange<T>) => {\n const { name: type, credentialSchema } = exchange;\n\n // GetCredentialId\n terminal.server.provideService<{ credential: { type: string; payload: T } }, string>(\n 'GetCredentialId',\n {\n type: 'object',\n required: ['credential'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n },\n },\n async (msg) => {\n const credentialId = await exchange.getCredentialId(msg.req.credential.payload);\n return { res: { code: 0, message: 'OK', data: credentialId } };\n },\n );\n\n // ListProducts\n terminal.server.provideService<void, IProduct[]>(\n 'ListProducts',\n {\n type: 'object',\n required: ['type'],\n properties: {\n type: { const: exchange.name },\n },\n },\n async () => {\n const products = await exchange.listProducts();\n return { res: { code: 0, message: 'OK', data: products } };\n },\n );\n\n // GetPositions\n terminal.server.provideService<\n { credential: { type: string; payload: T }; product_id?: string },\n IPosition[]\n >(\n 'GetPositions',\n {\n type: 'object',\n required: ['credential'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n product_id: { type: 'string' },\n },\n },\n async (msg) => {\n if (msg.req.product_id) {\n const positions = await exchange.getPositionsByProductId(\n msg.req.credential.payload,\n msg.req.product_id,\n );\n return { res: { code: 0, message: 'OK', data: positions } };\n }\n const positions = await exchange.getPositions(msg.req.credential.payload);\n return { res: { code: 0, message: 'OK', data: positions } };\n },\n );\n\n // GetOrders\n terminal.server.provideService<{ credential: { type: string; payload: T }; product_id?: string }, IOrder[]>(\n 'GetOrders',\n {\n type: 'object',\n required: ['credential'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n product_id: { type: 'string' },\n },\n },\n async (msg) => {\n if (msg.req.product_id) {\n const orders = await exchange.getOrdersByProductId(msg.req.credential.payload, msg.req.product_id);\n return { res: { code: 0, message: 'OK', data: orders } };\n }\n const orders = await exchange.getOrders(msg.req.credential.payload);\n return { res: { code: 0, message: 'OK', data: orders } };\n },\n );\n\n // SubmitOrder\n terminal.server.provideService<\n { credential: { type: string; payload: T }; order: IOrder },\n { order_id: string }\n >(\n 'SubmitOrder',\n {\n type: 'object',\n required: ['credential', 'order'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n order: { type: 'object' },\n },\n },\n async (msg) => {\n const result = await exchange.submitOrder(msg.req.credential.payload, msg.req.order);\n return { res: { code: 0, message: 'OK', data: result } };\n },\n );\n\n // ModifyOrder\n terminal.server.provideService<{ credential: { type: string; payload: T }; order: IOrder }, void>(\n 'ModifyOrder',\n {\n type: 'object',\n required: ['credential', 'order'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n order: { type: 'object' },\n },\n },\n async (msg) => {\n await exchange.modifyOrder(msg.req.credential.payload, msg.req.order);\n return { res: { code: 0, message: 'OK' } };\n },\n );\n\n // CancelOrder\n terminal.server.provideService<{ credential: { type: string; payload: T }; order: IOrder }, void>(\n 'CancelOrder',\n {\n type: 'object',\n required: ['credential', 'order'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n order: { type: 'object' },\n },\n },\n async (msg) => {\n await exchange.cancelOrder(msg.req.credential.payload, msg.req.order);\n return { res: { code: 0, message: 'OK' } };\n },\n );\n};\n\n/**\n * Get credential ID\n *\n * @public\n */\nexport const getCredentialId = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n): Promise<IResponse<string>> => {\n return terminal.client.requestForResponse('GetCredentialId', { credential });\n};\n\n/**\n * List products\n *\n * @public\n */\nexport const listProducts = async (terminal: Terminal, type: string): Promise<IResponse<IProduct[]>> => {\n return terminal.client.requestForResponse('ListProducts', { type });\n};\n\n/**\n * Get positions\n *\n * @public\n */\nexport const getPositions = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n product_id?: string,\n): Promise<IResponse<IPosition[]>> => {\n return terminal.client.requestForResponse('GetPositions', { credential, product_id });\n};\n\n/**\n * Get orders\n *\n * @public\n */\nexport const getOrders = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n product_id?: string,\n): Promise<IResponse<IOrder[]>> => {\n return terminal.client.requestForResponse('GetOrders', { credential, product_id });\n};\n\n/**\n * Submit order\n *\n * @public\n */\nexport const submitOrder = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n order: IOrder,\n): Promise<IResponse<{ order_id: string }>> => {\n return terminal.client.requestForResponse('SubmitOrder', { credential, order });\n};\n\n/**\n * Modify order\n *\n * @public\n */\nexport const modifyOrder = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n order: IOrder,\n): Promise<IResponse<void>> => {\n return terminal.client.requestForResponse('ModifyOrder', { credential, order });\n};\n\n/**\n * Cancel order\n *\n * @public\n */\nexport const cancelOrder = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n order: IOrder,\n): Promise<IResponse<void>> => {\n return terminal.client.requestForResponse('CancelOrder', { credential, order });\n};\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAKA,0CAAwB;AACxB,yCAAuB;AACvB,kDAAgC;AAChC,0CAAwB;AAqFxB,MAAM,oBAAoB,GAAG,CAAC,IAAY,EAAE,aA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ACxC,KAAa,EACa,EAAE;IAC5B,OAAO,QAAQ,CAAC,MAAM,CAAC,kBAAkB,CAAC,aAAa,EAAE,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC,CAAC;AAClF,CAAC,CAAC;AANW,QAAA,WAAW,eAMtB;AAEF;;;;GAIG;AACI,MAAM,WAAW,GAAG,KAAK,EAC9B,QAAkB,EAClB,UAAwC,EACxC,KAAa,EACa,EAAE;IAC5B,OAAO,QAAQ,CAAC,MAAM,CAAC,kBAAkB,CAAC,aAAa,EAAE,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC,CAAC;AAClF,CAAC,CAAC;AANW,QAAA,WAAW,eAMtB","sourcesContent":["import { IPosition } from '@yuants/data-account';\nimport { IOrder } from '@yuants/data-order';\nimport { IProduct } from '@yuants/data-product';\nimport { IResponse, Terminal } from '@yuants/protocol';\nimport { JSONSchema7 } from 'json-schema';\nexport * from './quote';\nexport * from './ohlc';\nexport * from './interest_rate';\nexport * from './types';\n\n/**\n * Exchange Interface\n *\n * @public\n */\nexport interface IExchange<T = any> {\n /**\n * Exchange name / type (e.g. 'Binance', 'OKX')\n */\n name: string;\n\n /**\n * JSON Schema for the credential payload\n */\n credentialSchema: JSONSchema7;\n\n /**\n * Get credential ID from credential\n *\n * @param credential - The credential object\n */\n getCredentialId(credential: T): Promise<string>;\n\n /**\n * List all products\n */\n listProducts(): Promise<IProduct[]>;\n\n /**\n * Get all positions\n *\n * @param credential - The credential object\n */\n getPositions(credential: T): Promise<IPosition[]>;\n\n /**\n * Get all orders\n *\n * @param credential - The credential object\n */\n getOrders(credential: T): Promise<IOrder[]>;\n\n /**\n * Get positions by product_id\n *\n * @param credential - The credential object\n * @param product_id - The product ID\n */\n getPositionsByProductId(credential: T, product_id: string): Promise<IPosition[]>;\n\n /**\n * Get orders by product_id\n *\n * @param credential - The credential object\n * @param product_id - The product ID\n */\n getOrdersByProductId(credential: T, product_id: string): Promise<IOrder[]>;\n\n /**\n * Submit an order\n *\n * @param credential - The credential object\n * @param order - The order object\n */\n submitOrder(credential: T, order: IOrder): Promise<{ order_id: string }>;\n\n /**\n * Modify an order\n *\n * @param credential - The credential object\n * @param order - The order object\n */\n modifyOrder(credential: T, order: IOrder): Promise<void>;\n\n /**\n * Cancel an order\n *\n * @param credential - The credential object\n * @param order - The order object\n */\n cancelOrder(credential: T, order: IOrder): Promise<void>;\n}\n\nconst makeCredentialSchema = (type: string, payloadSchema: JSONSchema7): JSONSchema7 => {\n return {\n type: 'object',\n required: ['type', 'payload'],\n properties: {\n type: { type: 'string', const: type },\n payload: payloadSchema,\n },\n };\n};\n\n/**\n * Provide exchange services\n *\n * @public\n */\nexport const provideExchangeServices = <T>(terminal: Terminal, exchange: IExchange<T>) => {\n const { name: type, credentialSchema } = exchange;\n\n // GetCredentialId\n terminal.server.provideService<{ credential: { type: string; payload: T } }, string>(\n 'GetCredentialId',\n {\n type: 'object',\n required: ['credential'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n },\n },\n async (msg) => {\n const credentialId = await exchange.getCredentialId(msg.req.credential.payload);\n return { res: { code: 0, message: 'OK', data: credentialId } };\n },\n );\n\n // ListProducts\n terminal.server.provideService<void, IProduct[]>(\n 'ListProducts',\n {\n type: 'object',\n required: ['type'],\n properties: {\n type: { const: exchange.name },\n },\n },\n async () => {\n const products = await exchange.listProducts();\n return { res: { code: 0, message: 'OK', data: products } };\n },\n );\n\n // GetPositions\n terminal.server.provideService<\n { credential: { type: string; payload: T }; product_id?: string },\n IPosition[]\n >(\n 'GetPositions',\n {\n type: 'object',\n required: ['credential'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n product_id: { type: 'string' },\n },\n },\n async (msg) => {\n if (msg.req.product_id) {\n const positions = await exchange.getPositionsByProductId(\n msg.req.credential.payload,\n msg.req.product_id,\n );\n return { res: { code: 0, message: 'OK', data: positions } };\n }\n const positions = await exchange.getPositions(msg.req.credential.payload);\n return { res: { code: 0, message: 'OK', data: positions } };\n },\n );\n\n // GetOrders\n terminal.server.provideService<{ credential: { type: string; payload: T }; product_id?: string }, IOrder[]>(\n 'GetOrders',\n {\n type: 'object',\n required: ['credential'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n product_id: { type: 'string' },\n },\n },\n async (msg) => {\n if (msg.req.product_id) {\n const orders = await exchange.getOrdersByProductId(msg.req.credential.payload, msg.req.product_id);\n return { res: { code: 0, message: 'OK', data: orders } };\n }\n const orders = await exchange.getOrders(msg.req.credential.payload);\n return { res: { code: 0, message: 'OK', data: orders } };\n },\n );\n\n // SubmitOrder\n terminal.server.provideService<\n { credential: { type: string; payload: T }; order: IOrder },\n { order_id: string }\n >(\n 'SubmitOrder',\n {\n type: 'object',\n required: ['credential', 'order'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n order: { type: 'object' },\n },\n },\n async (msg) => {\n const result = await exchange.submitOrder(msg.req.credential.payload, msg.req.order);\n return { res: { code: 0, message: 'OK', data: result } };\n },\n );\n\n // ModifyOrder\n terminal.server.provideService<{ credential: { type: string; payload: T }; order: IOrder }, void>(\n 'ModifyOrder',\n {\n type: 'object',\n required: ['credential', 'order'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n order: { type: 'object' },\n },\n },\n async (msg) => {\n await exchange.modifyOrder(msg.req.credential.payload, msg.req.order);\n return { res: { code: 0, message: 'OK' } };\n },\n );\n\n // CancelOrder\n terminal.server.provideService<{ credential: { type: string; payload: T }; order: IOrder }, void>(\n 'CancelOrder',\n {\n type: 'object',\n required: ['credential', 'order'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n order: { type: 'object' },\n },\n },\n async (msg) => {\n await exchange.cancelOrder(msg.req.credential.payload, msg.req.order);\n return { res: { code: 0, message: 'OK' } };\n },\n );\n};\n\n/**\n * Get credential ID\n *\n * @public\n */\nexport const getCredentialId = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n): Promise<IResponse<string>> => {\n return terminal.client.requestForResponse('GetCredentialId', { credential });\n};\n\n/**\n * List products\n *\n * @public\n */\nexport const listProducts = async (terminal: Terminal, type: string): Promise<IResponse<IProduct[]>> => {\n return terminal.client.requestForResponse('ListProducts', { type });\n};\n\n/**\n * Get positions\n *\n * @public\n */\nexport const getPositions = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n product_id?: string,\n): Promise<IResponse<IPosition[]>> => {\n return terminal.client.requestForResponse('GetPositions', { credential, product_id });\n};\n\n/**\n * Get orders\n *\n * @public\n */\nexport const getOrders = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n product_id?: string,\n): Promise<IResponse<IOrder[]>> => {\n return terminal.client.requestForResponse('GetOrders', { credential, product_id });\n};\n\n/**\n * Submit order\n *\n * @public\n */\nexport const submitOrder = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n order: IOrder,\n): Promise<IResponse<{ order_id: string }>> => {\n return terminal.client.requestForResponse('SubmitOrder', { credential, order });\n};\n\n/**\n * Modify order\n *\n * @public\n */\nexport const modifyOrder = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n order: IOrder,\n): Promise<IResponse<void>> => {\n return terminal.client.requestForResponse('ModifyOrder', { credential, order });\n};\n\n/**\n * Cancel order\n *\n * @public\n */\nexport const cancelOrder = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n order: IOrder,\n): Promise<IResponse<void>> => {\n return terminal.client.requestForResponse('CancelOrder', { credential, order });\n};\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"interest_rate.d.ts","sourceRoot":"","sources":["../src/interest_rate.ts"],"names":[],"mappings":"AAAA,OAAO,EAA8B,aAAa,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AACxG,OAAO,EAAE,eAAe,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAI7D,OAAO,EAAuB,oBAAoB,EAAE,MAAM,SAAS,CAAC;AAGpE;;;GAGG;AACH,MAAM,WAAW,4BAA4B;IAC3C,iBAAiB,EAAE,MAAM,CAA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;CACjC;AAED;;;GAGG;AACH,MAAM,WAAW,0BAA0B;IACzC,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,oBAAoB,CAAC;IAChC,IAAI,EAAE,MAAM,CAAC;CACd;AAuCD;;GAEG;AACH,eAAO,MAAM,0CAA0C,GAAI,QAAQ,GAAG,KAAG,4BAOxE,CAAC;AAqCF;;GAEG;AACH,eAAO,MAAM,0BAA0B,GACrC,UAAU,QAAQ,EAClB,UAAU,4BAA4B,EACtC,WAAW,CAAC,OAAO,EAAE,0BAA0B,GAAG;IAAE,SAAS,EAAE,MAAM,CAAA;CAAE,KAAK,OAAO,CAAC,aAAa,EAAE,CAAC,EACpG,iBAAiB,eAAe;;CAkFjC,CAAC;AACF,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,WAAW,EAAE,MAAM,CAAC;CACrB;AAWD;;GAEG;AACH,eAAO,MAAM,4BAA4B,GACvC,UAAU,QAAQ,EAClB,UAAU;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,EAC7C,WAAW,CAAC,OAAO,EAAE,4BAA4B,KAAK,OAAO,CAAC,eAAe,EAAE,CAAC,EAChF,iBAAiB,eAAe;;CAuFjC,CAAC"}
@@ -145,7 +145,7 @@ const provideInterestRateService = (terminal, metadata, fetchPage, serviceOption
145
145
  }, serviceOptions);
146
146
  };
147
147
  exports.provideInterestRateService = provideInterestRateService;
148
- const INTEREST_RATE_LEDGER_INSERT_COLUMNS = [
148
+ const ACCOUNT_INTEREST_LEDGER_INSERT_COLUMNS = [
149
149
  'created_at',
150
150
  'product_id',
151
151
  'account_id',
@@ -176,18 +176,18 @@ const provideInterestLedgerService = (terminal, metadata, fetchPage, serviceOpti
176
176
  },
177
177
  }, async (msg) => {
178
178
  try {
179
- const interestRateLedgers = await fetchPage(Object.assign({}, msg.req));
180
- const range = computeInterestRatePageRange(interestRateLedgers);
179
+ const accountInterestLedgers = await fetchPage(Object.assign({}, msg.req));
180
+ const range = computeInterestRatePageRange(accountInterestLedgers);
181
181
  // Atomic write: data rows + series_data_range in the same statement.
182
- if (interestRateLedgers.length > 0 && range) {
183
- const writeInterestRate = `${(0, sql_1.buildInsertManyIntoTableSQL)(interestRateLedgers, 'interest_rate_ledger', {
184
- columns: INTEREST_RATE_LEDGER_INSERT_COLUMNS,
182
+ if (accountInterestLedgers.length > 0 && range) {
183
+ const writeInterestRate = `${(0, sql_1.buildInsertManyIntoTableSQL)(accountInterestLedgers, 'account_interest_ledger', {
184
+ columns: ACCOUNT_INTEREST_LEDGER_INSERT_COLUMNS,
185
185
  conflictKeys: ['id'],
186
186
  })} RETURNING 1`;
187
187
  const writeRange = `${(0, sql_1.buildInsertManyIntoTableSQL)([
188
188
  {
189
189
  series_id: msg.req.account_id,
190
- table_name: 'interest_rate_ledger',
190
+ table_name: 'account_interest_ledger',
191
191
  start_time: range.start_time,
192
192
  end_time: range.end_time,
193
193
  },
@@ -197,7 +197,7 @@ const provideInterestLedgerService = (terminal, metadata, fetchPage, serviceOpti
197
197
  })} RETURNING 1`;
198
198
  await (0, sql_1.requestSQL)(terminal, `
199
199
  WITH
200
- write_interest_rate_ledger AS (
200
+ write_account_interest_ledger AS (
201
201
  ${writeInterestRate}
202
202
  ),
203
203
  write_range AS (
@@ -206,13 +206,15 @@ const provideInterestLedgerService = (terminal, metadata, fetchPage, serviceOpti
206
206
  SELECT 1 as ok;
207
207
  `);
208
208
  }
209
- else if (interestRateLedgers.length > 0) {
210
- await (0, sql_1.requestSQL)(terminal, (0, sql_1.buildInsertManyIntoTableSQL)(interestRateLedgers, 'interest_rate_ledger', {
211
- columns: INTEREST_RATE_LEDGER_INSERT_COLUMNS,
209
+ else if (accountInterestLedgers.length > 0) {
210
+ await (0, sql_1.requestSQL)(terminal, (0, sql_1.buildInsertManyIntoTableSQL)(accountInterestLedgers, 'account_interest_ledger', {
211
+ columns: ACCOUNT_INTEREST_LEDGER_INSERT_COLUMNS,
212
212
  conflictKeys: ['id'],
213
213
  }));
214
214
  }
215
- return { res: { code: 0, message: 'OK', data: { wrote_count: interestRateLedgers.length, range } } };
215
+ return {
216
+ res: { code: 0, message: 'OK', data: { wrote_count: accountInterestLedgers.length, range } },
217
+ };
216
218
  }
217
219
  catch (error) {
218
220
  const message = error instanceof Error ? error.message : `${error}`;
@@ -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,S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c,CAAC;gBAEhB,MAAM,IAAA,gBAAU,EACd,QAAQ,EACR;;;kBAGM,iBAAiB;;;kBAGjB,UAAU;;;aAGf,CACF,CAAC;aACH;iBAAM,IAAI,mBAAmB,CAAC,MAAM,GAAG,CAAC,EAAE;gBACzC,MAAM,IAAA,gBAAU,EACd,QAAQ,EACR,IAAA,iCAA2B,EAAC,mBAAmB,EAAE,sBAAsB,EAAE;oBACvE,OAAO,EAAE,mCAAmC;oBAC5C,YAAY,EAAE,CAAC,IAAI,CAAC;iBACrB,CAAC,CACH,CAAC;aACH;YAED,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,WAAW,EAAE,mBAAmB,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC;SACtG;QAAC,OAAO,KAAK,EAAE;YACd,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;SACtC;IACH,CAAC,EACD,cAAc,CACf,CAAC;AACJ,CAAC,CAAC;AAzFW,QAAA,4BAA4B,gCAyFvC","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 INTEREST_RATE_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 interestRateLedgers = await fetchPage({ ...msg.req });\n const range = computeInterestRatePageRange(interestRateLedgers);\n\n // Atomic write: data rows + series_data_range in the same statement.\n if (interestRateLedgers.length > 0 && range) {\n const writeInterestRate = `${buildInsertManyIntoTableSQL(\n interestRateLedgers,\n 'interest_rate_ledger',\n {\n columns: INTEREST_RATE_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: 'interest_rate_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_interest_rate_ledger AS (\n ${writeInterestRate}\n ),\n write_range AS (\n ${writeRange}\n )\n SELECT 1 as ok;\n `,\n );\n } else if (interestRateLedgers.length > 0) {\n await requestSQL(\n terminal,\n buildInsertManyIntoTableSQL(interestRateLedgers, 'interest_rate_ledger', {\n columns: INTEREST_RATE_LEDGER_INSERT_COLUMNS,\n conflictKeys: ['id'],\n }),\n );\n }\n\n return { res: { code: 0, message: 'OK', data: { wrote_count: interestRateLedgers.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};\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,I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E,KAAK,CAAC,QAAQ;qBACzB;iBACF,EACD,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,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"]}
package/lib/ohlc.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"ohlc.d.ts","sourceRoot":"","sources":["../src/ohlc.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAC9D,OAAO,EAAE,eAAe,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAI7D,OAAO,EAAuB,oBAAoB,EAAE,MAAM,SAAS,CAAC;AAEpE;;;GAGG;AACH,MAAM,WAAW,oBAAoB;IACnC,iBAAiB,EAAE,MAAM,CAAC;IAC1B,aAAa,EAAE,MAAM,EAAE,CAAC;IACxB,SAAS,EAAE,oBAAoB,CAAC;CACjC;AAED;;;GAGG;AACH,MAAM,WAAW,kBAAkB;IACjC,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,oBAAo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