skewtape 0.0.1 → 0.1.4
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 +21 -0
- package/README.md +182 -0
- package/dist/cjs/backtest.d.ts +146 -0
- package/dist/cjs/backtest.js +410 -0
- package/dist/cjs/client.d.ts +255 -0
- package/dist/cjs/client.js +447 -0
- package/dist/cjs/download.d.ts +33 -0
- package/dist/cjs/download.js +160 -0
- package/dist/cjs/errors.d.ts +76 -0
- package/dist/cjs/errors.js +159 -0
- package/dist/cjs/exact.d.ts +36 -0
- package/dist/cjs/exact.js +168 -0
- package/dist/cjs/fees.d.ts +68 -0
- package/dist/cjs/fees.js +196 -0
- package/dist/cjs/geoblock.d.ts +48 -0
- package/dist/cjs/geoblock.js +78 -0
- package/dist/cjs/index.d.ts +31 -0
- package/dist/cjs/index.js +98 -0
- package/dist/cjs/kalshi.d.ts +261 -0
- package/dist/cjs/kalshi.js +416 -0
- package/dist/cjs/keccak.d.ts +10 -0
- package/dist/cjs/keccak.js +84 -0
- package/dist/cjs/msgpack.d.ts +15 -0
- package/dist/cjs/msgpack.js +112 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/paginated.d.ts +26 -0
- package/dist/cjs/paginated.js +51 -0
- package/dist/cjs/quote-math.d.ts +146 -0
- package/dist/cjs/quote-math.js +446 -0
- package/dist/cjs/quote.d.ts +108 -0
- package/dist/cjs/quote.js +307 -0
- package/dist/cjs/route-hyperliquid.d.ts +228 -0
- package/dist/cjs/route-hyperliquid.js +387 -0
- package/dist/cjs/route.d.ts +297 -0
- package/dist/cjs/route.js +516 -0
- package/dist/cjs/stream.d.ts +99 -0
- package/dist/cjs/stream.js +529 -0
- package/dist/cjs/version.d.ts +6 -0
- package/dist/cjs/version.js +9 -0
- package/dist/esm/backtest.d.ts +146 -0
- package/dist/esm/backtest.js +403 -0
- package/dist/esm/client.d.ts +255 -0
- package/dist/esm/client.js +441 -0
- package/dist/esm/download.d.ts +33 -0
- package/dist/esm/download.js +155 -0
- package/dist/esm/errors.d.ts +76 -0
- package/dist/esm/errors.js +142 -0
- package/dist/esm/exact.d.ts +36 -0
- package/dist/esm/exact.js +158 -0
- package/dist/esm/fees.d.ts +68 -0
- package/dist/esm/fees.js +185 -0
- package/dist/esm/geoblock.d.ts +48 -0
- package/dist/esm/geoblock.js +74 -0
- package/dist/esm/index.d.ts +31 -0
- package/dist/esm/index.js +30 -0
- package/dist/esm/kalshi.d.ts +261 -0
- package/dist/esm/kalshi.js +404 -0
- package/dist/esm/keccak.d.ts +10 -0
- package/dist/esm/keccak.js +80 -0
- package/dist/esm/msgpack.d.ts +15 -0
- package/dist/esm/msgpack.js +109 -0
- package/dist/esm/paginated.d.ts +26 -0
- package/dist/esm/paginated.js +47 -0
- package/dist/esm/quote-math.d.ts +146 -0
- package/dist/esm/quote-math.js +437 -0
- package/dist/esm/quote.d.ts +108 -0
- package/dist/esm/quote.js +297 -0
- package/dist/esm/route-hyperliquid.d.ts +228 -0
- package/dist/esm/route-hyperliquid.js +376 -0
- package/dist/esm/route.d.ts +297 -0
- package/dist/esm/route.js +507 -0
- package/dist/esm/stream.d.ts +99 -0
- package/dist/esm/stream.js +524 -0
- package/dist/esm/version.d.ts +6 -0
- package/dist/esm/version.js +6 -0
- package/package.json +48 -1
- package/src/backtest.ts +490 -0
- package/src/client.ts +595 -0
- package/src/download.ts +161 -0
- package/src/errors.ts +143 -0
- package/src/exact.ts +159 -0
- package/src/fees.ts +189 -0
- package/src/generated/schema.d.ts +2500 -0
- package/src/geoblock.ts +90 -0
- package/src/index.ts +146 -0
- package/src/kalshi.ts +563 -0
- package/src/keccak.ts +82 -0
- package/src/msgpack.ts +83 -0
- package/src/paginated.ts +51 -0
- package/src/quote-math.ts +555 -0
- package/src/quote.ts +324 -0
- package/src/route-hyperliquid.ts +457 -0
- package/src/route.ts +631 -0
- package/src/stream.ts +539 -0
- package/src/version.ts +6 -0
package/src/quote.ts
ADDED
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* sk.quote(eventId, {outcome, side, size}): the best venue, or split across
|
|
3
|
+
* venues, to take a size of one outcome of a matched event once each venue's
|
|
4
|
+
* taker fee is counted. It places nothing.
|
|
5
|
+
*
|
|
6
|
+
* One call to Skewtape (GET /v1/quote-context/{event_id}, a metered data
|
|
7
|
+
* call) gives the members, their tokens, outcome maps and fee rows; the books
|
|
8
|
+
* are read from each venue, from this machine, with a plain fetch that never
|
|
9
|
+
* carries the Skewtape key. A venue that cannot be reached is left out and
|
|
10
|
+
* named (venue_unreachable); nothing routes around a block. The arithmetic is
|
|
11
|
+
* quote-math.ts, which passes tape's conformance vectors.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import type { components } from "./generated/schema.js";
|
|
15
|
+
import { Kalshi, KalshiError, type KalshiOrderbook } from "./kalshi.js";
|
|
16
|
+
import {
|
|
17
|
+
InvalidQuoteInputError,
|
|
18
|
+
MICRO,
|
|
19
|
+
RuleMismatchError,
|
|
20
|
+
computeQuote,
|
|
21
|
+
type Book,
|
|
22
|
+
type FeeSchedule,
|
|
23
|
+
type QuoteMember,
|
|
24
|
+
type QuoteResult,
|
|
25
|
+
} from "./quote-math.js";
|
|
26
|
+
import { VERSION } from "./version.js";
|
|
27
|
+
|
|
28
|
+
export type QuoteContext = components["schemas"]["QuoteContext"];
|
|
29
|
+
export type QuoteContextMember = components["schemas"]["QuoteContextMember"];
|
|
30
|
+
|
|
31
|
+
export const POLYMARKET_CLOB_URL = "https://clob.polymarket.com";
|
|
32
|
+
/** Kalshi's smallest count where a market allows fractions: 0.01 contracts. */
|
|
33
|
+
export const KALSHI_FRACTION_MICRO = 10_000;
|
|
34
|
+
|
|
35
|
+
/** A book reader could not read the book; message is the reason the quote names (venue_unreachable). */
|
|
36
|
+
export class BookUnavailable extends Error {
|
|
37
|
+
constructor(message: string) {
|
|
38
|
+
super(message);
|
|
39
|
+
this.name = "BookUnavailable";
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** What a book reader is asked for: the member's venue and market, the token of the outcome the quote takes, and the side (buy reads the asks, sell the bids). */
|
|
44
|
+
export interface BookRequest {
|
|
45
|
+
venue: string;
|
|
46
|
+
marketId: string;
|
|
47
|
+
tokenId: string;
|
|
48
|
+
side: "buy" | "sell";
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Reads one book with fetchImpl, a plain fetch that carries no Skewtape key; throws BookUnavailable to leave the member out with a reason. */
|
|
52
|
+
export type BookReader = (request: BookRequest, fetchImpl: typeof fetch) => Promise<Book>;
|
|
53
|
+
|
|
54
|
+
export interface QuoteOptions {
|
|
55
|
+
/** The event's outcome to take, as the members' outcome maps name it ("yes", "no", ...). */
|
|
56
|
+
outcome: string;
|
|
57
|
+
side: "buy" | "sell";
|
|
58
|
+
/** Contracts, up to six decimals: a number or a decimal string. */
|
|
59
|
+
size: number | string;
|
|
60
|
+
/** Quote members that do not resolve alike, with a warning; refused by default. */
|
|
61
|
+
allowRuleMismatch?: boolean;
|
|
62
|
+
/** Readers by venue, used before the defaults (Polymarket's CLOB; Kalshi's public orderbook; kalshi_dflow through DFlow, not set up yet). */
|
|
63
|
+
bookReaders?: Record<string, BookReader>;
|
|
64
|
+
/** The fetch the books are read with; the global one by default. It must not carry the Skewtape key. */
|
|
65
|
+
venueFetch?: typeof fetch;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* A decimal amount in millionths, exact when it has at most six decimals,
|
|
70
|
+
* otherwise rounded up or down. Strings are parsed exactly; a number goes
|
|
71
|
+
* through its shortest decimal form.
|
|
72
|
+
*/
|
|
73
|
+
export function micros(value: number | string, up = false): number {
|
|
74
|
+
const text = typeof value === "number" ? numberText(value) : value.trim();
|
|
75
|
+
const m = /^([+-]?)(\d*)(?:\.(\d*))?(?:[eE]([+-]?\d+))?$/.exec(text);
|
|
76
|
+
if (!m || (m[2] === "" && (m[3] ?? "") === "")) throw new Error(`not a number: ${JSON.stringify(value)}`);
|
|
77
|
+
const negative = m[1] === "-";
|
|
78
|
+
let digits = (m[2] ?? "") + (m[3] ?? "");
|
|
79
|
+
// The decimal point sits after point digits; exp moves it.
|
|
80
|
+
let point = (m[2] ?? "").length + Number(m[4] ?? 0) + 6;
|
|
81
|
+
if (point < 0) {
|
|
82
|
+
digits = "0".repeat(-point) + digits;
|
|
83
|
+
point = 0;
|
|
84
|
+
}
|
|
85
|
+
digits = digits.padEnd(point, "0");
|
|
86
|
+
const whole = BigInt(digits.slice(0, point) || "0");
|
|
87
|
+
const rest = digits.slice(point).replace(/0+$/, "");
|
|
88
|
+
let v = whole;
|
|
89
|
+
// Toward +infinity for up, -infinity otherwise.
|
|
90
|
+
if (rest !== "" && up !== negative) v += 1n;
|
|
91
|
+
const out = Number(negative ? -v : v);
|
|
92
|
+
if (!Number.isSafeInteger(out)) throw new Error(`${JSON.stringify(value)} is past the safe integer range`);
|
|
93
|
+
return out;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function numberText(n: number): string {
|
|
97
|
+
if (!Number.isFinite(n)) throw new Error(`not a finite number: ${n}`);
|
|
98
|
+
return String(n);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** How long a book read may take, the request and its body, in seconds. */
|
|
102
|
+
export const BOOK_TIMEOUT_SECONDS = 10;
|
|
103
|
+
|
|
104
|
+
/** A failed book request or body read, as the reason the quote names: a timeout by its seconds, anything else by its error. */
|
|
105
|
+
function fetchFailure(err: unknown): string {
|
|
106
|
+
if (err instanceof Error && (err.name === "TimeoutError" || err.name === "AbortError")) {
|
|
107
|
+
return `clob.polymarket.com did not answer within ${BOOK_TIMEOUT_SECONDS} s`;
|
|
108
|
+
}
|
|
109
|
+
return err instanceof Error ? `${err.name}: ${err.message}` : String(err);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* The token's book from Polymarket's CLOB (GET /book?token_id=), the asks
|
|
114
|
+
* for a buy and the bids for a sell: levels as {"price", "size"} decimal
|
|
115
|
+
* strings, in dollars and contracts, as the CLOB's market channel sends them,
|
|
116
|
+
* and min_order_size in contracts. An ask's price rounds up and a bid's down
|
|
117
|
+
* past six decimals, and sizes down, so a quote never counts on more than the
|
|
118
|
+
* book holds.
|
|
119
|
+
*
|
|
120
|
+
* TODO(decision): the shape is the CLOB's book as the market channel's book
|
|
121
|
+
* event carries it (tape internal/venue/polymarket/live.go); the REST page
|
|
122
|
+
* could not be read on 14 Nov 2026 (docs.polymarket.com served an expired
|
|
123
|
+
* certificate). A staging recording of one /book reply confirms it, and shows
|
|
124
|
+
* whether the CLOB answers 404 for a token with no book (a closed market),
|
|
125
|
+
* which would then read as an empty book, not unreachable.
|
|
126
|
+
*/
|
|
127
|
+
export const readPolymarketBook: BookReader = async (request, fetchImpl) => {
|
|
128
|
+
const url = `${POLYMARKET_CLOB_URL}/book?token_id=${encodeURIComponent(request.tokenId)}`;
|
|
129
|
+
let response: Response;
|
|
130
|
+
try {
|
|
131
|
+
// A redirect is not followed: a block page reached through one is a
|
|
132
|
+
// status the check below names, as Python's client reports it.
|
|
133
|
+
response = await fetchImpl(url, {
|
|
134
|
+
method: "GET",
|
|
135
|
+
redirect: "manual",
|
|
136
|
+
headers: { "User-Agent": `skewtape-ts/${VERSION}` },
|
|
137
|
+
signal: AbortSignal.timeout(BOOK_TIMEOUT_SECONDS * 1000),
|
|
138
|
+
});
|
|
139
|
+
} catch (err) {
|
|
140
|
+
throw new BookUnavailable(fetchFailure(err));
|
|
141
|
+
}
|
|
142
|
+
if (response.type === "opaqueredirect") throw new BookUnavailable("clob.polymarket.com answered with a redirect");
|
|
143
|
+
if (response.status !== 200) throw new BookUnavailable(`clob.polymarket.com answered HTTP ${response.status}`);
|
|
144
|
+
// The timeout covers the body read too, and is named as the request's is.
|
|
145
|
+
let text: string;
|
|
146
|
+
try {
|
|
147
|
+
text = await response.text();
|
|
148
|
+
} catch (err) {
|
|
149
|
+
throw new BookUnavailable(fetchFailure(err));
|
|
150
|
+
}
|
|
151
|
+
try {
|
|
152
|
+
const body = JSON.parse(text) as { asks?: Array<{ price: string; size: string }>; bids?: Array<{ price: string; size: string }>; min_order_size?: string | null };
|
|
153
|
+
const raw = (request.side === "buy" ? body.asks : body.bids) ?? [];
|
|
154
|
+
const levels = [];
|
|
155
|
+
for (const l of raw) {
|
|
156
|
+
const size = micros(l.size);
|
|
157
|
+
if (size > 0) levels.push({ price_micros: micros(l.price, request.side === "buy"), size_micro: size });
|
|
158
|
+
}
|
|
159
|
+
const min = body.min_order_size;
|
|
160
|
+
return { levels, min_size_micro: min === undefined || min === null || min === "" ? 0 : micros(min, true) };
|
|
161
|
+
} catch (err) {
|
|
162
|
+
throw new BookUnavailable(`clob.polymarket.com sent a book this version cannot read (${err instanceof Error ? err.message : String(err)})`);
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* A kalshi member's book from Kalshi's public orderbook (GET
|
|
168
|
+
* /markets/{ticker}/orderbook, through kalshi.ts with no key, one try).
|
|
169
|
+
* Kalshi lists bids only, yes and no: a no bid at p is a yes ask at 1 - p.
|
|
170
|
+
* So a buy of yes takes the no bids at 1 - p (rounded up past six decimals)
|
|
171
|
+
* and a buy of no the yes bids at 1 - p; a sell of yes takes the yes bids
|
|
172
|
+
* and a sell of no the no bids (rounded down). Counts are rounded down to
|
|
173
|
+
* millionths.
|
|
174
|
+
*
|
|
175
|
+
* The size step: Kalshi counts in hundredths of a contract where a market
|
|
176
|
+
* allows fractions and in whole contracts elsewhere (docs.kalshi.com,
|
|
177
|
+
* fixed-point migration, 5 Nov 2026), and names no field that says which. A
|
|
178
|
+
* book with a fractional count on either side is taken as fractional (0.01);
|
|
179
|
+
* any other as whole contracts, so a quote never counts on a size the market
|
|
180
|
+
* may refuse. Kalshi documents no minimum order beyond that.
|
|
181
|
+
*/
|
|
182
|
+
export const readKalshiBook: BookReader = async (request, fetchImpl) => {
|
|
183
|
+
if (request.tokenId !== "yes" && request.tokenId !== "no") {
|
|
184
|
+
throw new BookUnavailable(`Kalshi's book has a yes and a no side, and the token is ${JSON.stringify(request.tokenId)}`);
|
|
185
|
+
}
|
|
186
|
+
let book: KalshiOrderbook;
|
|
187
|
+
try {
|
|
188
|
+
book = await new Kalshi({ fetch: fetchImpl, maxRetries: 0, timeout: BOOK_TIMEOUT_SECONDS }).orderbook(request.marketId);
|
|
189
|
+
} catch (err) {
|
|
190
|
+
if (err instanceof KalshiError) throw new BookUnavailable(`Kalshi's orderbook for ${request.marketId}: ${err.message}`);
|
|
191
|
+
throw new BookUnavailable(err instanceof Error ? `${err.name}: ${err.message}` : String(err));
|
|
192
|
+
}
|
|
193
|
+
// kalshi.ts leaves out a row that is not [price, count]: here that is a
|
|
194
|
+
// book this version cannot read, not a thinner side, which would quietly
|
|
195
|
+
// move a quote to another venue.
|
|
196
|
+
const sides = book.raw.orderbook_fp as Record<string, unknown> | undefined;
|
|
197
|
+
for (const [side, parsed] of [
|
|
198
|
+
["yes_dollars", book.yes],
|
|
199
|
+
["no_dollars", book.no],
|
|
200
|
+
] as const) {
|
|
201
|
+
const rows = sides?.[side];
|
|
202
|
+
if (rows !== undefined && rows !== null && (!Array.isArray(rows) || rows.length !== parsed.length)) {
|
|
203
|
+
throw new BookUnavailable(`Kalshi sent a book this version cannot read (a row of ${side} is not [price, count])`);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
const [same, other] = request.tokenId === "yes" ? [book.yes, book.no] : [book.no, book.yes];
|
|
207
|
+
const buy = request.side === "buy";
|
|
208
|
+
try {
|
|
209
|
+
const levels = [];
|
|
210
|
+
for (const l of buy ? other : same) {
|
|
211
|
+
const size = micros(l.count_fp);
|
|
212
|
+
// floor and ceil of the price in millionths: 1 - p rounded up is
|
|
213
|
+
// MICRO - floor(p).
|
|
214
|
+
const lo = micros(l.price_dollars);
|
|
215
|
+
if (!(micros(l.price_dollars, true) > 0 && lo < MICRO)) throw new Error(`price ${l.price_dollars} is outside (0, 1)`);
|
|
216
|
+
if (size > 0) levels.push({ price_micros: buy ? MICRO - lo : lo, size_micro: size });
|
|
217
|
+
}
|
|
218
|
+
const fractional = [...book.yes, ...book.no].some((l) => micros(l.count_fp) % MICRO !== 0);
|
|
219
|
+
return { levels, size_step_micro: fractional ? KALSHI_FRACTION_MICRO : MICRO };
|
|
220
|
+
} catch (err) {
|
|
221
|
+
throw new BookUnavailable(`Kalshi sent a book this version cannot read (${err instanceof Error ? err.message : String(err)})`);
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* A kalshi_dflow member trades through DFlow (ADR-0016), whose orderbook
|
|
227
|
+
* needs DFlow access, not set up yet, so the member is left out with that
|
|
228
|
+
* reason, plainly; nothing reads the book another way. (A kalshi member is
|
|
229
|
+
* read from Kalshi's own book: readKalshiBook.)
|
|
230
|
+
*
|
|
231
|
+
* TODO(decision): DFlow's orderbook endpoint, its shape and the key it needs
|
|
232
|
+
* (production access is by request). Until then a caller can pass its own
|
|
233
|
+
* reader in bookReaders.
|
|
234
|
+
*/
|
|
235
|
+
export const readKalshiBookThroughDflow: BookReader = async () => {
|
|
236
|
+
throw new BookUnavailable("kalshi_dflow's book is read through DFlow's orderbook, and DFlow access is not set up yet");
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
export const DEFAULT_BOOK_READERS: Readonly<Record<string, BookReader>> = {
|
|
240
|
+
polymarket: readPolymarketBook,
|
|
241
|
+
kalshi: readKalshiBook,
|
|
242
|
+
kalshi_dflow: readKalshiBookThroughDflow,
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
/** Each outcome's name (its label, or its token id when the market gives two outcomes the same label) to its token id. */
|
|
246
|
+
function names(member: QuoteContextMember): Map<string, string> {
|
|
247
|
+
const labels = member.tokens.map((o) => o.label);
|
|
248
|
+
const repeats = new Set(labels).size !== labels.length;
|
|
249
|
+
return new Map(member.tokens.map((o) => [repeats ? o.token_id : o.label, o.token_id]));
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function tokenFor(member: QuoteContextMember, outcome: string): string | undefined {
|
|
253
|
+
const byName = names(member);
|
|
254
|
+
for (const [name, eventOutcome] of Object.entries(member.outcome_map)) {
|
|
255
|
+
if (eventOutcome === outcome && byName.has(name)) return byName.get(name);
|
|
256
|
+
}
|
|
257
|
+
return undefined;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function feeOf(member: QuoteContextMember): FeeSchedule | null {
|
|
261
|
+
const fee = member.fee;
|
|
262
|
+
if (!fee) return null;
|
|
263
|
+
return { formula: fee.formula, rounding: fee.rounding, taker_params: { ...fee.taker_params }, min_fee_micros: fee.min_fee_micros ?? null };
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** The event's outcome names a quote can take, as the members' outcome maps name them. */
|
|
267
|
+
export function outcomesOf(context: QuoteContext): string[] {
|
|
268
|
+
const seen: string[] = [];
|
|
269
|
+
for (const m of context.members) for (const v of Object.values(m.outcome_map)) if (!seen.includes(v)) seen.push(v);
|
|
270
|
+
return seen;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* A quote on a context already read: each member's book read with its
|
|
275
|
+
* venue's reader (bookReaders first, then the defaults), then computeQuote.
|
|
276
|
+
* A member whose fee_status is blocked is left out without reading anything,
|
|
277
|
+
* and a rule mismatch is refused before any book is read.
|
|
278
|
+
*/
|
|
279
|
+
export async function quoteFromContext(context: QuoteContext, options: QuoteOptions): Promise<QuoteResult> {
|
|
280
|
+
const { outcome, side } = options;
|
|
281
|
+
if (side !== "buy" && side !== "sell") throw new InvalidQuoteInputError(`side ${JSON.stringify(side)} is neither buy nor sell`);
|
|
282
|
+
let size: number;
|
|
283
|
+
try {
|
|
284
|
+
size = micros(options.size);
|
|
285
|
+
if (micros(options.size, true) !== size) throw new Error(`size ${options.size} has more than six decimals`);
|
|
286
|
+
} catch (err) {
|
|
287
|
+
throw new InvalidQuoteInputError(err instanceof Error ? err.message : String(err));
|
|
288
|
+
}
|
|
289
|
+
const known = outcomesOf(context);
|
|
290
|
+
if (!known.includes(outcome)) throw new InvalidQuoteInputError(`outcome ${JSON.stringify(outcome)} is not one of the event's: ${known.join(", ") || "none"}`);
|
|
291
|
+
// Before any book is read (ARCHITECTURE 13.1, step 2).
|
|
292
|
+
if (!context.resolution_equal && !options.allowRuleMismatch) throw new RuleMismatchError(context.rule_note);
|
|
293
|
+
const readers = { ...DEFAULT_BOOK_READERS, ...(options.bookReaders ?? {}) };
|
|
294
|
+
const fetchImpl = options.venueFetch ?? fetch;
|
|
295
|
+
const members = await Promise.all(
|
|
296
|
+
context.members.map(async (m): Promise<QuoteMember> => {
|
|
297
|
+
const member: QuoteMember = { venue: m.venue, market_id: m.market_id, token_id: "", fee_status: m.fee_status, fee_per: m.fee_per, fee: feeOf(m), book: null };
|
|
298
|
+
if (m.fee_status === "blocked") return { ...member, blocked: true };
|
|
299
|
+
const token = tokenFor(m, outcome);
|
|
300
|
+
if (token === undefined) return { ...member, unreachable: `no token of the market maps to ${JSON.stringify(outcome)}` };
|
|
301
|
+
member.token_id = token;
|
|
302
|
+
const reader = readers[m.venue];
|
|
303
|
+
if (!reader) return { ...member, unreachable: `sk.quote has no book reader for ${m.venue}; pass one in bookReaders` };
|
|
304
|
+
try {
|
|
305
|
+
member.book = await reader({ venue: m.venue, marketId: m.market_id, tokenId: token, side }, fetchImpl);
|
|
306
|
+
} catch (err) {
|
|
307
|
+
if (!(err instanceof BookUnavailable)) throw err;
|
|
308
|
+
member.unreachable = err.message || "the book could not be read";
|
|
309
|
+
}
|
|
310
|
+
return member;
|
|
311
|
+
}),
|
|
312
|
+
);
|
|
313
|
+
return computeQuote({
|
|
314
|
+
event_id: context.event_id,
|
|
315
|
+
side,
|
|
316
|
+
size_micro: size,
|
|
317
|
+
resolution_equal: context.resolution_equal,
|
|
318
|
+
rule_note: context.rule_note,
|
|
319
|
+
allow_rule_mismatch: options.allowRuleMismatch ?? false,
|
|
320
|
+
members,
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
export { MICRO };
|