@openmarketsai/order-sheet 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/LICENSE +201 -0
- package/NOTICE +5 -0
- package/README.md +183 -0
- package/dist/auth.d.ts +24 -0
- package/dist/auth.js +36 -0
- package/dist/auth.js.map +1 -0
- package/dist/client.d.ts +158 -0
- package/dist/client.js +253 -0
- package/dist/client.js.map +1 -0
- package/dist/errors.d.ts +34 -0
- package/dist/errors.js +34 -0
- package/dist/errors.js.map +1 -0
- package/dist/generated/contract.d.ts +28 -0
- package/dist/generated/contract.js +176 -0
- package/dist/generated/contract.js.map +1 -0
- package/dist/http.d.ts +19 -0
- package/dist/http.js +58 -0
- package/dist/http.js.map +1 -0
- package/dist/index.cjs +1742 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.js +31 -0
- package/dist/index.js.map +1 -0
- package/dist/math.d.ts +22 -0
- package/dist/math.js +55 -0
- package/dist/math.js.map +1 -0
- package/dist/messages.d.ts +21 -0
- package/dist/messages.js +97 -0
- package/dist/messages.js.map +1 -0
- package/dist/odds.d.ts +12 -0
- package/dist/odds.js +33 -0
- package/dist/odds.js.map +1 -0
- package/dist/quote.d.ts +20 -0
- package/dist/quote.js +117 -0
- package/dist/quote.js.map +1 -0
- package/dist/react.cjs +2137 -0
- package/dist/react.d.ts +157 -0
- package/dist/react.js +297 -0
- package/dist/react.js.map +1 -0
- package/dist/sheet.d.ts +77 -0
- package/dist/sheet.js +648 -0
- package/dist/sheet.js.map +1 -0
- package/dist/stream.d.ts +71 -0
- package/dist/stream.js +160 -0
- package/dist/stream.js.map +1 -0
- package/dist/types.d.ts +287 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/ui/styles.css +284 -0
- package/dist/ui/theme.d.ts +108 -0
- package/dist/ui/theme.js +180 -0
- package/dist/ui/theme.js.map +1 -0
- package/dist/ui.cjs +2606 -0
- package/dist/ui.d.ts +124 -0
- package/dist/ui.js +154 -0
- package/dist/ui.js.map +1 -0
- package/dist/validate.d.ts +20 -0
- package/dist/validate.js +126 -0
- package/dist/validate.js.map +1 -0
- package/dist/venues.d.ts +19 -0
- package/dist/venues.js +103 -0
- package/dist/venues.js.map +1 -0
- package/dist/wire.d.ts +382 -0
- package/dist/wire.js +2 -0
- package/dist/wire.js.map +1 -0
- package/package.json +78 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
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.
|
package/NOTICE
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
# @openmarketsai/order-sheet
|
|
2
|
+
|
|
3
|
+
**The bet slip, solved.** Every venue's rules, fees and balances checked before the tap — in your app's style.
|
|
4
|
+
|
|
5
|
+
Zero runtime dependencies. React optional. Types included.
|
|
6
|
+
|
|
7
|
+
The sheet carries **no venue name and no venue rule**. The OpenMarkets API serves the rules for the account in front of it (`GET /flow/v1/auth/orders/constraints`); the sheet validates against that table for instant feedback, refines the numbers through `POST /orders/preflight`, places through `POST /orders/buy`, and follows the order on the stream to fill and settlement. When a venue changes its minimum, nothing in your app changes. (The only numbers of its own are platform-wide fallbacks used before the constraints load — the 1¢–99¢ probability band — and the default 2¢ market-order slippage, which you set with `defaults.slippage`.)
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @openmarketsai/order-sheet
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Three entry points:
|
|
14
|
+
|
|
15
|
+
| Entry | What | Needs |
|
|
16
|
+
|---|---|---|
|
|
17
|
+
| `@openmarketsai/order-sheet` | headless core — a state machine + API client for one slip; browser, Node, agents | nothing |
|
|
18
|
+
| `@openmarketsai/order-sheet/react` | hooks + **unstyled** primitives with `data-oms-part` attributes | `react` |
|
|
19
|
+
| `@openmarketsai/order-sheet/ui` | the styled `<OrderSheetCard>`, OpenMarkets brand by default, themeable | `react` + `ui/styles.css` |
|
|
20
|
+
|
|
21
|
+
## Founder rules the SDK enforces
|
|
22
|
+
|
|
23
|
+
- **The user picks the venue.** The sheet lists every venue quoting the position, sorted cheapest-first with a `best` badge, and may say *"40¢ available on Polymarket — switch?"*. It never switches for them. Best-price routing is coming; nothing here pre-empts it.
|
|
24
|
+
- **Practice by default; real money is opt-in twice.** Every leg is sent with an explicit `mode` (the API's own default is *real*) — the raw `om.buy()` / `om.preflight()` refuse a leg without one, and `om.buy()` refuses a `real` leg unless `realMoney.enabled`. Real money requires the developer to enable it (`realMoney.enabled`) **and** the account to be able to (`money_modes` from the server: plan, environment, execution policy) — and then a second confirm click per order. A confirmation covers exactly the inputs it was shown with: any edit voids it and the user confirms again.
|
|
25
|
+
- **Never re-encode a venue rule.** Minimums, whole contracts, `min_contracts`, increments, price bands and ticks, fee direction, fill semantics, what's connected and funded — all read from the server. The server stays the authority: a preflight or buy answer always overrides a local check.
|
|
26
|
+
- **No auto-retry of a buy, no double submit.** A fresh `Idempotency-Key` per placement (generated by `om.buy()` too when you pass none), kept across a transport failure so *your* retry replays instead of doubling. While a placement is in flight, `place()` / `confirm()` return that same promise.
|
|
27
|
+
|
|
28
|
+
## Headless — no framework, no DOM
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { createOrderSheetClient } from '@openmarketsai/order-sheet'
|
|
32
|
+
|
|
33
|
+
const om = createOrderSheetClient({
|
|
34
|
+
apiKey: process.env.OM_EXEC_KEY!, // or { getToken } (player JWT) or { auth } (app-sdk)
|
|
35
|
+
// baseUrl: 'https://api.openmarkets.ai',
|
|
36
|
+
// realMoney: { enabled: false, confirm: true }, ← the defaults. `confirm: false` removes the
|
|
37
|
+
// second click on real orders: only do that if YOUR UI already makes the user confirm the exact
|
|
38
|
+
// order — the SDK then places real money on the first place(), and that is your liability.
|
|
39
|
+
// defaults: { mode: 'paper', orderType: 'market', stakeUnit: 'amount', slippage: 0.02, oddsFormat: 'cents' },
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
// One slip per position. From a liquidity cell, a position hash, or a full position.
|
|
43
|
+
const sheet = om.openSheet({ liquidityHash: cell.liquidity_hash })
|
|
44
|
+
|
|
45
|
+
sheet.subscribe((s) => console.log(s.phase, s.validation.blocking.map((i) => i.message)))
|
|
46
|
+
|
|
47
|
+
sheet.set({ stake: { amount: 5 } }) // or { shares: 10 }; also maxPrice, orderType, mode, oddsFormat
|
|
48
|
+
sheet.select('pg-kalshi') // the user's choice — never the cheapest by default
|
|
49
|
+
|
|
50
|
+
const s = sheet.getState()
|
|
51
|
+
s.venues // VenueOption[] — price, available, account { connected, expectedBalance }, eligibility { status, issues }, best
|
|
52
|
+
s.quote // contracts, cost, payout, leftover, fee (estimated → exact after preflight), effectivePrice, profit, display.*
|
|
53
|
+
s.validation // { ok, blocking, warnings, info } — Issue { code, severity, message, fix, scope, source }
|
|
54
|
+
|
|
55
|
+
await sheet.preflight() // exact fees, expected fill, every pre-placement check, in the server's words
|
|
56
|
+
const out = await sheet.place() // { status: 'placed', result } | { status: 'blocked', issues } | { status: 'confirm_required' }
|
|
57
|
+
if (out.status === 'confirm_required') await sheet.confirm() // places only if nothing changed since; else { status: 'blocked', issues: [confirm_expired] }
|
|
58
|
+
if (out.status === 'placed') console.log(out.result.summary, out.result.legs[0].fill) // 'filled' { shares: 11, notional: 4.62, avgPrice: 0.42, unfilled: 0.38 }
|
|
59
|
+
|
|
60
|
+
// Afterwards
|
|
61
|
+
sheet.getState().phase // 'filled' | 'partial' | 'resting' | 'pending' | 'failed' → 'settled' | 'cancelled' (follows the orders stream)
|
|
62
|
+
await sheet.cancel() // the resting order(s) this sheet placed
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Errors: a transport/4xx failure **throws** `OrderSheetError { code, status, retryable, details }` (the code is the API's — `real_money_not_permitted`, `unsupported_order_type`, …). A leg the venue declines **never throws**: it is `result.legs[i].issue`, the same `Issue` shape the validation uses, with the server's plain-language `message` and a `fix`.
|
|
66
|
+
|
|
67
|
+
Multi-leg: `om.buy(legs, { atomic: true })` expands to the hedge recipe (`check_all_first` + `on_leg_failure: 'halt'` + `placement: 'parallel'` + `verify_liquidity`). A multi-leg *sheet* (`openSlip`) is a later phase; the types are shaped for it.
|
|
68
|
+
|
|
69
|
+
## React — hooks + unstyled primitives
|
|
70
|
+
|
|
71
|
+
```tsx
|
|
72
|
+
import { OrderSheet, useOrderSheet } from '@openmarketsai/order-sheet/react'
|
|
73
|
+
|
|
74
|
+
function Slip({ om, cell }) {
|
|
75
|
+
const h = useOrderSheet(om, { liquidityHash: cell.liquidity_hash }) // subscribes; re-renders on state
|
|
76
|
+
return (
|
|
77
|
+
<OrderSheet.Root handle={h} onOutcome={(o) => console.log(o)}>
|
|
78
|
+
<OrderSheet.Venues /> {/* <div data-oms-part="venue" data-state="ok|warn|blocked" data-best data-selected> */}
|
|
79
|
+
<OrderSheet.Mode /> {/* practice / real, aria-pressed */}
|
|
80
|
+
<OrderSheet.Stake /> <OrderSheet.Price />
|
|
81
|
+
<OrderSheet.Quote /> <OrderSheet.Issues /> <OrderSheet.Hint />
|
|
82
|
+
<OrderSheet.Submit /> {/* disabled until canPlace; label "Place practice order · $5.00" */}
|
|
83
|
+
<OrderSheet.Outcome />
|
|
84
|
+
</OrderSheet.Root>
|
|
85
|
+
)
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
`useOrderSheet` opens the sheet in an effect, so server rendering (Next.js client components included) fetches nothing and starts no timer; until the effect runs the handle is `phase: 'loading'` and its actions are inert. `<OrderSheet.Venues>` is a WAI-ARIA radio group: one venue is in the tab order (the selected one, else the first enabled), arrow keys / Home / End move and select, skipping venues the mode blocks.
|
|
90
|
+
|
|
91
|
+
Every primitive renders a plain element with `data-oms-part="…"` and state attributes and **no class of its own**, so a Tailwind user styles them with `[data-oms-part=submit][data-mode=real]:bg-red-600`. Each takes `className`, spreads the rest, and accepts `render` / `children` to swap the markup while keeping the wiring. Wrap the tree in `<OrderSheetProvider client={om}>` to omit the client argument; `useOrderSheetAccount()` gives `money_modes` before any position is open.
|
|
92
|
+
|
|
93
|
+
## Styled — `<OrderSheetCard>` in the OpenMarkets brand
|
|
94
|
+
|
|
95
|
+
```tsx
|
|
96
|
+
import { OrderSheetCard } from '@openmarketsai/order-sheet/ui'
|
|
97
|
+
import '@openmarketsai/order-sheet/ui/styles.css'
|
|
98
|
+
|
|
99
|
+
<OrderSheetCard client={om} position={{ liquidityHash }} />
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Zinc neutrals, indigo accent, JetBrains Mono for numbers, 4px radius, no shadows; light / dark by system preference. Practice CTA in the accent; the real-money CTA in the danger colour with a confirm step. Container queries, not media queries, drive the layout — the sheet adapts to *its* box.
|
|
103
|
+
|
|
104
|
+
### Theming
|
|
105
|
+
|
|
106
|
+
Three levels, cheapest first.
|
|
107
|
+
|
|
108
|
+
**1. A theme** — colours, type, radius, density, scheme. Partial overrides deep-merge over a preset; a strong colour given alone derives its tint.
|
|
109
|
+
|
|
110
|
+
```tsx
|
|
111
|
+
import { OrderSheetCard, OrderSheetTheme, createTheme, presets } from '@openmarketsai/order-sheet/ui'
|
|
112
|
+
|
|
113
|
+
const theme = createTheme({
|
|
114
|
+
scheme: 'system', // 'light' | 'dark' | 'system'
|
|
115
|
+
accent: '#c7780f', // one value for both schemes …
|
|
116
|
+
danger: { light: '#b91c1c', dark: '#f87171' }, // … or one per scheme
|
|
117
|
+
font: '"Söhne", system-ui, sans-serif',
|
|
118
|
+
radius: '10px',
|
|
119
|
+
density: 0.9, // 0.85 compact · 1 · 1.15 touch
|
|
120
|
+
})
|
|
121
|
+
// or start from a preset: createTheme({ accent: '#000' }, presets.slate)
|
|
122
|
+
|
|
123
|
+
<OrderSheetTheme theme={theme}> {/* <div class="oms-root oms-theme-…"> + a scoped <style> of --oms-* vars */}
|
|
124
|
+
<OrderSheetCard client={om} position={{ liquidityHash }} />
|
|
125
|
+
</OrderSheetTheme>
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
The stylesheet's contract with your page: every rule under `.oms-root`, every class `oms-*`, every variable `--oms-*`, all inside `@layer oms` (your unlayered rules always win), no `:root` / `body` / `*` rules, no `@font-face`. Force a scheme on any ancestor with `data-oms-theme="light|dark"`.
|
|
129
|
+
|
|
130
|
+
**2. `classNames` per part** — a string, or a function of that part's state:
|
|
131
|
+
|
|
132
|
+
```tsx
|
|
133
|
+
<OrderSheetCard … classNames={{
|
|
134
|
+
root: 'my-slip',
|
|
135
|
+
submit: ({ handle }) => handle.mode === 'real' && 'my-danger-cta',
|
|
136
|
+
venueName: ({ venue }) => venue?.best && 'font-bold',
|
|
137
|
+
}} />
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
Parts: `root header title subtitle venues venue venueName venuePrice venueReason mode orderType fields stake price quote issues issue hint submit outcome footer`.
|
|
141
|
+
|
|
142
|
+
**3. `slots`** — replace a part with your own component, receiving the same `handle` (and `venue` / `issue`) the default gets:
|
|
143
|
+
|
|
144
|
+
```tsx
|
|
145
|
+
<OrderSheetCard … slots={{ VenueRow: MyVenueRow, Outcome: MyOutcome }} labels={{ practice: 'Play money', real: 'Cash' }} />
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Below that is the unstyled `/react` layer, and below that the headless core.
|
|
149
|
+
|
|
150
|
+
### Card props
|
|
151
|
+
|
|
152
|
+
`client?` · `position` (a ref, or a `Sheet` you opened) · `init?` · `theme?` · `scheme?` · `classNames?` · `slots?` · `labels?` · `oddsFormat?` (`cents` default, `american`, `percent`, `decimal`) · `showBlockedVenues?` (default **true** — the user should see where the better price is and why they can't use it) · `showModeSwitch?` · `showOrderType?` · `showFees?` · `onOutcome?` · `onPlaceError?` · `onFix?` (for fixes the sheet cannot apply itself: `connect_venue`, `fund_venue`, `grant_scope`, `upgrade_plan`).
|
|
153
|
+
|
|
154
|
+
## Credentials
|
|
155
|
+
|
|
156
|
+
| Option | Sent as | Stream |
|
|
157
|
+
|---|---|---|
|
|
158
|
+
| `{ apiKey }` — an execution key (`om_exec_live_…`) | `X-API-Key` | yes |
|
|
159
|
+
| `{ auth }` — `createOpenMarketsAuth()` from `@openmarketsai/app-sdk` | `X-API-Key`, `getApiKey()` per request | yes |
|
|
160
|
+
| `{ getToken }` — a player JWT | `Authorization: Bearer` | no — order updates are polled |
|
|
161
|
+
|
|
162
|
+
`actAs: '<external_user_id>'` adds `X-OpenMarkets-Account` for OpenMarkets Connect (needs `connect:trade`). A test key (`om_*_test_*`) never sees the real-money option.
|
|
163
|
+
|
|
164
|
+
## What the server answers (and the SDK never computes)
|
|
165
|
+
|
|
166
|
+
| Question | Endpoint |
|
|
167
|
+
|---|---|
|
|
168
|
+
| Can this account place an order at this venue, in this mode, and what are the rules? | `GET /flow/v1/auth/orders/constraints` (cached; re-read on `balance.update`) |
|
|
169
|
+
| What would this exact order cost, fill and pay — and would `/buy` refuse it? | `POST /flow/v1/auth/orders/preflight` (debounced 300 ms after each change; `preflight: { auto: false }` to run it yourself) |
|
|
170
|
+
| Place it | `POST /flow/v1/auth/orders/buy` with `Idempotency-Key` |
|
|
171
|
+
| Did it fill / settle? | `/flow/v1/stream` channels `orders` + `balance`, or `GET /auth/orders` polling on a JWT |
|
|
172
|
+
|
|
173
|
+
Every rejection is one closed code (`IssueCode`) generated from the server's union, with a server-authored user message beside the developer reason.
|
|
174
|
+
|
|
175
|
+
## Example
|
|
176
|
+
|
|
177
|
+
```bash
|
|
178
|
+
cd packages/order-sheet && npm run example # http://localhost:8000 — the styled sheet against a mocked API, with a theme switcher
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## License
|
|
182
|
+
|
|
183
|
+
Apache-2.0
|
package/dist/auth.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export interface AppSdkAuthLike {
|
|
2
|
+
getApiKey(): Promise<string | null> | string | null;
|
|
3
|
+
}
|
|
4
|
+
export type AuthOptions = {
|
|
5
|
+
apiKey: string;
|
|
6
|
+
getToken?: never;
|
|
7
|
+
auth?: never;
|
|
8
|
+
} | {
|
|
9
|
+
getToken: () => Promise<string | null> | string | null;
|
|
10
|
+
apiKey?: never;
|
|
11
|
+
auth?: never;
|
|
12
|
+
} | {
|
|
13
|
+
auth: AppSdkAuthLike;
|
|
14
|
+
apiKey?: never;
|
|
15
|
+
getToken?: never;
|
|
16
|
+
};
|
|
17
|
+
export interface ResolvedCredential {
|
|
18
|
+
kind: 'api_key' | 'jwt';
|
|
19
|
+
value: string;
|
|
20
|
+
headers: Record<string, string>;
|
|
21
|
+
}
|
|
22
|
+
export declare function resolveCredential(opts: AuthOptions): Promise<ResolvedCredential>;
|
|
23
|
+
/** Which transport the credential supports for live order updates. */
|
|
24
|
+
export declare function credentialSupportsStream(opts: AuthOptions): boolean;
|
package/dist/auth.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Credentials — every credential the platform has, one option each:
|
|
3
|
+
*
|
|
4
|
+
* { apiKey } an owner_self / server key (om_exec_live_…). Sent as X-API-Key.
|
|
5
|
+
* { getToken } a player JWT from a first-party app. Sent as Authorization: Bearer.
|
|
6
|
+
* { auth } a marketplace app's `createOpenMarketsAuth()` from @openmarketsai/app-sdk —
|
|
7
|
+
* `getApiKey()` is called PER REQUEST, never cached, so a refreshed key
|
|
8
|
+
* is always the one on the wire.
|
|
9
|
+
*
|
|
10
|
+
* The stream (/flow/v1/stream) authenticates with an API key only; a JWT session
|
|
11
|
+
* falls back to polling for order updates.
|
|
12
|
+
*/
|
|
13
|
+
import { OrderSheetError } from './errors.js';
|
|
14
|
+
export async function resolveCredential(opts) {
|
|
15
|
+
if ('apiKey' in opts && opts.apiKey) {
|
|
16
|
+
return { kind: 'api_key', value: opts.apiKey, headers: { 'X-API-Key': opts.apiKey } };
|
|
17
|
+
}
|
|
18
|
+
if ('auth' in opts && opts.auth) {
|
|
19
|
+
const key = await opts.auth.getApiKey();
|
|
20
|
+
if (!key)
|
|
21
|
+
throw new OrderSheetError({ code: 'no_credential', message: 'Not signed in to OpenMarkets — no API key is available for this request.' });
|
|
22
|
+
return { kind: 'api_key', value: key, headers: { 'X-API-Key': key } };
|
|
23
|
+
}
|
|
24
|
+
if ('getToken' in opts && opts.getToken) {
|
|
25
|
+
const token = await opts.getToken();
|
|
26
|
+
if (!token)
|
|
27
|
+
throw new OrderSheetError({ code: 'no_credential', message: 'No session token is available for this request.' });
|
|
28
|
+
return { kind: 'jwt', value: token, headers: { Authorization: `Bearer ${token}` } };
|
|
29
|
+
}
|
|
30
|
+
throw new OrderSheetError({ code: 'not_configured', message: 'createOrderSheetClient needs one of { apiKey }, { getToken } or { auth }.' });
|
|
31
|
+
}
|
|
32
|
+
/** Which transport the credential supports for live order updates. */
|
|
33
|
+
export function credentialSupportsStream(opts) {
|
|
34
|
+
return ('apiKey' in opts && !!opts.apiKey) || ('auth' in opts && !!opts.auth);
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=auth.js.map
|
package/dist/auth.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.js","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAiB7C,MAAM,CAAC,KAAK,UAAU,iBAAiB,CAAC,IAAiB;IACrD,IAAI,QAAQ,IAAI,IAAI,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;QAClC,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,IAAI,CAAC,MAAM,EAAE,OAAO,EAAE,EAAE,WAAW,EAAE,IAAI,CAAC,MAAM,EAAE,EAAE,CAAA;IACzF,CAAC;IACD,IAAI,MAAM,IAAI,IAAI,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC;QAC9B,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,CAAA;QACvC,IAAI,CAAC,GAAG;YAAE,MAAM,IAAI,eAAe,CAAC,EAAE,IAAI,EAAE,eAAe,EAAE,OAAO,EAAE,0EAA0E,EAAE,CAAC,CAAA;QACnJ,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,GAAG,EAAE,OAAO,EAAE,EAAE,WAAW,EAAE,GAAG,EAAE,EAAE,CAAA;IACzE,CAAC;IACD,IAAI,UAAU,IAAI,IAAI,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;QACtC,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,QAAQ,EAAE,CAAA;QACnC,IAAI,CAAC,KAAK;YAAE,MAAM,IAAI,eAAe,CAAC,EAAE,IAAI,EAAE,eAAe,EAAE,OAAO,EAAE,iDAAiD,EAAE,CAAC,CAAA;QAC5H,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE,aAAa,EAAE,UAAU,KAAK,EAAE,EAAE,EAAE,CAAA;IACvF,CAAC;IACD,MAAM,IAAI,eAAe,CAAC,EAAE,IAAI,EAAE,gBAAgB,EAAE,OAAO,EAAE,2EAA2E,EAAE,CAAC,CAAA;AAC/I,CAAC;AAED,sEAAsE;AACtE,MAAM,UAAU,wBAAwB,CAAC,IAAiB;IACtD,OAAO,CAAC,QAAQ,IAAI,IAAI,IAAI,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,IAAI,IAAI,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;AACjF,CAAC"}
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The client: one per credential. Caches the account's constraints, loads
|
|
3
|
+
* positions, opens sheets, talks to /preflight, /buy, /cancel and the resting
|
|
4
|
+
* list, and owns the stream. Everything venue-specific arrives from the server.
|
|
5
|
+
*/
|
|
6
|
+
import { type AuthOptions } from './auth.js';
|
|
7
|
+
import { type FetchLike, type Http } from './http.js';
|
|
8
|
+
import type { OddsFormat } from './odds.js';
|
|
9
|
+
import { type Sheet, type SheetInit } from './sheet.js';
|
|
10
|
+
import { type OrderStream, type WebSocketCtor } from './stream.js';
|
|
11
|
+
import type { AccountFacts, MoneyMode, OrderType, PositionRef, StakeUnit } from './types.js';
|
|
12
|
+
import type { BuyLeg, BuyResponse, CancelResponse, Me, OrderConstraints, OrderRow, Position, PreflightResponse, RestingOrder, StreamBalanceUpdate, StreamOrderSettled, StreamOrderUpdate } from './wire.js';
|
|
13
|
+
export declare const DEFAULT_API_BASE = "https://api.openmarkets.ai";
|
|
14
|
+
export interface OrderSheetLabels {
|
|
15
|
+
placePractice: string;
|
|
16
|
+
placeReal: string;
|
|
17
|
+
confirmReal: string;
|
|
18
|
+
placing: string;
|
|
19
|
+
practice: string;
|
|
20
|
+
real: string;
|
|
21
|
+
}
|
|
22
|
+
export interface OrderSheetClientOptions {
|
|
23
|
+
/** One of the three credentials — see ./auth.ts. */
|
|
24
|
+
apiKey?: string;
|
|
25
|
+
getToken?: () => Promise<string | null> | string | null;
|
|
26
|
+
auth?: {
|
|
27
|
+
getApiKey(): Promise<string | null> | string | null;
|
|
28
|
+
};
|
|
29
|
+
baseUrl?: string;
|
|
30
|
+
/** Defaults to baseUrl with ws(s):// and /flow/v1/stream. */
|
|
31
|
+
streamUrl?: string;
|
|
32
|
+
/** X-OpenMarkets-Account — act for a Connect end user. */
|
|
33
|
+
actAs?: string | null;
|
|
34
|
+
fetch?: FetchLike;
|
|
35
|
+
WebSocket?: WebSocketCtor;
|
|
36
|
+
defaults?: Partial<ResolvedClientOptions['defaults']>;
|
|
37
|
+
/**
|
|
38
|
+
* The developer arms real money; the user still confirms every real order (`confirm`). Both default to safe.
|
|
39
|
+
* `confirm: false` places a real order on the first `place()` — set it only when your own UI already has the
|
|
40
|
+
* user confirm the exact order; skipping the confirmation is then the integrating app's responsibility.
|
|
41
|
+
*/
|
|
42
|
+
realMoney?: Partial<ResolvedClientOptions['realMoney']>;
|
|
43
|
+
preflight?: Partial<ResolvedClientOptions['preflight']>;
|
|
44
|
+
constraints?: Partial<ResolvedClientOptions['constraints']>;
|
|
45
|
+
stream?: Partial<ResolvedClientOptions['stream']>;
|
|
46
|
+
labels?: Partial<OrderSheetLabels>;
|
|
47
|
+
/** Test hooks. */
|
|
48
|
+
now?: () => number;
|
|
49
|
+
timers?: {
|
|
50
|
+
set: typeof setTimeout;
|
|
51
|
+
clear: typeof clearTimeout;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
export interface ResolvedClientOptions {
|
|
55
|
+
auth: AuthOptions;
|
|
56
|
+
baseUrl: string;
|
|
57
|
+
streamUrl: string;
|
|
58
|
+
actAs: string | null;
|
|
59
|
+
defaults: {
|
|
60
|
+
mode: MoneyMode;
|
|
61
|
+
orderType: OrderType;
|
|
62
|
+
stakeUnit: StakeUnit;
|
|
63
|
+
slippage: number;
|
|
64
|
+
oddsFormat: OddsFormat;
|
|
65
|
+
staleAfterMs: number;
|
|
66
|
+
};
|
|
67
|
+
realMoney: {
|
|
68
|
+
enabled: boolean;
|
|
69
|
+
confirm: boolean;
|
|
70
|
+
};
|
|
71
|
+
preflight: {
|
|
72
|
+
auto: boolean;
|
|
73
|
+
debounceMs: number;
|
|
74
|
+
};
|
|
75
|
+
constraints: {
|
|
76
|
+
ttlMs: number;
|
|
77
|
+
};
|
|
78
|
+
stream: {
|
|
79
|
+
enabled: boolean;
|
|
80
|
+
pollIntervalMs: number;
|
|
81
|
+
};
|
|
82
|
+
labels: OrderSheetLabels;
|
|
83
|
+
now: () => number;
|
|
84
|
+
timers: {
|
|
85
|
+
set: typeof setTimeout;
|
|
86
|
+
clear: typeof clearTimeout;
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
export interface ClientEvents {
|
|
90
|
+
'order.update': StreamOrderUpdate;
|
|
91
|
+
'order.settled': StreamOrderSettled;
|
|
92
|
+
'balance.update': StreamBalanceUpdate;
|
|
93
|
+
'stream.connected': {
|
|
94
|
+
server_time: string | null;
|
|
95
|
+
};
|
|
96
|
+
/** The cached constraints were replaced or marked stale. */
|
|
97
|
+
constraints: OrderConstraints | null;
|
|
98
|
+
}
|
|
99
|
+
export interface ClientEventBus {
|
|
100
|
+
on<K extends keyof ClientEvents>(event: K, handler: (payload: ClientEvents[K]) => void): () => void;
|
|
101
|
+
emit<K extends keyof ClientEvents>(event: K, payload: ClientEvents[K]): void;
|
|
102
|
+
}
|
|
103
|
+
export interface OrderSheetClient {
|
|
104
|
+
readonly options: ResolvedClientOptions;
|
|
105
|
+
readonly http: Http;
|
|
106
|
+
readonly events: ClientEventBus;
|
|
107
|
+
/** Account facts: money modes, practice wallet, policy. Cached with the constraints. */
|
|
108
|
+
account(opts?: {
|
|
109
|
+
force?: boolean;
|
|
110
|
+
}): Promise<AccountFacts>;
|
|
111
|
+
/** The pre-trade answer, cached for `constraints.ttlMs` and re-read on a balance update. */
|
|
112
|
+
constraints(opts?: {
|
|
113
|
+
force?: boolean;
|
|
114
|
+
}): Promise<OrderConstraints>;
|
|
115
|
+
getCachedConstraints(): OrderConstraints | null;
|
|
116
|
+
/** True when the server's constraints_version differs from the one this SDK was built for: trust preflight over local checks. */
|
|
117
|
+
constraintsStale(): boolean;
|
|
118
|
+
accountFromConstraints(c: OrderConstraints): AccountFacts;
|
|
119
|
+
invalidateConstraints(): void;
|
|
120
|
+
loadPosition(ref: PositionRef): Promise<{
|
|
121
|
+
position: Position;
|
|
122
|
+
partnerId: string | null;
|
|
123
|
+
}>;
|
|
124
|
+
openSheet(ref: PositionRef, init?: SheetInit): Sheet;
|
|
125
|
+
preflight(legs: BuyLeg[], opts?: {
|
|
126
|
+
verifyLiquidity?: boolean;
|
|
127
|
+
atomic?: boolean;
|
|
128
|
+
}): Promise<PreflightResponse>;
|
|
129
|
+
/**
|
|
130
|
+
* Raw multi-leg buy. `atomic: true` = check_all_first + halt + parallel + verify_liquidity. No retries, ever.
|
|
131
|
+
* Every leg must carry an explicit `mode` (the API defaults an omitted mode to REAL); a `real` leg is
|
|
132
|
+
* refused unless the developer armed `realMoney.enabled`. An `Idempotency-Key` is generated when none
|
|
133
|
+
* is given — pass your own to make YOUR retry replay.
|
|
134
|
+
*/
|
|
135
|
+
buy(legs: BuyLeg[], opts?: {
|
|
136
|
+
idempotencyKey?: string;
|
|
137
|
+
verifyLiquidity?: boolean;
|
|
138
|
+
atomic?: boolean;
|
|
139
|
+
opportunityId?: string;
|
|
140
|
+
modelId?: string;
|
|
141
|
+
}): Promise<BuyResponse>;
|
|
142
|
+
restingOrders(): Promise<RestingOrder[]>;
|
|
143
|
+
listOrders(opts?: {
|
|
144
|
+
contestId?: string;
|
|
145
|
+
status?: string;
|
|
146
|
+
limit?: number;
|
|
147
|
+
}): Promise<OrderRow[]>;
|
|
148
|
+
cancelOrders(routerOrderIds: string[]): Promise<CancelResponse>;
|
|
149
|
+
me(): Promise<Me>;
|
|
150
|
+
/** The stream, once connected (API-key credentials only). */
|
|
151
|
+
readonly stream: OrderStream | null;
|
|
152
|
+
ensureStream(): Promise<OrderStream | null>;
|
|
153
|
+
/** Session switch: drop every cache, close the stream. */
|
|
154
|
+
reset(): void;
|
|
155
|
+
destroy(): void;
|
|
156
|
+
}
|
|
157
|
+
export declare function resolveOptions(input: OrderSheetClientOptions): ResolvedClientOptions;
|
|
158
|
+
export declare function createOrderSheetClient(input: OrderSheetClientOptions): OrderSheetClient;
|