@yuants/app-virtual-exchange 0.5.2 → 0.5.3

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
+ import { createCache } from '@yuants/cache';
1
2
  import { getCredentialId } from '@yuants/exchange';
2
3
  import { Terminal } from '@yuants/protocol';
3
4
  import { readSecret, writeSecret } from '@yuants/secret';
4
5
  import { escapeSQL, requestSQL } from '@yuants/sql';
6
+ import { newError } from '@yuants/utils';
5
7
  import { defer, firstValueFrom, map, repeat, retry, shareReplay } from 'rxjs';
6
8
  const terminal = Terminal.fromNodeEnv();
7
9
  const credentialReader = process.env.NODE_UNIT_PUBLIC_KEY || terminal.keyPair.public_key;
@@ -33,8 +35,8 @@ terminal.server.provideService('VEX/RegisterExchangeCredential', {
33
35
  }, async (msg) => {
34
36
  const credential = msg.req;
35
37
  const secretData = new TextEncoder().encode(JSON.stringify(credential));
36
- await writeSecret(terminal, credentialReader, { type: 'exchange_credential' }, secretData);
37
- return { res: { code: 0, message: 'OK' } };
38
+ const secret = await writeSecret(terminal, credentialReader, { type: 'exchange_credential' }, secretData);
39
+ return { res: { code: 0, message: 'OK', data: secret } };
38
40
  });
39
41
  terminal.server.provideService('VEX/ListExchangeCredential', {}, async () => {
40
42
  return { res: { code: 0, message: 'OK', data: await listAllCredentials() } };
@@ -43,8 +45,11 @@ terminal.server.provideService('VEX/ListCredentials', {}, async () => {
43
45
  const credentials = await firstValueFrom(validCredentials$);
44
46
  return { res: { code: 0, message: 'OK', data: [...credentials.keys()] } };
45
47
  });
46
- export const validCredentials$ = defer(() => listAllCredentials()).pipe(map((x) => {
48
+ const credentialCache = createCache(() => listAllCredentials(), { swrAfter: 10000, expire: 3600000 });
49
+ export const validCredentials$ = defer(() => credentialCache.query('')).pipe(map((x) => {
47
50
  const map = new Map();
51
+ if (!x)
52
+ return map;
48
53
  for (const xx of x) {
49
54
  if (xx.credentialId && xx.credential) {
50
55
  map.set(xx.credentialId, xx.credential);
@@ -63,4 +68,22 @@ export const getCredentialById = async (credential_id) => {
63
68
  const credentials = await firstValueFrom(validCredentials$);
64
69
  return credentials.get(credential_id);
65
70
  };
71
+ export const getCredentialBySecretId = async (secret_id) => {
72
+ const allCredentials = await credentialCache.query('');
73
+ const theCredential = allCredentials === null || allCredentials === void 0 ? void 0 : allCredentials.find((x) => x.secret.sign === secret_id);
74
+ if (!theCredential) {
75
+ throw newError('CREDENTIAL_NOT_FOUND', { secret_id });
76
+ }
77
+ if (!theCredential.credential) {
78
+ throw newError('CREDENTIAL_NOT_RESOLVED', { secret_id });
79
+ }
80
+ if (!theCredential.credentialId) {
81
+ throw newError('CREDENTIAL_ID_NOT_RESOLVED', { secret_id });
82
+ }
83
+ return {
84
+ secret: theCredential.secret,
85
+ credential: theCredential.credential,
86
+ credentialId: theCredential.credentialId,
87
+ };
88
+ };
66
89
  //# sourceMappingURL=credential.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"credential.js","sourceRoot":"","sources":["../src/credential.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AACnD,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,cAAc,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,EAAE,MAAM,MAAM,CAAC;AAO9E,MAAM,QAAQ,GAAG,QAAQ,CAAC,WAAW,EAAE,CAAC;AAExC,MAAM,gBAAgB,GAAG,OAAO,CAAC,GAAG,CAAC,oBAAoB,IAAI,QAAQ,CAAC,OAAO,CAAC,UAAU,CAAC;AASzF,MAAM,CAAC,MAAM,kBAAkB,GAAG,KAAK,IAAI,EAAE;IAC3C,MAAM,OAAO,GAAG,M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CAAC,YAAY,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC;SACzC;KACF;IACD,OAAO,GAAG,CAAC;AACb,CAAC,CAAC,EACF,MAAM,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,EACxB,KAAK,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,EACtB,WAAW,CAAC,CAAC,CAAC,CACf,CAAC;AAEF,MAAM,CAAC,MAAM,qBAAqB,GAAG,iBAAiB,CAAC,IAAI,CACzD,GAAG,CAAC,CAAC,WAAW,EAAE,EAAE;IAClB,MAAM,KAAK,GAAG,IAAI,GAAG,EAAU,CAAC;IAChC,WAAW,CAAC,OAAO,CAAC,CAAC,UAAU,EAAE,EAAE;QACjC,KAAK,CAAC,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;IAC7B,CAAC,CAAC,CAAC;IACH,OAAO,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAC3B,CAAC,CAAC,CACH,CAAC;AAEF,MAAM,CAAC,MAAM,iBAAiB,GAAG,KAAK,EAAE,aAAqB,EAAE,EAAE;IAC/D,MAAM,WAAW,GAAG,MAAM,cAAc,CAAC,iBAAiB,CAAC,CAAC;IAC5D,OAAO,WAAW,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;AACxC,CAAC,CAAC","sourcesContent":["import { getCredentialId } from '@yuants/exchange';\nimport { Terminal } from '@yuants/protocol';\nimport { ISecret, readSecret, writeSecret } from '@yuants/secret';\nimport { escapeSQL, requestSQL } from '@yuants/sql';\nimport { defer, firstValueFrom, map, repeat, retry, shareReplay } from 'rxjs';\n\nexport interface IExchangeCredential {\n type: string;\n payload: any;\n}\n\nconst terminal = Terminal.fromNodeEnv();\n\nconst credentialReader = process.env.NODE_UNIT_PUBLIC_KEY || terminal.keyPair.public_key;\n\ninterface ICredentialResolvedStatus {\n secret: ISecret;\n credential?: IExchangeCredential;\n credentialId?: string;\n reason?: string;\n}\n\nexport const listAllCredentials = async () => {\n const secrets = await requestSQL<ISecret[]>(\n terminal,\n `select * from secret where tags->>'type' = 'exchange_credential' and reader = ${escapeSQL(\n credentialReader,\n )}`,\n );\n\n const results = secrets.map((secret): ICredentialResolvedStatus => ({ secret }));\n\n await Promise.all(\n results.map(async (result) => {\n try {\n const decrypted = await readSecret(terminal, result.secret);\n const credential = JSON.parse(new TextDecoder().decode(decrypted)) as IExchangeCredential;\n result.credential = credential;\n\n const res = await getCredentialId(terminal, credential);\n const credentialId = res.data;\n result.credentialId = credentialId;\n } catch (e) {\n result.reason = `${e}`;\n }\n }),\n );\n\n return results;\n};\n\nterminal.server.provideService<IExchangeCredential, void>(\n 'VEX/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, credentialReader, { type: 'exchange_credential' }, secretData);\n return { res: { code: 0, message: 'OK' } };\n },\n);\n\nterminal.server.provideService<void, ICredentialResolvedStatus[]>(\n 'VEX/ListExchangeCredential',\n {},\n async () => {\n return { res: { code: 0, message: 'OK', data: await listAllCredentials() } };\n },\n);\n\nterminal.server.provideService<void, string[]>('VEX/ListCredentials', {}, async () => {\n const credentials = await firstValueFrom(validCredentials$);\n return { res: { code: 0, message: 'OK', data: [...credentials.keys()] } };\n});\n\nexport const validCredentials$ = defer(() => listAllCredentials()).pipe(\n map((x) => {\n const map = new Map<string, IExchangeCredential>();\n for (const xx of x) {\n if (xx.credentialId && xx.credential) {\n map.set(xx.credentialId, xx.credential);\n }\n }\n return map;\n }),\n repeat({ delay: 10000 }),\n retry({ delay: 5000 }),\n shareReplay(1),\n);\n\nexport const validCredentialTypes$ = validCredentials$.pipe(\n map((credentials) => {\n const types = new Set<string>();\n credentials.forEach((credential) => {\n types.add(credential.type);\n });\n return Array.from(types);\n }),\n);\n\nexport const getCredentialById = async (credential_id: string) => {\n const credentials = await firstValueFrom(validCredentials$);\n return credentials.get(credential_id);\n};\n"]}
1
+ {"version":3,"file":"credential.js","sourceRoot":"","sources":["../src/credential.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AACnD,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,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,KAAK,EAAE,cAAc,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,EAAE,MAAM,MAAM,CAAC;AAO9E,MAAM,QAAQ,GAAG,QAAQ,CAAC,WAAW,EAAE,CAAC;AAExC,MAAM,gBAAgB,GAAG,OAAO,CAAC,GAAG,CAAC,oBAAoB,IAAI,QAAQ,CAAC,OAAO,CAAC,UAAU,CAAC;AASzF,MAAM,CAAC,MAAM,kBAAkB,GAAG,KAAK,IAAI,EAAE;IAC3C,MAAM,OAAO,GAAG,MAAM,UAAU,CAC9B,QAAQ,EACR,iFAAiF,SAAS,CACxF,gBAAgB,CACjB,EAAE,CACJ,CAAC;IAEF,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAA6B,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC;IAEjF,MAAM,OAAO,CAAC,GAAG,CACf,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE;QAC3B,IAAI;YACF,MAAM,SAAS,GAAG,MAAM,UAAU,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;YAC5D,MAAM,UAAU,GAAG,IAAI,CAAC,KAAK,CAAC,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AC,4BAA4B,EAAE,EAAE,SAAS,EAAE,CAAC,CAAC;KAC7D;IAED,OAAO;QACL,MAAM,EAAE,aAAa,CAAC,MAAM;QAC5B,UAAU,EAAE,aAAa,CAAC,UAAU;QACpC,YAAY,EAAE,aAAa,CAAC,YAAY;KACzC,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import { createCache } from '@yuants/cache';\nimport { getCredentialId } from '@yuants/exchange';\nimport { Terminal } from '@yuants/protocol';\nimport { ISecret, readSecret, writeSecret } from '@yuants/secret';\nimport { escapeSQL, requestSQL } from '@yuants/sql';\nimport { newError } from '@yuants/utils';\nimport { defer, firstValueFrom, map, repeat, retry, shareReplay } from 'rxjs';\n\nexport interface IExchangeCredential {\n type: string;\n payload: any;\n}\n\nconst terminal = Terminal.fromNodeEnv();\n\nconst credentialReader = process.env.NODE_UNIT_PUBLIC_KEY || terminal.keyPair.public_key;\n\ninterface ICredentialResolvedStatus {\n secret: ISecret;\n credential?: IExchangeCredential;\n credentialId?: string;\n reason?: string;\n}\n\nexport const listAllCredentials = async () => {\n const secrets = await requestSQL<ISecret[]>(\n terminal,\n `select * from secret where tags->>'type' = 'exchange_credential' and reader = ${escapeSQL(\n credentialReader,\n )}`,\n );\n\n const results = secrets.map((secret): ICredentialResolvedStatus => ({ secret }));\n\n await Promise.all(\n results.map(async (result) => {\n try {\n const decrypted = await readSecret(terminal, result.secret);\n const credential = JSON.parse(new TextDecoder().decode(decrypted)) as IExchangeCredential;\n result.credential = credential;\n\n const res = await getCredentialId(terminal, credential);\n const credentialId = res.data;\n result.credentialId = credentialId;\n } catch (e) {\n result.reason = `${e}`;\n }\n }),\n );\n\n return results;\n};\n\nterminal.server.provideService<IExchangeCredential, ISecret>(\n 'VEX/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 const secret = await writeSecret(terminal, credentialReader, { type: 'exchange_credential' }, secretData);\n return { res: { code: 0, message: 'OK', data: secret } };\n },\n);\n\nterminal.server.provideService<void, ICredentialResolvedStatus[]>(\n 'VEX/ListExchangeCredential',\n {},\n async () => {\n return { res: { code: 0, message: 'OK', data: await listAllCredentials() } };\n },\n);\n\nterminal.server.provideService<void, string[]>('VEX/ListCredentials', {}, async () => {\n const credentials = await firstValueFrom(validCredentials$);\n return { res: { code: 0, message: 'OK', data: [...credentials.keys()] } };\n});\n\nconst credentialCache = createCache(() => listAllCredentials(), { swrAfter: 10_000, expire: 3600_000 });\n\nexport const validCredentials$ = defer(() => credentialCache.query('')).pipe(\n map((x) => {\n const map = new Map<string, IExchangeCredential>();\n if (!x) return map;\n for (const xx of x) {\n if (xx.credentialId && xx.credential) {\n map.set(xx.credentialId, xx.credential);\n }\n }\n return map;\n }),\n repeat({ delay: 10000 }),\n retry({ delay: 5000 }),\n shareReplay(1),\n);\n\nexport const validCredentialTypes$ = validCredentials$.pipe(\n map((credentials) => {\n const types = new Set<string>();\n credentials.forEach((credential) => {\n types.add(credential.type);\n });\n return Array.from(types);\n }),\n);\n\nexport const getCredentialById = async (credential_id: string) => {\n const credentials = await firstValueFrom(validCredentials$);\n return credentials.get(credential_id);\n};\n\nexport const getCredentialBySecretId = async (\n secret_id: string,\n): Promise<Required<Pick<ICredentialResolvedStatus, 'secret' | 'credential' | 'credentialId'>>> => {\n const allCredentials = await credentialCache.query('');\n const theCredential = allCredentials?.find((x) => x.secret.sign === secret_id);\n if (!theCredential) {\n throw newError('CREDENTIAL_NOT_FOUND', { secret_id });\n }\n if (!theCredential.credential) {\n throw newError('CREDENTIAL_NOT_RESOLVED', { secret_id });\n }\n if (!theCredential.credentialId) {\n throw newError('CREDENTIAL_ID_NOT_RESOLVED', { secret_id });\n }\n\n return {\n secret: theCredential.secret,\n credential: theCredential.credential,\n credentialId: theCredential.credentialId,\n };\n};\n"]}
package/dist/general.js CHANGED
@@ -1,38 +1,32 @@
1
1
  import { cancelOrder, getOrders, getPositions, modifyOrder, submitOrder } from '@yuants/exchange';
2
2
  import { Terminal } from '@yuants/protocol';
3
- import { getCredentialById } from './credential';
3
+ import { getCredentialBySecretId } from './credential';
4
4
  const terminal = Terminal.fromNodeEnv();
5
5
  terminal.server.provideService('VEX/GetPositions', {
6
6
  type: 'object',
7
- required: ['credential_id'],
7
+ required: ['secret_id'],
8
8
  properties: {
9
- credential_id: { type: 'string' },
9
+ secret_id: { type: 'string' },
10
10
  product_id: { type: 'string' },
11
11
  },
12
12
  }, async (msg) => {
13
- const credential = await getCredentialById(msg.req.credential_id);
14
- if (!credential) {
15
- return { res: { code: 404, message: 'Credential not found' } };
16
- }
17
- const res = await getPositions(terminal, credential, msg.req.product_id);
13
+ const credential = await getCredentialBySecretId(msg.req.secret_id);
14
+ const res = await getPositions(terminal, credential.credential, msg.req.product_id);
18
15
  return { res };
19
16
  });
20
17
  terminal.server.provideService('VEX/GetOrders', {
21
18
  type: 'object',
22
- required: ['credential_id'],
19
+ required: ['secret_id'],
23
20
  properties: {
24
- credential_id: { type: 'string' },
21
+ secret_id: { type: 'string' },
25
22
  product_id: { type: 'string' },
26
23
  },
27
24
  }, async (msg) => {
28
25
  var _a;
29
- const credential = await getCredentialById(msg.req.credential_id);
30
- if (!credential) {
31
- return { res: { code: 404, message: 'Credential not found' } };
32
- }
33
- const res = await getOrders(terminal, credential, msg.req.product_id);
26
+ const credential = await getCredentialBySecretId(msg.req.secret_id);
27
+ const res = await getOrders(terminal, credential.credential, msg.req.product_id);
34
28
  (_a = res.data) === null || _a === void 0 ? void 0 : _a.forEach((order) => {
35
- order.account_id = msg.req.credential_id;
29
+ order.account_id = credential.credentialId;
36
30
  });
37
31
  return { res };
38
32
  });
@@ -40,49 +34,40 @@ terminal.server.provideService('VEX/GetOrders', {
40
34
  // SubmitOrder
41
35
  terminal.server.provideService('VEX/SubmitOrder', {
42
36
  type: 'object',
43
- required: ['order', 'credential_id'],
37
+ required: ['order', 'secret_id'],
44
38
  properties: {
45
39
  order: { type: 'object' },
46
- credential_id: { type: 'string' },
40
+ secret_id: { type: 'string' },
47
41
  },
48
42
  }, async (msg) => {
49
- const credential = await getCredentialById(msg.req.credential_id);
50
- if (!credential) {
51
- return { res: { code: 404, message: 'Credential not found' } };
52
- }
53
- const res = await submitOrder(terminal, credential, msg.req.order);
43
+ const credential = await getCredentialBySecretId(msg.req.secret_id);
44
+ const res = await submitOrder(terminal, credential.credential, msg.req.order);
54
45
  return { res };
55
46
  });
56
47
  // ModifyOrder
57
48
  terminal.server.provideService('VEX/ModifyOrder', {
58
49
  type: 'object',
59
- required: ['order', 'credential_id'],
50
+ required: ['order', 'secret_id'],
60
51
  properties: {
61
52
  order: { type: 'object' },
62
- credential_id: { type: 'string' },
53
+ secret_id: { type: 'string' },
63
54
  },
64
55
  }, async (msg) => {
65
- const credential = await getCredentialById(msg.req.credential_id);
66
- if (!credential) {
67
- return { res: { code: 404, message: 'Credential not found' } };
68
- }
69
- const res = await modifyOrder(terminal, credential, msg.req.order);
56
+ const credential = await getCredentialBySecretId(msg.req.secret_id);
57
+ const res = await modifyOrder(terminal, credential.credential, msg.req.order);
70
58
  return { res };
71
59
  });
72
60
  // CancelOrder
73
61
  terminal.server.provideService('VEX/CancelOrder', {
74
62
  type: 'object',
75
- required: ['order', 'credential_id'],
63
+ required: ['order', 'secret_id'],
76
64
  properties: {
77
65
  order: { type: 'object' },
78
- credential_id: { type: 'string' },
66
+ secret_id: { type: 'string' },
79
67
  },
80
68
  }, async (msg) => {
81
- const credential = await getCredentialById(msg.req.credential_id);
82
- if (!credential) {
83
- return { res: { code: 404, message: 'Credential not found' } };
84
- }
85
- const res = await cancelOrder(terminal, credential, msg.req.order);
69
+ const credential = await getCredentialBySecretId(msg.req.secret_id);
70
+ const res = await cancelOrder(terminal, credential.credential, msg.req.order);
86
71
  return { res };
87
72
  });
88
73
  //# sourceMappingURL=general.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"general.js","sourceR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E,EAAE,CAAC;KAChE;IACD,MAAM,GAAG,GAAG,MAAM,WAAW,CAAC,QAAQ,EAAE,UAAU,EAAE,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IACnE,OAAO,EAAE,GAAG,EAAE,CAAC;AACjB,CAAC,CACF,CAAC","sourcesContent":["import { IPosition } from '@yuants/data-account';\nimport { IOrder } from '@yuants/data-order';\nimport { cancelOrder, getOrders, getPositions, modifyOrder, submitOrder } from '@yuants/exchange';\nimport { Terminal } from '@yuants/protocol';\nimport { getCredentialById } from './credential';\n\nconst terminal = Terminal.fromNodeEnv();\n\nterminal.server.provideService<{ credential_id: string; product_id?: string }, IPosition[]>(\n 'VEX/GetPositions',\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 = await getCredentialById(msg.req.credential_id);\n if (!credential) {\n return { res: { code: 404, message: 'Credential not found' } };\n }\n const res = await getPositions(terminal, credential, msg.req.product_id);\n return { res };\n },\n);\n\nterminal.server.provideService<{ credential_id: string; product_id?: string }, IOrder[]>(\n 'VEX/GetOrders',\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 = await getCredentialById(msg.req.credential_id);\n if (!credential) {\n return { res: { code: 404, message: 'Credential not found' } };\n }\n const res = await getOrders(terminal, credential, msg.req.product_id);\n res.data?.forEach((order) => {\n order.account_id = msg.req.credential_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 'VEX/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 = await getCredentialById(msg.req.credential_id);\n if (!credential) {\n return { res: { code: 404, message: 'Credential not found' } };\n }\n const res = await submitOrder(terminal, credential, msg.req.order);\n return { res };\n },\n);\n\n// ModifyOrder\nterminal.server.provideService<{ order: IOrder; credential_id: string }, void>(\n 'VEX/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 = await getCredentialById(msg.req.credential_id);\n if (!credential) {\n return { res: { code: 404, message: 'Credential not found' } };\n }\n const res = await modifyOrder(terminal, credential, msg.req.order);\n return { res };\n },\n);\n\n// CancelOrder\nterminal.server.provideService<{ order: IOrder; credential_id: string }, void>(\n 'VEX/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 = await getCredentialById(msg.req.credential_id);\n if (!credential) {\n return { res: { code: 404, message: 'Credential not found' } };\n }\n const res = await cancelOrder(terminal, credential, msg.req.order);\n return { res };\n },\n);\n"]}
1
+ {"version":3,"file":"general.js","sourceRoot":"","sources":["../src/general.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,YAAY,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAClG,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAC;AAEvD,MAAM,QAAQ,GAAG,QAAQ,CAAC,WAAW,EAAE,CAAC;AAExC,QAAQ,CAAC,MAAM,CAAC,cAAc,CAC5B,kBAAkB,EAClB;IACE,IAAI,EAAE,QAAQ;IACd,QAAQ,EAAE,CAAC,WAAW,CAAC;IACvB,UAAU,EAAE;QACV,SAAS,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;QAC7B,UAAU,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;KAC/B;CACF,EACD,KAAK,EAAE,GAAG,EAAE,EAAE;IACZ,MAAM,UAAU,GAAG,MAAM,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CAAC,SAAS,CAAC,CAAC;IACpE,MAAM,GAAG,GAAG,MAAM,WAAW,CAAC,QAAQ,EAAE,UAAU,CAAC,UAAU,EAAE,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IAC9E,OAAO,EAAE,GAAG,EAAE,CAAC;AACjB,CAAC,CACF,CAAC","sourcesContent":["import { IPosition } from '@yuants/data-account';\nimport { IOrder } from '@yuants/data-order';\nimport { cancelOrder, getOrders, getPositions, modifyOrder, submitOrder } from '@yuants/exchange';\nimport { Terminal } from '@yuants/protocol';\nimport { getCredentialBySecretId } from './credential';\n\nconst terminal = Terminal.fromNodeEnv();\n\nterminal.server.provideService<{ secret_id: string; product_id?: string }, IPosition[]>(\n 'VEX/GetPositions',\n {\n type: 'object',\n required: ['secret_id'],\n properties: {\n secret_id: { type: 'string' },\n product_id: { type: 'string' },\n },\n },\n async (msg) => {\n const credential = await getCredentialBySecretId(msg.req.secret_id);\n const res = await getPositions(terminal, credential.credential, msg.req.product_id);\n return { res };\n },\n);\n\nterminal.server.provideService<{ secret_id: string; product_id?: string }, IOrder[]>(\n 'VEX/GetOrders',\n {\n type: 'object',\n required: ['secret_id'],\n properties: {\n secret_id: { type: 'string' },\n product_id: { type: 'string' },\n },\n },\n async (msg) => {\n const credential = await getCredentialBySecretId(msg.req.secret_id);\n const res = await getOrders(terminal, credential.credential, msg.req.product_id);\n res.data?.forEach((order) => {\n order.account_id = credential.credentialId;\n });\n return { res };\n },\n);\n\n// 10. Proxy Orders\n// SubmitOrder\nterminal.server.provideService<{ order: IOrder; secret_id: string }, { order_id: string }>(\n 'VEX/SubmitOrder',\n {\n type: 'object',\n required: ['order', 'secret_id'],\n properties: {\n order: { type: 'object' },\n secret_id: { type: 'string' },\n },\n },\n async (msg) => {\n const credential = await getCredentialBySecretId(msg.req.secret_id);\n const res = await submitOrder(terminal, credential.credential, msg.req.order);\n return { res };\n },\n);\n\n// ModifyOrder\nterminal.server.provideService<{ order: IOrder; secret_id: string }, void>(\n 'VEX/ModifyOrder',\n {\n type: 'object',\n required: ['order', 'secret_id'],\n properties: {\n order: { type: 'object' },\n secret_id: { type: 'string' },\n },\n },\n async (msg) => {\n const credential = await getCredentialBySecretId(msg.req.secret_id);\n const res = await modifyOrder(terminal, credential.credential, msg.req.order);\n return { res };\n },\n);\n\n// CancelOrder\nterminal.server.provideService<{ order: IOrder; secret_id: string }, void>(\n 'VEX/CancelOrder',\n {\n type: 'object',\n required: ['order', 'secret_id'],\n properties: {\n order: { type: 'object' },\n secret_id: { type: 'string' },\n },\n },\n async (msg) => {\n const credential = await getCredentialBySecretId(msg.req.secret_id);\n const res = await cancelOrder(terminal, credential.credential, msg.req.order);\n return { res };\n },\n);\n"]}
@@ -13,5 +13,6 @@ export declare const listAllCredentials: () => Promise<ICredentialResolvedStatus
13
13
  export declare const validCredentials$: import("rxjs").Observable<Map<string, IExchangeCredential>>;
14
14
  export declare const validCredentialTypes$: import("rxjs").Observable<string[]>;
15
15
  export declare const getCredentialById: (credential_id: string) => Promise<IExchangeCredential | undefined>;
16
+ export declare const getCredentialBySecretId: (secret_id: string) => Promise<Required<Pick<ICredentialResolvedStatus, 'secret' | 'credential' | 'credentialId'>>>;
16
17
  export {};
17
18
  //# sourceMappingURL=credential.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"credential.d.ts","sourceRoot":"","sources":["../src/credential.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAA2B,MAAM,gBAAgB,CAAC;AAIlE,MAAM,WAAW,mBAAmB;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,GAAG,CAAC;CACd;AAMD,UAAU,yBAAyB;IACjC,MAAM,EAAE,OAAO,CAAC;IAChB,UAAU,CAAC,EAAE,mBAAmB,CAAC;IACjC,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,eAAO,MAAM,kBAAkB,4CA2B9B,CAAC;AAiCF,eAAO,MAAM,iBAAiB,6DAa7B,CAAC;AAEF,eAAO,MAAM,qBAAqB,qCAQjC,CAAC;AAEF,eAAO,MAAM,iBAAiB,kBAAyB,MAAM,6CAG5D,CAAC"}
1
+ {"version":3,"file":"credential.d.ts","sourceRoot":"","sources":["../src/credential.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,OAAO,EAA2B,MAAM,gBAAgB,CAAC;AAKlE,MAAM,WAAW,mBAAmB;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,GAAG,CAAC;CACd;AAMD,UAAU,yBAAyB;IACjC,MAAM,EAAE,OAAO,CAAC;IAChB,UAAU,CAAC,EAAE,mBAAmB,CAAC;IACjC,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,eAAO,MAAM,kBAAkB,4CA2B9B,CAAC;AAmCF,eAAO,MAAM,iBAAiB,6DAc7B,CAAC;AAEF,eAAO,MAAM,qBAAqB,qCAQjC,CAAC;AAEF,eAAO,MAAM,iBAAiB,kBAAyB,MAAM,6CAG5D,CAAC;AAEF,eAAO,MAAM,uBAAuB,cACvB,MAAM,KAChB,QAAQ,SAAS,KAAK,yBAAyB,EAAE,QAAQ,GAAG,YAAY,GAAG,cAAc,CAAC,CAAC,CAkB7F,CAAC"}
package/lib/credential.js CHANGED
@@ -1,10 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getCredentialById = exports.validCredentialTypes$ = exports.validCredentials$ = exports.listAllCredentials = void 0;
3
+ exports.getCredentialBySecretId = exports.getCredentialById = exports.validCredentialTypes$ = exports.validCredentials$ = exports.listAllCredentials = void 0;
4
+ const cache_1 = require("@yuants/cache");
4
5
  const exchange_1 = require("@yuants/exchange");
5
6
  const protocol_1 = require("@yuants/protocol");
6
7
  const secret_1 = require("@yuants/secret");
7
8
  const sql_1 = require("@yuants/sql");
9
+ const utils_1 = require("@yuants/utils");
8
10
  const rxjs_1 = require("rxjs");
9
11
  const terminal = protocol_1.Terminal.fromNodeEnv();
10
12
  const credentialReader = process.env.NODE_UNIT_PUBLIC_KEY || terminal.keyPair.public_key;
@@ -37,8 +39,8 @@ terminal.server.provideService('VEX/RegisterExchangeCredential', {
37
39
  }, async (msg) => {
38
40
  const credential = msg.req;
39
41
  const secretData = new TextEncoder().encode(JSON.stringify(credential));
40
- await (0, secret_1.writeSecret)(terminal, credentialReader, { type: 'exchange_credential' }, secretData);
41
- return { res: { code: 0, message: 'OK' } };
42
+ const secret = await (0, secret_1.writeSecret)(terminal, credentialReader, { type: 'exchange_credential' }, secretData);
43
+ return { res: { code: 0, message: 'OK', data: secret } };
42
44
  });
43
45
  terminal.server.provideService('VEX/ListExchangeCredential', {}, async () => {
44
46
  return { res: { code: 0, message: 'OK', data: await (0, exports.listAllCredentials)() } };
@@ -47,8 +49,11 @@ terminal.server.provideService('VEX/ListCredentials', {}, async () => {
47
49
  const credentials = await (0, rxjs_1.firstValueFrom)(exports.validCredentials$);
48
50
  return { res: { code: 0, message: 'OK', data: [...credentials.keys()] } };
49
51
  });
50
- exports.validCredentials$ = (0, rxjs_1.defer)(() => (0, exports.listAllCredentials)()).pipe((0, rxjs_1.map)((x) => {
52
+ const credentialCache = (0, cache_1.createCache)(() => (0, exports.listAllCredentials)(), { swrAfter: 10000, expire: 3600000 });
53
+ exports.validCredentials$ = (0, rxjs_1.defer)(() => credentialCache.query('')).pipe((0, rxjs_1.map)((x) => {
51
54
  const map = new Map();
55
+ if (!x)
56
+ return map;
52
57
  for (const xx of x) {
53
58
  if (xx.credentialId && xx.credential) {
54
59
  map.set(xx.credentialId, xx.credential);
@@ -68,4 +73,23 @@ const getCredentialById = async (credential_id) => {
68
73
  return credentials.get(credential_id);
69
74
  };
70
75
  exports.getCredentialById = getCredentialById;
76
+ const getCredentialBySecretId = async (secret_id) => {
77
+ const allCredentials = await credentialCache.query('');
78
+ const theCredential = allCredentials === null || allCredentials === void 0 ? void 0 : allCredentials.find((x) => x.secret.sign === secret_id);
79
+ if (!theCredential) {
80
+ throw (0, utils_1.newError)('CREDENTIAL_NOT_FOUND', { secret_id });
81
+ }
82
+ if (!theCredential.credential) {
83
+ throw (0, utils_1.newError)('CREDENTIAL_NOT_RESOLVED', { secret_id });
84
+ }
85
+ if (!theCredential.credentialId) {
86
+ throw (0, utils_1.newError)('CREDENTIAL_ID_NOT_RESOLVED', { secret_id });
87
+ }
88
+ return {
89
+ secret: theCredential.secret,
90
+ credential: theCredential.credential,
91
+ credentialId: theCredential.credentialId,
92
+ };
93
+ };
94
+ exports.getCredentialBySecretId = getCredentialBySecretId;
71
95
  //# sourceMappingURL=credential.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"credential.js","sourceRoot":"","sources":["../src/credential.ts"],"names":[],"mappings":";;;AAAA,+CAAmD;AACnD,+CAA4C;AAC5C,2CAAkE;AAClE,qCAAoD;AACpD,+BAA8E;AAO9E,MAAM,QAAQ,GAAG,mBAAQ,CAAC,WAAW,EAAE,CAAC;AAExC,MAAM,gBAAgB,GAAG,OAAO,CAAC,GAAG,CAAC,oBAAoB,IAAI,QAAQ,CAAC,OAAO,CAAC,UAAU,CAAC;AASlF,MAAM,kBAAkB,GAAG,KAAK,IAAI,EAAE;IAC3C,MAAM,OAAO,GAAG,MAAM,IAAA,gBAAU,EAC9B,QAAQ,EACR,iFAAiF,IAAA,eAAS,EACxF,gBAAgB,CACjB,EAAE,CACJ,CAAC;IAEF,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAA6B,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC;IAEjF,MAAM,OAAO,CAAC,GAAG,CACf,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE;QAC3B,IAAI;YACF,MAAM,SAAS,GAAG,MAAM,IAAA,mBAAU,EAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;YAC5D,MAAM,U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,WAAW,EAAE,EAAE;IAClB,MAAM,KAAK,GAAG,IAAI,GAAG,EAAU,CAAC;IAChC,WAAW,CAAC,OAAO,CAAC,CAAC,UAAU,EAAE,EAAE;QACjC,KAAK,CAAC,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;IAC7B,CAAC,CAAC,CAAC;IACH,OAAO,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAC3B,CAAC,CAAC,CACH,CAAC;AAEK,MAAM,iBAAiB,GAAG,KAAK,EAAE,aAAqB,EAAE,EAAE;IAC/D,MAAM,WAAW,GAAG,MAAM,IAAA,qBAAc,EAAC,yBAAiB,CAAC,CAAC;IAC5D,OAAO,WAAW,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;AACxC,CAAC,CAAC;AAHW,QAAA,iBAAiB,qBAG5B","sourcesContent":["import { getCredentialId } from '@yuants/exchange';\nimport { Terminal } from '@yuants/protocol';\nimport { ISecret, readSecret, writeSecret } from '@yuants/secret';\nimport { escapeSQL, requestSQL } from '@yuants/sql';\nimport { defer, firstValueFrom, map, repeat, retry, shareReplay } from 'rxjs';\n\nexport interface IExchangeCredential {\n type: string;\n payload: any;\n}\n\nconst terminal = Terminal.fromNodeEnv();\n\nconst credentialReader = process.env.NODE_UNIT_PUBLIC_KEY || terminal.keyPair.public_key;\n\ninterface ICredentialResolvedStatus {\n secret: ISecret;\n credential?: IExchangeCredential;\n credentialId?: string;\n reason?: string;\n}\n\nexport const listAllCredentials = async () => {\n const secrets = await requestSQL<ISecret[]>(\n terminal,\n `select * from secret where tags->>'type' = 'exchange_credential' and reader = ${escapeSQL(\n credentialReader,\n )}`,\n );\n\n const results = secrets.map((secret): ICredentialResolvedStatus => ({ secret }));\n\n await Promise.all(\n results.map(async (result) => {\n try {\n const decrypted = await readSecret(terminal, result.secret);\n const credential = JSON.parse(new TextDecoder().decode(decrypted)) as IExchangeCredential;\n result.credential = credential;\n\n const res = await getCredentialId(terminal, credential);\n const credentialId = res.data;\n result.credentialId = credentialId;\n } catch (e) {\n result.reason = `${e}`;\n }\n }),\n );\n\n return results;\n};\n\nterminal.server.provideService<IExchangeCredential, void>(\n 'VEX/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, credentialReader, { type: 'exchange_credential' }, secretData);\n return { res: { code: 0, message: 'OK' } };\n },\n);\n\nterminal.server.provideService<void, ICredentialResolvedStatus[]>(\n 'VEX/ListExchangeCredential',\n {},\n async () => {\n return { res: { code: 0, message: 'OK', data: await listAllCredentials() } };\n },\n);\n\nterminal.server.provideService<void, string[]>('VEX/ListCredentials', {}, async () => {\n const credentials = await firstValueFrom(validCredentials$);\n return { res: { code: 0, message: 'OK', data: [...credentials.keys()] } };\n});\n\nexport const validCredentials$ = defer(() => listAllCredentials()).pipe(\n map((x) => {\n const map = new Map<string, IExchangeCredential>();\n for (const xx of x) {\n if (xx.credentialId && xx.credential) {\n map.set(xx.credentialId, xx.credential);\n }\n }\n return map;\n }),\n repeat({ delay: 10000 }),\n retry({ delay: 5000 }),\n shareReplay(1),\n);\n\nexport const validCredentialTypes$ = validCredentials$.pipe(\n map((credentials) => {\n const types = new Set<string>();\n credentials.forEach((credential) => {\n types.add(credential.type);\n });\n return Array.from(types);\n }),\n);\n\nexport const getCredentialById = async (credential_id: string) => {\n const credentials = await firstValueFrom(validCredentials$);\n return credentials.get(credential_id);\n};\n"]}
1
+ {"version":3,"file":"credential.js","sourceRoot":"","sources":["../src/credential.ts"],"names":[],"mappings":";;;AAAA,yCAA4C;AAC5C,+CAAmD;AACnD,+CAA4C;AAC5C,2CAAkE;AAClE,qCAAoD;AACpD,yCAAyC;AACzC,+BAA8E;AAO9E,MAAM,QAAQ,GAAG,mBAAQ,CAAC,WAAW,EAAE,CAAC;AAExC,MAAM,gBAAgB,GAAG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EAAC,yBAAiB,CAAC,CAAC;IAC5D,OAAO,WAAW,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;AACxC,CAAC,CAAC;AAHW,QAAA,iBAAiB,qBAG5B;AAEK,MAAM,uBAAuB,GAAG,KAAK,EAC1C,SAAiB,EAC6E,EAAE;IAChG,MAAM,cAAc,GAAG,MAAM,eAAe,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;IACvD,MAAM,aAAa,GAAG,cAAc,aAAd,cAAc,uBAAd,cAAc,CAAE,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC;IAC/E,IAAI,CAAC,aAAa,EAAE;QAClB,MAAM,IAAA,gBAAQ,EAAC,sBAAsB,EAAE,EAAE,SAAS,EAAE,CAAC,CAAC;KACvD;IACD,IAAI,CAAC,aAAa,CAAC,UAAU,EAAE;QAC7B,MAAM,IAAA,gBAAQ,EAAC,yBAAyB,EAAE,EAAE,SAAS,EAAE,CAAC,CAAC;KAC1D;IACD,IAAI,CAAC,aAAa,CAAC,YAAY,EAAE;QAC/B,MAAM,IAAA,gBAAQ,EAAC,4BAA4B,EAAE,EAAE,SAAS,EAAE,CAAC,CAAC;KAC7D;IAED,OAAO;QACL,MAAM,EAAE,aAAa,CAAC,MAAM;QAC5B,UAAU,EAAE,aAAa,CAAC,UAAU;QACpC,YAAY,EAAE,aAAa,CAAC,YAAY;KACzC,CAAC;AACJ,CAAC,CAAC;AApBW,QAAA,uBAAuB,2BAoBlC","sourcesContent":["import { createCache } from '@yuants/cache';\nimport { getCredentialId } from '@yuants/exchange';\nimport { Terminal } from '@yuants/protocol';\nimport { ISecret, readSecret, writeSecret } from '@yuants/secret';\nimport { escapeSQL, requestSQL } from '@yuants/sql';\nimport { newError } from '@yuants/utils';\nimport { defer, firstValueFrom, map, repeat, retry, shareReplay } from 'rxjs';\n\nexport interface IExchangeCredential {\n type: string;\n payload: any;\n}\n\nconst terminal = Terminal.fromNodeEnv();\n\nconst credentialReader = process.env.NODE_UNIT_PUBLIC_KEY || terminal.keyPair.public_key;\n\ninterface ICredentialResolvedStatus {\n secret: ISecret;\n credential?: IExchangeCredential;\n credentialId?: string;\n reason?: string;\n}\n\nexport const listAllCredentials = async () => {\n const secrets = await requestSQL<ISecret[]>(\n terminal,\n `select * from secret where tags->>'type' = 'exchange_credential' and reader = ${escapeSQL(\n credentialReader,\n )}`,\n );\n\n const results = secrets.map((secret): ICredentialResolvedStatus => ({ secret }));\n\n await Promise.all(\n results.map(async (result) => {\n try {\n const decrypted = await readSecret(terminal, result.secret);\n const credential = JSON.parse(new TextDecoder().decode(decrypted)) as IExchangeCredential;\n result.credential = credential;\n\n const res = await getCredentialId(terminal, credential);\n const credentialId = res.data;\n result.credentialId = credentialId;\n } catch (e) {\n result.reason = `${e}`;\n }\n }),\n );\n\n return results;\n};\n\nterminal.server.provideService<IExchangeCredential, ISecret>(\n 'VEX/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 const secret = await writeSecret(terminal, credentialReader, { type: 'exchange_credential' }, secretData);\n return { res: { code: 0, message: 'OK', data: secret } };\n },\n);\n\nterminal.server.provideService<void, ICredentialResolvedStatus[]>(\n 'VEX/ListExchangeCredential',\n {},\n async () => {\n return { res: { code: 0, message: 'OK', data: await listAllCredentials() } };\n },\n);\n\nterminal.server.provideService<void, string[]>('VEX/ListCredentials', {}, async () => {\n const credentials = await firstValueFrom(validCredentials$);\n return { res: { code: 0, message: 'OK', data: [...credentials.keys()] } };\n});\n\nconst credentialCache = createCache(() => listAllCredentials(), { swrAfter: 10_000, expire: 3600_000 });\n\nexport const validCredentials$ = defer(() => credentialCache.query('')).pipe(\n map((x) => {\n const map = new Map<string, IExchangeCredential>();\n if (!x) return map;\n for (const xx of x) {\n if (xx.credentialId && xx.credential) {\n map.set(xx.credentialId, xx.credential);\n }\n }\n return map;\n }),\n repeat({ delay: 10000 }),\n retry({ delay: 5000 }),\n shareReplay(1),\n);\n\nexport const validCredentialTypes$ = validCredentials$.pipe(\n map((credentials) => {\n const types = new Set<string>();\n credentials.forEach((credential) => {\n types.add(credential.type);\n });\n return Array.from(types);\n }),\n);\n\nexport const getCredentialById = async (credential_id: string) => {\n const credentials = await firstValueFrom(validCredentials$);\n return credentials.get(credential_id);\n};\n\nexport const getCredentialBySecretId = async (\n secret_id: string,\n): Promise<Required<Pick<ICredentialResolvedStatus, 'secret' | 'credential' | 'credentialId'>>> => {\n const allCredentials = await credentialCache.query('');\n const theCredential = allCredentials?.find((x) => x.secret.sign === secret_id);\n if (!theCredential) {\n throw newError('CREDENTIAL_NOT_FOUND', { secret_id });\n }\n if (!theCredential.credential) {\n throw newError('CREDENTIAL_NOT_RESOLVED', { secret_id });\n }\n if (!theCredential.credentialId) {\n throw newError('CREDENTIAL_ID_NOT_RESOLVED', { secret_id });\n }\n\n return {\n secret: theCredential.secret,\n credential: theCredential.credential,\n credentialId: theCredential.credentialId,\n };\n};\n"]}
package/lib/general.js CHANGED
@@ -6,35 +6,29 @@ const credential_1 = require("./credential");
6
6
  const terminal = protocol_1.Terminal.fromNodeEnv();
7
7
  terminal.server.provideService('VEX/GetPositions', {
8
8
  type: 'object',
9
- required: ['credential_id'],
9
+ required: ['secret_id'],
10
10
  properties: {
11
- credential_id: { type: 'string' },
11
+ secret_id: { type: 'string' },
12
12
  product_id: { type: 'string' },
13
13
  },
14
14
  }, async (msg) => {
15
- const credential = await (0, credential_1.getCredentialById)(msg.req.credential_id);
16
- if (!credential) {
17
- return { res: { code: 404, message: 'Credential not found' } };
18
- }
19
- const res = await (0, exchange_1.getPositions)(terminal, credential, msg.req.product_id);
15
+ const credential = await (0, credential_1.getCredentialBySecretId)(msg.req.secret_id);
16
+ const res = await (0, exchange_1.getPositions)(terminal, credential.credential, msg.req.product_id);
20
17
  return { res };
21
18
  });
22
19
  terminal.server.provideService('VEX/GetOrders', {
23
20
  type: 'object',
24
- required: ['credential_id'],
21
+ required: ['secret_id'],
25
22
  properties: {
26
- credential_id: { type: 'string' },
23
+ secret_id: { type: 'string' },
27
24
  product_id: { type: 'string' },
28
25
  },
29
26
  }, async (msg) => {
30
27
  var _a;
31
- const credential = await (0, credential_1.getCredentialById)(msg.req.credential_id);
32
- if (!credential) {
33
- return { res: { code: 404, message: 'Credential not found' } };
34
- }
35
- const res = await (0, exchange_1.getOrders)(terminal, credential, msg.req.product_id);
28
+ const credential = await (0, credential_1.getCredentialBySecretId)(msg.req.secret_id);
29
+ const res = await (0, exchange_1.getOrders)(terminal, credential.credential, msg.req.product_id);
36
30
  (_a = res.data) === null || _a === void 0 ? void 0 : _a.forEach((order) => {
37
- order.account_id = msg.req.credential_id;
31
+ order.account_id = credential.credentialId;
38
32
  });
39
33
  return { res };
40
34
  });
@@ -42,49 +36,40 @@ terminal.server.provideService('VEX/GetOrders', {
42
36
  // SubmitOrder
43
37
  terminal.server.provideService('VEX/SubmitOrder', {
44
38
  type: 'object',
45
- required: ['order', 'credential_id'],
39
+ required: ['order', 'secret_id'],
46
40
  properties: {
47
41
  order: { type: 'object' },
48
- credential_id: { type: 'string' },
42
+ secret_id: { type: 'string' },
49
43
  },
50
44
  }, async (msg) => {
51
- const credential = await (0, credential_1.getCredentialById)(msg.req.credential_id);
52
- if (!credential) {
53
- return { res: { code: 404, message: 'Credential not found' } };
54
- }
55
- const res = await (0, exchange_1.submitOrder)(terminal, credential, msg.req.order);
45
+ const credential = await (0, credential_1.getCredentialBySecretId)(msg.req.secret_id);
46
+ const res = await (0, exchange_1.submitOrder)(terminal, credential.credential, msg.req.order);
56
47
  return { res };
57
48
  });
58
49
  // ModifyOrder
59
50
  terminal.server.provideService('VEX/ModifyOrder', {
60
51
  type: 'object',
61
- required: ['order', 'credential_id'],
52
+ required: ['order', 'secret_id'],
62
53
  properties: {
63
54
  order: { type: 'object' },
64
- credential_id: { type: 'string' },
55
+ secret_id: { type: 'string' },
65
56
  },
66
57
  }, async (msg) => {
67
- const credential = await (0, credential_1.getCredentialById)(msg.req.credential_id);
68
- if (!credential) {
69
- return { res: { code: 404, message: 'Credential not found' } };
70
- }
71
- const res = await (0, exchange_1.modifyOrder)(terminal, credential, msg.req.order);
58
+ const credential = await (0, credential_1.getCredentialBySecretId)(msg.req.secret_id);
59
+ const res = await (0, exchange_1.modifyOrder)(terminal, credential.credential, msg.req.order);
72
60
  return { res };
73
61
  });
74
62
  // CancelOrder
75
63
  terminal.server.provideService('VEX/CancelOrder', {
76
64
  type: 'object',
77
- required: ['order', 'credential_id'],
65
+ required: ['order', 'secret_id'],
78
66
  properties: {
79
67
  order: { type: 'object' },
80
- credential_id: { type: 'string' },
68
+ secret_id: { type: 'string' },
81
69
  },
82
70
  }, async (msg) => {
83
- const credential = await (0, credential_1.getCredentialById)(msg.req.credential_id);
84
- if (!credential) {
85
- return { res: { code: 404, message: 'Credential not found' } };
86
- }
87
- const res = await (0, exchange_1.cancelOrder)(terminal, credential, msg.req.order);
71
+ const credential = await (0, credential_1.getCredentialBySecretId)(msg.req.secret_id);
72
+ const res = await (0, exchange_1.cancelOrder)(terminal, credential.credential, msg.req.order);
88
73
  return { res };
89
74
  });
90
75
  //# sourceMappingURL=general.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"general.js","sourceRoot":"","sources":["../src/general.ts"],"names":[],"mappings":";;AAEA,+CAAkG;AAClG,+CAA4C;AAC5C,6CAAiD;AAEjD,MAAM,QAAQ,GAAG,mBAAQ,CAAC,WAAW,EAAE,CAAC;AAExC,QAAQ,CAAC,MAAM,CAAC,cAAc,CAC5B,kBAAkB,EAClB;IACE,IAAI,EAAE,QAAQ;IACd,QAAQ,EAAE,CAAC,eAAe,CAAC;IAC3B,UAAU,EAAE;QACV,aAAa,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;QACjC,UAAU,EAAE,EAAE,IAAI,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AO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,GAAG,EAAE,OAAO,EAAE,sBAAsB,EAAE,EAAE,CAAC;KAChE;IACD,MAAM,GAAG,GAAG,MAAM,IAAA,sBAAW,EAAC,QAAQ,EAAE,UAAU,EAAE,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IACnE,OAAO,EAAE,GAAG,EAAE,CAAC;AACjB,CAAC,CACF,CAAC;AAEF,cAAc;AACd,QAAQ,CAAC,MAAM,CAAC,cAAc,CAC5B,iBAAiB,EACjB;IACE,IAAI,EAAE,QAAQ;IACd,QAAQ,EAAE,CAAC,OAAO,EAAE,eAAe,CAAC;IACpC,UAAU,EAAE;QACV,KAAK,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;QACzB,aAAa,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;KAClC;CACF,EACD,KAAK,EAAE,GAAG,EAAE,EAAE;IACZ,MAAM,UAAU,GAAG,MAAM,IAAA,8BAAiB,EAAC,GAAG,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;IAClE,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,IAAA,sBAAW,EAAC,QAAQ,EAAE,UAAU,EAAE,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IACnE,OAAO,EAAE,GAAG,EAAE,CAAC;AACjB,CAAC,CACF,CAAC","sourcesContent":["import { IPosition } from '@yuants/data-account';\nimport { IOrder } from '@yuants/data-order';\nimport { cancelOrder, getOrders, getPositions, modifyOrder, submitOrder } from '@yuants/exchange';\nimport { Terminal } from '@yuants/protocol';\nimport { getCredentialById } from './credential';\n\nconst terminal = Terminal.fromNodeEnv();\n\nterminal.server.provideService<{ credential_id: string; product_id?: string }, IPosition[]>(\n 'VEX/GetPositions',\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 = await getCredentialById(msg.req.credential_id);\n if (!credential) {\n return { res: { code: 404, message: 'Credential not found' } };\n }\n const res = await getPositions(terminal, credential, msg.req.product_id);\n return { res };\n },\n);\n\nterminal.server.provideService<{ credential_id: string; product_id?: string }, IOrder[]>(\n 'VEX/GetOrders',\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 = await getCredentialById(msg.req.credential_id);\n if (!credential) {\n return { res: { code: 404, message: 'Credential not found' } };\n }\n const res = await getOrders(terminal, credential, msg.req.product_id);\n res.data?.forEach((order) => {\n order.account_id = msg.req.credential_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 'VEX/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 = await getCredentialById(msg.req.credential_id);\n if (!credential) {\n return { res: { code: 404, message: 'Credential not found' } };\n }\n const res = await submitOrder(terminal, credential, msg.req.order);\n return { res };\n },\n);\n\n// ModifyOrder\nterminal.server.provideService<{ order: IOrder; credential_id: string }, void>(\n 'VEX/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 = await getCredentialById(msg.req.credential_id);\n if (!credential) {\n return { res: { code: 404, message: 'Credential not found' } };\n }\n const res = await modifyOrder(terminal, credential, msg.req.order);\n return { res };\n },\n);\n\n// CancelOrder\nterminal.server.provideService<{ order: IOrder; credential_id: string }, void>(\n 'VEX/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 = await getCredentialById(msg.req.credential_id);\n if (!credential) {\n return { res: { code: 404, message: 'Credential not found' } };\n }\n const res = await cancelOrder(terminal, credential, msg.req.order);\n return { res };\n },\n);\n"]}
1
+ {"version":3,"file":"general.js","sourceRoot":"","sources":["../src/general.ts"],"names":[],"mappings":";;AAEA,+CAAkG;AAClG,+CAA4C;AAC5C,6CAAuD;AAEvD,MAAM,QAAQ,GAAG,mBAAQ,CAAC,WAAW,EAAE,CAAC;AAExC,QAAQ,CAAC,MAAM,CAAC,cAAc,CAC5B,kBAAkB,EAClB;IACE,IAAI,EAAE,QAAQ;IACd,QAAQ,EAAE,CAAC,WAAW,CAAC;IACvB,UAAU,EAAE;QACV,SAAS,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;QAC7B,UAAU,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;KAC/B;CACF,EACD,KAAK,EAAE,GAAG,EAAE,EAAE;IACZ,MAAM,UAAU,GAAG,MAAM,IAAA,oCAAuB,EAAC,GAAG,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;IACpE,MAAM,GAAG,GAAG,MAAM,IAAA,uBAAY,EAAC,QAAQ,EAAE,UAAU,CAAC,UAAU,EAAE,GAAG,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IACpF,OAAO,EAAE,GAAG,EAAE,CAAC;AACjB,CAAC,CACF,CAAC;AAEF,QAAQ,CAAC,MAAM,CAAC,cAAc,CAC5B,eAAe,EACf;IACE,IAAI,EAAE,QAAQ;IACd,QAAQ,EAAE,CAAC,WAAW,CAAC;IACvB,UAAU,EAAE;QACV,SAAS,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;QAC7B,UAAU,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;KAC/B;CACF,EACD,KAAK,EAAE,GAAG,EAAE,EAAE;;IACZ,MAAM,UAAU,GAAG,MAAM,IAAA,oCAAuB,EAAC,GAAG,CAAC,GAAG,C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,EACD,KAAK,EAAE,GAAG,EAAE,EAAE;IACZ,MAAM,UAAU,GAAG,MAAM,IAAA,oCAAuB,EAAC,GAAG,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;IACpE,MAAM,GAAG,GAAG,MAAM,IAAA,sBAAW,EAAC,QAAQ,EAAE,UAAU,CAAC,UAAU,EAAE,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IAC9E,OAAO,EAAE,GAAG,EAAE,CAAC;AACjB,CAAC,CACF,CAAC;AAEF,cAAc;AACd,QAAQ,CAAC,MAAM,CAAC,cAAc,CAC5B,iBAAiB,EACjB;IACE,IAAI,EAAE,QAAQ;IACd,QAAQ,EAAE,CAAC,OAAO,EAAE,WAAW,CAAC;IAChC,UAAU,EAAE;QACV,KAAK,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;QACzB,SAAS,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;KAC9B;CACF,EACD,KAAK,EAAE,GAAG,EAAE,EAAE;IACZ,MAAM,UAAU,GAAG,MAAM,IAAA,oCAAuB,EAAC,GAAG,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;IACpE,MAAM,GAAG,GAAG,MAAM,IAAA,sBAAW,EAAC,QAAQ,EAAE,UAAU,CAAC,UAAU,EAAE,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IAC9E,OAAO,EAAE,GAAG,EAAE,CAAC;AACjB,CAAC,CACF,CAAC","sourcesContent":["import { IPosition } from '@yuants/data-account';\nimport { IOrder } from '@yuants/data-order';\nimport { cancelOrder, getOrders, getPositions, modifyOrder, submitOrder } from '@yuants/exchange';\nimport { Terminal } from '@yuants/protocol';\nimport { getCredentialBySecretId } from './credential';\n\nconst terminal = Terminal.fromNodeEnv();\n\nterminal.server.provideService<{ secret_id: string; product_id?: string }, IPosition[]>(\n 'VEX/GetPositions',\n {\n type: 'object',\n required: ['secret_id'],\n properties: {\n secret_id: { type: 'string' },\n product_id: { type: 'string' },\n },\n },\n async (msg) => {\n const credential = await getCredentialBySecretId(msg.req.secret_id);\n const res = await getPositions(terminal, credential.credential, msg.req.product_id);\n return { res };\n },\n);\n\nterminal.server.provideService<{ secret_id: string; product_id?: string }, IOrder[]>(\n 'VEX/GetOrders',\n {\n type: 'object',\n required: ['secret_id'],\n properties: {\n secret_id: { type: 'string' },\n product_id: { type: 'string' },\n },\n },\n async (msg) => {\n const credential = await getCredentialBySecretId(msg.req.secret_id);\n const res = await getOrders(terminal, credential.credential, msg.req.product_id);\n res.data?.forEach((order) => {\n order.account_id = credential.credentialId;\n });\n return { res };\n },\n);\n\n// 10. Proxy Orders\n// SubmitOrder\nterminal.server.provideService<{ order: IOrder; secret_id: string }, { order_id: string }>(\n 'VEX/SubmitOrder',\n {\n type: 'object',\n required: ['order', 'secret_id'],\n properties: {\n order: { type: 'object' },\n secret_id: { type: 'string' },\n },\n },\n async (msg) => {\n const credential = await getCredentialBySecretId(msg.req.secret_id);\n const res = await submitOrder(terminal, credential.credential, msg.req.order);\n return { res };\n },\n);\n\n// ModifyOrder\nterminal.server.provideService<{ order: IOrder; secret_id: string }, void>(\n 'VEX/ModifyOrder',\n {\n type: 'object',\n required: ['order', 'secret_id'],\n properties: {\n order: { type: 'object' },\n secret_id: { type: 'string' },\n },\n },\n async (msg) => {\n const credential = await getCredentialBySecretId(msg.req.secret_id);\n const res = await modifyOrder(terminal, credential.credential, msg.req.order);\n return { res };\n },\n);\n\n// CancelOrder\nterminal.server.provideService<{ order: IOrder; secret_id: string }, void>(\n 'VEX/CancelOrder',\n {\n type: 'object',\n required: ['order', 'secret_id'],\n properties: {\n order: { type: 'object' },\n secret_id: { type: 'string' },\n },\n },\n async (msg) => {\n const credential = await getCredentialBySecretId(msg.req.secret_id);\n const res = await cancelOrder(terminal, credential.credential, msg.req.order);\n return { res };\n },\n);\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yuants/app-virtual-exchange",
3
- "version": "0.5.2",
3
+ "version": "0.5.3",
4
4
  "main": "lib/index.js",
5
5
  "files": [
6
6
  "dist",
@@ -1,14 +1,14 @@
1
1
  {
2
- "apps/virtual-exchange/CHANGELOG.json": "07abfeb4640a3b9ed48aa5ef0b6034087132cc27",
3
- "apps/virtual-exchange/CHANGELOG.md": "e02fcc74fcc52942e6f63516fffe88abf7d13f96",
2
+ "apps/virtual-exchange/CHANGELOG.json": "ed8d1d46a245a760e06d66d24abde99a08a4df15",
3
+ "apps/virtual-exchange/CHANGELOG.md": "590da04f500576e1b92b82f4aa18829bad5ff254",
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": "c88895351429bb981e5e10dfa459c2834f575498",
10
- "apps/virtual-exchange/src/credential.ts": "f72728008d5b7d033de33a9466140a6db11c34c7",
11
- "apps/virtual-exchange/src/general.ts": "068a19f33440cbaad103a5b2a68f9594dc692b81",
9
+ "apps/virtual-exchange/package.json": "7c18e33583814fbf18950a565e1bcb54ecc79409",
10
+ "apps/virtual-exchange/src/credential.ts": "e03200075b547ffa4b99583e81dd1e89a26bea79",
11
+ "apps/virtual-exchange/src/general.ts": "5ffe52c4f30268ef82a285af27f07862bacacebb",
12
12
  "apps/virtual-exchange/src/index.ts": "8d7f19a07e6be09c4d8fd4a49ddb3127d3fbf3de",
13
13
  "apps/virtual-exchange/src/legacy-services.ts": "08284bbb1d6526c6af15ef1530866f15295554d9",
14
14
  "apps/virtual-exchange/src/product-collector.ts": "15ba0a692d694d20b607eaad0287a864577ef30c",