@mfxhub/sdk 0.0.0-stage → 0.1.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/CHANGELOG.md +30 -0
- package/LICENSE +202 -0
- package/README.md +106 -2
- package/SECURITY.md +32 -0
- package/dist/arguments.d.ts +10 -0
- package/dist/arguments.js +37 -0
- package/dist/catalog.d.ts +25 -0
- package/dist/catalog.js +39 -0
- package/dist/client.d.ts +101 -0
- package/dist/client.js +200 -0
- package/dist/decimals.d.ts +10 -0
- package/dist/decimals.js +51 -0
- package/dist/errors.d.ts +115 -0
- package/dist/errors.js +162 -0
- package/dist/execution.d.ts +48 -0
- package/dist/execution.js +119 -0
- package/dist/generated/api.d.ts +5012 -0
- package/dist/generated/api.js +5 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +19 -0
- package/dist/pages.d.ts +22 -0
- package/dist/pages.js +46 -0
- package/dist/results.d.ts +78 -0
- package/dist/results.js +119 -0
- package/dist/signing.d.ts +34 -0
- package/dist/signing.js +103 -0
- package/dist/stream.d.ts +139 -0
- package/dist/stream.js +387 -0
- package/dist/terminals.d.ts +55 -0
- package/dist/terminals.js +76 -0
- package/package.json +64 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
Every release of `@mfxhub/sdk`, newest first. The format follows
|
|
4
|
+
[Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and versions follow
|
|
5
|
+
[Semantic Versioning](https://semver.org/).
|
|
6
|
+
|
|
7
|
+
## Unreleased
|
|
8
|
+
|
|
9
|
+
## 0.1.0
|
|
10
|
+
|
|
11
|
+
The first release. It passes all 72 cases of the shared SDK test suite 0.2.0.
|
|
12
|
+
|
|
13
|
+
- The foundation: request signing (Ed25519, the published test vector), the client core with
|
|
14
|
+
rate-limited reads retried and writes never retried, and a typed error for every error code.
|
|
15
|
+
- The shared SDK test suite 0.2.0 runs against this SDK on every change.
|
|
16
|
+
- `client.terminals`: `get`, `page`, `list` (follows the cursor; there is no total) and `create`
|
|
17
|
+
(an idempotency key is required, and a write is never retried); `isUsable`; the `Page` type.
|
|
18
|
+
- `client.execution.placeOrder`: resolves to an `ExecutionResult` for every outcome and never
|
|
19
|
+
rejects for what the platform or the network does. A connection that drops after the order was
|
|
20
|
+
sent reads `UNKNOWN`, not retryable; a timeout is never retryable. Prices and volumes are decimal
|
|
21
|
+
strings, written as plain digits (`1E+3` is sent as `"1000"`); a number is refused.
|
|
22
|
+
- `client.catalog.quote`: one symbol's quote with its age; prices are the API's decimal strings.
|
|
23
|
+
- `client.stream()`: one signed WebSocket for every channel. `Channel` builds the keys from
|
|
24
|
+
ids. After a 4008 or 4009 close it reconnects and resumes events, results, account and expert
|
|
25
|
+
channels from the last `seq`; ticks and candles are subscribed again. 4001 stops with an
|
|
26
|
+
`AuthError`. A resync and a refused channel arrive as events, not errors, and gaps are counted
|
|
27
|
+
in `stats`. The socket pauses while frames wait, so a slow reader meets real backpressure.
|
|
28
|
+
- Reads and stream reconnects share one jittered backoff (`backoffMs`).
|
|
29
|
+
- All 72 cases of the shared SDK test suite 0.2.0 pass.
|
|
30
|
+
- CONTRIBUTING.md: how to set up, test, sign off and propose a change.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
202
|
+
|
package/README.md
CHANGED
|
@@ -1,3 +1,107 @@
|
|
|
1
|
-
#
|
|
1
|
+
# MFX Hub TypeScript SDK
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The typed TypeScript client for the [MFX Hub](https://mfxhub.com) API: hosted trading terminals for
|
|
4
|
+
your own broker accounts, with market data, execution and a live event stream behind one signed REST
|
|
5
|
+
and WebSocket API.
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @mfxhub/sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Node.js 22 or later, ESM. Every release passes the same shared test suite as the Python SDK.
|
|
12
|
+
|
|
13
|
+
## First calls
|
|
14
|
+
|
|
15
|
+
You need an API key: its id, and the private key shown once when it was issued. Keep the private
|
|
16
|
+
key out of your code; the SDK never prints it.
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { Channel, MfxClient, StreamMessage, isUsable } from "@mfxhub/sdk";
|
|
20
|
+
|
|
21
|
+
const client = new MfxClient(
|
|
22
|
+
"https://api.dev.mfxhub.com/v1", // https://api.mfxhub.com/v1 for production
|
|
23
|
+
process.env.MFX_KEY_ID!,
|
|
24
|
+
process.env.MFX_PRIVATE_KEY!, // PEM, or the base64 of the 32-byte key
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
// Your terminals, following the cursor: there is no total.
|
|
28
|
+
for await (const terminal of client.terminals.list()) {
|
|
29
|
+
console.log(terminal.id, terminal.state, isUsable(terminal));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// A quote always says how old it is. Prices are decimal strings, every digit kept.
|
|
33
|
+
const quote = await client.catalog.quote(brokerId, "EURUSD");
|
|
34
|
+
console.log(quote.bid, quote.ask, quote.age_s);
|
|
35
|
+
|
|
36
|
+
// Placing an order never throws for anything the platform or the network does: read the result.
|
|
37
|
+
const result = await client.execution.placeOrder(terminalId, {
|
|
38
|
+
symbol: "EURUSD",
|
|
39
|
+
side: "buy",
|
|
40
|
+
volume: "0.01", // a decimal string; a number is refused
|
|
41
|
+
idempotencyKey: `order-${Date.now()}`, // the same key again returns the same order, never a second
|
|
42
|
+
});
|
|
43
|
+
if (!result.ok && result.state === "UNKNOWN") {
|
|
44
|
+
// The order may have reached the broker. Never send it again under a new key.
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// One WebSocket for every channel, resumed after a reconnect.
|
|
48
|
+
const stream = await client.stream();
|
|
49
|
+
await stream.subscribe(Channel.results(terminalId));
|
|
50
|
+
for await (const event of stream) {
|
|
51
|
+
if (event instanceof StreamMessage) console.log(event.channel, event.seq, event.type);
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Use a **demo** account while you try it: an order is real on whatever account the terminal is
|
|
56
|
+
logged in to.
|
|
57
|
+
|
|
58
|
+
## What 0.1.0 covers
|
|
59
|
+
|
|
60
|
+
| Area | In this release |
|
|
61
|
+
| --------- | ------------------------------------------------------------------------------------- |
|
|
62
|
+
| Signing | every request and the stream's upgrade, Ed25519 |
|
|
63
|
+
| Errors | one typed error per error code the API names |
|
|
64
|
+
| Terminals | `get`, `page`, `list`, `create` |
|
|
65
|
+
| Execution | `placeOrder`, with `ExecutionResult` for every outcome |
|
|
66
|
+
| Catalogue | `quote` |
|
|
67
|
+
| Stream | `client.stream()`: events, results, account, ticks, candles, book and expert channels |
|
|
68
|
+
|
|
69
|
+
Submitting a terminal's login, farms, account reads, market-data subscriptions and the rest of
|
|
70
|
+
order management are next. Until then the [Python SDK](https://github.com/mfx-platform/mfx-sdk-python)
|
|
71
|
+
and the API itself cover them. The full API is described at [docs.mfxhub.com](https://docs.mfxhub.com).
|
|
72
|
+
|
|
73
|
+
## Three rules the SDK keeps
|
|
74
|
+
|
|
75
|
+
- **Every write takes an idempotency key**, and the SDK never invents one.
|
|
76
|
+
- **Execution never throws** for what the platform or the network does. A connection that dropped
|
|
77
|
+
after the order was sent reads `UNKNOWN` and `retryable: false`.
|
|
78
|
+
- **Prices, volumes and money are decimal strings.** A JavaScript number is refused before anything
|
|
79
|
+
is sent, because `0.07 * 3` is `0.21000000000000002`.
|
|
80
|
+
|
|
81
|
+
## Developing
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
npm ci
|
|
85
|
+
npm test # vitest, 100% coverage required
|
|
86
|
+
npm run lint && npm run format && npm run typecheck
|
|
87
|
+
npm run gen # after spec/openapi.yaml changes; CI fails when the generated types drift
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### The shared SDK test suite
|
|
91
|
+
|
|
92
|
+
Every MFX Hub SDK passes the same cases: a fake MFX Hub plays each one back and judges what the SDK
|
|
93
|
+
sent. The suite is vendored in `test/conformance/suite` at the version in
|
|
94
|
+
`test/conformance/suite-version.txt`; its fake hub is a Python 3.12 program.
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
uv venv --python 3.12 .hub
|
|
98
|
+
uv pip install --python .hub --require-hashes -r test/conformance/hub-requirements.txt
|
|
99
|
+
MFX_SUITE_PYTHON=.hub/bin/python npm run test:conformance # .hub\Scripts\python.exe on Windows
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
A case this SDK does not pass yet is listed in `test/conformance/not-yet.json` with the reason, and
|
|
103
|
+
must fail until it is removed. To move to a new suite release:
|
|
104
|
+
`node tools/update-suite.mjs <checkout of mfx-sdk-conformance at the tag>`.
|
|
105
|
+
|
|
106
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md) to propose a change, and [SECURITY.md](SECURITY.md) to report a
|
|
107
|
+
security problem.
|
package/SECURITY.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# Security policy
|
|
2
|
+
|
|
3
|
+
## Reporting a vulnerability
|
|
4
|
+
|
|
5
|
+
Please report a vulnerability in this SDK, or in the MFX Hub API it calls, privately:
|
|
6
|
+
|
|
7
|
+
- through GitHub's **Report a vulnerability** button on this repository's Security tab, or
|
|
8
|
+
- by email to **security@mfxhub.com**.
|
|
9
|
+
|
|
10
|
+
Please do not open a public issue, pull request or discussion for a security problem.
|
|
11
|
+
|
|
12
|
+
Tell us what you found, how to reproduce it, and what an attacker could do with it. Do not include
|
|
13
|
+
real broker credentials or another person's data; if a proof needs a key, use one of your own
|
|
14
|
+
development keys and say so.
|
|
15
|
+
|
|
16
|
+
## What to expect
|
|
17
|
+
|
|
18
|
+
- We acknowledge a report within two business days.
|
|
19
|
+
- We tell you what we found and what we will do within ten business days.
|
|
20
|
+
- We fix what is confirmed and say when the fix is released. We credit you in the release notes if
|
|
21
|
+
you want us to.
|
|
22
|
+
|
|
23
|
+
## Supported versions
|
|
24
|
+
|
|
25
|
+
Security fixes are made to the latest release of `@mfxhub/sdk`. Upgrade to it before reporting a problem
|
|
26
|
+
you saw on an older one.
|
|
27
|
+
|
|
28
|
+
## Handling credentials
|
|
29
|
+
|
|
30
|
+
The SDK never stores or logs an API private key or a broker password: `SigningKey` and `Secret`
|
|
31
|
+
render as `***` everywhere and cannot be pickled. If you believe a key has been exposed, write to
|
|
32
|
+
security@mfxhub.com so it can be revoked at once, and use a new one.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** An id the API names as a uuid. Returned lower-case, as the API writes ids. */
|
|
2
|
+
export declare function uuid(name: string, value: unknown): string;
|
|
3
|
+
/**
|
|
4
|
+
* The idempotency key every write takes. It has no default: a default would be a value nobody
|
|
5
|
+
* chose, and the point of the key is that the caller can send the same one again and get the same
|
|
6
|
+
* answer back instead of a second order or terminal.
|
|
7
|
+
*/
|
|
8
|
+
export declare function idempotencyKey(value: unknown): string;
|
|
9
|
+
/** A page size, when one is given: 1 to 200, as the API takes. */
|
|
10
|
+
export declare function limit(value: unknown): number | undefined;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// Checks on what a caller passes, made before anything is sent. A refused argument is a TypeError
|
|
2
|
+
// (the wrong kind of value) or a RangeError (the right kind, out of range), never a request.
|
|
3
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
4
|
+
/** An id the API names as a uuid. Returned lower-case, as the API writes ids. */
|
|
5
|
+
export function uuid(name, value) {
|
|
6
|
+
if (typeof value !== "string") {
|
|
7
|
+
throw new TypeError(`${name} is a uuid string; got ${typeof value}`);
|
|
8
|
+
}
|
|
9
|
+
if (!UUID.test(value))
|
|
10
|
+
throw new RangeError(`${name} is not a uuid: ${JSON.stringify(value)}`);
|
|
11
|
+
return value.toLowerCase();
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The idempotency key every write takes. It has no default: a default would be a value nobody
|
|
15
|
+
* chose, and the point of the key is that the caller can send the same one again and get the same
|
|
16
|
+
* answer back instead of a second order or terminal.
|
|
17
|
+
*/
|
|
18
|
+
export function idempotencyKey(value) {
|
|
19
|
+
if (typeof value !== "string") {
|
|
20
|
+
throw new TypeError(`idempotencyKey is required on every write; got ${typeof value}`);
|
|
21
|
+
}
|
|
22
|
+
if (value.trim() === "" || value.length > 128) {
|
|
23
|
+
throw new RangeError("idempotencyKey is 1 to 128 characters");
|
|
24
|
+
}
|
|
25
|
+
return value;
|
|
26
|
+
}
|
|
27
|
+
/** A page size, when one is given: 1 to 200, as the API takes. */
|
|
28
|
+
export function limit(value) {
|
|
29
|
+
if (value === undefined)
|
|
30
|
+
return undefined;
|
|
31
|
+
if (typeof value !== "number" || !Number.isInteger(value)) {
|
|
32
|
+
throw new TypeError(`limit is a whole number; got ${typeof value === "number" ? value : typeof value}`);
|
|
33
|
+
}
|
|
34
|
+
if (value < 1 || value > 200)
|
|
35
|
+
throw new RangeError(`limit is 1 to 200; got ${value}`);
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { MfxClient } from "./client.js";
|
|
2
|
+
import type { components } from "./generated/api.js";
|
|
3
|
+
/** One symbol's current quote, with its age and whether the market can be traded. */
|
|
4
|
+
export type Quote = components["schemas"]["SymbolQuote"];
|
|
5
|
+
/** Which of a tenant's servers and account types a symbol is meant on. */
|
|
6
|
+
export interface QuoteOptions {
|
|
7
|
+
/** Needed when the symbol is on more than one of your servers (409 `ambiguous_symbol`). */
|
|
8
|
+
readonly serverId?: string;
|
|
9
|
+
/** Needed when the symbol is listed under more than one account type on that server. */
|
|
10
|
+
readonly accountType?: string;
|
|
11
|
+
}
|
|
12
|
+
/** The tenant's catalogue of brokers and symbols. */
|
|
13
|
+
export declare class Catalog {
|
|
14
|
+
#private;
|
|
15
|
+
constructor(client: MfxClient);
|
|
16
|
+
/**
|
|
17
|
+
* The current quote for one symbol, read from one of your terminals on that broker's server.
|
|
18
|
+
* `state` says whether the market can be traded (`open`, `closed_session`, ...);
|
|
19
|
+
* `broker_time` is the broker's own clock, which is not UTC.
|
|
20
|
+
*
|
|
21
|
+
* @param brokerId the broker, from the catalogue
|
|
22
|
+
* @param symbol the broker's own symbol name (`EURUSD.sd`)
|
|
23
|
+
*/
|
|
24
|
+
quote(brokerId: string, symbol: string, options?: QuoteOptions): Promise<Quote>;
|
|
25
|
+
}
|
package/dist/catalog.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// `client.catalog`: what a tenant's brokers offer, and what it costs right now.
|
|
2
|
+
//
|
|
3
|
+
// This first slice is the quote. A quote is never returned without saying how old it is
|
|
4
|
+
// (`age_s`): a stale quote reads exactly like a live one and gets acted on. Its prices are the
|
|
5
|
+
// API's decimal strings, every digit kept.
|
|
6
|
+
import { uuid } from "./arguments.js";
|
|
7
|
+
/** The tenant's catalogue of brokers and symbols. */
|
|
8
|
+
export class Catalog {
|
|
9
|
+
#client;
|
|
10
|
+
constructor(client) {
|
|
11
|
+
this.#client = client;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The current quote for one symbol, read from one of your terminals on that broker's server.
|
|
15
|
+
* `state` says whether the market can be traded (`open`, `closed_session`, ...);
|
|
16
|
+
* `broker_time` is the broker's own clock, which is not UTC.
|
|
17
|
+
*
|
|
18
|
+
* @param brokerId the broker, from the catalogue
|
|
19
|
+
* @param symbol the broker's own symbol name (`EURUSD.sd`)
|
|
20
|
+
*/
|
|
21
|
+
async quote(brokerId, symbol, options = {}) {
|
|
22
|
+
const broker = uuid("brokerId", brokerId);
|
|
23
|
+
if (typeof symbol !== "string" || symbol === "") {
|
|
24
|
+
throw new TypeError("symbol is the broker's symbol name");
|
|
25
|
+
}
|
|
26
|
+
if (options.accountType !== undefined && typeof options.accountType !== "string") {
|
|
27
|
+
throw new TypeError("accountType is the account type's name");
|
|
28
|
+
}
|
|
29
|
+
const body = await this.#client.read({
|
|
30
|
+
method: "GET",
|
|
31
|
+
path: `/brokers/${broker}/symbols/${encodeURIComponent(symbol)}/quote`,
|
|
32
|
+
query: {
|
|
33
|
+
server_id: options.serverId === undefined ? undefined : uuid("serverId", options.serverId),
|
|
34
|
+
account_type: options.accountType,
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
return body;
|
|
38
|
+
}
|
|
39
|
+
}
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `MfxClient`: one signed connection to the MFX Hub API.
|
|
3
|
+
*
|
|
4
|
+
* Every request is signed with the tenant's Ed25519 key. Reads that are rate limited are retried
|
|
5
|
+
* after the platform's own `Retry-After`; writes are never retried, because a retried write is a
|
|
6
|
+
* second order. Failures become typed errors from `errors.ts`.
|
|
7
|
+
*/
|
|
8
|
+
import { MfxApiError } from "./errors.js";
|
|
9
|
+
import { RequestSigner, type SigningKey } from "./signing.js";
|
|
10
|
+
import { Catalog } from "./catalog.js";
|
|
11
|
+
import { Execution } from "./execution.js";
|
|
12
|
+
import { MfxStream, type StreamOptions } from "./stream.js";
|
|
13
|
+
import { Terminals } from "./terminals.js";
|
|
14
|
+
/** Default time a single request may take. */
|
|
15
|
+
export declare const DEFAULT_TIMEOUT_MS = 30000;
|
|
16
|
+
/** How many times a rate-limited read is tried again. */
|
|
17
|
+
export declare const DEFAULT_READ_RETRIES = 3;
|
|
18
|
+
/** What the key had left when MFX Hub last answered. */
|
|
19
|
+
export interface Budget {
|
|
20
|
+
readonly remaining: number;
|
|
21
|
+
readonly source: string;
|
|
22
|
+
}
|
|
23
|
+
/** Options for {@link MfxClient}. */
|
|
24
|
+
export interface ClientOptions {
|
|
25
|
+
/** Time one request may take, in milliseconds. */
|
|
26
|
+
readonly timeoutMs?: number;
|
|
27
|
+
/** How many times a rate-limited read is tried again. */
|
|
28
|
+
readonly readRetries?: number;
|
|
29
|
+
/** How the client waits between attempts; replaceable in tests. */
|
|
30
|
+
readonly sleep?: (ms: number) => Promise<void>;
|
|
31
|
+
/** The fetch implementation; the runtime's own by default. */
|
|
32
|
+
readonly fetch?: typeof fetch;
|
|
33
|
+
/** The signer's clock, in epoch milliseconds; replaceable in tests. */
|
|
34
|
+
readonly clock?: () => number;
|
|
35
|
+
/** A number in [0, 1) for the backoff's jitter; replaceable in tests. */
|
|
36
|
+
readonly random?: () => number;
|
|
37
|
+
}
|
|
38
|
+
/** One request, as the resource modules describe it. */
|
|
39
|
+
export interface RequestSpec {
|
|
40
|
+
readonly method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
41
|
+
/** The path after the version, such as `/terminals/{id}`, with ids already filled in. */
|
|
42
|
+
readonly path: string;
|
|
43
|
+
readonly query?: Readonly<Record<string, string | number | boolean | null | undefined>>;
|
|
44
|
+
readonly body?: unknown;
|
|
45
|
+
/** Required on every write; sent as the Idempotency-Key header. */
|
|
46
|
+
readonly idempotencyKey?: string;
|
|
47
|
+
}
|
|
48
|
+
/** The status and parsed body of an answer. */
|
|
49
|
+
export interface Answer {
|
|
50
|
+
readonly status: number;
|
|
51
|
+
readonly headers: Headers;
|
|
52
|
+
readonly body: unknown;
|
|
53
|
+
}
|
|
54
|
+
/** A tenant's connection to MFX Hub. */
|
|
55
|
+
export declare class MfxClient {
|
|
56
|
+
#private;
|
|
57
|
+
/** The terminals this key's tenant owns. */
|
|
58
|
+
readonly terminals: Terminals;
|
|
59
|
+
/** Order entry. Every call resolves to a result; none rejects. */
|
|
60
|
+
readonly execution: Execution;
|
|
61
|
+
/** Brokers, symbols and quotes. */
|
|
62
|
+
readonly catalog: Catalog;
|
|
63
|
+
/**
|
|
64
|
+
* @param baseUrl the API's base, including the version: `https://api.mfxhub.com/v1`, or
|
|
65
|
+
* `https://api.dev.mfxhub.com/v1` for development
|
|
66
|
+
* @param keyId the API key's id
|
|
67
|
+
* @param privateKey its private key: PEM, the base64 of its 32 bytes, or a SigningKey
|
|
68
|
+
*/
|
|
69
|
+
constructor(baseUrl: string, keyId: string, privateKey: string | Uint8Array | SigningKey, options?: ClientOptions);
|
|
70
|
+
/** The id of the key this client signs with. Safe to log, unlike the private key. */
|
|
71
|
+
get keyId(): string;
|
|
72
|
+
/** The REST base this client was pointed at. The stream's address is derived from it. */
|
|
73
|
+
get baseUrl(): string;
|
|
74
|
+
/** What the key had left when MFX Hub last answered; null before the first answer. */
|
|
75
|
+
get budget(): Budget | null;
|
|
76
|
+
/** The signer, for the stream's upgrade. Not part of the public surface. */
|
|
77
|
+
get signer(): RequestSigner;
|
|
78
|
+
/**
|
|
79
|
+
* Jittered exponential backoff for an attempt, when the platform gave no delay: up to 1 s, 2 s,
|
|
80
|
+
* 4 s ... capped at 30 s. The reads and the stream's reconnects share it, so they lean on this
|
|
81
|
+
* tenant's quota at the same rate.
|
|
82
|
+
*/
|
|
83
|
+
backoffMs(attempt: number): number;
|
|
84
|
+
/**
|
|
85
|
+
* The multiplexed event stream, opened: subscribe, then iterate. Close it when done.
|
|
86
|
+
* Reconnects underneath, resuming what replays; see {@link MfxStream}.
|
|
87
|
+
*/
|
|
88
|
+
stream(options?: StreamOptions): Promise<MfxStream>;
|
|
89
|
+
/** How the client waits; the stream uses it between reconnects. */
|
|
90
|
+
pause(ms: number): Promise<void>;
|
|
91
|
+
/** A read: retried after a 429 with the platform's own delay; a failure throws its typed error. */
|
|
92
|
+
read(spec: RequestSpec): Promise<unknown>;
|
|
93
|
+
/** A write: sent once, never retried; a failure throws its typed error. */
|
|
94
|
+
write(spec: RequestSpec): Promise<unknown>;
|
|
95
|
+
/** One attempt, signed afresh. Transport faults become MfxTransportError or MfxTimeout. */
|
|
96
|
+
send(spec: RequestSpec): Promise<Answer>;
|
|
97
|
+
toString(): string;
|
|
98
|
+
}
|
|
99
|
+
/** The typed error a failed answer means, or nothing for a success. */
|
|
100
|
+
export declare function throwFor(answer: Answer, spec: RequestSpec): void;
|
|
101
|
+
export { MfxApiError };
|