skewtape 0.0.1 → 0.1.5
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
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Errors, one class per thing the caller can act on, built from the API's
|
|
3
|
+
* error envelope: {"error": {"code", "message", "docs_url", "request_id",
|
|
4
|
+
* "plan", "resets_at", "upgrade_url", "block_reason_code"}}.
|
|
5
|
+
*/
|
|
6
|
+
export interface ErrorFields {
|
|
7
|
+
status?: number | undefined;
|
|
8
|
+
code?: string | undefined;
|
|
9
|
+
docsUrl?: string | undefined;
|
|
10
|
+
requestId?: string | undefined;
|
|
11
|
+
plan?: string | undefined;
|
|
12
|
+
resetsAt?: string | undefined;
|
|
13
|
+
upgradeUrl?: string | undefined;
|
|
14
|
+
blockReasonCode?: string | undefined;
|
|
15
|
+
retryAfter?: number | undefined;
|
|
16
|
+
}
|
|
17
|
+
/** An error from the API or from this client. message is the API's own sentence. */
|
|
18
|
+
export declare class SkewtapeError extends Error {
|
|
19
|
+
readonly status: number | undefined;
|
|
20
|
+
readonly code: string | undefined;
|
|
21
|
+
readonly docsUrl: string | undefined;
|
|
22
|
+
readonly requestId: string | undefined;
|
|
23
|
+
readonly plan: string | undefined;
|
|
24
|
+
readonly resetsAt: string | undefined;
|
|
25
|
+
readonly upgradeUrl: string | undefined;
|
|
26
|
+
readonly blockReasonCode: string | undefined;
|
|
27
|
+
/** The server's Retry-After in seconds, when it sent one. */
|
|
28
|
+
readonly retryAfter: number | undefined;
|
|
29
|
+
constructor(message: string, fields?: ErrorFields);
|
|
30
|
+
toString(): string;
|
|
31
|
+
}
|
|
32
|
+
/** An order route() refuses to build (route.ts, route-hyperliquid.ts). Nothing was signed or sent. */
|
|
33
|
+
export declare class RouteError extends SkewtapeError {
|
|
34
|
+
constructor(message: string);
|
|
35
|
+
}
|
|
36
|
+
/** The client cannot be built: no API key, or a bad option. */
|
|
37
|
+
export declare class ConfigurationError extends SkewtapeError {
|
|
38
|
+
}
|
|
39
|
+
/** 400 invalid_request: a parameter the API refuses. */
|
|
40
|
+
export declare class InvalidRequestError extends SkewtapeError {
|
|
41
|
+
}
|
|
42
|
+
/** 401 unauthorized: no valid API key; or 429 lookup_rate_limited: too many invalid keys from this address, never retried, since the fix is the key. */
|
|
43
|
+
export declare class AuthenticationError extends SkewtapeError {
|
|
44
|
+
}
|
|
45
|
+
/** 403 forbidden_plan: the request needs another plan; .plan names it. */
|
|
46
|
+
export declare class ForbiddenPlanError extends SkewtapeError {
|
|
47
|
+
}
|
|
48
|
+
/** 403 blocked: the data policy withholds the venue's data; .blockReasonCode says why. */
|
|
49
|
+
export declare class BlockedError extends SkewtapeError {
|
|
50
|
+
}
|
|
51
|
+
/** 404 not_found. */
|
|
52
|
+
export declare class NotFoundError extends SkewtapeError {
|
|
53
|
+
}
|
|
54
|
+
/** 429 rate_limited after the retries. */
|
|
55
|
+
export declare class RateLimitError extends SkewtapeError {
|
|
56
|
+
}
|
|
57
|
+
/** 429 quota_exceeded: the plan's monthly quota; .resetsAt says when. */
|
|
58
|
+
export declare class QuotaExceededError extends SkewtapeError {
|
|
59
|
+
}
|
|
60
|
+
/** 5xx: the API failed; retry later, quoting .requestId to support. */
|
|
61
|
+
export declare class ServerError extends SkewtapeError {
|
|
62
|
+
}
|
|
63
|
+
/** A downloaded file's sha256 is not the one expected. */
|
|
64
|
+
export declare class ChecksumError extends SkewtapeError {
|
|
65
|
+
readonly path: string;
|
|
66
|
+
readonly expected: string;
|
|
67
|
+
readonly actual: string;
|
|
68
|
+
constructor(path: string, expected: string, actual: string);
|
|
69
|
+
}
|
|
70
|
+
/** The Retry-After header as seconds, when it is a number. */
|
|
71
|
+
export declare function retryAfterSeconds(headers: Headers): number | undefined;
|
|
72
|
+
/**
|
|
73
|
+
* The error for a non-2xx response: the envelope's code chooses the class, the
|
|
74
|
+
* status decides when the body is not an envelope.
|
|
75
|
+
*/
|
|
76
|
+
export declare function errorFromResponse(status: number, headers: Headers, body: unknown): SkewtapeError;
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Errors, one class per thing the caller can act on, built from the API's
|
|
4
|
+
* error envelope: {"error": {"code", "message", "docs_url", "request_id",
|
|
5
|
+
* "plan", "resets_at", "upgrade_url", "block_reason_code"}}.
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.ChecksumError = exports.ServerError = exports.QuotaExceededError = exports.RateLimitError = exports.NotFoundError = exports.BlockedError = exports.ForbiddenPlanError = exports.AuthenticationError = exports.InvalidRequestError = exports.ConfigurationError = exports.RouteError = exports.SkewtapeError = void 0;
|
|
9
|
+
exports.retryAfterSeconds = retryAfterSeconds;
|
|
10
|
+
exports.errorFromResponse = errorFromResponse;
|
|
11
|
+
/** An error from the API or from this client. message is the API's own sentence. */
|
|
12
|
+
class SkewtapeError extends Error {
|
|
13
|
+
status;
|
|
14
|
+
code;
|
|
15
|
+
docsUrl;
|
|
16
|
+
requestId;
|
|
17
|
+
plan;
|
|
18
|
+
resetsAt;
|
|
19
|
+
upgradeUrl;
|
|
20
|
+
blockReasonCode;
|
|
21
|
+
/** The server's Retry-After in seconds, when it sent one. */
|
|
22
|
+
retryAfter;
|
|
23
|
+
constructor(message, fields = {}) {
|
|
24
|
+
super(message);
|
|
25
|
+
this.name = new.target.name;
|
|
26
|
+
this.status = fields.status;
|
|
27
|
+
this.code = fields.code;
|
|
28
|
+
this.docsUrl = fields.docsUrl;
|
|
29
|
+
this.requestId = fields.requestId;
|
|
30
|
+
this.plan = fields.plan;
|
|
31
|
+
this.resetsAt = fields.resetsAt;
|
|
32
|
+
this.upgradeUrl = fields.upgradeUrl;
|
|
33
|
+
this.blockReasonCode = fields.blockReasonCode;
|
|
34
|
+
this.retryAfter = fields.retryAfter;
|
|
35
|
+
}
|
|
36
|
+
toString() {
|
|
37
|
+
const parts = [this.message];
|
|
38
|
+
if (this.code)
|
|
39
|
+
parts.push(`[${this.code}]`);
|
|
40
|
+
if (this.requestId)
|
|
41
|
+
parts.push(`(request ${this.requestId})`);
|
|
42
|
+
return parts.join(" ");
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
exports.SkewtapeError = SkewtapeError;
|
|
46
|
+
/** An order route() refuses to build (route.ts, route-hyperliquid.ts). Nothing was signed or sent. */
|
|
47
|
+
class RouteError extends SkewtapeError {
|
|
48
|
+
constructor(message) {
|
|
49
|
+
super(message);
|
|
50
|
+
this.name = "RouteError";
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
exports.RouteError = RouteError;
|
|
54
|
+
/** The client cannot be built: no API key, or a bad option. */
|
|
55
|
+
class ConfigurationError extends SkewtapeError {
|
|
56
|
+
}
|
|
57
|
+
exports.ConfigurationError = ConfigurationError;
|
|
58
|
+
/** 400 invalid_request: a parameter the API refuses. */
|
|
59
|
+
class InvalidRequestError extends SkewtapeError {
|
|
60
|
+
}
|
|
61
|
+
exports.InvalidRequestError = InvalidRequestError;
|
|
62
|
+
/** 401 unauthorized: no valid API key; or 429 lookup_rate_limited: too many invalid keys from this address, never retried, since the fix is the key. */
|
|
63
|
+
class AuthenticationError extends SkewtapeError {
|
|
64
|
+
}
|
|
65
|
+
exports.AuthenticationError = AuthenticationError;
|
|
66
|
+
/** 403 forbidden_plan: the request needs another plan; .plan names it. */
|
|
67
|
+
class ForbiddenPlanError extends SkewtapeError {
|
|
68
|
+
}
|
|
69
|
+
exports.ForbiddenPlanError = ForbiddenPlanError;
|
|
70
|
+
/** 403 blocked: the data policy withholds the venue's data; .blockReasonCode says why. */
|
|
71
|
+
class BlockedError extends SkewtapeError {
|
|
72
|
+
}
|
|
73
|
+
exports.BlockedError = BlockedError;
|
|
74
|
+
/** 404 not_found. */
|
|
75
|
+
class NotFoundError extends SkewtapeError {
|
|
76
|
+
}
|
|
77
|
+
exports.NotFoundError = NotFoundError;
|
|
78
|
+
/** 429 rate_limited after the retries. */
|
|
79
|
+
class RateLimitError extends SkewtapeError {
|
|
80
|
+
}
|
|
81
|
+
exports.RateLimitError = RateLimitError;
|
|
82
|
+
/** 429 quota_exceeded: the plan's monthly quota; .resetsAt says when. */
|
|
83
|
+
class QuotaExceededError extends SkewtapeError {
|
|
84
|
+
}
|
|
85
|
+
exports.QuotaExceededError = QuotaExceededError;
|
|
86
|
+
/** 5xx: the API failed; retry later, quoting .requestId to support. */
|
|
87
|
+
class ServerError extends SkewtapeError {
|
|
88
|
+
}
|
|
89
|
+
exports.ServerError = ServerError;
|
|
90
|
+
/** A downloaded file's sha256 is not the one expected. */
|
|
91
|
+
class ChecksumError extends SkewtapeError {
|
|
92
|
+
path;
|
|
93
|
+
expected;
|
|
94
|
+
actual;
|
|
95
|
+
constructor(path, expected, actual) {
|
|
96
|
+
super(`${path}: sha256 ${actual} is not the expected ${expected}`);
|
|
97
|
+
this.path = path;
|
|
98
|
+
this.expected = expected;
|
|
99
|
+
this.actual = actual;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
exports.ChecksumError = ChecksumError;
|
|
103
|
+
const byCode = {
|
|
104
|
+
invalid_request: InvalidRequestError,
|
|
105
|
+
unauthorized: AuthenticationError,
|
|
106
|
+
lookup_rate_limited: AuthenticationError,
|
|
107
|
+
forbidden_plan: ForbiddenPlanError,
|
|
108
|
+
blocked: BlockedError,
|
|
109
|
+
not_found: NotFoundError,
|
|
110
|
+
rate_limited: RateLimitError,
|
|
111
|
+
quota_exceeded: QuotaExceededError,
|
|
112
|
+
};
|
|
113
|
+
/** The Retry-After header as seconds, when it is a number. */
|
|
114
|
+
function retryAfterSeconds(headers) {
|
|
115
|
+
const raw = headers.get("retry-after");
|
|
116
|
+
if (raw === null)
|
|
117
|
+
return undefined;
|
|
118
|
+
const n = Number(raw);
|
|
119
|
+
return Number.isFinite(n) ? Math.max(n, 0) : undefined;
|
|
120
|
+
}
|
|
121
|
+
function str(v) {
|
|
122
|
+
return typeof v === "string" ? v : undefined;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* The error for a non-2xx response: the envelope's code chooses the class, the
|
|
126
|
+
* status decides when the body is not an envelope.
|
|
127
|
+
*/
|
|
128
|
+
function errorFromResponse(status, headers, body) {
|
|
129
|
+
const envelope = body !== null && typeof body === "object" && "error" in body ? body.error : undefined;
|
|
130
|
+
const e = envelope !== null && typeof envelope === "object" ? envelope : {};
|
|
131
|
+
const code = str(e.code);
|
|
132
|
+
const message = str(e.message) ?? `HTTP ${status}${code ? ` ${code}` : ""}`;
|
|
133
|
+
let cls = code ? byCode[code] : undefined;
|
|
134
|
+
if (!cls) {
|
|
135
|
+
if (status === 400)
|
|
136
|
+
cls = InvalidRequestError;
|
|
137
|
+
else if (status === 401)
|
|
138
|
+
cls = AuthenticationError;
|
|
139
|
+
else if (status === 404)
|
|
140
|
+
cls = NotFoundError;
|
|
141
|
+
else if (status === 429)
|
|
142
|
+
cls = RateLimitError;
|
|
143
|
+
else if (status >= 500)
|
|
144
|
+
cls = ServerError;
|
|
145
|
+
else
|
|
146
|
+
cls = SkewtapeError;
|
|
147
|
+
}
|
|
148
|
+
return new cls(message, {
|
|
149
|
+
status,
|
|
150
|
+
code,
|
|
151
|
+
docsUrl: str(e.docs_url),
|
|
152
|
+
requestId: str(e.request_id) ?? headers.get("x-request-id") ?? undefined,
|
|
153
|
+
plan: str(e.plan),
|
|
154
|
+
resetsAt: str(e.resets_at),
|
|
155
|
+
upgradeUrl: str(e.upgrade_url),
|
|
156
|
+
blockReasonCode: str(e.block_reason_code),
|
|
157
|
+
retryAfter: retryAfterSeconds(headers),
|
|
158
|
+
});
|
|
159
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Exact numbers for fees and the backtest: fractions of BigInts, times in
|
|
3
|
+
* microseconds since the epoch, and the formats the Python SDK prints, so
|
|
4
|
+
* both SDKs give the same strings for the same values.
|
|
5
|
+
*/
|
|
6
|
+
/** An exact fraction, always reduced, with a positive denominator. */
|
|
7
|
+
export declare class Rat {
|
|
8
|
+
readonly n: bigint;
|
|
9
|
+
readonly d: bigint;
|
|
10
|
+
constructor(n: bigint, d?: bigint);
|
|
11
|
+
static of(v: bigint | number): Rat;
|
|
12
|
+
add(o: Rat): Rat;
|
|
13
|
+
mul(o: Rat): Rat;
|
|
14
|
+
cmp(o: Rat): number;
|
|
15
|
+
isInt(): boolean;
|
|
16
|
+
/** The largest integer at or below. */
|
|
17
|
+
floor(): bigint;
|
|
18
|
+
}
|
|
19
|
+
/** floor(x + 1/2): a half rounds toward the larger integer. */
|
|
20
|
+
export declare function roundHalfUp(x: Rat): bigint;
|
|
21
|
+
/** An exact fraction as a decimal string when it ends ("0.58", "-3"), else "n/d". */
|
|
22
|
+
export declare function fractionStr(x: Rat): string;
|
|
23
|
+
/**
|
|
24
|
+
* An exact fraction from a decimal string ("0.40", "1e-7"), an "n/d" string,
|
|
25
|
+
* a bigint or a number (read as its shortest decimal, as String gives it).
|
|
26
|
+
*/
|
|
27
|
+
export declare function parseRat(value: unknown, what: string): Rat;
|
|
28
|
+
/** A raw integer amount from a bigint, a safe integer number or a digit string. */
|
|
29
|
+
export declare function parseInt64(value: unknown, what: string): bigint;
|
|
30
|
+
/**
|
|
31
|
+
* A time as microseconds since the epoch, from a Date or an ISO 8601 string
|
|
32
|
+
* (no offset read as UTC); microseconds are kept, which a Date would drop.
|
|
33
|
+
*/
|
|
34
|
+
export declare function parseMicros(value: unknown, what: string): bigint;
|
|
35
|
+
/** Microseconds since the epoch as the Python SDK prints a UTC time: seconds, then six digits when there are microseconds, then Z. */
|
|
36
|
+
export declare function isoMicros(micros: bigint): string;
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Exact numbers for fees and the backtest: fractions of BigInts, times in
|
|
4
|
+
* microseconds since the epoch, and the formats the Python SDK prints, so
|
|
5
|
+
* both SDKs give the same strings for the same values.
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.Rat = void 0;
|
|
9
|
+
exports.roundHalfUp = roundHalfUp;
|
|
10
|
+
exports.fractionStr = fractionStr;
|
|
11
|
+
exports.parseRat = parseRat;
|
|
12
|
+
exports.parseInt64 = parseInt64;
|
|
13
|
+
exports.parseMicros = parseMicros;
|
|
14
|
+
exports.isoMicros = isoMicros;
|
|
15
|
+
const errors_js_1 = require("./errors.js");
|
|
16
|
+
function gcd(a, b) {
|
|
17
|
+
a = a < 0n ? -a : a;
|
|
18
|
+
b = b < 0n ? -b : b;
|
|
19
|
+
while (b)
|
|
20
|
+
[a, b] = [b, a % b];
|
|
21
|
+
return a;
|
|
22
|
+
}
|
|
23
|
+
/** An exact fraction, always reduced, with a positive denominator. */
|
|
24
|
+
class Rat {
|
|
25
|
+
n;
|
|
26
|
+
d;
|
|
27
|
+
constructor(n, d = 1n) {
|
|
28
|
+
if (d === 0n)
|
|
29
|
+
throw new errors_js_1.SkewtapeError("division by zero");
|
|
30
|
+
if (d < 0n) {
|
|
31
|
+
n = -n;
|
|
32
|
+
d = -d;
|
|
33
|
+
}
|
|
34
|
+
const g = gcd(n, d) || 1n;
|
|
35
|
+
this.n = n / g;
|
|
36
|
+
this.d = d / g;
|
|
37
|
+
}
|
|
38
|
+
static of(v) {
|
|
39
|
+
return new Rat(BigInt(v));
|
|
40
|
+
}
|
|
41
|
+
add(o) {
|
|
42
|
+
return new Rat(this.n * o.d + o.n * this.d, this.d * o.d);
|
|
43
|
+
}
|
|
44
|
+
mul(o) {
|
|
45
|
+
return new Rat(this.n * o.n, this.d * o.d);
|
|
46
|
+
}
|
|
47
|
+
cmp(o) {
|
|
48
|
+
const l = this.n * o.d;
|
|
49
|
+
const r = o.n * this.d;
|
|
50
|
+
return l < r ? -1 : l > r ? 1 : 0;
|
|
51
|
+
}
|
|
52
|
+
isInt() {
|
|
53
|
+
return this.d === 1n;
|
|
54
|
+
}
|
|
55
|
+
/** The largest integer at or below. */
|
|
56
|
+
floor() {
|
|
57
|
+
const q = this.n / this.d;
|
|
58
|
+
return this.n < 0n && this.n % this.d !== 0n ? q - 1n : q;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
exports.Rat = Rat;
|
|
62
|
+
/** floor(x + 1/2): a half rounds toward the larger integer. */
|
|
63
|
+
function roundHalfUp(x) {
|
|
64
|
+
return x.add(new Rat(1n, 2n)).floor();
|
|
65
|
+
}
|
|
66
|
+
/** An exact fraction as a decimal string when it ends ("0.58", "-3"), else "n/d". */
|
|
67
|
+
function fractionStr(x) {
|
|
68
|
+
let den = x.d;
|
|
69
|
+
let twos = 0;
|
|
70
|
+
let fives = 0;
|
|
71
|
+
while (den % 2n === 0n) {
|
|
72
|
+
den /= 2n;
|
|
73
|
+
twos++;
|
|
74
|
+
}
|
|
75
|
+
while (den % 5n === 0n) {
|
|
76
|
+
den /= 5n;
|
|
77
|
+
fives++;
|
|
78
|
+
}
|
|
79
|
+
if (den !== 1n)
|
|
80
|
+
return `${x.n}/${x.d}`;
|
|
81
|
+
const places = Math.max(twos, fives);
|
|
82
|
+
const abs = x.n < 0n ? -x.n : x.n;
|
|
83
|
+
const scaled = abs * (10n ** BigInt(places) / x.d);
|
|
84
|
+
const sign = x.n < 0n ? "-" : "";
|
|
85
|
+
if (places === 0)
|
|
86
|
+
return `${sign}${scaled}`;
|
|
87
|
+
const digits = scaled.toString().padStart(places + 1, "0");
|
|
88
|
+
const whole = digits.slice(0, -places);
|
|
89
|
+
const frac = digits.slice(-places).replace(/0+$/, "");
|
|
90
|
+
return frac ? `${sign}${whole}.${frac}` : `${sign}${whole}`;
|
|
91
|
+
}
|
|
92
|
+
const DECIMAL = /^([+-]?)(\d*)(?:\.(\d*))?(?:[eE]([+-]?\d+))?$/;
|
|
93
|
+
const FRACTION = /^([+-]?\d+)\s*\/\s*(\d+)$/;
|
|
94
|
+
/**
|
|
95
|
+
* An exact fraction from a decimal string ("0.40", "1e-7"), an "n/d" string,
|
|
96
|
+
* a bigint or a number (read as its shortest decimal, as String gives it).
|
|
97
|
+
*/
|
|
98
|
+
function parseRat(value, what) {
|
|
99
|
+
if (value instanceof Rat)
|
|
100
|
+
return value;
|
|
101
|
+
if (typeof value === "bigint")
|
|
102
|
+
return new Rat(value);
|
|
103
|
+
if (typeof value === "number") {
|
|
104
|
+
if (!Number.isFinite(value))
|
|
105
|
+
throw new errors_js_1.SkewtapeError(`${what}: ${value} is not finite`, { code: "bad_trade" });
|
|
106
|
+
return parseRat(String(value), what);
|
|
107
|
+
}
|
|
108
|
+
if (typeof value !== "string")
|
|
109
|
+
throw new errors_js_1.SkewtapeError(`${what}: ${String(value)} is not a number`, { code: "bad_trade" });
|
|
110
|
+
const s = value.trim();
|
|
111
|
+
const f = FRACTION.exec(s);
|
|
112
|
+
if (f)
|
|
113
|
+
return new Rat(BigInt(f[1]), BigInt(f[2]));
|
|
114
|
+
const m = DECIMAL.exec(s);
|
|
115
|
+
if (!m || (m[2] === "" && (m[3] === undefined || m[3] === "")))
|
|
116
|
+
throw new errors_js_1.SkewtapeError(`${what}: ${value} is not a number`, { code: "bad_trade" });
|
|
117
|
+
const digits = `${m[2] ?? ""}${m[3] ?? ""}` || "0";
|
|
118
|
+
const exp = Number(m[4] ?? "0") - (m[3]?.length ?? 0);
|
|
119
|
+
let r = exp >= 0 ? new Rat(BigInt(digits) * 10n ** BigInt(exp)) : new Rat(BigInt(digits), 10n ** BigInt(-exp));
|
|
120
|
+
if (m[1] === "-")
|
|
121
|
+
r = new Rat(-r.n, r.d);
|
|
122
|
+
return r;
|
|
123
|
+
}
|
|
124
|
+
/** A raw integer amount from a bigint, a safe integer number or a digit string. */
|
|
125
|
+
function parseInt64(value, what) {
|
|
126
|
+
if (typeof value === "bigint")
|
|
127
|
+
return value;
|
|
128
|
+
if (typeof value === "number" && Number.isSafeInteger(value))
|
|
129
|
+
return BigInt(value);
|
|
130
|
+
if (typeof value === "string" && /^[+-]?\d+$/.test(value.trim()))
|
|
131
|
+
return BigInt(value.trim());
|
|
132
|
+
throw new errors_js_1.SkewtapeError(`${what}: ${String(value)} is not an integer`, { code: "bad_trade" });
|
|
133
|
+
}
|
|
134
|
+
const ISO = /^(\d{4})-(\d{2})-(\d{2})[T ](\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{1,9}))?)?(Z|[+-]\d{2}:?\d{2})?$/i;
|
|
135
|
+
/**
|
|
136
|
+
* A time as microseconds since the epoch, from a Date or an ISO 8601 string
|
|
137
|
+
* (no offset read as UTC); microseconds are kept, which a Date would drop.
|
|
138
|
+
*/
|
|
139
|
+
function parseMicros(value, what) {
|
|
140
|
+
if (value instanceof Date) {
|
|
141
|
+
if (Number.isNaN(value.getTime()))
|
|
142
|
+
throw new errors_js_1.SkewtapeError(`${what}: not a time`, { code: "bad_trade" });
|
|
143
|
+
return BigInt(value.getTime()) * 1000n;
|
|
144
|
+
}
|
|
145
|
+
if (typeof value !== "string")
|
|
146
|
+
throw new errors_js_1.SkewtapeError(`${what}: ${String(value)} is not a time`, { code: "bad_trade" });
|
|
147
|
+
const m = ISO.exec(value.trim());
|
|
148
|
+
if (!m)
|
|
149
|
+
throw new errors_js_1.SkewtapeError(`${what}: ${value} is not a time`, { code: "bad_trade" });
|
|
150
|
+
const ms = Date.UTC(Number(m[1]), Number(m[2]) - 1, Number(m[3]), Number(m[4]), Number(m[5]), Number(m[6] ?? "0"));
|
|
151
|
+
const frac = (m[7] ?? "").padEnd(6, "0").slice(0, 6);
|
|
152
|
+
let micros = BigInt(ms) * 1000n + BigInt(frac || "0");
|
|
153
|
+
const zone = m[8];
|
|
154
|
+
if (zone && zone.toUpperCase() !== "Z") {
|
|
155
|
+
const sign = zone.startsWith("-") ? -1n : 1n;
|
|
156
|
+
const digits = zone.slice(1).replace(":", "");
|
|
157
|
+
const minutes = BigInt(Number(digits.slice(0, 2)) * 60 + Number(digits.slice(2, 4)));
|
|
158
|
+
micros -= sign * minutes * 60000000n;
|
|
159
|
+
}
|
|
160
|
+
return micros;
|
|
161
|
+
}
|
|
162
|
+
/** Microseconds since the epoch as the Python SDK prints a UTC time: seconds, then six digits when there are microseconds, then Z. */
|
|
163
|
+
function isoMicros(micros) {
|
|
164
|
+
const ms = Number(micros / 1000n - (micros < 0n && micros % 1000n !== 0n ? 1n : 0n));
|
|
165
|
+
const base = new Date(ms).toISOString().slice(0, 19);
|
|
166
|
+
const rem = ((micros % 1000000n) + 1000000n) % 1000000n;
|
|
167
|
+
return rem === 0n ? `${base}Z` : `${base}.${rem.toString().padStart(6, "0")}Z`;
|
|
168
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fees from the schedule: the row in force for a fill and the fee it gives,
|
|
3
|
+
* exactly as Skewtape computes them (tape's internal/fees,
|
|
4
|
+
* docs.skewtape.com/fees). The fee is kept as an exact fraction until it is
|
|
5
|
+
* rounded, as the row says, to a step given in millionths of a dollar; a
|
|
6
|
+
* minimum, when the row has one, raises a fee that is above zero after
|
|
7
|
+
* rounding. conformance/fees.json pins every case, and the tests run all of it.
|
|
8
|
+
*/
|
|
9
|
+
import { SkewtapeError } from "./errors.js";
|
|
10
|
+
import { Rat } from "./exact.js";
|
|
11
|
+
export declare const COLLATERAL_MICROS_DECIMALS = 6;
|
|
12
|
+
export declare const FALLBACK_CATEGORY = "*";
|
|
13
|
+
/** A fee that cannot be computed: an unknown formula or rounding, a missing parameter, a size out of range. */
|
|
14
|
+
export declare class FeeError extends SkewtapeError {
|
|
15
|
+
}
|
|
16
|
+
/** One row of the fee schedule, read from the API's Fee or the same shape. */
|
|
17
|
+
export interface FeeRow {
|
|
18
|
+
venue: string;
|
|
19
|
+
category: string;
|
|
20
|
+
/** Microseconds since the epoch. */
|
|
21
|
+
effectiveFrom: bigint;
|
|
22
|
+
effectiveTo: bigint | null;
|
|
23
|
+
formula: string;
|
|
24
|
+
rounding: string;
|
|
25
|
+
takerParams: Record<string, bigint>;
|
|
26
|
+
makerRebateParams: Record<string, bigint>;
|
|
27
|
+
minFeeMicros: bigint | null;
|
|
28
|
+
}
|
|
29
|
+
/** What a fee row can be given as: the API's Fee (snake_case) or a FeeRow. */
|
|
30
|
+
export type FeeRowInput = FeeRow | Record<string, unknown>;
|
|
31
|
+
/** A FeeRow from the API's Fee, its JSON, or a FeeRow. */
|
|
32
|
+
export declare function feeRowOf(row: FeeRowInput): FeeRow;
|
|
33
|
+
/** A millionth-of-a-dollar amount in raw units at decimals. */
|
|
34
|
+
export declare function microsToRaw(micros: bigint, decimals: number): bigint;
|
|
35
|
+
/**
|
|
36
|
+
* A fee formula's value for size shares against notional collateral (any one
|
|
37
|
+
* unit for both), exact, before rounding and any minimum: the one port of
|
|
38
|
+
* tape's internal/fees formulas, which takerFeeRaw and sk.quote both use.
|
|
39
|
+
*/
|
|
40
|
+
export declare function exactFee(formula: string, params: Record<string, bigint>, size: bigint, notional: bigint): Rat;
|
|
41
|
+
/**
|
|
42
|
+
* The taker fee in raw units for sizeRaw shares against notionalRaw
|
|
43
|
+
* collateral, both at decimals, under row: the formula exactly, rounded to
|
|
44
|
+
* round_step_micros (1 raw unit without one), then raised to min_fee_micros
|
|
45
|
+
* when it is above zero and below it.
|
|
46
|
+
*/
|
|
47
|
+
export declare function takerFeeRaw(row: FeeRowInput, sizeRaw: bigint, notionalRaw: bigint, decimals: number): bigint;
|
|
48
|
+
/** A fee's terms alone: what takerFeeOf reads of a row. */
|
|
49
|
+
export type FeeTerms = Pick<FeeRow, "formula" | "rounding" | "takerParams" | "minFeeMicros">;
|
|
50
|
+
/**
|
|
51
|
+
* takerFeeRaw over a fee's terms alone, for callers that hold no whole row:
|
|
52
|
+
* sk.quote computes its fees with it at 6 decimals.
|
|
53
|
+
*/
|
|
54
|
+
export declare function takerFeeOf(fee: FeeTerms, sizeRaw: bigint, notionalRaw: bigint, decimals: number): bigint;
|
|
55
|
+
/**
|
|
56
|
+
* The maker's fee on venues that charge makers (maker_fee_rate_micros in
|
|
57
|
+
* maker_rebate_params, quadratic, with the row's rounding and step); zero
|
|
58
|
+
* elsewhere. A rebate share is data about the venue, not a fee, and is not
|
|
59
|
+
* applied.
|
|
60
|
+
*/
|
|
61
|
+
export declare function makerFeeRaw(row: FeeRowInput, sizeRaw: bigint, notionalRaw: bigint, decimals: number): bigint;
|
|
62
|
+
/**
|
|
63
|
+
* The row in force for a fill of venue at time at (microseconds, a Date or
|
|
64
|
+
* an ISO string): the market's fee_category if a row has it, else the row
|
|
65
|
+
* with category *; of several, the latest effective_from. Undefined when no
|
|
66
|
+
* row applies.
|
|
67
|
+
*/
|
|
68
|
+
export declare function selectFeeRow(rows: Iterable<FeeRowInput>, venue: string, category: string | null | undefined, at: bigint | Date | string): FeeRow | undefined;
|