@yuants/exchange 0.5.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,9 @@
1
1
  import { IOrder } from '@yuants/data-order';
2
2
  import { IPosition } from '@yuants/data-account';
3
3
  import { IProduct } from '@yuants/data-product';
4
+ import { IQuote } from '@yuants/data-quote';
4
5
  import { IResponse } from '@yuants/protocol';
6
+ import { IServiceOptions } from '@yuants/protocol';
5
7
  import { JSONSchema7 } from 'json-schema';
6
8
  import { Terminal } from '@yuants/protocol';
7
9
 
@@ -120,6 +122,53 @@ export declare interface IExchange<T = any> {
120
122
  cancelOrder(credential: T, order: IOrder): Promise<void>;
121
123
  }
122
124
 
125
+ /**
126
+ * Quote 字段类型,排除掉 product_id, updated_at, datasource_id 三个字段
127
+ * @public
128
+ */
129
+ export declare type IQuoteField = Exclude<keyof IQuote, 'product_id' | 'updated_at' | 'datasource_id'>;
130
+
131
+ /**
132
+ * Quote Service Metadata
133
+ * @public
134
+ */
135
+ export declare interface IQuoteServiceMetadata<K extends IQuoteField = IQuoteField> {
136
+ product_id_prefix: string;
137
+ fields: K[];
138
+ max_products_per_request?: number;
139
+ }
140
+
141
+ /**
142
+ * Quote Service Request from VEX to Vendor
143
+ * @public
144
+ */
145
+ export declare interface IQuoteServiceRequestByVEX {
146
+ product_ids: string[];
147
+ fields: IQuoteField[];
148
+ }
149
+
150
+ /**
151
+ * 用于批量更新的数据结构
152
+ * 结构为:
153
+ * product_id -\> field_name (keyof IQuote) -\> [value: string, updated_at: number]
154
+ * 这样设计的目的是为了减少更新的数据量,同时保留每个字段的更新时间
155
+ *
156
+ * 例如:
157
+ * ```json
158
+ * {
159
+ * "product_123": {
160
+ * "last_price": ["100.5", 1627890123456],
161
+ * "volume": ["1500", 1627890123456]
162
+ * },
163
+ * "product_456": {
164
+ * "last_price": ["200.0", 1627890123456]
165
+ * }
166
+ * }
167
+ * ```
168
+ * @public
169
+ */
170
+ export declare type IQuoteUpdateAction = Record<string, Partial<Record<IQuoteField, [value: string, updated_at: number]>>>;
171
+
123
172
  /**
124
173
  * List products
125
174
  *
@@ -137,6 +186,12 @@ export declare const modifyOrder: <T>(terminal: Terminal, credential: {
137
186
  payload: T;
138
187
  }, order: IOrder) => Promise<IResponse<void>>;
139
188
 
189
+ /**
190
+ * Extract Quote Service Metadata from JSON Schema
191
+ * @public
192
+ */
193
+ export declare const parseQuoteServiceMetadataFromSchema: (schema: any) => IQuoteServiceMetadata;
194
+
140
195
  /**
141
196
  * Provide exchange services
142
197
  *
@@ -144,6 +199,17 @@ export declare const modifyOrder: <T>(terminal: Terminal, credential: {
144
199
  */
145
200
  export declare const provideExchangeServices: <T>(terminal: Terminal, exchange: IExchange<T>) => void;
146
201
 
202
+ /**
203
+ * Provide Quote Service
204
+ * @public
205
+ */
206
+ export declare const provideQuoteService: <K extends IQuoteField>(terminal: Terminal, metadata: IQuoteServiceMetadata<K>, requestFunc: (request: IQuoteServiceRequestByVEX) => Promise<(Pick<IQuote, K> & {
207
+ product_id: string;
208
+ updated_at: number;
209
+ })[]>, serviceOptions?: IServiceOptions) => {
210
+ dispose: () => void;
211
+ };
212
+
147
213
  /**
148
214
  * Submit order
149
215
  *
package/dist/index.js CHANGED
@@ -1,6 +1,8 @@
1
1
  import { createCache } from '@yuants/cache';
2
2
  import { createClientProductCache } from '@yuants/data-product';
3
3
  import { escapeSQL, requestSQL } from '@yuants/sql';
4
+ export * from './quote';
5
+ export * from './types';
4
6
  const makeCredentialSchema = (type, payloadSchema) => {
5
7
  return {
6
8
  type: 'object',
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAG5C,OAAO,EAAE,wBAAwB,EAAY,MAAM,sBAAsB,CAAC;AAG1E,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE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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 { createCache } from '@yuants/cache';\nimport { IPosition } from '@yuants/data-account';\nimport { IOrder } from '@yuants/data-order';\nimport { createClientProductCache, IProduct } from '@yuants/data-product';\nimport { IQuote } from '@yuants/data-quote';\nimport { IResponse, Terminal } from '@yuants/protocol';\nimport { escapeSQL, requestSQL } from '@yuants/sql';\nimport { JSONSchema7 } from 'json-schema';\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 const quoteCache = createCache<IQuote>(\n async (product_id) => {\n const sql = `select * from quote where product_id = ${escapeSQL(product_id)}`;\n const [quote] = await requestSQL<IQuote[]>(terminal, sql);\n return quote;\n },\n { expire: 30_000 },\n );\n\n const productCache = createClientProductCache(terminal, { expire: 3600_000 });\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":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAG5C,OAAO,EAAE,wBAAwB,EAAY,MAAM,sBAAsB,CAAC;AAG1E,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEpD,cAAc,SAAS,CAAC;AACxB,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,MAAM,UAAU,GAAG,WAAW,CAC5B,KAAK,EAAE,UAAU,EAAE,EAAE;QACnB,MAAM,GAAG,GAAG,0CAA0C,SAAS,CAAC,UAAU,CAAC,EAAE,CAAC;QAC9E,MAAM,CAAC,KAAK,CAAC,GAAG,MAAM,UAAU,CAAW,QAAQ,EAAE,GAAG,CAAC,CAAC;QAC1D,OAAO,KAAK,CAAC;IACf,CAAC,EACD,EAAE,MA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ent":["import { createCache } from '@yuants/cache';\nimport { IPosition } from '@yuants/data-account';\nimport { IOrder } from '@yuants/data-order';\nimport { createClientProductCache, IProduct } from '@yuants/data-product';\nimport { IQuote } from '@yuants/data-quote';\nimport { IResponse, Terminal } from '@yuants/protocol';\nimport { escapeSQL, requestSQL } from '@yuants/sql';\nimport { JSONSchema7 } from 'json-schema';\nexport * from './quote';\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 const quoteCache = createCache<IQuote>(\n async (product_id) => {\n const sql = `select * from quote where product_id = ${escapeSQL(product_id)}`;\n const [quote] = await requestSQL<IQuote[]>(terminal, sql);\n return quote;\n },\n { expire: 30_000 },\n );\n\n const productCache = createClientProductCache(terminal, { expire: 3600_000 });\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"]}
package/dist/quote.js ADDED
@@ -0,0 +1,96 @@
1
+ var __rest = (this && this.__rest) || function (s, e) {
2
+ var t = {};
3
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
+ t[p] = s[p];
5
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
+ t[p[i]] = s[p[i]];
9
+ }
10
+ return t;
11
+ };
12
+ import { createValidator } from '@yuants/protocol/lib/schema';
13
+ import { newError } from '../../utils/lib';
14
+ const schemaValidator = createValidator({
15
+ type: 'object',
16
+ required: ['type', 'properties'],
17
+ properties: {
18
+ type: { type: 'string', const: 'object' },
19
+ required: { type: 'array', const: ['product_ids', 'fields'] },
20
+ properties: {
21
+ type: 'object',
22
+ required: ['product_ids', 'fields'],
23
+ properties: {
24
+ product_ids: {
25
+ type: 'object',
26
+ required: ['type', 'pattern'],
27
+ properties: {
28
+ type: { type: 'string', const: 'array' },
29
+ maxItems: { type: ['number', 'null'] },
30
+ pattern: { type: 'string', pattern: '^\\^' },
31
+ },
32
+ },
33
+ fields: {
34
+ type: 'object',
35
+ required: ['type', 'const'],
36
+ properties: {
37
+ type: { type: 'string', const: 'array' },
38
+ const: { type: 'array', items: { type: 'string' } },
39
+ },
40
+ },
41
+ },
42
+ },
43
+ },
44
+ });
45
+ /**
46
+ * Extract Quote Service Metadata from JSON Schema
47
+ * @public
48
+ */
49
+ export const parseQuoteServiceMetadataFromSchema = (schema) => {
50
+ if (!schema)
51
+ throw newError('QUOTE_SERVICE_SCHEMA_MISSING', { schema });
52
+ if (!schemaValidator(schema))
53
+ throw newError('QUOTE_SERVICE_SCHEMA_INVALID', { schema });
54
+ return {
55
+ product_id_prefix: schema.properties.product_ids.items.pattern.slice(1),
56
+ fields: schema.properties.fields.const,
57
+ max_products_per_request: schema.properties.product_ids.maxItems,
58
+ };
59
+ };
60
+ /**
61
+ * Provide Quote Service
62
+ * @public
63
+ */
64
+ export const provideQuoteService = (terminal, metadata, requestFunc, serviceOptions) => {
65
+ return terminal.server.provideService('GetQuotes', {
66
+ type: 'object',
67
+ required: ['product_ids', 'fields'],
68
+ properties: {
69
+ product_ids: {
70
+ type: 'array',
71
+ maxItems: metadata.max_products_per_request,
72
+ items: { type: 'string', pattern: `^${metadata.product_id_prefix}` },
73
+ },
74
+ fields: {
75
+ type: 'array',
76
+ const: metadata.fields.sort(),
77
+ },
78
+ },
79
+ }, async (msg) => {
80
+ var _a;
81
+ const data = await requestFunc(msg.req);
82
+ const action = {};
83
+ for (const quote of data) {
84
+ const { product_id, updated_at } = quote, fields = __rest(quote, ["product_id", "updated_at"]);
85
+ (_a = action[product_id]) !== null && _a !== void 0 ? _a : (action[product_id] = {});
86
+ for (const key of Object.keys(fields)) {
87
+ const value = fields[key];
88
+ if (value !== undefined) {
89
+ action[product_id][key] = [value, updated_at];
90
+ }
91
+ }
92
+ }
93
+ return { res: { code: 0, message: 'OK', data: action } };
94
+ }, serviceOptions);
95
+ };
96
+ //# sourceMappingURL=quote.js.map
@@ -0,0 +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,SAAS,CAAC;oBAC7B,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,OAAO,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE;qBAC7C;iBACF;gBACD,MAAM,EAAE;oBACN,IAAI,EAAE,QAAQ;oBACd,QAAQ,EAAE,C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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', 'pattern'],\n properties: {\n type: { type: 'string', const: 'array' },\n maxItems: { type: ['number', 'null'] },\n pattern: { type: 'string', pattern: '^\\\\^' },\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"]}
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import { IQuote } from '@yuants/data-quote';\n\n/**\n * Quote 字段类型,排除掉 product_id, updated_at, datasource_id 三个字段\n * @public\n */\nexport type IQuoteField = Exclude<keyof IQuote, 'product_id' | 'updated_at' | 'datasource_id'>;\n\n/**\n * Quote Service Metadata\n * @public\n */\nexport interface IQuoteServiceMetadata<K extends IQuoteField = IQuoteField> {\n product_id_prefix: string;\n fields: K[];\n max_products_per_request?: number;\n}\n\n/**\n * Quote Service Request from VEX to Vendor\n * @public\n */\nexport interface IQuoteServiceRequestByVEX {\n product_ids: string[];\n fields: IQuoteField[];\n}\n/**\n * 用于批量更新的数据结构\n * 结构为:\n * product_id -\\> field_name (keyof IQuote) -\\> [value: string, updated_at: number]\n * 这样设计的目的是为了减少更新的数据量,同时保留每个字段的更新时间\n *\n * 例如:\n * ```json\n * {\n * \"product_123\": {\n * \"last_price\": [\"100.5\", 1627890123456],\n * \"volume\": [\"1500\", 1627890123456]\n * },\n * \"product_456\": {\n * \"last_price\": [\"200.0\", 1627890123456]\n * }\n * }\n * ```\n * @public\n */\nexport type IQuoteUpdateAction = Record<\n string,\n Partial<Record<IQuoteField, [value: string, updated_at: number]>>\n>;\n"]}
package/lib/index.d.ts CHANGED
@@ -3,6 +3,8 @@ import { IOrder } from '@yuants/data-order';
3
3
  import { IProduct } from '@yuants/data-product';
4
4
  import { IResponse, Terminal } from '@yuants/protocol';
5
5
  import { JSONSchema7 } from 'json-schema';
6
+ export * from './quote';
7
+ export * from './types';
6
8
  /**
7
9
  * Exchange Interface
8
10
  *
@@ -1 +1 @@
1
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(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
2
16
  Object.defineProperty(exports, "__esModule", { value: true });
3
17
  exports.cancelOrder = exports.modifyOrder = exports.submitOrder = exports.getOrders = exports.getPositions = exports.listProducts = exports.getCredentialId = exports.provideExchangeServices = void 0;
4
18
  const cache_1 = require("@yuants/cache");
5
19
  const data_product_1 = require("@yuants/data-product");
6
20
  const sql_1 = require("@yuants/sql");
21
+ __exportStar(require("./quote"), exports);
22
+ __exportStar(require("./types"), exports);
7
23
  const makeCredentialSchema = (type, payloadSchema) => {
8
24
  return {
9
25
  type: 'object',
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,yCAA4C;AAG5C,uDAA0E;AAG1E,qCAAoD;AAsFpD,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;AACI,MAAM,uBAAuB,GAAG,CAAI,QAAkB,EAAE,QAAsB,E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AW,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 { createCache } from '@yuants/cache';\nimport { IPosition } from '@yuants/data-account';\nimport { IOrder } from '@yuants/data-order';\nimport { createClientProductCache, IProduct } from '@yuants/data-product';\nimport { IQuote } from '@yuants/data-quote';\nimport { IResponse, Terminal } from '@yuants/protocol';\nimport { escapeSQL, requestSQL } from '@yuants/sql';\nimport { JSONSchema7 } from 'json-schema';\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 const quoteCache = createCache<IQuote>(\n async (product_id) => {\n const sql = `select * from quote where product_id = ${escapeSQL(product_id)}`;\n const [quote] = await requestSQL<IQuote[]>(terminal, sql);\n return quote;\n },\n { expire: 30_000 },\n );\n\n const productCache = createClientProductCache(terminal, { expire: 3600_000 });\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":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,gBAMvB;AAEF;;;;GAIG;AACI,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;AANW,QAAA,SAAS,aAMpB;AAEF;;;;GAIG;AACI,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;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 { createCache } from '@yuants/cache';\nimport { IPosition } from '@yuants/data-account';\nimport { IOrder } from '@yuants/data-order';\nimport { createClientProductCache, IProduct } from '@yuants/data-product';\nimport { IQuote } from '@yuants/data-quote';\nimport { IResponse, Terminal } from '@yuants/protocol';\nimport { escapeSQL, requestSQL } from '@yuants/sql';\nimport { JSONSchema7 } from 'json-schema';\nexport * from './quote';\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 const quoteCache = createCache<IQuote>(\n async (product_id) => {\n const sql = `select * from quote where product_id = ${escapeSQL(product_id)}`;\n const [quote] = await requestSQL<IQuote[]>(terminal, sql);\n return quote;\n },\n { expire: 30_000 },\n );\n\n const productCache = createClientProductCache(terminal, { expire: 3600_000 });\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"]}
package/lib/quote.d.ts ADDED
@@ -0,0 +1,19 @@
1
+ import { IQuote } from '@yuants/data-quote';
2
+ import { IServiceOptions, Terminal } from '@yuants/protocol';
3
+ import { IQuoteField, IQuoteServiceMetadata, IQuoteServiceRequestByVEX } from './types';
4
+ /**
5
+ * Extract Quote Service Metadata from JSON Schema
6
+ * @public
7
+ */
8
+ export declare const parseQuoteServiceMetadataFromSchema: (schema: any) => IQuoteServiceMetadata;
9
+ /**
10
+ * Provide Quote Service
11
+ * @public
12
+ */
13
+ export declare const provideQuoteService: <K extends IQuoteField>(terminal: Terminal, metadata: IQuoteServiceMetadata<K>, requestFunc: (request: IQuoteServiceRequestByVEX) => Promise<(Pick<IQuote, K> & {
14
+ product_id: string;
15
+ updated_at: number;
16
+ })[]>, serviceOptions?: IServiceOptions) => {
17
+ dispose: () => void;
18
+ };
19
+ //# sourceMappingURL=quote.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"quote.d.ts","sourceRoot":"","sources":["../src/quote.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAG7D,OAAO,EAAE,WAAW,EAAE,qBAAqB,EAAE,yBAAyB,EAAsB,MAAM,SAAS,CAAC;AAkC5G;;;GAGG;AACH,eAAO,MAAM,mCAAmC,WAAY,GAAG,KAAG,qBASjE,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,mBAAmB,oCACpB,QAAQ,6DAGP,yBAAyB;gBACe,MAAM;gBAAc,MAAM;wBAC5D,eAAe;;CAsCjC,CAAC"}
package/lib/quote.js ADDED
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ var __rest = (this && this.__rest) || function (s, e) {
3
+ var t = {};
4
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
5
+ t[p] = s[p];
6
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
7
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
8
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
9
+ t[p[i]] = s[p[i]];
10
+ }
11
+ return t;
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.provideQuoteService = exports.parseQuoteServiceMetadataFromSchema = void 0;
15
+ const schema_1 = require("@yuants/protocol/lib/schema");
16
+ const lib_1 = require("../../utils/lib");
17
+ const schemaValidator = (0, schema_1.createValidator)({
18
+ type: 'object',
19
+ required: ['type', 'properties'],
20
+ properties: {
21
+ type: { type: 'string', const: 'object' },
22
+ required: { type: 'array', const: ['product_ids', 'fields'] },
23
+ properties: {
24
+ type: 'object',
25
+ required: ['product_ids', 'fields'],
26
+ properties: {
27
+ product_ids: {
28
+ type: 'object',
29
+ required: ['type', 'pattern'],
30
+ properties: {
31
+ type: { type: 'string', const: 'array' },
32
+ maxItems: { type: ['number', 'null'] },
33
+ pattern: { type: 'string', pattern: '^\\^' },
34
+ },
35
+ },
36
+ fields: {
37
+ type: 'object',
38
+ required: ['type', 'const'],
39
+ properties: {
40
+ type: { type: 'string', const: 'array' },
41
+ const: { type: 'array', items: { type: 'string' } },
42
+ },
43
+ },
44
+ },
45
+ },
46
+ },
47
+ });
48
+ /**
49
+ * Extract Quote Service Metadata from JSON Schema
50
+ * @public
51
+ */
52
+ const parseQuoteServiceMetadataFromSchema = (schema) => {
53
+ if (!schema)
54
+ throw (0, lib_1.newError)('QUOTE_SERVICE_SCHEMA_MISSING', { schema });
55
+ if (!schemaValidator(schema))
56
+ throw (0, lib_1.newError)('QUOTE_SERVICE_SCHEMA_INVALID', { schema });
57
+ return {
58
+ product_id_prefix: schema.properties.product_ids.items.pattern.slice(1),
59
+ fields: schema.properties.fields.const,
60
+ max_products_per_request: schema.properties.product_ids.maxItems,
61
+ };
62
+ };
63
+ exports.parseQuoteServiceMetadataFromSchema = parseQuoteServiceMetadataFromSchema;
64
+ /**
65
+ * Provide Quote Service
66
+ * @public
67
+ */
68
+ const provideQuoteService = (terminal, metadata, requestFunc, serviceOptions) => {
69
+ return terminal.server.provideService('GetQuotes', {
70
+ type: 'object',
71
+ required: ['product_ids', 'fields'],
72
+ properties: {
73
+ product_ids: {
74
+ type: 'array',
75
+ maxItems: metadata.max_products_per_request,
76
+ items: { type: 'string', pattern: `^${metadata.product_id_prefix}` },
77
+ },
78
+ fields: {
79
+ type: 'array',
80
+ const: metadata.fields.sort(),
81
+ },
82
+ },
83
+ }, async (msg) => {
84
+ var _a;
85
+ const data = await requestFunc(msg.req);
86
+ const action = {};
87
+ for (const quote of data) {
88
+ const { product_id, updated_at } = quote, fields = __rest(quote, ["product_id", "updated_at"]);
89
+ (_a = action[product_id]) !== null && _a !== void 0 ? _a : (action[product_id] = {});
90
+ for (const key of Object.keys(fields)) {
91
+ const value = fields[key];
92
+ if (value !== undefined) {
93
+ action[product_id][key] = [value, updated_at];
94
+ }
95
+ }
96
+ }
97
+ return { res: { code: 0, message: 'OK', data: action } };
98
+ }, serviceOptions);
99
+ };
100
+ exports.provideQuoteService = provideQuoteService;
101
+ //# sourceMappingURL=quote.js.map
@@ -0,0 +1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AExC,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;AA5CW,QAAA,mBAAmB,uBA4C9B","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', 'pattern'],\n properties: {\n type: { type: 'string', const: 'array' },\n maxItems: { type: ['number', 'null'] },\n pattern: { type: 'string', pattern: '^\\\\^' },\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"]}
package/lib/types.d.ts ADDED
@@ -0,0 +1,45 @@
1
+ import { IQuote } from '@yuants/data-quote';
2
+ /**
3
+ * Quote 字段类型,排除掉 product_id, updated_at, datasource_id 三个字段
4
+ * @public
5
+ */
6
+ export declare type IQuoteField = Exclude<keyof IQuote, 'product_id' | 'updated_at' | 'datasource_id'>;
7
+ /**
8
+ * Quote Service Metadata
9
+ * @public
10
+ */
11
+ export interface IQuoteServiceMetadata<K extends IQuoteField = IQuoteField> {
12
+ product_id_prefix: string;
13
+ fields: K[];
14
+ max_products_per_request?: number;
15
+ }
16
+ /**
17
+ * Quote Service Request from VEX to Vendor
18
+ * @public
19
+ */
20
+ export interface IQuoteServiceRequestByVEX {
21
+ product_ids: string[];
22
+ fields: IQuoteField[];
23
+ }
24
+ /**
25
+ * 用于批量更新的数据结构
26
+ * 结构为:
27
+ * product_id -\> field_name (keyof IQuote) -\> [value: string, updated_at: number]
28
+ * 这样设计的目的是为了减少更新的数据量,同时保留每个字段的更新时间
29
+ *
30
+ * 例如:
31
+ * ```json
32
+ * {
33
+ * "product_123": {
34
+ * "last_price": ["100.5", 1627890123456],
35
+ * "volume": ["1500", 1627890123456]
36
+ * },
37
+ * "product_456": {
38
+ * "last_price": ["200.0", 1627890123456]
39
+ * }
40
+ * }
41
+ * ```
42
+ * @public
43
+ */
44
+ export declare type IQuoteUpdateAction = Record<string, Partial<Record<IQuoteField, [value: string, updated_at: number]>>>;
45
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C;;;GAGG;AACH,oBAAY,WAAW,GAAG,OAAO,CAAC,MAAM,MAAM,EAAE,YAAY,GAAG,YAAY,GAAG,eAAe,CAAC,CAAC;AAE/F;;;GAGG;AACH,MAAM,WAAW,qBAAqB,CAAC,CAAC,SAAS,WAAW,GAAG,WAAW;IACxE,iBAAiB,EAAE,MAAM,CAAC;IAC1B,MAAM,EAAE,CAAC,EAAE,CAAC;IACZ,wBAAwB,CAAC,EAAE,MAAM,CAAC;CACnC;AAED;;;GAGG;AACH,MAAM,WAAW,yBAAyB;IACxC,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,MAAM,EAAE,WAAW,EAAE,CAAC;CACvB;AACD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,oBAAY,kBAAkB,GAAG,MAAM,CACrC,MAAM,EACN,OAAO,CAAC,MAAM,CAAC,WAAW,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,CAAC,CAAC,CAAC,CAClE,CAAC"}
package/lib/types.js ADDED
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import { IQuote } from '@yuants/data-quote';\n\n/**\n * Quote 字段类型,排除掉 product_id, updated_at, datasource_id 三个字段\n * @public\n */\nexport type IQuoteField = Exclude<keyof IQuote, 'product_id' | 'updated_at' | 'datasource_id'>;\n\n/**\n * Quote Service Metadata\n * @public\n */\nexport interface IQuoteServiceMetadata<K extends IQuoteField = IQuoteField> {\n product_id_prefix: string;\n fields: K[];\n max_products_per_request?: number;\n}\n\n/**\n * Quote Service Request from VEX to Vendor\n * @public\n */\nexport interface IQuoteServiceRequestByVEX {\n product_ids: string[];\n fields: IQuoteField[];\n}\n/**\n * 用于批量更新的数据结构\n * 结构为:\n * product_id -\\> field_name (keyof IQuote) -\\> [value: string, updated_at: number]\n * 这样设计的目的是为了减少更新的数据量,同时保留每个字段的更新时间\n *\n * 例如:\n * ```json\n * {\n * \"product_123\": {\n * \"last_price\": [\"100.5\", 1627890123456],\n * \"volume\": [\"1500\", 1627890123456]\n * },\n * \"product_456\": {\n * \"last_price\": [\"200.0\", 1627890123456]\n * }\n * }\n * ```\n * @public\n */\nexport type IQuoteUpdateAction = Record<\n string,\n Partial<Record<IQuoteField, [value: string, updated_at: number]>>\n>;\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yuants/exchange",
3
- "version": "0.5.1",
3
+ "version": "0.6.0",
4
4
  "main": "lib/index.js",
5
5
  "module": "dist/index.js",
6
6
  "files": [
@@ -28,7 +28,7 @@
28
28
  "@yuants/data-account": "0.10.1",
29
29
  "@yuants/data-order": "0.7.0",
30
30
  "@yuants/data-product": "0.5.0",
31
- "@yuants/data-quote": "0.2.43",
31
+ "@yuants/data-quote": "0.3.0",
32
32
  "@yuants/cache": "0.3.3",
33
33
  "@yuants/sql": "0.9.30",
34
34
  "@yuants/utils": "0.14.0",
@@ -1049,6 +1049,307 @@
1049
1049
  ],
1050
1050
  "extendsTokenRanges": []
1051
1051
  },
1052
+ {
1053
+ "kind": "TypeAlias",
1054
+ "canonicalReference": "@yuants/exchange!IQuoteField:type",
1055
+ "docComment": "/**\n * Quote 字段类型,排除掉 product_id, updated_at, datasource_id 三个字段\n *\n * @public\n */\n",
1056
+ "excerptTokens": [
1057
+ {
1058
+ "kind": "Content",
1059
+ "text": "export declare type IQuoteField = "
1060
+ },
1061
+ {
1062
+ "kind": "Reference",
1063
+ "text": "Exclude",
1064
+ "canonicalReference": "!Exclude:type"
1065
+ },
1066
+ {
1067
+ "kind": "Content",
1068
+ "text": "<keyof "
1069
+ },
1070
+ {
1071
+ "kind": "Reference",
1072
+ "text": "IQuote",
1073
+ "canonicalReference": "@yuants/data-quote!IQuote:interface"
1074
+ },
1075
+ {
1076
+ "kind": "Content",
1077
+ "text": ", 'product_id' | 'updated_at' | 'datasource_id'>"
1078
+ },
1079
+ {
1080
+ "kind": "Content",
1081
+ "text": ";"
1082
+ }
1083
+ ],
1084
+ "releaseTag": "Public",
1085
+ "name": "IQuoteField",
1086
+ "typeTokenRange": {
1087
+ "startIndex": 1,
1088
+ "endIndex": 5
1089
+ }
1090
+ },
1091
+ {
1092
+ "kind": "Interface",
1093
+ "canonicalReference": "@yuants/exchange!IQuoteServiceMetadata:interface",
1094
+ "docComment": "/**\n * Quote Service Metadata\n *\n * @public\n */\n",
1095
+ "excerptTokens": [
1096
+ {
1097
+ "kind": "Content",
1098
+ "text": "export interface IQuoteServiceMetadata<K extends "
1099
+ },
1100
+ {
1101
+ "kind": "Reference",
1102
+ "text": "IQuoteField",
1103
+ "canonicalReference": "@yuants/exchange!IQuoteField:type"
1104
+ },
1105
+ {
1106
+ "kind": "Content",
1107
+ "text": " = "
1108
+ },
1109
+ {
1110
+ "kind": "Reference",
1111
+ "text": "IQuoteField",
1112
+ "canonicalReference": "@yuants/exchange!IQuoteField:type"
1113
+ },
1114
+ {
1115
+ "kind": "Content",
1116
+ "text": "> "
1117
+ }
1118
+ ],
1119
+ "releaseTag": "Public",
1120
+ "typeParameters": [
1121
+ {
1122
+ "typeParameterName": "K",
1123
+ "constraintTokenRange": {
1124
+ "startIndex": 1,
1125
+ "endIndex": 2
1126
+ },
1127
+ "defaultTypeTokenRange": {
1128
+ "startIndex": 3,
1129
+ "endIndex": 4
1130
+ }
1131
+ }
1132
+ ],
1133
+ "name": "IQuoteServiceMetadata",
1134
+ "preserveMemberOrder": false,
1135
+ "members": [
1136
+ {
1137
+ "kind": "PropertySignature",
1138
+ "canonicalReference": "@yuants/exchange!IQuoteServiceMetadata#fields:member",
1139
+ "docComment": "",
1140
+ "excerptTokens": [
1141
+ {
1142
+ "kind": "Content",
1143
+ "text": "fields: "
1144
+ },
1145
+ {
1146
+ "kind": "Content",
1147
+ "text": "K[]"
1148
+ },
1149
+ {
1150
+ "kind": "Content",
1151
+ "text": ";"
1152
+ }
1153
+ ],
1154
+ "isReadonly": false,
1155
+ "isOptional": false,
1156
+ "releaseTag": "Public",
1157
+ "name": "fields",
1158
+ "propertyTypeTokenRange": {
1159
+ "startIndex": 1,
1160
+ "endIndex": 2
1161
+ }
1162
+ },
1163
+ {
1164
+ "kind": "PropertySignature",
1165
+ "canonicalReference": "@yuants/exchange!IQuoteServiceMetadata#max_products_per_request:member",
1166
+ "docComment": "",
1167
+ "excerptTokens": [
1168
+ {
1169
+ "kind": "Content",
1170
+ "text": "max_products_per_request?: "
1171
+ },
1172
+ {
1173
+ "kind": "Content",
1174
+ "text": "number"
1175
+ },
1176
+ {
1177
+ "kind": "Content",
1178
+ "text": ";"
1179
+ }
1180
+ ],
1181
+ "isReadonly": false,
1182
+ "isOptional": true,
1183
+ "releaseTag": "Public",
1184
+ "name": "max_products_per_request",
1185
+ "propertyTypeTokenRange": {
1186
+ "startIndex": 1,
1187
+ "endIndex": 2
1188
+ }
1189
+ },
1190
+ {
1191
+ "kind": "PropertySignature",
1192
+ "canonicalReference": "@yuants/exchange!IQuoteServiceMetadata#product_id_prefix:member",
1193
+ "docComment": "",
1194
+ "excerptTokens": [
1195
+ {
1196
+ "kind": "Content",
1197
+ "text": "product_id_prefix: "
1198
+ },
1199
+ {
1200
+ "kind": "Content",
1201
+ "text": "string"
1202
+ },
1203
+ {
1204
+ "kind": "Content",
1205
+ "text": ";"
1206
+ }
1207
+ ],
1208
+ "isReadonly": false,
1209
+ "isOptional": false,
1210
+ "releaseTag": "Public",
1211
+ "name": "product_id_prefix",
1212
+ "propertyTypeTokenRange": {
1213
+ "startIndex": 1,
1214
+ "endIndex": 2
1215
+ }
1216
+ }
1217
+ ],
1218
+ "extendsTokenRanges": []
1219
+ },
1220
+ {
1221
+ "kind": "Interface",
1222
+ "canonicalReference": "@yuants/exchange!IQuoteServiceRequestByVEX:interface",
1223
+ "docComment": "/**\n * Quote Service Request from VEX to Vendor\n *\n * @public\n */\n",
1224
+ "excerptTokens": [
1225
+ {
1226
+ "kind": "Content",
1227
+ "text": "export interface IQuoteServiceRequestByVEX "
1228
+ }
1229
+ ],
1230
+ "releaseTag": "Public",
1231
+ "name": "IQuoteServiceRequestByVEX",
1232
+ "preserveMemberOrder": false,
1233
+ "members": [
1234
+ {
1235
+ "kind": "PropertySignature",
1236
+ "canonicalReference": "@yuants/exchange!IQuoteServiceRequestByVEX#fields:member",
1237
+ "docComment": "",
1238
+ "excerptTokens": [
1239
+ {
1240
+ "kind": "Content",
1241
+ "text": "fields: "
1242
+ },
1243
+ {
1244
+ "kind": "Reference",
1245
+ "text": "IQuoteField",
1246
+ "canonicalReference": "@yuants/exchange!IQuoteField:type"
1247
+ },
1248
+ {
1249
+ "kind": "Content",
1250
+ "text": "[]"
1251
+ },
1252
+ {
1253
+ "kind": "Content",
1254
+ "text": ";"
1255
+ }
1256
+ ],
1257
+ "isReadonly": false,
1258
+ "isOptional": false,
1259
+ "releaseTag": "Public",
1260
+ "name": "fields",
1261
+ "propertyTypeTokenRange": {
1262
+ "startIndex": 1,
1263
+ "endIndex": 3
1264
+ }
1265
+ },
1266
+ {
1267
+ "kind": "PropertySignature",
1268
+ "canonicalReference": "@yuants/exchange!IQuoteServiceRequestByVEX#product_ids:member",
1269
+ "docComment": "",
1270
+ "excerptTokens": [
1271
+ {
1272
+ "kind": "Content",
1273
+ "text": "product_ids: "
1274
+ },
1275
+ {
1276
+ "kind": "Content",
1277
+ "text": "string[]"
1278
+ },
1279
+ {
1280
+ "kind": "Content",
1281
+ "text": ";"
1282
+ }
1283
+ ],
1284
+ "isReadonly": false,
1285
+ "isOptional": false,
1286
+ "releaseTag": "Public",
1287
+ "name": "product_ids",
1288
+ "propertyTypeTokenRange": {
1289
+ "startIndex": 1,
1290
+ "endIndex": 2
1291
+ }
1292
+ }
1293
+ ],
1294
+ "extendsTokenRanges": []
1295
+ },
1296
+ {
1297
+ "kind": "TypeAlias",
1298
+ "canonicalReference": "@yuants/exchange!IQuoteUpdateAction:type",
1299
+ "docComment": "/**\n * 用于批量更新的数据结构 结构为: product_id -\\> field_name (keyof IQuote) -\\> [value: string, updated_at: number] 这样设计的目的是为了减少更新的数据量,同时保留每个字段的更新时间\n *\n * 例如:\n * ```json\n * {\n * \"product_123\": {\n * \"last_price\": [\"100.5\", 1627890123456],\n * \"volume\": [\"1500\", 1627890123456]\n * },\n * \"product_456\": {\n * \"last_price\": [\"200.0\", 1627890123456]\n * }\n * }\n * ```\n *\n * @public\n */\n",
1300
+ "excerptTokens": [
1301
+ {
1302
+ "kind": "Content",
1303
+ "text": "export declare type IQuoteUpdateAction = "
1304
+ },
1305
+ {
1306
+ "kind": "Reference",
1307
+ "text": "Record",
1308
+ "canonicalReference": "!Record:type"
1309
+ },
1310
+ {
1311
+ "kind": "Content",
1312
+ "text": "<string, "
1313
+ },
1314
+ {
1315
+ "kind": "Reference",
1316
+ "text": "Partial",
1317
+ "canonicalReference": "!Partial:type"
1318
+ },
1319
+ {
1320
+ "kind": "Content",
1321
+ "text": "<"
1322
+ },
1323
+ {
1324
+ "kind": "Reference",
1325
+ "text": "Record",
1326
+ "canonicalReference": "!Record:type"
1327
+ },
1328
+ {
1329
+ "kind": "Content",
1330
+ "text": "<"
1331
+ },
1332
+ {
1333
+ "kind": "Reference",
1334
+ "text": "IQuoteField",
1335
+ "canonicalReference": "@yuants/exchange!IQuoteField:type"
1336
+ },
1337
+ {
1338
+ "kind": "Content",
1339
+ "text": ", [value: string, updated_at: number]>>>"
1340
+ },
1341
+ {
1342
+ "kind": "Content",
1343
+ "text": ";"
1344
+ }
1345
+ ],
1346
+ "releaseTag": "Public",
1347
+ "name": "IQuoteUpdateAction",
1348
+ "typeTokenRange": {
1349
+ "startIndex": 1,
1350
+ "endIndex": 9
1351
+ }
1352
+ },
1052
1353
  {
1053
1354
  "kind": "Variable",
1054
1355
  "canonicalReference": "@yuants/exchange!listProducts:var",
@@ -1165,6 +1466,33 @@
1165
1466
  "endIndex": 10
1166
1467
  }
1167
1468
  },
1469
+ {
1470
+ "kind": "Variable",
1471
+ "canonicalReference": "@yuants/exchange!parseQuoteServiceMetadataFromSchema:var",
1472
+ "docComment": "/**\n * Extract Quote Service Metadata from JSON Schema\n *\n * @public\n */\n",
1473
+ "excerptTokens": [
1474
+ {
1475
+ "kind": "Content",
1476
+ "text": "parseQuoteServiceMetadataFromSchema: "
1477
+ },
1478
+ {
1479
+ "kind": "Content",
1480
+ "text": "(schema: any) => "
1481
+ },
1482
+ {
1483
+ "kind": "Reference",
1484
+ "text": "IQuoteServiceMetadata",
1485
+ "canonicalReference": "@yuants/exchange!IQuoteServiceMetadata:interface"
1486
+ }
1487
+ ],
1488
+ "isReadonly": true,
1489
+ "releaseTag": "Public",
1490
+ "name": "parseQuoteServiceMetadataFromSchema",
1491
+ "variableTypeTokenRange": {
1492
+ "startIndex": 1,
1493
+ "endIndex": 3
1494
+ }
1495
+ },
1168
1496
  {
1169
1497
  "kind": "Variable",
1170
1498
  "canonicalReference": "@yuants/exchange!provideExchangeServices:var",
@@ -1205,6 +1533,100 @@
1205
1533
  "endIndex": 6
1206
1534
  }
1207
1535
  },
1536
+ {
1537
+ "kind": "Variable",
1538
+ "canonicalReference": "@yuants/exchange!provideQuoteService:var",
1539
+ "docComment": "/**\n * Provide Quote Service\n *\n * @public\n */\n",
1540
+ "excerptTokens": [
1541
+ {
1542
+ "kind": "Content",
1543
+ "text": "provideQuoteService: "
1544
+ },
1545
+ {
1546
+ "kind": "Content",
1547
+ "text": "<K extends "
1548
+ },
1549
+ {
1550
+ "kind": "Reference",
1551
+ "text": "IQuoteField",
1552
+ "canonicalReference": "@yuants/exchange!IQuoteField:type"
1553
+ },
1554
+ {
1555
+ "kind": "Content",
1556
+ "text": ">(terminal: "
1557
+ },
1558
+ {
1559
+ "kind": "Reference",
1560
+ "text": "Terminal",
1561
+ "canonicalReference": "@yuants/protocol!Terminal:class"
1562
+ },
1563
+ {
1564
+ "kind": "Content",
1565
+ "text": ", metadata: "
1566
+ },
1567
+ {
1568
+ "kind": "Reference",
1569
+ "text": "IQuoteServiceMetadata",
1570
+ "canonicalReference": "@yuants/exchange!IQuoteServiceMetadata:interface"
1571
+ },
1572
+ {
1573
+ "kind": "Content",
1574
+ "text": "<K>, requestFunc: (request: "
1575
+ },
1576
+ {
1577
+ "kind": "Reference",
1578
+ "text": "IQuoteServiceRequestByVEX",
1579
+ "canonicalReference": "@yuants/exchange!IQuoteServiceRequestByVEX:interface"
1580
+ },
1581
+ {
1582
+ "kind": "Content",
1583
+ "text": ") => "
1584
+ },
1585
+ {
1586
+ "kind": "Reference",
1587
+ "text": "Promise",
1588
+ "canonicalReference": "!Promise:interface"
1589
+ },
1590
+ {
1591
+ "kind": "Content",
1592
+ "text": "<("
1593
+ },
1594
+ {
1595
+ "kind": "Reference",
1596
+ "text": "Pick",
1597
+ "canonicalReference": "!Pick:type"
1598
+ },
1599
+ {
1600
+ "kind": "Content",
1601
+ "text": "<"
1602
+ },
1603
+ {
1604
+ "kind": "Reference",
1605
+ "text": "IQuote",
1606
+ "canonicalReference": "@yuants/data-quote!IQuote:interface"
1607
+ },
1608
+ {
1609
+ "kind": "Content",
1610
+ "text": ", K> & {\n product_id: string;\n updated_at: number;\n})[]>, serviceOptions?: "
1611
+ },
1612
+ {
1613
+ "kind": "Reference",
1614
+ "text": "IServiceOptions",
1615
+ "canonicalReference": "@yuants/protocol!IServiceOptions:interface"
1616
+ },
1617
+ {
1618
+ "kind": "Content",
1619
+ "text": ") => {\n dispose: () => void;\n}"
1620
+ }
1621
+ ],
1622
+ "isReadonly": true,
1623
+ "releaseTag": "Public",
1624
+ "name": "provideQuoteService",
1625
+ "variableTypeTokenRange": {
1626
+ "startIndex": 1,
1627
+ "endIndex": 18
1628
+ }
1629
+ },
1208
1630
  {
1209
1631
  "kind": "Variable",
1210
1632
  "canonicalReference": "@yuants/exchange!submitOrder:var",
@@ -7,7 +7,9 @@
7
7
  import { IOrder } from '@yuants/data-order';
8
8
  import { IPosition } from '@yuants/data-account';
9
9
  import { IProduct } from '@yuants/data-product';
10
+ import { IQuote } from '@yuants/data-quote';
10
11
  import { IResponse } from '@yuants/protocol';
12
+ import { IServiceOptions } from '@yuants/protocol';
11
13
  import { JSONSchema7 } from 'json-schema';
12
14
  import { Terminal } from '@yuants/protocol';
13
15
 
@@ -52,6 +54,30 @@ export interface IExchange<T = any> {
52
54
  }>;
53
55
  }
54
56
 
57
+ // @public
58
+ export type IQuoteField = Exclude<keyof IQuote, 'product_id' | 'updated_at' | 'datasource_id'>;
59
+
60
+ // @public
61
+ export interface IQuoteServiceMetadata<K extends IQuoteField = IQuoteField> {
62
+ // (undocumented)
63
+ fields: K[];
64
+ // (undocumented)
65
+ max_products_per_request?: number;
66
+ // (undocumented)
67
+ product_id_prefix: string;
68
+ }
69
+
70
+ // @public
71
+ export interface IQuoteServiceRequestByVEX {
72
+ // (undocumented)
73
+ fields: IQuoteField[];
74
+ // (undocumented)
75
+ product_ids: string[];
76
+ }
77
+
78
+ // @public
79
+ export type IQuoteUpdateAction = Record<string, Partial<Record<IQuoteField, [value: string, updated_at: number]>>>;
80
+
55
81
  // @public
56
82
  export const listProducts: (terminal: Terminal, type: string) => Promise<IResponse<IProduct[]>>;
57
83
 
@@ -61,9 +87,20 @@ export const modifyOrder: <T>(terminal: Terminal, credential: {
61
87
  payload: T;
62
88
  }, order: IOrder) => Promise<IResponse<void>>;
63
89
 
90
+ // @public
91
+ export const parseQuoteServiceMetadataFromSchema: (schema: any) => IQuoteServiceMetadata;
92
+
64
93
  // @public
65
94
  export const provideExchangeServices: <T>(terminal: Terminal, exchange: IExchange<T>) => void;
66
95
 
96
+ // @public
97
+ export const provideQuoteService: <K extends IQuoteField>(terminal: Terminal, metadata: IQuoteServiceMetadata<K>, requestFunc: (request: IQuoteServiceRequestByVEX) => Promise<(Pick<IQuote, K> & {
98
+ product_id: string;
99
+ updated_at: number;
100
+ })[]>, serviceOptions?: IServiceOptions) => {
101
+ dispose: () => void;
102
+ };
103
+
67
104
  // @public
68
105
  export const submitOrder: <T>(terminal: Terminal, credential: {
69
106
  type: string;
@@ -1,20 +1,22 @@
1
1
  {
2
- "libraries/exchange/CHANGELOG.json": "4c5ced1e4f03daaccc059801bb3c4f03fbb6aeaa",
3
- "libraries/exchange/CHANGELOG.md": "c52000baaaed3ba1912e5c8de5c3d6761fb76fa9",
2
+ "libraries/exchange/CHANGELOG.json": "90ce36eb369dc6906186a7b5fc321fade1cdb99b",
3
+ "libraries/exchange/CHANGELOG.md": "ffe98fc0df331347b2e75c9075fdb8a97a42a229",
4
4
  "libraries/exchange/api-extractor.json": "62f4fd324425b9a235f0c117975967aab09ced0c",
5
5
  "libraries/exchange/config/jest.config.json": "4bb17bde3ee911163a3edb36a6eb71491d80b1bd",
6
6
  "libraries/exchange/config/rig.json": "f6c7b5537dc77a3170ba9f008bae3b6c3ee11956",
7
7
  "libraries/exchange/config/typescript.json": "854907e8a821f2050f6533368db160c649c25348",
8
- "libraries/exchange/etc/exchange.api.md": "bbd88b1f693916f855340a6af809250c3e22bf1c",
9
- "libraries/exchange/package.json": "4d2baafc631c5d50ed442e5b8e875038cfe088f5",
10
- "libraries/exchange/src/index.ts": "6a93d776eb1902c68b4d29d2fd2fb6ae20c45aff",
8
+ "libraries/exchange/etc/exchange.api.md": "ebba089fe78ffc5864dd07ad41ad27b8c29ac07c",
9
+ "libraries/exchange/package.json": "a0504951443943c11062a48b04dc5710fb4ec46b",
10
+ "libraries/exchange/src/index.ts": "b63a97684af91046cf6ee2df92a7561a9a088cb5",
11
+ "libraries/exchange/src/quote.ts": "4b4469c5cf454434423d3508f817a0d42169d884",
12
+ "libraries/exchange/src/types.ts": "523c219c9c3c67c8ed68ffed0d3498abfe68f8d6",
11
13
  "libraries/exchange/tsconfig.json": "22f94ca28b507f8ddcc21b9053158eefd3f726a9",
12
14
  "libraries/exchange/.rush/temp/shrinkwrap-deps.json": "f769a3dbf84c1bff16d82dcfcefa30369a27dd36",
13
15
  "libraries/protocol/temp/package-deps.json": "0bd43721e96039b52d7b59c834dc6df45cf75e3f",
14
16
  "libraries/data-account/temp/package-deps.json": "e6ba0067a1b68f17266564f15987936ab2672eb9",
15
17
  "libraries/data-order/temp/package-deps.json": "ccdc9819f254f37a3591bb37876a86c703df33a6",
16
18
  "libraries/data-product/temp/package-deps.json": "a03f08f30800d5fb52c5d019bda4f8e7ec04e344",
17
- "libraries/data-quote/temp/package-deps.json": "34d079eab44d2bf65e07b112ac2099c6e92a015e",
19
+ "libraries/data-quote/temp/package-deps.json": "d5fd89abda84b46f6c14925eda0c381c36b94dfe",
18
20
  "libraries/cache/temp/package-deps.json": "a4afa15e6462983f9d3735d31dc1ed8a683fb4dc",
19
21
  "libraries/sql/temp/package-deps.json": "4a9a7ec55f04b20459e664e81e76fa74b6c77b39",
20
22
  "libraries/utils/temp/package-deps.json": "6d58e9b325e8d16de8a878c32010f626b12a01da",