@yuants/app-virtual-exchange 0.1.0 → 0.2.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.
- package/dist/index.js +50 -47
- package/dist/index.js.map +1 -1
- package/lib/index.js +50 -47
- package/lib/index.js.map +1 -1
- package/package.json +7 -7
- package/temp/package-deps.json +10 -10
package/dist/index.js
CHANGED
|
@@ -3,9 +3,9 @@ import { readSecret, writeSecret } from '@yuants/secret';
|
|
|
3
3
|
import { escapeSQL, requestSQL } from '@yuants/sql';
|
|
4
4
|
import { defer, mergeMap, retry, timer } from 'rxjs';
|
|
5
5
|
const terminal = Terminal.fromNodeEnv();
|
|
6
|
-
const
|
|
6
|
+
const mapCredentialIdToCredential = new Map();
|
|
7
7
|
// 1. RegisterExchangeCredential
|
|
8
|
-
terminal.server.provideService('RegisterExchangeCredential', {
|
|
8
|
+
terminal.server.provideService('VirtualExchange/RegisterExchangeCredential', {
|
|
9
9
|
type: 'object',
|
|
10
10
|
required: ['type', 'payload'],
|
|
11
11
|
properties: {
|
|
@@ -19,7 +19,7 @@ terminal.server.provideService('RegisterExchangeCredential', {
|
|
|
19
19
|
return { res: { code: 0, message: 'OK' } };
|
|
20
20
|
});
|
|
21
21
|
// 2. ListExchangeCredential
|
|
22
|
-
terminal.server.provideService('ListExchangeCredential', {}, async () => {
|
|
22
|
+
terminal.server.provideService('VirtualExchange/ListExchangeCredential', {}, async () => {
|
|
23
23
|
const secrets = await requestSQL(terminal, `select * from secret where tags->>'type' = 'exchange_credential' and reader = ${escapeSQL(terminal.keyPair.public_key)}`);
|
|
24
24
|
const credentials = [];
|
|
25
25
|
for (const secret of secrets) {
|
|
@@ -34,6 +34,9 @@ terminal.server.provideService('ListExchangeCredential', {}, async () => {
|
|
|
34
34
|
}
|
|
35
35
|
return { res: { code: 0, message: 'OK', data: credentials } };
|
|
36
36
|
});
|
|
37
|
+
terminal.server.provideService('VirtualExchange/ListCredentials', {}, async () => {
|
|
38
|
+
return { res: { code: 0, message: 'OK', data: [...mapCredentialIdToCredential.keys()] } };
|
|
39
|
+
});
|
|
37
40
|
// 9. Background listWatch
|
|
38
41
|
const updateIndex = async () => {
|
|
39
42
|
const secrets = await requestSQL(terminal, `select * from secret where tags->>'type' = 'exchange_credential' and reader = ${escapeSQL(terminal.keyPair.public_key)}`);
|
|
@@ -41,12 +44,11 @@ const updateIndex = async () => {
|
|
|
41
44
|
try {
|
|
42
45
|
const decrypted = await readSecret(terminal, secret);
|
|
43
46
|
const credential = JSON.parse(new TextDecoder().decode(decrypted));
|
|
44
|
-
// Call
|
|
45
|
-
const res = await terminal.client.requestForResponse('
|
|
47
|
+
// Call GetCredentialId to get credential id
|
|
48
|
+
const res = await terminal.client.requestForResponse('GetCredentialId', { credential });
|
|
46
49
|
if (res.code === 0 && res.data) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
+
const credentialId = res.data;
|
|
51
|
+
mapCredentialIdToCredential.set(credentialId, credential);
|
|
50
52
|
}
|
|
51
53
|
}
|
|
52
54
|
catch (e) {
|
|
@@ -58,87 +60,88 @@ const updateIndex = async () => {
|
|
|
58
60
|
timer(0, 60000)
|
|
59
61
|
.pipe(mergeMap(() => defer(updateIndex).pipe(retry({ delay: 5000 }))))
|
|
60
62
|
.subscribe();
|
|
61
|
-
// 7. QueryAccounts
|
|
62
|
-
terminal.server.provideService('QueryAccounts', {}, async () => {
|
|
63
|
-
return { res: { code: 0, message: 'OK', data: Array.from(mapAccountIdToCredential.keys()) } };
|
|
64
|
-
});
|
|
65
63
|
// 8. QueryAccountInfo
|
|
66
|
-
terminal.server.provideService('
|
|
64
|
+
terminal.server.provideService('VirtualExchange/QueryPositions', {
|
|
67
65
|
type: 'object',
|
|
68
|
-
required: ['
|
|
66
|
+
required: ['credential_id'],
|
|
69
67
|
properties: {
|
|
70
|
-
|
|
68
|
+
credential_id: { type: 'string' },
|
|
69
|
+
product_id: { type: 'string' },
|
|
71
70
|
},
|
|
72
71
|
}, async (msg) => {
|
|
73
|
-
const credential =
|
|
72
|
+
const credential = mapCredentialIdToCredential.get(msg.req.credential_id);
|
|
74
73
|
if (!credential) {
|
|
75
|
-
return { res: { code: 404, message: '
|
|
74
|
+
return { res: { code: 404, message: 'Credential not found' } };
|
|
76
75
|
}
|
|
77
|
-
const res = await terminal.client.requestForResponse('
|
|
76
|
+
const res = await terminal.client.requestForResponse('QueryPositions', { credential, product_id: msg.req.product_id });
|
|
77
|
+
return { res };
|
|
78
|
+
});
|
|
79
|
+
terminal.server.provideService('VirtualExchange/QueryOrders', {
|
|
80
|
+
type: 'object',
|
|
81
|
+
required: ['credential_id'],
|
|
82
|
+
properties: {
|
|
83
|
+
credential_id: { type: 'string' },
|
|
84
|
+
product_id: { type: 'string' },
|
|
85
|
+
},
|
|
86
|
+
}, async (msg) => {
|
|
87
|
+
const credential = mapCredentialIdToCredential.get(msg.req.credential_id);
|
|
88
|
+
if (!credential) {
|
|
89
|
+
return { res: { code: 404, message: 'Credential not found' } };
|
|
90
|
+
}
|
|
91
|
+
const res = await terminal.client.requestForResponse('QueryOrders', {
|
|
92
|
+
credential,
|
|
93
|
+
product_id: msg.req.product_id,
|
|
94
|
+
});
|
|
78
95
|
return { res };
|
|
79
96
|
});
|
|
80
97
|
// 10. Proxy Orders
|
|
81
98
|
// SubmitOrder
|
|
82
|
-
terminal.server.provideService('SubmitOrder', {
|
|
99
|
+
terminal.server.provideService('VirtualExchange/SubmitOrder', {
|
|
83
100
|
type: 'object',
|
|
84
|
-
required: ['order'],
|
|
101
|
+
required: ['order', 'credential_id'],
|
|
85
102
|
properties: {
|
|
86
103
|
order: { type: 'object' },
|
|
104
|
+
credential_id: { type: 'string' },
|
|
87
105
|
},
|
|
88
106
|
}, async (msg) => {
|
|
89
|
-
const credential =
|
|
107
|
+
const credential = mapCredentialIdToCredential.get(msg.req.credential_id);
|
|
90
108
|
if (!credential) {
|
|
91
|
-
return { res: { code: 404, message: '
|
|
109
|
+
return { res: { code: 404, message: 'Credential not found' } };
|
|
92
110
|
}
|
|
93
111
|
const res = await terminal.client.requestForResponse('SubmitOrder', { credential, order: msg.req.order });
|
|
94
112
|
return { res };
|
|
95
113
|
});
|
|
96
114
|
// ModifyOrder
|
|
97
|
-
terminal.server.provideService('ModifyOrder', {
|
|
115
|
+
terminal.server.provideService('VirtualExchange/ModifyOrder', {
|
|
98
116
|
type: 'object',
|
|
99
|
-
required: ['order'],
|
|
117
|
+
required: ['order', 'credential_id'],
|
|
100
118
|
properties: {
|
|
101
119
|
order: { type: 'object' },
|
|
120
|
+
credential_id: { type: 'string' },
|
|
102
121
|
},
|
|
103
122
|
}, async (msg) => {
|
|
104
|
-
const credential =
|
|
123
|
+
const credential = mapCredentialIdToCredential.get(msg.req.credential_id);
|
|
105
124
|
if (!credential) {
|
|
106
|
-
return { res: { code: 404, message: '
|
|
125
|
+
return { res: { code: 404, message: 'Credential not found' } };
|
|
107
126
|
}
|
|
108
127
|
const res = await terminal.client.requestForResponse('ModifyOrder', { credential, order: msg.req.order });
|
|
109
128
|
return { res };
|
|
110
129
|
});
|
|
111
130
|
// CancelOrder
|
|
112
|
-
terminal.server.provideService('CancelOrder', {
|
|
131
|
+
terminal.server.provideService('VirtualExchange/CancelOrder', {
|
|
113
132
|
type: 'object',
|
|
114
|
-
required: ['order'],
|
|
133
|
+
required: ['order', 'credential_id'],
|
|
115
134
|
properties: {
|
|
116
135
|
order: { type: 'object' },
|
|
136
|
+
credential_id: { type: 'string' },
|
|
117
137
|
},
|
|
118
138
|
}, async (msg) => {
|
|
119
|
-
const credential =
|
|
139
|
+
const credential = mapCredentialIdToCredential.get(msg.req.credential_id);
|
|
120
140
|
if (!credential) {
|
|
121
|
-
return { res: { code: 404, message: '
|
|
141
|
+
return { res: { code: 404, message: 'Credential not found' } };
|
|
122
142
|
}
|
|
123
143
|
const res = await terminal.client.requestForResponse('CancelOrder', { credential, order: msg.req.order });
|
|
124
144
|
return { res };
|
|
125
145
|
});
|
|
126
146
|
// ListOrders
|
|
127
|
-
terminal.server.provideService('ListOrders', {
|
|
128
|
-
type: 'object',
|
|
129
|
-
required: ['account_id'],
|
|
130
|
-
properties: {
|
|
131
|
-
account_id: { type: 'string' },
|
|
132
|
-
},
|
|
133
|
-
}, async (msg) => {
|
|
134
|
-
const credential = mapAccountIdToCredential.get(msg.req.account_id);
|
|
135
|
-
if (!credential) {
|
|
136
|
-
return { res: { code: 404, message: 'Account not found' } };
|
|
137
|
-
}
|
|
138
|
-
const res = await terminal.client.requestForResponse('ListOrders', {
|
|
139
|
-
credential,
|
|
140
|
-
account_id: msg.req.account_id,
|
|
141
|
-
});
|
|
142
|
-
return { res };
|
|
143
|
-
});
|
|
144
147
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAW,UAAU,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAClE,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,MAAM,CAAC;AAErD,MAAM,QAAQ,GAAG,QAAQ,CAAC,WAAW,EAAE,CAAC;AAOxC,MAAM,wBAAwB,GAAG,IAAI,GAAG,EAA+B,CAAC;AAExE,gCAAgC;AAChC,QAAQ,CAAC,MAAM,CAAC,cAAc,CAC5B,4BAA4B,EAC5B;IACE,IAAI,EAAE,QAAQ;IACd,QAAQ,EAAE,CAAC,MAAM,EAAE,SAAS,CAAC;IAC7B,UAAU,EAAE;QACV,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;QACxB,OAAO,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;KAC5B;CACF,EACD,KAAK,EA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ourcesContent":["import { IAccountInfo } from '@yuants/data-account';\nimport { IOrder, ITypedCredential } from '@yuants/data-order';\nimport { Terminal } from '@yuants/protocol';\nimport { ISecret, readSecret, writeSecret } from '@yuants/secret';\nimport { escapeSQL, requestSQL } from '@yuants/sql';\nimport { defer, mergeMap, retry, timer } from 'rxjs';\n\nconst terminal = Terminal.fromNodeEnv();\n\ninterface IExchangeCredential {\n type: string;\n payload: any;\n}\n\nconst mapAccountIdToCredential = new Map<string, IExchangeCredential>();\n\n// 1. RegisterExchangeCredential\nterminal.server.provideService<IExchangeCredential, void>(\n 'RegisterExchangeCredential',\n {\n type: 'object',\n required: ['type', 'payload'],\n properties: {\n type: { type: 'string' },\n payload: { type: 'object' },\n },\n },\n async (msg) => {\n const credential = msg.req;\n const secretData = new TextEncoder().encode(JSON.stringify(credential));\n await writeSecret(terminal, terminal.keyPair.public_key, { type: 'exchange_credential' }, secretData);\n return { res: { code: 0, message: 'OK' } };\n },\n);\n\n// 2. ListExchangeCredential\nterminal.server.provideService<void, IExchangeCredential[]>('ListExchangeCredential', {}, async () => {\n const secrets = await requestSQL<ISecret[]>(\n terminal,\n `select * from secret where tags->>'type' = 'exchange_credential' and reader = ${escapeSQL(\n terminal.keyPair.public_key,\n )}`,\n );\n const credentials: IExchangeCredential[] = [];\n for (const secret of secrets) {\n try {\n const decrypted = await readSecret(terminal, secret);\n const credential = JSON.parse(new TextDecoder().decode(decrypted));\n credentials.push(credential);\n } catch (e) {\n console.error('Failed to decrypt secret', e);\n }\n }\n return { res: { code: 0, message: 'OK', data: credentials } };\n});\n\n// 9. Background listWatch\nconst updateIndex = async () => {\n const secrets = await requestSQL<ISecret[]>(\n terminal,\n `select * from secret where tags->>'type' = 'exchange_credential' and reader = ${escapeSQL(\n terminal.keyPair.public_key,\n )}`,\n );\n for (const secret of secrets) {\n try {\n const decrypted = await readSecret(terminal, secret);\n const credential = JSON.parse(new TextDecoder().decode(decrypted)) as IExchangeCredential;\n\n // Call ListAccounts to get account_ids\n const res = await terminal.client.requestForResponse<\n { credential: ITypedCredential<any> },\n Array<{ account_id: string }>\n >('ListAccounts', { credential });\n\n if (res.code === 0 && res.data) {\n for (const account of res.data) {\n mapAccountIdToCredential.set(account.account_id, credential);\n }\n }\n } catch (e) {\n console.error('Failed to process secret', e);\n }\n }\n};\n\n// Run updateIndex periodically\ntimer(0, 60000)\n .pipe(mergeMap(() => defer(updateIndex).pipe(retry({ delay: 5000 }))))\n .subscribe();\n\n// 7. QueryAccounts\nterminal.server.provideService<void, string[]>('QueryAccounts', {}, async () => {\n return { res: { code: 0, message: 'OK', data: Array.from(mapAccountIdToCredential.keys()) } };\n});\n\n// 8. QueryAccountInfo\nterminal.server.provideService<{ account_id: string }, IAccountInfo>(\n 'QueryAccountInfo',\n {\n type: 'object',\n required: ['account_id'],\n properties: {\n account_id: { type: 'string' },\n },\n },\n async (msg) => {\n const credential = mapAccountIdToCredential.get(msg.req.account_id);\n if (!credential) {\n return { res: { code: 404, message: 'Account not found' } };\n }\n const res = await terminal.client.requestForResponse<\n { credential: ITypedCredential<any>; account_id: string },\n IAccountInfo\n >('GetAccountInfo', { credential, account_id: msg.req.account_id });\n return { res };\n },\n);\n\n// 10. Proxy Orders\n// SubmitOrder\nterminal.server.provideService<{ order: IOrder }, { order_id: string }>(\n 'SubmitOrder',\n {\n type: 'object',\n required: ['order'],\n properties: {\n order: { type: 'object' },\n },\n },\n async (msg) => {\n const credential = mapAccountIdToCredential.get(msg.req.order.account_id);\n if (!credential) {\n return { res: { code: 404, message: 'Account not found' } };\n }\n const res = await terminal.client.requestForResponse<\n { credential: ITypedCredential<any>; order: IOrder },\n { order_id: string }\n >('SubmitOrder', { credential, order: msg.req.order });\n return { res };\n },\n);\n\n// ModifyOrder\nterminal.server.provideService<{ order: IOrder }, void>(\n 'ModifyOrder',\n {\n type: 'object',\n required: ['order'],\n properties: {\n order: { type: 'object' },\n },\n },\n async (msg) => {\n const credential = mapAccountIdToCredential.get(msg.req.order.account_id);\n if (!credential) {\n return { res: { code: 404, message: 'Account not found' } };\n }\n const res = await terminal.client.requestForResponse<\n { credential: ITypedCredential<any>; order: IOrder },\n void\n >('ModifyOrder', { credential, order: msg.req.order });\n return { res };\n },\n);\n\n// CancelOrder\nterminal.server.provideService<{ order: IOrder }, void>(\n 'CancelOrder',\n {\n type: 'object',\n required: ['order'],\n properties: {\n order: { type: 'object' },\n },\n },\n async (msg) => {\n const credential = mapAccountIdToCredential.get(msg.req.order.account_id);\n if (!credential) {\n return { res: { code: 404, message: 'Account not found' } };\n }\n const res = await terminal.client.requestForResponse<\n { credential: ITypedCredential<any>; order: IOrder },\n void\n >('CancelOrder', { credential, order: msg.req.order });\n return { res };\n },\n);\n\n// ListOrders\nterminal.server.provideService<{ account_id: string }, { orders: IOrder[] }>(\n 'ListOrders',\n {\n type: 'object',\n required: ['account_id'],\n properties: {\n account_id: { type: 'string' },\n },\n },\n async (msg) => {\n const credential = mapAccountIdToCredential.get(msg.req.account_id);\n if (!credential) {\n return { res: { code: 404, message: 'Account not found' } };\n }\n const res = await terminal.client.requestForResponse<\n { credential: ITypedCredential<any>; account_id: string },\n { orders: IOrder[] }\n >('ListOrders', {\n credential,\n account_id: msg.req.account_id,\n });\n return { res };\n },\n);\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAW,UAAU,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAClE,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,MAAM,CAAC;AAErD,MAAM,QAAQ,GAAG,QAAQ,CAAC,WAAW,EAAE,CAAC;AAOxC,MAAM,2BAA2B,GAAG,IAAI,GAAG,EAA+B,CAAC;AAE3E,gCAAgC;AAChC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AE,GAAG,EAAE,EAAE;IACZ,MAAM,UAAU,GAAG,2BAA2B,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;IAC1E,IAAI,CAAC,UAAU,EAAE;QACf,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,GAAG,EAAE,OAAO,EAAE,sBAAsB,EAAE,EAAE,CAAC;KAChE;IACD,MAAM,GAAG,GAAG,MAAM,QAAQ,CAAC,MAAM,CAAC,kBAAkB,CAGlD,aAAa,EAAE,EAAE,UAAU,EAAE,KAAK,EAAE,GAAG,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,CAAC;IACvD,OAAO,EAAE,GAAG,EAAE,CAAC;AACjB,CAAC,CACF,CAAC;AAEF,aAAa","sourcesContent":["import { IAccountInfo } from '@yuants/data-account';\nimport { IOrder, ITypedCredential } from '@yuants/data-order';\nimport { Terminal } from '@yuants/protocol';\nimport { ISecret, readSecret, writeSecret } from '@yuants/secret';\nimport { escapeSQL, requestSQL } from '@yuants/sql';\nimport { defer, mergeMap, retry, timer } from 'rxjs';\n\nconst terminal = Terminal.fromNodeEnv();\n\ninterface IExchangeCredential {\n type: string;\n payload: any;\n}\n\nconst mapCredentialIdToCredential = new Map<string, IExchangeCredential>();\n\n// 1. RegisterExchangeCredential\nterminal.server.provideService<IExchangeCredential, void>(\n 'VirtualExchange/RegisterExchangeCredential',\n {\n type: 'object',\n required: ['type', 'payload'],\n properties: {\n type: { type: 'string' },\n payload: { type: 'object' },\n },\n },\n async (msg) => {\n const credential = msg.req;\n const secretData = new TextEncoder().encode(JSON.stringify(credential));\n await writeSecret(terminal, terminal.keyPair.public_key, { type: 'exchange_credential' }, secretData);\n return { res: { code: 0, message: 'OK' } };\n },\n);\n\n// 2. ListExchangeCredential\nterminal.server.provideService<void, IExchangeCredential[]>(\n 'VirtualExchange/ListExchangeCredential',\n {},\n async () => {\n const secrets = await requestSQL<ISecret[]>(\n terminal,\n `select * from secret where tags->>'type' = 'exchange_credential' and reader = ${escapeSQL(\n terminal.keyPair.public_key,\n )}`,\n );\n const credentials: IExchangeCredential[] = [];\n for (const secret of secrets) {\n try {\n const decrypted = await readSecret(terminal, secret);\n const credential = JSON.parse(new TextDecoder().decode(decrypted));\n credentials.push(credential);\n } catch (e) {\n console.error('Failed to decrypt secret', e);\n }\n }\n return { res: { code: 0, message: 'OK', data: credentials } };\n },\n);\n\nterminal.server.provideService<void, string[]>('VirtualExchange/ListCredentials', {}, async () => {\n return { res: { code: 0, message: 'OK', data: [...mapCredentialIdToCredential.keys()] } };\n});\n\n// 9. Background listWatch\nconst updateIndex = async () => {\n const secrets = await requestSQL<ISecret[]>(\n terminal,\n `select * from secret where tags->>'type' = 'exchange_credential' and reader = ${escapeSQL(\n terminal.keyPair.public_key,\n )}`,\n );\n for (const secret of secrets) {\n try {\n const decrypted = await readSecret(terminal, secret);\n const credential = JSON.parse(new TextDecoder().decode(decrypted)) as IExchangeCredential;\n\n // Call GetCredentialId to get credential id\n const res = await terminal.client.requestForResponse<{ credential: ITypedCredential<any> }, string>(\n 'GetCredentialId',\n { credential },\n );\n\n if (res.code === 0 && res.data) {\n const credentialId = res.data;\n mapCredentialIdToCredential.set(credentialId, credential);\n }\n } catch (e) {\n console.error('Failed to process secret', e);\n }\n }\n};\n\n// Run updateIndex periodically\ntimer(0, 60000)\n .pipe(mergeMap(() => defer(updateIndex).pipe(retry({ delay: 5000 }))))\n .subscribe();\n\n// 8. QueryAccountInfo\nterminal.server.provideService<{ credential_id: string; product_id?: string }, IAccountInfo>(\n 'VirtualExchange/QueryPositions',\n {\n type: 'object',\n required: ['credential_id'],\n properties: {\n credential_id: { type: 'string' },\n product_id: { type: 'string' },\n },\n },\n async (msg) => {\n const credential = mapCredentialIdToCredential.get(msg.req.credential_id);\n if (!credential) {\n return { res: { code: 404, message: 'Credential not found' } };\n }\n const res = await terminal.client.requestForResponse<\n { credential: ITypedCredential<any>; product_id?: string },\n IAccountInfo\n >('QueryPositions', { credential, product_id: msg.req.product_id });\n return { res };\n },\n);\n\nterminal.server.provideService<{ credential_id: string; product_id?: string }, { orders: IOrder[] }>(\n 'VirtualExchange/QueryOrders',\n {\n type: 'object',\n required: ['credential_id'],\n properties: {\n credential_id: { type: 'string' },\n product_id: { type: 'string' },\n },\n },\n async (msg) => {\n const credential = mapCredentialIdToCredential.get(msg.req.credential_id);\n if (!credential) {\n return { res: { code: 404, message: 'Credential not found' } };\n }\n const res = await terminal.client.requestForResponse<\n { credential: ITypedCredential<any>; product_id?: string },\n { orders: IOrder[] }\n >('QueryOrders', {\n credential,\n product_id: msg.req.product_id,\n });\n return { res };\n },\n);\n\n// 10. Proxy Orders\n// SubmitOrder\nterminal.server.provideService<{ order: IOrder; credential_id: string }, { order_id: string }>(\n 'VirtualExchange/SubmitOrder',\n {\n type: 'object',\n required: ['order', 'credential_id'],\n properties: {\n order: { type: 'object' },\n credential_id: { type: 'string' },\n },\n },\n async (msg) => {\n const credential = mapCredentialIdToCredential.get(msg.req.credential_id);\n if (!credential) {\n return { res: { code: 404, message: 'Credential not found' } };\n }\n const res = await terminal.client.requestForResponse<\n { credential: ITypedCredential<any>; order: IOrder },\n { order_id: string }\n >('SubmitOrder', { credential, order: msg.req.order });\n return { res };\n },\n);\n\n// ModifyOrder\nterminal.server.provideService<{ order: IOrder; credential_id: string }, void>(\n 'VirtualExchange/ModifyOrder',\n {\n type: 'object',\n required: ['order', 'credential_id'],\n properties: {\n order: { type: 'object' },\n credential_id: { type: 'string' },\n },\n },\n async (msg) => {\n const credential = mapCredentialIdToCredential.get(msg.req.credential_id);\n if (!credential) {\n return { res: { code: 404, message: 'Credential not found' } };\n }\n const res = await terminal.client.requestForResponse<\n { credential: ITypedCredential<any>; order: IOrder },\n void\n >('ModifyOrder', { credential, order: msg.req.order });\n return { res };\n },\n);\n\n// CancelOrder\nterminal.server.provideService<{ order: IOrder; credential_id: string }, void>(\n 'VirtualExchange/CancelOrder',\n {\n type: 'object',\n required: ['order', 'credential_id'],\n properties: {\n order: { type: 'object' },\n credential_id: { type: 'string' },\n },\n },\n async (msg) => {\n const credential = mapCredentialIdToCredential.get(msg.req.credential_id);\n if (!credential) {\n return { res: { code: 404, message: 'Credential not found' } };\n }\n const res = await terminal.client.requestForResponse<\n { credential: ITypedCredential<any>; order: IOrder },\n void\n >('CancelOrder', { credential, order: msg.req.order });\n return { res };\n },\n);\n\n// ListOrders\n"]}
|
package/lib/index.js
CHANGED
|
@@ -5,9 +5,9 @@ const secret_1 = require("@yuants/secret");
|
|
|
5
5
|
const sql_1 = require("@yuants/sql");
|
|
6
6
|
const rxjs_1 = require("rxjs");
|
|
7
7
|
const terminal = protocol_1.Terminal.fromNodeEnv();
|
|
8
|
-
const
|
|
8
|
+
const mapCredentialIdToCredential = new Map();
|
|
9
9
|
// 1. RegisterExchangeCredential
|
|
10
|
-
terminal.server.provideService('RegisterExchangeCredential', {
|
|
10
|
+
terminal.server.provideService('VirtualExchange/RegisterExchangeCredential', {
|
|
11
11
|
type: 'object',
|
|
12
12
|
required: ['type', 'payload'],
|
|
13
13
|
properties: {
|
|
@@ -21,7 +21,7 @@ terminal.server.provideService('RegisterExchangeCredential', {
|
|
|
21
21
|
return { res: { code: 0, message: 'OK' } };
|
|
22
22
|
});
|
|
23
23
|
// 2. ListExchangeCredential
|
|
24
|
-
terminal.server.provideService('ListExchangeCredential', {}, async () => {
|
|
24
|
+
terminal.server.provideService('VirtualExchange/ListExchangeCredential', {}, async () => {
|
|
25
25
|
const secrets = await (0, sql_1.requestSQL)(terminal, `select * from secret where tags->>'type' = 'exchange_credential' and reader = ${(0, sql_1.escapeSQL)(terminal.keyPair.public_key)}`);
|
|
26
26
|
const credentials = [];
|
|
27
27
|
for (const secret of secrets) {
|
|
@@ -36,6 +36,9 @@ terminal.server.provideService('ListExchangeCredential', {}, async () => {
|
|
|
36
36
|
}
|
|
37
37
|
return { res: { code: 0, message: 'OK', data: credentials } };
|
|
38
38
|
});
|
|
39
|
+
terminal.server.provideService('VirtualExchange/ListCredentials', {}, async () => {
|
|
40
|
+
return { res: { code: 0, message: 'OK', data: [...mapCredentialIdToCredential.keys()] } };
|
|
41
|
+
});
|
|
39
42
|
// 9. Background listWatch
|
|
40
43
|
const updateIndex = async () => {
|
|
41
44
|
const secrets = await (0, sql_1.requestSQL)(terminal, `select * from secret where tags->>'type' = 'exchange_credential' and reader = ${(0, sql_1.escapeSQL)(terminal.keyPair.public_key)}`);
|
|
@@ -43,12 +46,11 @@ const updateIndex = async () => {
|
|
|
43
46
|
try {
|
|
44
47
|
const decrypted = await (0, secret_1.readSecret)(terminal, secret);
|
|
45
48
|
const credential = JSON.parse(new TextDecoder().decode(decrypted));
|
|
46
|
-
// Call
|
|
47
|
-
const res = await terminal.client.requestForResponse('
|
|
49
|
+
// Call GetCredentialId to get credential id
|
|
50
|
+
const res = await terminal.client.requestForResponse('GetCredentialId', { credential });
|
|
48
51
|
if (res.code === 0 && res.data) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
52
|
+
const credentialId = res.data;
|
|
53
|
+
mapCredentialIdToCredential.set(credentialId, credential);
|
|
52
54
|
}
|
|
53
55
|
}
|
|
54
56
|
catch (e) {
|
|
@@ -60,87 +62,88 @@ const updateIndex = async () => {
|
|
|
60
62
|
(0, rxjs_1.timer)(0, 60000)
|
|
61
63
|
.pipe((0, rxjs_1.mergeMap)(() => (0, rxjs_1.defer)(updateIndex).pipe((0, rxjs_1.retry)({ delay: 5000 }))))
|
|
62
64
|
.subscribe();
|
|
63
|
-
// 7. QueryAccounts
|
|
64
|
-
terminal.server.provideService('QueryAccounts', {}, async () => {
|
|
65
|
-
return { res: { code: 0, message: 'OK', data: Array.from(mapAccountIdToCredential.keys()) } };
|
|
66
|
-
});
|
|
67
65
|
// 8. QueryAccountInfo
|
|
68
|
-
terminal.server.provideService('
|
|
66
|
+
terminal.server.provideService('VirtualExchange/QueryPositions', {
|
|
69
67
|
type: 'object',
|
|
70
|
-
required: ['
|
|
68
|
+
required: ['credential_id'],
|
|
71
69
|
properties: {
|
|
72
|
-
|
|
70
|
+
credential_id: { type: 'string' },
|
|
71
|
+
product_id: { type: 'string' },
|
|
73
72
|
},
|
|
74
73
|
}, async (msg) => {
|
|
75
|
-
const credential =
|
|
74
|
+
const credential = mapCredentialIdToCredential.get(msg.req.credential_id);
|
|
76
75
|
if (!credential) {
|
|
77
|
-
return { res: { code: 404, message: '
|
|
76
|
+
return { res: { code: 404, message: 'Credential not found' } };
|
|
78
77
|
}
|
|
79
|
-
const res = await terminal.client.requestForResponse('
|
|
78
|
+
const res = await terminal.client.requestForResponse('QueryPositions', { credential, product_id: msg.req.product_id });
|
|
79
|
+
return { res };
|
|
80
|
+
});
|
|
81
|
+
terminal.server.provideService('VirtualExchange/QueryOrders', {
|
|
82
|
+
type: 'object',
|
|
83
|
+
required: ['credential_id'],
|
|
84
|
+
properties: {
|
|
85
|
+
credential_id: { type: 'string' },
|
|
86
|
+
product_id: { type: 'string' },
|
|
87
|
+
},
|
|
88
|
+
}, async (msg) => {
|
|
89
|
+
const credential = mapCredentialIdToCredential.get(msg.req.credential_id);
|
|
90
|
+
if (!credential) {
|
|
91
|
+
return { res: { code: 404, message: 'Credential not found' } };
|
|
92
|
+
}
|
|
93
|
+
const res = await terminal.client.requestForResponse('QueryOrders', {
|
|
94
|
+
credential,
|
|
95
|
+
product_id: msg.req.product_id,
|
|
96
|
+
});
|
|
80
97
|
return { res };
|
|
81
98
|
});
|
|
82
99
|
// 10. Proxy Orders
|
|
83
100
|
// SubmitOrder
|
|
84
|
-
terminal.server.provideService('SubmitOrder', {
|
|
101
|
+
terminal.server.provideService('VirtualExchange/SubmitOrder', {
|
|
85
102
|
type: 'object',
|
|
86
|
-
required: ['order'],
|
|
103
|
+
required: ['order', 'credential_id'],
|
|
87
104
|
properties: {
|
|
88
105
|
order: { type: 'object' },
|
|
106
|
+
credential_id: { type: 'string' },
|
|
89
107
|
},
|
|
90
108
|
}, async (msg) => {
|
|
91
|
-
const credential =
|
|
109
|
+
const credential = mapCredentialIdToCredential.get(msg.req.credential_id);
|
|
92
110
|
if (!credential) {
|
|
93
|
-
return { res: { code: 404, message: '
|
|
111
|
+
return { res: { code: 404, message: 'Credential not found' } };
|
|
94
112
|
}
|
|
95
113
|
const res = await terminal.client.requestForResponse('SubmitOrder', { credential, order: msg.req.order });
|
|
96
114
|
return { res };
|
|
97
115
|
});
|
|
98
116
|
// ModifyOrder
|
|
99
|
-
terminal.server.provideService('ModifyOrder', {
|
|
117
|
+
terminal.server.provideService('VirtualExchange/ModifyOrder', {
|
|
100
118
|
type: 'object',
|
|
101
|
-
required: ['order'],
|
|
119
|
+
required: ['order', 'credential_id'],
|
|
102
120
|
properties: {
|
|
103
121
|
order: { type: 'object' },
|
|
122
|
+
credential_id: { type: 'string' },
|
|
104
123
|
},
|
|
105
124
|
}, async (msg) => {
|
|
106
|
-
const credential =
|
|
125
|
+
const credential = mapCredentialIdToCredential.get(msg.req.credential_id);
|
|
107
126
|
if (!credential) {
|
|
108
|
-
return { res: { code: 404, message: '
|
|
127
|
+
return { res: { code: 404, message: 'Credential not found' } };
|
|
109
128
|
}
|
|
110
129
|
const res = await terminal.client.requestForResponse('ModifyOrder', { credential, order: msg.req.order });
|
|
111
130
|
return { res };
|
|
112
131
|
});
|
|
113
132
|
// CancelOrder
|
|
114
|
-
terminal.server.provideService('CancelOrder', {
|
|
133
|
+
terminal.server.provideService('VirtualExchange/CancelOrder', {
|
|
115
134
|
type: 'object',
|
|
116
|
-
required: ['order'],
|
|
135
|
+
required: ['order', 'credential_id'],
|
|
117
136
|
properties: {
|
|
118
137
|
order: { type: 'object' },
|
|
138
|
+
credential_id: { type: 'string' },
|
|
119
139
|
},
|
|
120
140
|
}, async (msg) => {
|
|
121
|
-
const credential =
|
|
141
|
+
const credential = mapCredentialIdToCredential.get(msg.req.credential_id);
|
|
122
142
|
if (!credential) {
|
|
123
|
-
return { res: { code: 404, message: '
|
|
143
|
+
return { res: { code: 404, message: 'Credential not found' } };
|
|
124
144
|
}
|
|
125
145
|
const res = await terminal.client.requestForResponse('CancelOrder', { credential, order: msg.req.order });
|
|
126
146
|
return { res };
|
|
127
147
|
});
|
|
128
148
|
// ListOrders
|
|
129
|
-
terminal.server.provideService('ListOrders', {
|
|
130
|
-
type: 'object',
|
|
131
|
-
required: ['account_id'],
|
|
132
|
-
properties: {
|
|
133
|
-
account_id: { type: 'string' },
|
|
134
|
-
},
|
|
135
|
-
}, async (msg) => {
|
|
136
|
-
const credential = mapAccountIdToCredential.get(msg.req.account_id);
|
|
137
|
-
if (!credential) {
|
|
138
|
-
return { res: { code: 404, message: 'Account not found' } };
|
|
139
|
-
}
|
|
140
|
-
const res = await terminal.client.requestForResponse('ListOrders', {
|
|
141
|
-
credential,
|
|
142
|
-
account_id: msg.req.account_id,
|
|
143
|
-
});
|
|
144
|
-
return { res };
|
|
145
|
-
});
|
|
146
149
|
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAEA,+CAA4C;AAC5C,2CAAkE;AAClE,qCAAoD;AACpD,+BAAqD;AAErD,MAAM,QAAQ,GAAG,mBAAQ,CAAC,WAAW,EAAE,CAAC;AAOxC,MAAM,wBAAwB,GAAG,IAAI,GAAG,EAA+B,CAAC;AAExE,gCAAgC;AAChC,QAAQ,CAAC,MAAM,CAAC,cAAc,CAC5B,4BAA4B,EAC5B;IACE,IAAI,EAAE,QAAQ;IACd,QAAQ,EAAE,CAAC,MAAM,EAAE,SAAS,CAAC;IAC7B,UAAU,EAAE;QACV,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;QACxB,OAAO,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;KAC5B;CACF,EACD,KAAK,EAAE,GAAG,EAAE,EAAE;IACZ,MAAM,UAAU,GAAG,GAAG,CAAC,GAAG,CAAC;IAC3B,MAAM,UAA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ountInfo } from '@yuants/data-account';\nimport { IOrder, ITypedCredential } from '@yuants/data-order';\nimport { Terminal } from '@yuants/protocol';\nimport { ISecret, readSecret, writeSecret } from '@yuants/secret';\nimport { escapeSQL, requestSQL } from '@yuants/sql';\nimport { defer, mergeMap, retry, timer } from 'rxjs';\n\nconst terminal = Terminal.fromNodeEnv();\n\ninterface IExchangeCredential {\n type: string;\n payload: any;\n}\n\nconst mapAccountIdToCredential = new Map<string, IExchangeCredential>();\n\n// 1. RegisterExchangeCredential\nterminal.server.provideService<IExchangeCredential, void>(\n 'RegisterExchangeCredential',\n {\n type: 'object',\n required: ['type', 'payload'],\n properties: {\n type: { type: 'string' },\n payload: { type: 'object' },\n },\n },\n async (msg) => {\n const credential = msg.req;\n const secretData = new TextEncoder().encode(JSON.stringify(credential));\n await writeSecret(terminal, terminal.keyPair.public_key, { type: 'exchange_credential' }, secretData);\n return { res: { code: 0, message: 'OK' } };\n },\n);\n\n// 2. ListExchangeCredential\nterminal.server.provideService<void, IExchangeCredential[]>('ListExchangeCredential', {}, async () => {\n const secrets = await requestSQL<ISecret[]>(\n terminal,\n `select * from secret where tags->>'type' = 'exchange_credential' and reader = ${escapeSQL(\n terminal.keyPair.public_key,\n )}`,\n );\n const credentials: IExchangeCredential[] = [];\n for (const secret of secrets) {\n try {\n const decrypted = await readSecret(terminal, secret);\n const credential = JSON.parse(new TextDecoder().decode(decrypted));\n credentials.push(credential);\n } catch (e) {\n console.error('Failed to decrypt secret', e);\n }\n }\n return { res: { code: 0, message: 'OK', data: credentials } };\n});\n\n// 9. Background listWatch\nconst updateIndex = async () => {\n const secrets = await requestSQL<ISecret[]>(\n terminal,\n `select * from secret where tags->>'type' = 'exchange_credential' and reader = ${escapeSQL(\n terminal.keyPair.public_key,\n )}`,\n );\n for (const secret of secrets) {\n try {\n const decrypted = await readSecret(terminal, secret);\n const credential = JSON.parse(new TextDecoder().decode(decrypted)) as IExchangeCredential;\n\n // Call ListAccounts to get account_ids\n const res = await terminal.client.requestForResponse<\n { credential: ITypedCredential<any> },\n Array<{ account_id: string }>\n >('ListAccounts', { credential });\n\n if (res.code === 0 && res.data) {\n for (const account of res.data) {\n mapAccountIdToCredential.set(account.account_id, credential);\n }\n }\n } catch (e) {\n console.error('Failed to process secret', e);\n }\n }\n};\n\n// Run updateIndex periodically\ntimer(0, 60000)\n .pipe(mergeMap(() => defer(updateIndex).pipe(retry({ delay: 5000 }))))\n .subscribe();\n\n// 7. QueryAccounts\nterminal.server.provideService<void, string[]>('QueryAccounts', {}, async () => {\n return { res: { code: 0, message: 'OK', data: Array.from(mapAccountIdToCredential.keys()) } };\n});\n\n// 8. QueryAccountInfo\nterminal.server.provideService<{ account_id: string }, IAccountInfo>(\n 'QueryAccountInfo',\n {\n type: 'object',\n required: ['account_id'],\n properties: {\n account_id: { type: 'string' },\n },\n },\n async (msg) => {\n const credential = mapAccountIdToCredential.get(msg.req.account_id);\n if (!credential) {\n return { res: { code: 404, message: 'Account not found' } };\n }\n const res = await terminal.client.requestForResponse<\n { credential: ITypedCredential<any>; account_id: string },\n IAccountInfo\n >('GetAccountInfo', { credential, account_id: msg.req.account_id });\n return { res };\n },\n);\n\n// 10. Proxy Orders\n// SubmitOrder\nterminal.server.provideService<{ order: IOrder }, { order_id: string }>(\n 'SubmitOrder',\n {\n type: 'object',\n required: ['order'],\n properties: {\n order: { type: 'object' },\n },\n },\n async (msg) => {\n const credential = mapAccountIdToCredential.get(msg.req.order.account_id);\n if (!credential) {\n return { res: { code: 404, message: 'Account not found' } };\n }\n const res = await terminal.client.requestForResponse<\n { credential: ITypedCredential<any>; order: IOrder },\n { order_id: string }\n >('SubmitOrder', { credential, order: msg.req.order });\n return { res };\n },\n);\n\n// ModifyOrder\nterminal.server.provideService<{ order: IOrder }, void>(\n 'ModifyOrder',\n {\n type: 'object',\n required: ['order'],\n properties: {\n order: { type: 'object' },\n },\n },\n async (msg) => {\n const credential = mapAccountIdToCredential.get(msg.req.order.account_id);\n if (!credential) {\n return { res: { code: 404, message: 'Account not found' } };\n }\n const res = await terminal.client.requestForResponse<\n { credential: ITypedCredential<any>; order: IOrder },\n void\n >('ModifyOrder', { credential, order: msg.req.order });\n return { res };\n },\n);\n\n// CancelOrder\nterminal.server.provideService<{ order: IOrder }, void>(\n 'CancelOrder',\n {\n type: 'object',\n required: ['order'],\n properties: {\n order: { type: 'object' },\n },\n },\n async (msg) => {\n const credential = mapAccountIdToCredential.get(msg.req.order.account_id);\n if (!credential) {\n return { res: { code: 404, message: 'Account not found' } };\n }\n const res = await terminal.client.requestForResponse<\n { credential: ITypedCredential<any>; order: IOrder },\n void\n >('CancelOrder', { credential, order: msg.req.order });\n return { res };\n },\n);\n\n// ListOrders\nterminal.server.provideService<{ account_id: string }, { orders: IOrder[] }>(\n 'ListOrders',\n {\n type: 'object',\n required: ['account_id'],\n properties: {\n account_id: { type: 'string' },\n },\n },\n async (msg) => {\n const credential = mapAccountIdToCredential.get(msg.req.account_id);\n if (!credential) {\n return { res: { code: 404, message: 'Account not found' } };\n }\n const res = await terminal.client.requestForResponse<\n { credential: ITypedCredential<any>; account_id: string },\n { orders: IOrder[] }\n >('ListOrders', {\n credential,\n account_id: msg.req.account_id,\n });\n return { res };\n },\n);\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAEA,+CAA4C;AAC5C,2CAAkE;AAClE,qCAAoD;AACpD,+BAAqD;AAErD,MAAM,QAAQ,GAAG,mBAAQ,CAAC,WAAW,EAAE,CAAC;AAOxC,MAAM,2BAA2B,GAAG,IAAI,GAAG,EAA+B,CAAC;AAE3E,gCAAgC;AAChC,QAAQ,CAAC,MAAM,CAAC,cAAc,CAC5B,4CAA4C,EAC5C;IACE,IAAI,EAAE,QAAQ;IACd,QAAQ,EAAE,CAAC,MAAM,EAAE,SAAS,CAAC;IAC7B,UAAU,EAAE;QACV,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;QACxB,OAAO,EAAE,E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AE;QACf,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,GAAG,EAAE,OAAO,EAAE,sBAAsB,EAAE,EAAE,CAAC;KAChE;IACD,MAAM,GAAG,GAAG,MAAM,QAAQ,CAAC,MAAM,CAAC,kBAAkB,CAGlD,aAAa,EAAE,EAAE,UAAU,EAAE,KAAK,EAAE,GAAG,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,CAAC;IACvD,OAAO,EAAE,GAAG,EAAE,CAAC;AACjB,CAAC,CACF,CAAC;AAEF,aAAa","sourcesContent":["import { IAccountInfo } from '@yuants/data-account';\nimport { IOrder, ITypedCredential } from '@yuants/data-order';\nimport { Terminal } from '@yuants/protocol';\nimport { ISecret, readSecret, writeSecret } from '@yuants/secret';\nimport { escapeSQL, requestSQL } from '@yuants/sql';\nimport { defer, mergeMap, retry, timer } from 'rxjs';\n\nconst terminal = Terminal.fromNodeEnv();\n\ninterface IExchangeCredential {\n type: string;\n payload: any;\n}\n\nconst mapCredentialIdToCredential = new Map<string, IExchangeCredential>();\n\n// 1. RegisterExchangeCredential\nterminal.server.provideService<IExchangeCredential, void>(\n 'VirtualExchange/RegisterExchangeCredential',\n {\n type: 'object',\n required: ['type', 'payload'],\n properties: {\n type: { type: 'string' },\n payload: { type: 'object' },\n },\n },\n async (msg) => {\n const credential = msg.req;\n const secretData = new TextEncoder().encode(JSON.stringify(credential));\n await writeSecret(terminal, terminal.keyPair.public_key, { type: 'exchange_credential' }, secretData);\n return { res: { code: 0, message: 'OK' } };\n },\n);\n\n// 2. ListExchangeCredential\nterminal.server.provideService<void, IExchangeCredential[]>(\n 'VirtualExchange/ListExchangeCredential',\n {},\n async () => {\n const secrets = await requestSQL<ISecret[]>(\n terminal,\n `select * from secret where tags->>'type' = 'exchange_credential' and reader = ${escapeSQL(\n terminal.keyPair.public_key,\n )}`,\n );\n const credentials: IExchangeCredential[] = [];\n for (const secret of secrets) {\n try {\n const decrypted = await readSecret(terminal, secret);\n const credential = JSON.parse(new TextDecoder().decode(decrypted));\n credentials.push(credential);\n } catch (e) {\n console.error('Failed to decrypt secret', e);\n }\n }\n return { res: { code: 0, message: 'OK', data: credentials } };\n },\n);\n\nterminal.server.provideService<void, string[]>('VirtualExchange/ListCredentials', {}, async () => {\n return { res: { code: 0, message: 'OK', data: [...mapCredentialIdToCredential.keys()] } };\n});\n\n// 9. Background listWatch\nconst updateIndex = async () => {\n const secrets = await requestSQL<ISecret[]>(\n terminal,\n `select * from secret where tags->>'type' = 'exchange_credential' and reader = ${escapeSQL(\n terminal.keyPair.public_key,\n )}`,\n );\n for (const secret of secrets) {\n try {\n const decrypted = await readSecret(terminal, secret);\n const credential = JSON.parse(new TextDecoder().decode(decrypted)) as IExchangeCredential;\n\n // Call GetCredentialId to get credential id\n const res = await terminal.client.requestForResponse<{ credential: ITypedCredential<any> }, string>(\n 'GetCredentialId',\n { credential },\n );\n\n if (res.code === 0 && res.data) {\n const credentialId = res.data;\n mapCredentialIdToCredential.set(credentialId, credential);\n }\n } catch (e) {\n console.error('Failed to process secret', e);\n }\n }\n};\n\n// Run updateIndex periodically\ntimer(0, 60000)\n .pipe(mergeMap(() => defer(updateIndex).pipe(retry({ delay: 5000 }))))\n .subscribe();\n\n// 8. QueryAccountInfo\nterminal.server.provideService<{ credential_id: string; product_id?: string }, IAccountInfo>(\n 'VirtualExchange/QueryPositions',\n {\n type: 'object',\n required: ['credential_id'],\n properties: {\n credential_id: { type: 'string' },\n product_id: { type: 'string' },\n },\n },\n async (msg) => {\n const credential = mapCredentialIdToCredential.get(msg.req.credential_id);\n if (!credential) {\n return { res: { code: 404, message: 'Credential not found' } };\n }\n const res = await terminal.client.requestForResponse<\n { credential: ITypedCredential<any>; product_id?: string },\n IAccountInfo\n >('QueryPositions', { credential, product_id: msg.req.product_id });\n return { res };\n },\n);\n\nterminal.server.provideService<{ credential_id: string; product_id?: string }, { orders: IOrder[] }>(\n 'VirtualExchange/QueryOrders',\n {\n type: 'object',\n required: ['credential_id'],\n properties: {\n credential_id: { type: 'string' },\n product_id: { type: 'string' },\n },\n },\n async (msg) => {\n const credential = mapCredentialIdToCredential.get(msg.req.credential_id);\n if (!credential) {\n return { res: { code: 404, message: 'Credential not found' } };\n }\n const res = await terminal.client.requestForResponse<\n { credential: ITypedCredential<any>; product_id?: string },\n { orders: IOrder[] }\n >('QueryOrders', {\n credential,\n product_id: msg.req.product_id,\n });\n return { res };\n },\n);\n\n// 10. Proxy Orders\n// SubmitOrder\nterminal.server.provideService<{ order: IOrder; credential_id: string }, { order_id: string }>(\n 'VirtualExchange/SubmitOrder',\n {\n type: 'object',\n required: ['order', 'credential_id'],\n properties: {\n order: { type: 'object' },\n credential_id: { type: 'string' },\n },\n },\n async (msg) => {\n const credential = mapCredentialIdToCredential.get(msg.req.credential_id);\n if (!credential) {\n return { res: { code: 404, message: 'Credential not found' } };\n }\n const res = await terminal.client.requestForResponse<\n { credential: ITypedCredential<any>; order: IOrder },\n { order_id: string }\n >('SubmitOrder', { credential, order: msg.req.order });\n return { res };\n },\n);\n\n// ModifyOrder\nterminal.server.provideService<{ order: IOrder; credential_id: string }, void>(\n 'VirtualExchange/ModifyOrder',\n {\n type: 'object',\n required: ['order', 'credential_id'],\n properties: {\n order: { type: 'object' },\n credential_id: { type: 'string' },\n },\n },\n async (msg) => {\n const credential = mapCredentialIdToCredential.get(msg.req.credential_id);\n if (!credential) {\n return { res: { code: 404, message: 'Credential not found' } };\n }\n const res = await terminal.client.requestForResponse<\n { credential: ITypedCredential<any>; order: IOrder },\n void\n >('ModifyOrder', { credential, order: msg.req.order });\n return { res };\n },\n);\n\n// CancelOrder\nterminal.server.provideService<{ order: IOrder; credential_id: string }, void>(\n 'VirtualExchange/CancelOrder',\n {\n type: 'object',\n required: ['order', 'credential_id'],\n properties: {\n order: { type: 'object' },\n credential_id: { type: 'string' },\n },\n },\n async (msg) => {\n const credential = mapCredentialIdToCredential.get(msg.req.credential_id);\n if (!credential) {\n return { res: { code: 404, message: 'Credential not found' } };\n }\n const res = await terminal.client.requestForResponse<\n { credential: ITypedCredential<any>; order: IOrder },\n void\n >('CancelOrder', { credential, order: msg.req.order });\n return { res };\n },\n);\n\n// ListOrders\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yuants/app-virtual-exchange",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist",
|
|
@@ -8,12 +8,12 @@
|
|
|
8
8
|
"temp"
|
|
9
9
|
],
|
|
10
10
|
"dependencies": {
|
|
11
|
-
"@yuants/protocol": "0.
|
|
12
|
-
"@yuants/utils": "0.
|
|
13
|
-
"@yuants/data-account": "0.8.
|
|
14
|
-
"@yuants/data-order": "0.6.
|
|
15
|
-
"@yuants/secret": "0.3.
|
|
16
|
-
"@yuants/sql": "0.9.
|
|
11
|
+
"@yuants/protocol": "0.53.0",
|
|
12
|
+
"@yuants/utils": "0.13.0",
|
|
13
|
+
"@yuants/data-account": "0.8.2",
|
|
14
|
+
"@yuants/data-order": "0.6.4",
|
|
15
|
+
"@yuants/secret": "0.3.11",
|
|
16
|
+
"@yuants/sql": "0.9.28",
|
|
17
17
|
"rxjs": "~7.5.6",
|
|
18
18
|
"ajv": "~8.12.0"
|
|
19
19
|
},
|
package/temp/package-deps.json
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
{
|
|
2
|
-
"apps/virtual-exchange/CHANGELOG.json": "
|
|
3
|
-
"apps/virtual-exchange/CHANGELOG.md": "
|
|
2
|
+
"apps/virtual-exchange/CHANGELOG.json": "2cd374d6d4056bd811d5d5203b1d42bb384ca5c3",
|
|
3
|
+
"apps/virtual-exchange/CHANGELOG.md": "b6255a27348857b64bff46895baada2240aefc99",
|
|
4
4
|
"apps/virtual-exchange/api-extractor.json": "62f4fd324425b9a235f0c117975967aab09ced0c",
|
|
5
5
|
"apps/virtual-exchange/config/jest.config.json": "4bb17bde3ee911163a3edb36a6eb71491d80b1bd",
|
|
6
6
|
"apps/virtual-exchange/config/rig.json": "f6c7b5537dc77a3170ba9f008bae3b6c3ee11956",
|
|
7
7
|
"apps/virtual-exchange/config/typescript.json": "854907e8a821f2050f6533368db160c649c25348",
|
|
8
8
|
"apps/virtual-exchange/etc/app-virtual-exchange.api.md": "6cb40ec1fa2d40a31a7b0dd3f02b8b24a4d7c4de",
|
|
9
|
-
"apps/virtual-exchange/package.json": "
|
|
10
|
-
"apps/virtual-exchange/src/index.ts": "
|
|
9
|
+
"apps/virtual-exchange/package.json": "b88aa74b5095fe55485fe08ccd30ecc511c283ba",
|
|
10
|
+
"apps/virtual-exchange/src/index.ts": "8bc86fc3fdf97602be9772127fcec48f5edb5c6c",
|
|
11
11
|
"apps/virtual-exchange/tsconfig.json": "22f94ca28b507f8ddcc21b9053158eefd3f726a9",
|
|
12
12
|
"apps/virtual-exchange/.rush/temp/shrinkwrap-deps.json": "8c5c8eb58b6b8757344017c67c08f71d4e6dd4d7",
|
|
13
|
-
"libraries/protocol/temp/package-deps.json": "
|
|
14
|
-
"libraries/utils/temp/package-deps.json": "
|
|
15
|
-
"libraries/data-account/temp/package-deps.json": "
|
|
16
|
-
"libraries/data-order/temp/package-deps.json": "
|
|
17
|
-
"libraries/secret/temp/package-deps.json": "
|
|
18
|
-
"libraries/sql/temp/package-deps.json": "
|
|
13
|
+
"libraries/protocol/temp/package-deps.json": "00d9a96a90bfc0e57ef3b9547151f2970a600402",
|
|
14
|
+
"libraries/utils/temp/package-deps.json": "4a683c07df7c75079b700111b9f6beb36c74ff7d",
|
|
15
|
+
"libraries/data-account/temp/package-deps.json": "bf8b41c4d878dab0f39ea684fffaf6bc3c74c988",
|
|
16
|
+
"libraries/data-order/temp/package-deps.json": "4a0272551da88c9eb52150775640b12c31786de8",
|
|
17
|
+
"libraries/secret/temp/package-deps.json": "8ebb302ac2e6fc0e98a3fc31f316796d9a0718e6",
|
|
18
|
+
"libraries/sql/temp/package-deps.json": "d227fb17cbb9f6e3b445a3b88e0d9978f5c1c823",
|
|
19
19
|
"tools/toolkit/temp/package-deps.json": "23e053490eb8feade23e4d45de4e54883e322711"
|
|
20
20
|
}
|