skewtape 0.1.5 → 0.1.7
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/README.md +4 -13
- package/dist/cjs/client.d.ts +37 -6
- package/dist/cjs/client.js +42 -13
- package/dist/cjs/fees.d.ts +4 -2
- package/dist/cjs/fees.js +48 -8
- package/dist/cjs/index.d.ts +2 -2
- package/dist/cjs/index.js +2 -5
- package/dist/cjs/kalshi.d.ts +3 -3
- package/dist/cjs/kalshi.js +4 -4
- package/dist/cjs/paginated.d.ts +1 -1
- package/dist/cjs/paginated.js +1 -1
- package/dist/cjs/quote-math.js +4 -1
- package/dist/cjs/quote.d.ts +1 -12
- package/dist/cjs/quote.js +1 -16
- package/dist/cjs/route.d.ts +6 -65
- package/dist/cjs/route.js +3 -120
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/esm/client.d.ts +37 -6
- package/dist/esm/client.js +42 -13
- package/dist/esm/fees.d.ts +4 -2
- package/dist/esm/fees.js +48 -8
- package/dist/esm/index.d.ts +2 -2
- package/dist/esm/index.js +2 -2
- package/dist/esm/kalshi.d.ts +3 -3
- package/dist/esm/kalshi.js +4 -4
- package/dist/esm/paginated.d.ts +1 -1
- package/dist/esm/paginated.js +1 -1
- package/dist/esm/quote-math.js +4 -1
- package/dist/esm/quote.d.ts +1 -12
- package/dist/esm/quote.js +0 -14
- package/dist/esm/route.d.ts +6 -65
- package/dist/esm/route.js +2 -117
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/package.json +1 -1
- package/src/client.ts +72 -21
- package/src/fees.ts +47 -7
- package/src/generated/schema.d.ts +388 -34
- package/src/index.ts +0 -4
- package/src/kalshi.ts +4 -4
- package/src/paginated.ts +1 -1
- package/src/quote-math.ts +3 -1
- package/src/quote.ts +1 -16
- package/src/route.ts +7 -147
- package/src/version.ts +1 -1
package/src/paginated.ts
CHANGED
|
@@ -20,7 +20,7 @@ export class Paginated<T> implements AsyncIterable<T> {
|
|
|
20
20
|
private readonly keep?: (item: T) => boolean,
|
|
21
21
|
) {}
|
|
22
22
|
|
|
23
|
-
/** The pages as the API returns them; a client-side filter (
|
|
23
|
+
/** The pages as the API returns them; a client-side filter (sk.pairs: the matched events of two active members) applies to each. */
|
|
24
24
|
async *pages(): AsyncGenerator<Page<T>, void, undefined> {
|
|
25
25
|
let cursor: string | undefined;
|
|
26
26
|
for (;;) {
|
package/src/quote-math.ts
CHANGED
|
@@ -514,7 +514,9 @@ export function computeQuote(q: QuoteInput): QuoteResult {
|
|
|
514
514
|
res.venues.push({ depth_micro: safe(m.depth, "depth"), ...legOut(leg, q.side) });
|
|
515
515
|
if (m.fee === null && feeKnown) {
|
|
516
516
|
res.warnings.push(`fee_unknown: ${m.m.venue} (${m.m.fee_status}): its fee is unknown, so it is left out of the plan; venues shows it alone, before fees`);
|
|
517
|
-
|
|
517
|
+
// The partial line names the venues left out only when one of them
|
|
518
|
+
// could have filled some of the size.
|
|
519
|
+
if (alloc > 0n) leftOut = true;
|
|
518
520
|
continue;
|
|
519
521
|
}
|
|
520
522
|
if (m.fee === null) res.warnings.push(`fee_unknown: ${m.m.venue} (${m.m.fee_status}): its fee is not counted`);
|
package/src/quote.ts
CHANGED
|
@@ -59,7 +59,7 @@ export interface QuoteOptions {
|
|
|
59
59
|
size: number | string;
|
|
60
60
|
/** Quote members that do not resolve alike, with a warning; refused by default. */
|
|
61
61
|
allowRuleMismatch?: boolean;
|
|
62
|
-
/** Readers by venue, used before the defaults (Polymarket's CLOB; Kalshi's public orderbook
|
|
62
|
+
/** Readers by venue, used before the defaults (Polymarket's CLOB; Kalshi's public orderbook). */
|
|
63
63
|
bookReaders?: Record<string, BookReader>;
|
|
64
64
|
/** The fetch the books are read with; the global one by default. It must not carry the Skewtape key. */
|
|
65
65
|
venueFetch?: typeof fetch;
|
|
@@ -222,24 +222,9 @@ export const readKalshiBook: BookReader = async (request, fetchImpl) => {
|
|
|
222
222
|
}
|
|
223
223
|
};
|
|
224
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
225
|
export const DEFAULT_BOOK_READERS: Readonly<Record<string, BookReader>> = {
|
|
240
226
|
polymarket: readPolymarketBook,
|
|
241
227
|
kalshi: readKalshiBook,
|
|
242
|
-
kalshi_dflow: readKalshiBookThroughDflow,
|
|
243
228
|
};
|
|
244
229
|
|
|
245
230
|
/** Each outcome's name (its label, or its token id when the market gives two outcomes the same label) to its token id. */
|
package/src/route.ts
CHANGED
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
*
|
|
5
5
|
* route() builds and checks an order, and stops there: it never signs, never
|
|
6
6
|
* sends, never takes a private key, and needs no Skewtape key. Every code and
|
|
7
|
-
* fee it carries is the developer's own: the Polymarket builder code
|
|
8
|
-
*
|
|
7
|
+
* fee it carries is the developer's own: the Polymarket builder code and the
|
|
8
|
+
* Hyperliquid builder fee. Omitting them sends none, and no Skewtape value
|
|
9
9
|
* exists in this package (a test scans it). The developer is responsible for
|
|
10
10
|
* their users: who may trade where, and under which terms (sk.geoblock helps
|
|
11
11
|
* with Polymarket's).
|
|
@@ -373,10 +373,6 @@ export const ROUTE_FIELDS = {
|
|
|
373
373
|
"venue", "exchange", "tokenId", "side", "price", "tickSize", "maker", "size", "amount", "orderType",
|
|
374
374
|
"signatureType", "signer", "builder", "metadata", "expiration", "salt", "timestampMs",
|
|
375
375
|
],
|
|
376
|
-
kalshi_dflow: [
|
|
377
|
-
"venue", "inputMint", "outputMint", "amount", "userPublicKey", "slippageBps", "predictionMarketSlippageBps",
|
|
378
|
-
"platformFeeScale", "platformFeeBps", "platformFeeMode", "feeAccount", "baseUrl",
|
|
379
|
-
],
|
|
380
376
|
hyperliquid: HYPERLIQUID_FIELDS,
|
|
381
377
|
} as const;
|
|
382
378
|
|
|
@@ -477,155 +473,19 @@ export function polymarketOrder(input: PolymarketRoute): PolymarketOrder {
|
|
|
477
473
|
);
|
|
478
474
|
}
|
|
479
475
|
|
|
480
|
-
// --- DFlow --------------------------------------------------------------------
|
|
481
|
-
|
|
482
|
-
/**
|
|
483
|
-
* The Trading API that returns the unsigned transaction: GET /order on
|
|
484
|
-
* https://quote-api.dflow.net (pond.dflow.net/get-started/endpoints.md), with
|
|
485
|
-
* the developer's own key in the x-api-key header ("All calls to the
|
|
486
|
-
* production endpoints must use an API key", pond.dflow.net/get-started/api-key.md).
|
|
487
|
-
*/
|
|
488
|
-
export const DFLOW_TRADING_API = "https://quote-api.dflow.net";
|
|
489
|
-
export const DFLOW_API_KEY_HEADER = "x-api-key";
|
|
490
|
-
/**
|
|
491
|
-
* platformFeeScale: "Optional platform fee scale for World prediction market
|
|
492
|
-
* swaps and async prediction market swaps, with 3 decimals ... Must be >= 0 and
|
|
493
|
-
* < 1000. For example, a value of 50 means 0.050." platformFeeBps: a fixed fee
|
|
494
|
-
* in basis points, for spot trades (pond.dflow.net/spot/trading/platform-fees.md).
|
|
495
|
-
*/
|
|
496
|
-
export const DFLOW_FEE_SCALE_MAX = 999;
|
|
497
|
-
export const DFLOW_FEE_BPS_MAX = 10_000;
|
|
498
|
-
const DFLOW_FEE_MODES = ["outputMint", "inputMint"] as const;
|
|
499
|
-
const BASE58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
500
|
-
|
|
501
|
-
function solanaAddress(name: string, value: unknown): string {
|
|
502
|
-
if (typeof value !== "string" || value.length < 32 || value.length > 44 || [...value].some((c) => !BASE58.includes(c))) {
|
|
503
|
-
throw new RouteError(`${name}: ${JSON.stringify(value)} is not a base58 Solana address`);
|
|
504
|
-
}
|
|
505
|
-
let n = 0n;
|
|
506
|
-
for (const c of value) n = n * 58n + BigInt(BASE58.indexOf(c));
|
|
507
|
-
let bytes = n === 0n ? 0 : Math.ceil(n.toString(16).length / 2);
|
|
508
|
-
bytes += value.length - value.replace(/^1+/, "").length;
|
|
509
|
-
if (bytes !== 32) throw new RouteError(`${name}: ${JSON.stringify(value)} does not decode to 32 bytes`);
|
|
510
|
-
return value;
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
/**
|
|
514
|
-
* The request that returns an unsigned DFlow transaction: GET `url` with
|
|
515
|
-
* `params` and your own DFlow key in the x-api-key header. The response's
|
|
516
|
-
* `transaction` (base64) is for your user to sign and submit. Nothing was
|
|
517
|
-
* sent: route() never calls DFlow.
|
|
518
|
-
*/
|
|
519
|
-
export class DFlowOrderRequest {
|
|
520
|
-
readonly venue = "kalshi_dflow" as const;
|
|
521
|
-
readonly apiKeyHeader = DFLOW_API_KEY_HEADER;
|
|
522
|
-
|
|
523
|
-
constructor(
|
|
524
|
-
readonly url: string,
|
|
525
|
-
readonly params: Readonly<Record<string, string>>,
|
|
526
|
-
) {}
|
|
527
|
-
|
|
528
|
-
get fullUrl(): string {
|
|
529
|
-
return `${this.url}?${new URLSearchParams(this.params).toString()}`;
|
|
530
|
-
}
|
|
531
|
-
}
|
|
532
|
-
|
|
533
|
-
/**
|
|
534
|
-
* A DFlow order to build, for tokenized Kalshi outcome tokens or any DFlow
|
|
535
|
-
* trade, carrying the developer's own fee (ARCHITECTURE 13.3). amount: the
|
|
536
|
-
* input amount in the input mint's base units. userPublicKey: your user's
|
|
537
|
-
* wallet, so the response carries the transaction to sign. platformFeeScale:
|
|
538
|
-
* your fee for a prediction market swap, 0 to 999 in thousandths (DFlow's
|
|
539
|
-
* platformFeeScale; its docs give no formula). Or platformFeeBps: a fixed fee
|
|
540
|
-
* in basis points, DFlow's spot fee. Either needs feeAccount, your token
|
|
541
|
-
* account for the fee mint (the stablecoin for a prediction market swap),
|
|
542
|
-
* which must exist before the trade. With no fee, none is sent.
|
|
543
|
-
*/
|
|
544
|
-
export interface DFlowRoute {
|
|
545
|
-
venue: "kalshi_dflow";
|
|
546
|
-
inputMint: string;
|
|
547
|
-
outputMint: string;
|
|
548
|
-
amount: number | bigint;
|
|
549
|
-
userPublicKey: string;
|
|
550
|
-
slippageBps?: number | "auto";
|
|
551
|
-
predictionMarketSlippageBps?: number;
|
|
552
|
-
platformFeeScale?: number;
|
|
553
|
-
platformFeeBps?: number;
|
|
554
|
-
platformFeeMode?: "outputMint" | "inputMint";
|
|
555
|
-
feeAccount?: string;
|
|
556
|
-
baseUrl?: string;
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
function intIn(name: string, value: unknown, max: number): number {
|
|
560
|
-
if (typeof value !== "number" || !Number.isInteger(value) || value < 0 || value > max) {
|
|
561
|
-
throw new RouteError(`${name}: an integer from 0 to ${max}`);
|
|
562
|
-
}
|
|
563
|
-
return value;
|
|
564
|
-
}
|
|
565
|
-
|
|
566
|
-
export function dflowOrder(input: DFlowRoute): DFlowOrderRequest {
|
|
567
|
-
onlyFields("kalshi_dflow", input);
|
|
568
|
-
const params: Record<string, string> = {
|
|
569
|
-
inputMint: solanaAddress("inputMint", input.inputMint),
|
|
570
|
-
outputMint: solanaAddress("outputMint", input.outputMint),
|
|
571
|
-
};
|
|
572
|
-
const amount = input.amount;
|
|
573
|
-
if (!(typeof amount === "bigint" ? amount > 0n : Number.isSafeInteger(amount) && amount > 0)) {
|
|
574
|
-
throw new RouteError("amount: a positive integer, in the input mint's base units");
|
|
575
|
-
}
|
|
576
|
-
params.amount = amount.toString();
|
|
577
|
-
params.userPublicKey = solanaAddress("userPublicKey", input.userPublicKey);
|
|
578
|
-
if (input.slippageBps !== undefined) {
|
|
579
|
-
if (input.slippageBps !== "auto") intIn('slippageBps (or "auto")', input.slippageBps, 65535);
|
|
580
|
-
params.slippageBps = String(input.slippageBps);
|
|
581
|
-
}
|
|
582
|
-
if (input.predictionMarketSlippageBps !== undefined) {
|
|
583
|
-
params.predictionMarketSlippageBps = String(intIn("predictionMarketSlippageBps", input.predictionMarketSlippageBps, 65535));
|
|
584
|
-
}
|
|
585
|
-
if (input.platformFeeScale !== undefined && input.platformFeeBps !== undefined) {
|
|
586
|
-
throw new RouteError("platformFeeScale (a prediction market swap) or platformFeeBps (spot), not both");
|
|
587
|
-
}
|
|
588
|
-
let fee = 0;
|
|
589
|
-
if (input.platformFeeScale !== undefined) {
|
|
590
|
-
fee = intIn("platformFeeScale (in thousandths, 50 is 0.050)", input.platformFeeScale, DFLOW_FEE_SCALE_MAX);
|
|
591
|
-
if (fee) params.platformFeeScale = String(fee);
|
|
592
|
-
}
|
|
593
|
-
if (input.platformFeeBps !== undefined) {
|
|
594
|
-
fee = intIn("platformFeeBps", input.platformFeeBps, DFLOW_FEE_BPS_MAX);
|
|
595
|
-
if (fee) params.platformFeeBps = String(fee);
|
|
596
|
-
}
|
|
597
|
-
if (fee && input.feeAccount === undefined) {
|
|
598
|
-
throw new RouteError("feeAccount: your token account for the fee mint; DFlow requires it with a fee");
|
|
599
|
-
}
|
|
600
|
-
if (!fee && input.feeAccount !== undefined) {
|
|
601
|
-
throw new RouteError("feeAccount without a fee: DFlow collects nothing, so send neither");
|
|
602
|
-
}
|
|
603
|
-
if (input.feeAccount !== undefined) params.feeAccount = solanaAddress("feeAccount", input.feeAccount);
|
|
604
|
-
if (input.platformFeeMode !== undefined) {
|
|
605
|
-
if (!DFLOW_FEE_MODES.includes(input.platformFeeMode)) throw new RouteError(`platformFeeMode: one of ${DFLOW_FEE_MODES.join(", ")}`);
|
|
606
|
-
params.platformFeeMode = input.platformFeeMode;
|
|
607
|
-
}
|
|
608
|
-
const base = input.baseUrl ?? DFLOW_TRADING_API;
|
|
609
|
-
if (typeof base !== "string" || !base.startsWith("https://")) throw new RouteError("baseUrl: an https URL of DFlow's Trading API");
|
|
610
|
-
return new DFlowOrderRequest(`${base.replace(/\/+$/, "")}/order`, params);
|
|
611
|
-
}
|
|
612
|
-
|
|
613
476
|
// --- route --------------------------------------------------------------------
|
|
614
477
|
|
|
615
|
-
export type RouteInput = PolymarketRoute |
|
|
478
|
+
export type RouteInput = PolymarketRoute | HyperliquidRoute;
|
|
616
479
|
|
|
617
480
|
/**
|
|
618
|
-
* An unsigned order for `venue` ("polymarket"
|
|
619
|
-
*
|
|
620
|
-
* fee. It never signs, never sends, and takes no private key.
|
|
481
|
+
* An unsigned order for `venue` ("polymarket" or "hyperliquid", a HIP-4
|
|
482
|
+
* outcome), with the developer's own builder code or fee. It never signs, never sends, and takes no private key.
|
|
621
483
|
*/
|
|
622
484
|
export function route(order: PolymarketRoute): PolymarketOrder;
|
|
623
|
-
export function route(order: DFlowRoute): DFlowOrderRequest;
|
|
624
485
|
export function route(order: HyperliquidRoute): HyperliquidOrder;
|
|
625
|
-
export function route(order: RouteInput): PolymarketOrder |
|
|
626
|
-
export function route(order: RouteInput): PolymarketOrder |
|
|
486
|
+
export function route(order: RouteInput): PolymarketOrder | HyperliquidOrder;
|
|
487
|
+
export function route(order: RouteInput): PolymarketOrder | HyperliquidOrder {
|
|
627
488
|
if (order.venue === "polymarket") return polymarketOrder(order);
|
|
628
|
-
if (order.venue === "kalshi_dflow") return dflowOrder(order);
|
|
629
489
|
if (order.venue === "hyperliquid") return hyperliquidOrder(order);
|
|
630
490
|
throw new RouteError(`venue: one of ${Object.keys(ROUTE_FIELDS).join(", ")}`);
|
|
631
491
|
}
|
package/src/version.ts
CHANGED