@yuants/exchange 0.0.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.
@@ -0,0 +1,159 @@
1
+ import { IOrder } from '@yuants/data-order';
2
+ import { IPosition } from '@yuants/data-account';
3
+ import { IProduct } from '@yuants/data-product';
4
+ import { IResponse } from '@yuants/protocol';
5
+ import { JSONSchema7 } from 'json-schema';
6
+ import { Terminal } from '@yuants/protocol';
7
+
8
+ /**
9
+ * Cancel order
10
+ *
11
+ * @public
12
+ */
13
+ export declare const cancelOrder: <T>(terminal: Terminal, credential: {
14
+ type: string;
15
+ payload: T;
16
+ }, order: IOrder) => Promise<IResponse<void>>;
17
+
18
+ /**
19
+ * Get credential ID
20
+ *
21
+ * @public
22
+ */
23
+ export declare const getCredentialId: <T>(terminal: Terminal, credential: {
24
+ type: string;
25
+ payload: T;
26
+ }) => Promise<IResponse<string>>;
27
+
28
+ /**
29
+ * Get orders
30
+ *
31
+ * @public
32
+ */
33
+ export declare const getOrders: <T>(terminal: Terminal, credential: {
34
+ type: string;
35
+ payload: T;
36
+ }, product_id?: string) => Promise<IResponse<IOrder[]>>;
37
+
38
+ /**
39
+ * Get positions
40
+ *
41
+ * @public
42
+ */
43
+ export declare const getPositions: <T>(terminal: Terminal, credential: {
44
+ type: string;
45
+ payload: T;
46
+ }, product_id?: string) => Promise<IResponse<IPosition[]>>;
47
+
48
+ /**
49
+ * Exchange Interface
50
+ *
51
+ * @public
52
+ */
53
+ export declare interface IExchange<T = any> {
54
+ /**
55
+ * Exchange name / type (e.g. 'Binance', 'OKX')
56
+ */
57
+ name: string;
58
+ /**
59
+ * JSON Schema for the credential payload
60
+ */
61
+ credentialSchema: JSONSchema7;
62
+ /**
63
+ * Get credential ID from credential
64
+ *
65
+ * @param credential - The credential object
66
+ */
67
+ getCredentialId(credential: T): Promise<string>;
68
+ /**
69
+ * List all products
70
+ */
71
+ listProducts(): Promise<IProduct[]>;
72
+ /**
73
+ * Get all positions
74
+ *
75
+ * @param credential - The credential object
76
+ */
77
+ getPositions(credential: T): Promise<IPosition[]>;
78
+ /**
79
+ * Get all orders
80
+ *
81
+ * @param credential - The credential object
82
+ */
83
+ getOrders(credential: T): Promise<IOrder[]>;
84
+ /**
85
+ * Get positions by product_id
86
+ *
87
+ * @param credential - The credential object
88
+ * @param product_id - The product ID
89
+ */
90
+ getPositionsByProductId(credential: T, product_id: string): Promise<IPosition[]>;
91
+ /**
92
+ * Get orders by product_id
93
+ *
94
+ * @param credential - The credential object
95
+ * @param product_id - The product ID
96
+ */
97
+ getOrdersByProductId(credential: T, product_id: string): Promise<IOrder[]>;
98
+ /**
99
+ * Submit an order
100
+ *
101
+ * @param credential - The credential object
102
+ * @param order - The order object
103
+ */
104
+ submitOrder(credential: T, order: IOrder): Promise<{
105
+ order_id: string;
106
+ }>;
107
+ /**
108
+ * Modify an order
109
+ *
110
+ * @param credential - The credential object
111
+ * @param order - The order object
112
+ */
113
+ modifyOrder(credential: T, order: IOrder): Promise<void>;
114
+ /**
115
+ * Cancel an order
116
+ *
117
+ * @param credential - The credential object
118
+ * @param order - The order object
119
+ */
120
+ cancelOrder(credential: T, order: IOrder): Promise<void>;
121
+ }
122
+
123
+ /**
124
+ * List products
125
+ *
126
+ * @public
127
+ */
128
+ export declare const listProducts: (terminal: Terminal) => Promise<IResponse<IProduct[]>>;
129
+
130
+ /**
131
+ * Modify order
132
+ *
133
+ * @public
134
+ */
135
+ export declare const modifyOrder: <T>(terminal: Terminal, credential: {
136
+ type: string;
137
+ payload: T;
138
+ }, order: IOrder) => Promise<IResponse<void>>;
139
+
140
+ /**
141
+ * Provide exchange services
142
+ *
143
+ * @public
144
+ */
145
+ export declare const provideExchangeServices: <T>(terminal: Terminal, exchange: IExchange<T>) => void;
146
+
147
+ /**
148
+ * Submit order
149
+ *
150
+ * @public
151
+ */
152
+ export declare const submitOrder: <T>(terminal: Terminal, credential: {
153
+ type: string;
154
+ payload: T;
155
+ }, order: IOrder) => Promise<IResponse<{
156
+ order_id: string;
157
+ }>>;
158
+
159
+ export { }
package/dist/index.js ADDED
@@ -0,0 +1,159 @@
1
+ const makeCredentialSchema = (type, payloadSchema) => {
2
+ return {
3
+ type: 'object',
4
+ required: ['type', 'payload'],
5
+ properties: {
6
+ type: { type: 'string', const: type },
7
+ payload: payloadSchema,
8
+ },
9
+ };
10
+ };
11
+ /**
12
+ * Provide exchange services
13
+ *
14
+ * @public
15
+ */
16
+ export const provideExchangeServices = (terminal, exchange) => {
17
+ const { name: type, credentialSchema } = exchange;
18
+ // GetCredentialId
19
+ terminal.server.provideService('GetCredentialId', {
20
+ type: 'object',
21
+ required: ['credential'],
22
+ properties: {
23
+ credential: makeCredentialSchema(type, credentialSchema),
24
+ },
25
+ }, async (msg) => {
26
+ const credentialId = await exchange.getCredentialId(msg.req.credential.payload);
27
+ return { res: { code: 0, message: 'OK', data: credentialId } };
28
+ });
29
+ // ListProducts
30
+ terminal.server.provideService('ListProducts', {}, async () => {
31
+ const products = await exchange.listProducts();
32
+ return { res: { code: 0, message: 'OK', data: products } };
33
+ });
34
+ // GetPositions
35
+ terminal.server.provideService('GetPositions', {
36
+ type: 'object',
37
+ required: ['credential'],
38
+ properties: {
39
+ credential: makeCredentialSchema(type, credentialSchema),
40
+ product_id: { type: 'string' },
41
+ },
42
+ }, async (msg) => {
43
+ if (msg.req.product_id) {
44
+ const positions = await exchange.getPositionsByProductId(msg.req.credential.payload, msg.req.product_id);
45
+ return { res: { code: 0, message: 'OK', data: positions } };
46
+ }
47
+ const positions = await exchange.getPositions(msg.req.credential.payload);
48
+ return { res: { code: 0, message: 'OK', data: positions } };
49
+ });
50
+ // GetOrders
51
+ terminal.server.provideService('GetOrders', {
52
+ type: 'object',
53
+ required: ['credential'],
54
+ properties: {
55
+ credential: makeCredentialSchema(type, credentialSchema),
56
+ product_id: { type: 'string' },
57
+ },
58
+ }, async (msg) => {
59
+ if (msg.req.product_id) {
60
+ const orders = await exchange.getOrdersByProductId(msg.req.credential.payload, msg.req.product_id);
61
+ return { res: { code: 0, message: 'OK', data: orders } };
62
+ }
63
+ const orders = await exchange.getOrders(msg.req.credential.payload);
64
+ return { res: { code: 0, message: 'OK', data: orders } };
65
+ });
66
+ // SubmitOrder
67
+ terminal.server.provideService('SubmitOrder', {
68
+ type: 'object',
69
+ required: ['credential', 'order'],
70
+ properties: {
71
+ credential: makeCredentialSchema(type, credentialSchema),
72
+ order: { type: 'object' },
73
+ },
74
+ }, async (msg) => {
75
+ const result = await exchange.submitOrder(msg.req.credential.payload, msg.req.order);
76
+ return { res: { code: 0, message: 'OK', data: result } };
77
+ });
78
+ // ModifyOrder
79
+ terminal.server.provideService('ModifyOrder', {
80
+ type: 'object',
81
+ required: ['credential', 'order'],
82
+ properties: {
83
+ credential: makeCredentialSchema(type, credentialSchema),
84
+ order: { type: 'object' },
85
+ },
86
+ }, async (msg) => {
87
+ await exchange.modifyOrder(msg.req.credential.payload, msg.req.order);
88
+ return { res: { code: 0, message: 'OK' } };
89
+ });
90
+ // CancelOrder
91
+ terminal.server.provideService('CancelOrder', {
92
+ type: 'object',
93
+ required: ['credential', 'order'],
94
+ properties: {
95
+ credential: makeCredentialSchema(type, credentialSchema),
96
+ order: { type: 'object' },
97
+ },
98
+ }, async (msg) => {
99
+ await exchange.cancelOrder(msg.req.credential.payload, msg.req.order);
100
+ return { res: { code: 0, message: 'OK' } };
101
+ });
102
+ };
103
+ /**
104
+ * Get credential ID
105
+ *
106
+ * @public
107
+ */
108
+ export const getCredentialId = async (terminal, credential) => {
109
+ return terminal.client.requestForResponse('GetCredentialId', { credential });
110
+ };
111
+ /**
112
+ * List products
113
+ *
114
+ * @public
115
+ */
116
+ export const listProducts = async (terminal) => {
117
+ return terminal.client.requestForResponse('ListProducts', {});
118
+ };
119
+ /**
120
+ * Get positions
121
+ *
122
+ * @public
123
+ */
124
+ export const getPositions = async (terminal, credential, product_id) => {
125
+ return terminal.client.requestForResponse('GetPositions', { credential, product_id });
126
+ };
127
+ /**
128
+ * Get orders
129
+ *
130
+ * @public
131
+ */
132
+ export const getOrders = async (terminal, credential, product_id) => {
133
+ return terminal.client.requestForResponse('GetOrders', { credential, product_id });
134
+ };
135
+ /**
136
+ * Submit order
137
+ *
138
+ * @public
139
+ */
140
+ export const submitOrder = async (terminal, credential, order) => {
141
+ return terminal.client.requestForResponse('SubmitOrder', { credential, order });
142
+ };
143
+ /**
144
+ * Modify order
145
+ *
146
+ * @public
147
+ */
148
+ export const modifyOrder = async (terminal, credential, order) => {
149
+ return terminal.client.requestForResponse('ModifyOrder', { credential, order });
150
+ };
151
+ /**
152
+ * Cancel order
153
+ *
154
+ * @public
155
+ */
156
+ export const cancelOrder = async (terminal, credential, order) => {
157
+ return terminal.client.requestForResponse('CancelOrder', { credential, order });
158
+ };
159
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAyFA,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,oBAA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,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';\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[]>('ListProducts', {}, async () => {\n const products = await exchange.listProducts();\n return { res: { code: 0, message: 'OK', data: products } };\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): Promise<IResponse<IProduct[]>> => {\n return terminal.client.requestForResponse('ListProducts', {});\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/index.d.ts ADDED
@@ -0,0 +1,148 @@
1
+ import { IPosition } from '@yuants/data-account';
2
+ import { IOrder } from '@yuants/data-order';
3
+ import { IProduct } from '@yuants/data-product';
4
+ import { IResponse, Terminal } from '@yuants/protocol';
5
+ import { JSONSchema7 } from 'json-schema';
6
+ /**
7
+ * Exchange Interface
8
+ *
9
+ * @public
10
+ */
11
+ export interface IExchange<T = any> {
12
+ /**
13
+ * Exchange name / type (e.g. 'Binance', 'OKX')
14
+ */
15
+ name: string;
16
+ /**
17
+ * JSON Schema for the credential payload
18
+ */
19
+ credentialSchema: JSONSchema7;
20
+ /**
21
+ * Get credential ID from credential
22
+ *
23
+ * @param credential - The credential object
24
+ */
25
+ getCredentialId(credential: T): Promise<string>;
26
+ /**
27
+ * List all products
28
+ */
29
+ listProducts(): Promise<IProduct[]>;
30
+ /**
31
+ * Get all positions
32
+ *
33
+ * @param credential - The credential object
34
+ */
35
+ getPositions(credential: T): Promise<IPosition[]>;
36
+ /**
37
+ * Get all orders
38
+ *
39
+ * @param credential - The credential object
40
+ */
41
+ getOrders(credential: T): Promise<IOrder[]>;
42
+ /**
43
+ * Get positions by product_id
44
+ *
45
+ * @param credential - The credential object
46
+ * @param product_id - The product ID
47
+ */
48
+ getPositionsByProductId(credential: T, product_id: string): Promise<IPosition[]>;
49
+ /**
50
+ * Get orders by product_id
51
+ *
52
+ * @param credential - The credential object
53
+ * @param product_id - The product ID
54
+ */
55
+ getOrdersByProductId(credential: T, product_id: string): Promise<IOrder[]>;
56
+ /**
57
+ * Submit an order
58
+ *
59
+ * @param credential - The credential object
60
+ * @param order - The order object
61
+ */
62
+ submitOrder(credential: T, order: IOrder): Promise<{
63
+ order_id: string;
64
+ }>;
65
+ /**
66
+ * Modify an order
67
+ *
68
+ * @param credential - The credential object
69
+ * @param order - The order object
70
+ */
71
+ modifyOrder(credential: T, order: IOrder): Promise<void>;
72
+ /**
73
+ * Cancel an order
74
+ *
75
+ * @param credential - The credential object
76
+ * @param order - The order object
77
+ */
78
+ cancelOrder(credential: T, order: IOrder): Promise<void>;
79
+ }
80
+ /**
81
+ * Provide exchange services
82
+ *
83
+ * @public
84
+ */
85
+ export declare const provideExchangeServices: <T>(terminal: Terminal, exchange: IExchange<T>) => void;
86
+ /**
87
+ * Get credential ID
88
+ *
89
+ * @public
90
+ */
91
+ export declare const getCredentialId: <T>(terminal: Terminal, credential: {
92
+ type: string;
93
+ payload: T;
94
+ }) => Promise<IResponse<string>>;
95
+ /**
96
+ * List products
97
+ *
98
+ * @public
99
+ */
100
+ export declare const listProducts: (terminal: Terminal) => Promise<IResponse<IProduct[]>>;
101
+ /**
102
+ * Get positions
103
+ *
104
+ * @public
105
+ */
106
+ export declare const getPositions: <T>(terminal: Terminal, credential: {
107
+ type: string;
108
+ payload: T;
109
+ }, product_id?: string) => Promise<IResponse<IPosition[]>>;
110
+ /**
111
+ * Get orders
112
+ *
113
+ * @public
114
+ */
115
+ export declare const getOrders: <T>(terminal: Terminal, credential: {
116
+ type: string;
117
+ payload: T;
118
+ }, product_id?: string) => Promise<IResponse<IOrder[]>>;
119
+ /**
120
+ * Submit order
121
+ *
122
+ * @public
123
+ */
124
+ export declare const submitOrder: <T>(terminal: Terminal, credential: {
125
+ type: string;
126
+ payload: T;
127
+ }, order: IOrder) => Promise<IResponse<{
128
+ order_id: string;
129
+ }>>;
130
+ /**
131
+ * Modify order
132
+ *
133
+ * @public
134
+ */
135
+ export declare const modifyOrder: <T>(terminal: Terminal, credential: {
136
+ type: string;
137
+ payload: T;
138
+ }, order: IOrder) => Promise<IResponse<void>>;
139
+ /**
140
+ * Cancel order
141
+ *
142
+ * @public
143
+ */
144
+ export declare const cancelOrder: <T>(terminal: Terminal, credential: {
145
+ type: string;
146
+ payload: T;
147
+ }, order: IOrder) => Promise<IResponse<void>>;
148
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +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,EAA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AAC,IAAI,CAAC,CAAC;CAC1D;AAaD;;;;GAIG;AACH,eAAO,MAAM,uBAAuB,gBAAiB,QAAQ,iCA8H5D,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,eAAe,gBAChB,QAAQ;UACE,MAAM;;MACzB,QAAQ,UAAU,MAAM,CAAC,CAE3B,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,YAAY,aAAoB,QAAQ,KAAG,QAAQ,UAAU,QAAQ,EAAE,CAAC,CAEpF,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,YAAY,gBACb,QAAQ;UACE,MAAM;;gBACb,MAAM,KAClB,QAAQ,UAAU,SAAS,EAAE,CAAC,CAEhC,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,SAAS,gBACV,QAAQ;UACE,MAAM;;gBACb,MAAM,KAClB,QAAQ,UAAU,MAAM,EAAE,CAAC,CAE7B,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,WAAW,gBACZ,QAAQ;UACE,MAAM;;UACnB,MAAM,KACZ,QAAQ,UAAU;IAAE,QAAQ,EAAE,MAAM,CAAA;CAAE,CAAC,CAEzC,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,WAAW,gBACZ,QAAQ;UACE,MAAM;;UACnB,MAAM,KACZ,QAAQ,UAAU,IAAI,CAAC,CAEzB,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,WAAW,gBACZ,QAAQ;UACE,MAAM;;UACnB,MAAM,KACZ,QAAQ,UAAU,IAAI,CAAC,CAEzB,CAAC"}
package/lib/index.js ADDED
@@ -0,0 +1,170 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.cancelOrder = exports.modifyOrder = exports.submitOrder = exports.getOrders = exports.getPositions = exports.listProducts = exports.getCredentialId = exports.provideExchangeServices = void 0;
4
+ const makeCredentialSchema = (type, payloadSchema) => {
5
+ return {
6
+ type: 'object',
7
+ required: ['type', 'payload'],
8
+ properties: {
9
+ type: { type: 'string', const: type },
10
+ payload: payloadSchema,
11
+ },
12
+ };
13
+ };
14
+ /**
15
+ * Provide exchange services
16
+ *
17
+ * @public
18
+ */
19
+ const provideExchangeServices = (terminal, exchange) => {
20
+ const { name: type, credentialSchema } = exchange;
21
+ // GetCredentialId
22
+ terminal.server.provideService('GetCredentialId', {
23
+ type: 'object',
24
+ required: ['credential'],
25
+ properties: {
26
+ credential: makeCredentialSchema(type, credentialSchema),
27
+ },
28
+ }, async (msg) => {
29
+ const credentialId = await exchange.getCredentialId(msg.req.credential.payload);
30
+ return { res: { code: 0, message: 'OK', data: credentialId } };
31
+ });
32
+ // ListProducts
33
+ terminal.server.provideService('ListProducts', {}, async () => {
34
+ const products = await exchange.listProducts();
35
+ return { res: { code: 0, message: 'OK', data: products } };
36
+ });
37
+ // GetPositions
38
+ terminal.server.provideService('GetPositions', {
39
+ type: 'object',
40
+ required: ['credential'],
41
+ properties: {
42
+ credential: makeCredentialSchema(type, credentialSchema),
43
+ product_id: { type: 'string' },
44
+ },
45
+ }, async (msg) => {
46
+ if (msg.req.product_id) {
47
+ const positions = await exchange.getPositionsByProductId(msg.req.credential.payload, msg.req.product_id);
48
+ return { res: { code: 0, message: 'OK', data: positions } };
49
+ }
50
+ const positions = await exchange.getPositions(msg.req.credential.payload);
51
+ return { res: { code: 0, message: 'OK', data: positions } };
52
+ });
53
+ // GetOrders
54
+ terminal.server.provideService('GetOrders', {
55
+ type: 'object',
56
+ required: ['credential'],
57
+ properties: {
58
+ credential: makeCredentialSchema(type, credentialSchema),
59
+ product_id: { type: 'string' },
60
+ },
61
+ }, async (msg) => {
62
+ if (msg.req.product_id) {
63
+ const orders = await exchange.getOrdersByProductId(msg.req.credential.payload, msg.req.product_id);
64
+ return { res: { code: 0, message: 'OK', data: orders } };
65
+ }
66
+ const orders = await exchange.getOrders(msg.req.credential.payload);
67
+ return { res: { code: 0, message: 'OK', data: orders } };
68
+ });
69
+ // SubmitOrder
70
+ terminal.server.provideService('SubmitOrder', {
71
+ type: 'object',
72
+ required: ['credential', 'order'],
73
+ properties: {
74
+ credential: makeCredentialSchema(type, credentialSchema),
75
+ order: { type: 'object' },
76
+ },
77
+ }, async (msg) => {
78
+ const result = await exchange.submitOrder(msg.req.credential.payload, msg.req.order);
79
+ return { res: { code: 0, message: 'OK', data: result } };
80
+ });
81
+ // ModifyOrder
82
+ terminal.server.provideService('ModifyOrder', {
83
+ type: 'object',
84
+ required: ['credential', 'order'],
85
+ properties: {
86
+ credential: makeCredentialSchema(type, credentialSchema),
87
+ order: { type: 'object' },
88
+ },
89
+ }, async (msg) => {
90
+ await exchange.modifyOrder(msg.req.credential.payload, msg.req.order);
91
+ return { res: { code: 0, message: 'OK' } };
92
+ });
93
+ // CancelOrder
94
+ terminal.server.provideService('CancelOrder', {
95
+ type: 'object',
96
+ required: ['credential', 'order'],
97
+ properties: {
98
+ credential: makeCredentialSchema(type, credentialSchema),
99
+ order: { type: 'object' },
100
+ },
101
+ }, async (msg) => {
102
+ await exchange.cancelOrder(msg.req.credential.payload, msg.req.order);
103
+ return { res: { code: 0, message: 'OK' } };
104
+ });
105
+ };
106
+ exports.provideExchangeServices = provideExchangeServices;
107
+ /**
108
+ * Get credential ID
109
+ *
110
+ * @public
111
+ */
112
+ const getCredentialId = async (terminal, credential) => {
113
+ return terminal.client.requestForResponse('GetCredentialId', { credential });
114
+ };
115
+ exports.getCredentialId = getCredentialId;
116
+ /**
117
+ * List products
118
+ *
119
+ * @public
120
+ */
121
+ const listProducts = async (terminal) => {
122
+ return terminal.client.requestForResponse('ListProducts', {});
123
+ };
124
+ exports.listProducts = listProducts;
125
+ /**
126
+ * Get positions
127
+ *
128
+ * @public
129
+ */
130
+ const getPositions = async (terminal, credential, product_id) => {
131
+ return terminal.client.requestForResponse('GetPositions', { credential, product_id });
132
+ };
133
+ exports.getPositions = getPositions;
134
+ /**
135
+ * Get orders
136
+ *
137
+ * @public
138
+ */
139
+ const getOrders = async (terminal, credential, product_id) => {
140
+ return terminal.client.requestForResponse('GetOrders', { credential, product_id });
141
+ };
142
+ exports.getOrders = getOrders;
143
+ /**
144
+ * Submit order
145
+ *
146
+ * @public
147
+ */
148
+ const submitOrder = async (terminal, credential, order) => {
149
+ return terminal.client.requestForResponse('SubmitOrder', { credential, order });
150
+ };
151
+ exports.submitOrder = submitOrder;
152
+ /**
153
+ * Modify order
154
+ *
155
+ * @public
156
+ */
157
+ const modifyOrder = async (terminal, credential, order) => {
158
+ return terminal.client.requestForResponse('ModifyOrder', { credential, order });
159
+ };
160
+ exports.modifyOrder = modifyOrder;
161
+ /**
162
+ * Cancel order
163
+ *
164
+ * @public
165
+ */
166
+ const cancelOrder = async (terminal, credential, order) => {
167
+ return terminal.client.requestForResponse('CancelOrder', { credential, order });
168
+ };
169
+ exports.cancelOrder = cancelOrder;
170
+ //# sourceMappingURL=index.js.map