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,387 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* sk.route for Hyperliquid's HIP-4 outcome markets (E21-T3): an unsigned
|
|
4
|
+
* order, with the developer's own builder code, for the developer to sign.
|
|
5
|
+
*
|
|
6
|
+
* An order is an L1 action: its MessagePack bytes, the nonce, the vault flag
|
|
7
|
+
* and expiresAfter are hashed with Keccak-256, and the hash is signed as a
|
|
8
|
+
* "phantom agent" under the EIP-712 domain "Exchange" on chain 1337 (the
|
|
9
|
+
* signing page and the official Python SDK's signing.py, read 14 Nov 2026). A
|
|
10
|
+
* wallet that only signs for its own chain will not sign chain 1337: L1
|
|
11
|
+
* actions are signed by a key the developer holds, usually an API wallet the
|
|
12
|
+
* user approved. Nothing here signs, sends or takes a key.
|
|
13
|
+
*
|
|
14
|
+
* A builder code is the developer's: {"b": address, "f": fee in tenths of a
|
|
15
|
+
* basis point}. The user approves a maximum fee for that builder first, with
|
|
16
|
+
* their main wallet (hyperliquidApproveBuilderFee builds that request,
|
|
17
|
+
* unsigned). With no builder, the order carries none: no default code exists.
|
|
18
|
+
*
|
|
19
|
+
* const order = route({
|
|
20
|
+
* venue: "hyperliquid", outcome: 96, side: 0, isBuy: false,
|
|
21
|
+
* price: "0.55", size: "20", builder: "0xYourBuilder", builderFee: 10,
|
|
22
|
+
* });
|
|
23
|
+
* const signature = await apiWallet.signTypedData(order.typedData);
|
|
24
|
+
* const body = order.postBody(signature); // POST to HYPERLIQUID_EXCHANGE_URL
|
|
25
|
+
*
|
|
26
|
+
* Fees, read 14 Nov 2026. The HIP-4 page: "Fees are currently zero for outcome
|
|
27
|
+
* markets for initial testing. However, builder codes do work the same as
|
|
28
|
+
* normal spot trading, where builders earn builder fees on sell orders that
|
|
29
|
+
* specify their builder code." The fees page: "Outcome trading only charges
|
|
30
|
+
* fees when closing or settling, not when opening outcome positions." The two
|
|
31
|
+
* do not agree, and the testnet-only deployer page says builder fees also
|
|
32
|
+
* apply to buy orders, best effort; the order is the same either way.
|
|
33
|
+
*/
|
|
34
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
|
+
exports.HyperliquidApproveBuilderFee = exports.HyperliquidOrder = exports.HYPERLIQUID_APPROVE_FIELDS = exports.HYPERLIQUID_FIELDS = exports.HYPERLIQUID_APPROVE_BUILDER_FEE_TYPE = exports.HYPERLIQUID_SIGN_DOMAIN_NAME = exports.HYPERLIQUID_BUILDER_FEE_MAX = exports.HYPERLIQUID_SPOT_MAX_DECIMALS = exports.HYPERLIQUID_PRICE_SIG_FIGS = exports.HYPERLIQUID_TIFS = exports.HYPERLIQUID_AGENT_TYPE = exports.HYPERLIQUID_L1_DOMAIN = exports.HYPERLIQUID_OUTCOME_ASSET_BASE = exports.HYPERLIQUID_TESTNET_EXCHANGE_URL = exports.HYPERLIQUID_EXCHANGE_URL = void 0;
|
|
36
|
+
exports.hyperliquidOutcomeAsset = hyperliquidOutcomeAsset;
|
|
37
|
+
exports.hyperliquidActionHash = hyperliquidActionHash;
|
|
38
|
+
exports.hyperliquidL1TypedData = hyperliquidL1TypedData;
|
|
39
|
+
exports.eip712Digest = eip712Digest;
|
|
40
|
+
exports.hyperliquidOrder = hyperliquidOrder;
|
|
41
|
+
exports.hyperliquidApproveBuilderFee = hyperliquidApproveBuilderFee;
|
|
42
|
+
const errors_js_1 = require("./errors.js");
|
|
43
|
+
const keccak_js_1 = require("./keccak.js");
|
|
44
|
+
const msgpack_js_1 = require("./msgpack.js");
|
|
45
|
+
/** Endpoints the developer posts to ("Exchange endpoint"); route never calls them. */
|
|
46
|
+
exports.HYPERLIQUID_EXCHANGE_URL = "https://api.hyperliquid.xyz/exchange";
|
|
47
|
+
exports.HYPERLIQUID_TESTNET_EXCHANGE_URL = "https://api.hyperliquid-testnet.xyz/exchange";
|
|
48
|
+
/** "Outcome asset ID: 100_000_000 + encoding", encoding = 10 * outcome + side (api/asset-ids). */
|
|
49
|
+
exports.HYPERLIQUID_OUTCOME_ASSET_BASE = 100_000_000;
|
|
50
|
+
/** The L1 action's EIP-712 domain and type (signing.py, l1_payload). */
|
|
51
|
+
exports.HYPERLIQUID_L1_DOMAIN = {
|
|
52
|
+
name: "Exchange",
|
|
53
|
+
version: "1",
|
|
54
|
+
chainId: 1337,
|
|
55
|
+
verifyingContract: "0x0000000000000000000000000000000000000000",
|
|
56
|
+
};
|
|
57
|
+
exports.HYPERLIQUID_AGENT_TYPE = [
|
|
58
|
+
{ name: "source", type: "string" },
|
|
59
|
+
{ name: "connectionId", type: "bytes32" },
|
|
60
|
+
];
|
|
61
|
+
/** Time in force: "Alo" adds liquidity only, "Ioc" takes what crosses now (a market order is an Ioc at the worst price accepted). */
|
|
62
|
+
exports.HYPERLIQUID_TIFS = ["Gtc", "Ioc", "Alo"];
|
|
63
|
+
/**
|
|
64
|
+
* "Prices can have up to 5 significant figures, but no more than MAX_DECIMALS
|
|
65
|
+
* - szDecimals decimal places where MAX_DECIMALS is 6 for perps and 8 for
|
|
66
|
+
* spot" (api/tick-and-lot-size). Outcomes trade like spot, so 8.
|
|
67
|
+
*/
|
|
68
|
+
exports.HYPERLIQUID_PRICE_SIG_FIGS = 5;
|
|
69
|
+
exports.HYPERLIQUID_SPOT_MAX_DECIMALS = 8;
|
|
70
|
+
/** "at most 0.1% on perps and 1% on spot" in tenths of a basis point: spot's 1% is 1000; outcomes inherit spot's behaviour. */
|
|
71
|
+
exports.HYPERLIQUID_BUILDER_FEE_MAX = 1000;
|
|
72
|
+
exports.HYPERLIQUID_SIGN_DOMAIN_NAME = "HyperliquidSignTransaction";
|
|
73
|
+
exports.HYPERLIQUID_APPROVE_BUILDER_FEE_TYPE = [
|
|
74
|
+
{ name: "hyperliquidChain", type: "string" },
|
|
75
|
+
{ name: "maxFeeRate", type: "string" },
|
|
76
|
+
{ name: "builder", type: "address" },
|
|
77
|
+
{ name: "nonce", type: "uint64" },
|
|
78
|
+
];
|
|
79
|
+
const EIP712_DOMAIN = [
|
|
80
|
+
{ name: "name", type: "string" },
|
|
81
|
+
{ name: "version", type: "string" },
|
|
82
|
+
{ name: "chainId", type: "uint256" },
|
|
83
|
+
{ name: "verifyingContract", type: "address" },
|
|
84
|
+
];
|
|
85
|
+
const ADDRESS = /^0x[0-9a-fA-F]{40}$/;
|
|
86
|
+
const SIGNATURE = /^0x[0-9a-fA-F]{130}$/;
|
|
87
|
+
const CLOID = /^0x[0-9a-fA-F]{32}$/;
|
|
88
|
+
const DECIMAL = /^(\d+)(?:\.(\d+))?$/;
|
|
89
|
+
const U64 = 2n ** 64n;
|
|
90
|
+
/** The fields a Hyperliquid route takes; anything else, a key above all, is refused. */
|
|
91
|
+
exports.HYPERLIQUID_FIELDS = [
|
|
92
|
+
"venue", "outcome", "side", "isBuy", "price", "size", "tif", "reduceOnly", "cloid", "builder", "builderFee",
|
|
93
|
+
"szDecimals", "nonce", "vaultAddress", "expiresAfter", "mainnet",
|
|
94
|
+
];
|
|
95
|
+
/** The fields hyperliquidApproveBuilderFee takes; anything else, a key above all, is refused. */
|
|
96
|
+
exports.HYPERLIQUID_APPROVE_FIELDS = ["builder", "maxFeeRate", "signatureChainId", "nonce", "mainnet"];
|
|
97
|
+
/** Refuses a field not in known: from JavaScript or a cast, a privateKey would otherwise be ignored silently. */
|
|
98
|
+
function onlyFields(what, input, known) {
|
|
99
|
+
const unknown = Object.keys(input).filter((k) => !known.includes(k));
|
|
100
|
+
if (unknown.length)
|
|
101
|
+
throw new errors_js_1.RouteError(`${what}: unknown field ${unknown.join(", ")}; it takes no key and nothing else`);
|
|
102
|
+
}
|
|
103
|
+
/** The order's asset id for side (0 or 1) of outcome: 100_000_000 + 10 * outcome + side. */
|
|
104
|
+
function hyperliquidOutcomeAsset(outcome, side) {
|
|
105
|
+
if (!Number.isSafeInteger(outcome) || outcome < 0)
|
|
106
|
+
throw new errors_js_1.RouteError(`outcome: a non-negative integer, not ${JSON.stringify(outcome)}`);
|
|
107
|
+
if (side !== 0 && side !== 1)
|
|
108
|
+
throw new errors_js_1.RouteError(`side: 0 or 1 (the outcome's two sides), not ${JSON.stringify(side)}`);
|
|
109
|
+
const asset = exports.HYPERLIQUID_OUTCOME_ASSET_BASE + 10 * outcome + side;
|
|
110
|
+
if (!Number.isSafeInteger(asset))
|
|
111
|
+
throw new errors_js_1.RouteError("outcome: too large");
|
|
112
|
+
return asset;
|
|
113
|
+
}
|
|
114
|
+
function num(name, value) {
|
|
115
|
+
let text;
|
|
116
|
+
if (typeof value === "number") {
|
|
117
|
+
if (!Number.isSafeInteger(value))
|
|
118
|
+
throw new errors_js_1.RouteError(`${name}: pass a decimal string, not the float ${value}`);
|
|
119
|
+
text = String(value);
|
|
120
|
+
}
|
|
121
|
+
else if (typeof value === "string") {
|
|
122
|
+
text = value;
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
throw new errors_js_1.RouteError(`${name}: a decimal string`);
|
|
126
|
+
}
|
|
127
|
+
const m = DECIMAL.exec(text);
|
|
128
|
+
if (!m)
|
|
129
|
+
throw new errors_js_1.RouteError(`${name}: ${JSON.stringify(value)} is not a decimal number`);
|
|
130
|
+
const int = (m[1] ?? "").replace(/^0+(?=\d)/, "");
|
|
131
|
+
const frac = (m[2] ?? "").replace(/0+$/, "");
|
|
132
|
+
if (/^0*$/.test(int + frac))
|
|
133
|
+
throw new errors_js_1.RouteError(`${name}: must be above 0, not ${JSON.stringify(value)}`);
|
|
134
|
+
const digits = (int + frac).replace(/^0+/, "");
|
|
135
|
+
// As Decimal.normalize counts them: an integer's trailing zeros are not significant.
|
|
136
|
+
const significant = frac ? digits.length : digits.replace(/0+$/, "").length;
|
|
137
|
+
return { wire: frac ? `${int}.${frac}` : int, decimals: frac.length, significant, integer: !frac };
|
|
138
|
+
}
|
|
139
|
+
function address(name, value) {
|
|
140
|
+
if (typeof value !== "string" || !ADDRESS.test(value))
|
|
141
|
+
throw new errors_js_1.RouteError(`${name}: ${JSON.stringify(value)} is not a 0x-prefixed 20-byte address`);
|
|
142
|
+
return value.toLowerCase();
|
|
143
|
+
}
|
|
144
|
+
function u64(name, value, what) {
|
|
145
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value <= 0)
|
|
146
|
+
throw new errors_js_1.RouteError(`${name}: ${what}`);
|
|
147
|
+
return value;
|
|
148
|
+
}
|
|
149
|
+
function be8(n) {
|
|
150
|
+
const out = new Uint8Array(8);
|
|
151
|
+
let v = BigInt(n) % U64;
|
|
152
|
+
for (let i = 7; i >= 0; i--) {
|
|
153
|
+
out[i] = Number(v & 0xffn);
|
|
154
|
+
v >>= 8n;
|
|
155
|
+
}
|
|
156
|
+
return out;
|
|
157
|
+
}
|
|
158
|
+
const concat = (...parts) => {
|
|
159
|
+
const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
|
|
160
|
+
let at = 0;
|
|
161
|
+
for (const p of parts) {
|
|
162
|
+
out.set(p, at);
|
|
163
|
+
at += p.length;
|
|
164
|
+
}
|
|
165
|
+
return out;
|
|
166
|
+
};
|
|
167
|
+
const hexBytes = (hex) => Uint8Array.from(hex.slice(2).match(/../g) ?? [], (b) => Number.parseInt(b, 16));
|
|
168
|
+
/**
|
|
169
|
+
* Keccak-256 of the action's MessagePack bytes, the nonce (8 bytes, big
|
|
170
|
+
* endian), the vault flag (0x00, or 0x01 and the vault's 20 bytes) and, when
|
|
171
|
+
* set, 0x00 and expiresAfter (8 bytes): signing.py's action_hash.
|
|
172
|
+
*/
|
|
173
|
+
function hyperliquidActionHash(action, nonce, vaultAddress, expiresAfter) {
|
|
174
|
+
const parts = [(0, msgpack_js_1.packb)(action), be8(nonce), vaultAddress ? concat(Uint8Array.of(1), hexBytes(vaultAddress)) : Uint8Array.of(0)];
|
|
175
|
+
if (expiresAfter !== undefined && expiresAfter !== null)
|
|
176
|
+
parts.push(Uint8Array.of(0), be8(expiresAfter));
|
|
177
|
+
return (0, keccak_js_1.toHex)((0, keccak_js_1.keccak256)(concat(...parts)));
|
|
178
|
+
}
|
|
179
|
+
/** The typed data of an L1 action's phantom agent: source "a" on mainnet, "b" on testnet. */
|
|
180
|
+
function hyperliquidL1TypedData(connectionId, mainnet = true) {
|
|
181
|
+
return { domain: exports.HYPERLIQUID_L1_DOMAIN, types: { Agent: exports.HYPERLIQUID_AGENT_TYPE }, primaryType: "Agent", message: { source: mainnet ? "a" : "b", connectionId } };
|
|
182
|
+
}
|
|
183
|
+
function splitSignature(signature) {
|
|
184
|
+
if (typeof signature !== "string" || !SIGNATURE.test(signature)) {
|
|
185
|
+
throw new errors_js_1.RouteError("signature: the 65-byte 0x-prefixed signature (r, s, v) of typedData");
|
|
186
|
+
}
|
|
187
|
+
let v = Number.parseInt(signature.slice(130, 132), 16);
|
|
188
|
+
if (v < 27)
|
|
189
|
+
v += 27;
|
|
190
|
+
if (v !== 27 && v !== 28)
|
|
191
|
+
throw new errors_js_1.RouteError(`signature: v is ${signature.slice(130, 132)}, not 27 or 28 (or 0 or 1)`);
|
|
192
|
+
// As the official SDK sends it: r and s as 0x hex without leading zeros, v a number.
|
|
193
|
+
const hex = (h) => `0x${BigInt(`0x${h}`).toString(16)}`;
|
|
194
|
+
return { r: hex(signature.slice(2, 66)), s: hex(signature.slice(66, 130)), v };
|
|
195
|
+
}
|
|
196
|
+
function word(n) {
|
|
197
|
+
const out = new Uint8Array(32);
|
|
198
|
+
for (let i = 31; i >= 0 && n > 0n; i--) {
|
|
199
|
+
out[i] = Number(n & 0xffn);
|
|
200
|
+
n >>= 8n;
|
|
201
|
+
}
|
|
202
|
+
return out;
|
|
203
|
+
}
|
|
204
|
+
function encodeField(type, value) {
|
|
205
|
+
if (type === "string")
|
|
206
|
+
return (0, keccak_js_1.keccak256)(new TextEncoder().encode(String(value)));
|
|
207
|
+
if (type === "bytes32")
|
|
208
|
+
return hexBytes(String(value));
|
|
209
|
+
if (type === "address")
|
|
210
|
+
return word(BigInt(String(value)));
|
|
211
|
+
if (/^uint\d+$/.test(type))
|
|
212
|
+
return word(BigInt(value));
|
|
213
|
+
throw new errors_js_1.RouteError(`EIP-712: type ${type} is not one this package hashes`);
|
|
214
|
+
}
|
|
215
|
+
function hashStruct(name, fields, value) {
|
|
216
|
+
const typeHash = (0, keccak_js_1.keccak256)(new TextEncoder().encode(`${name}(${fields.map((f) => `${f.type} ${f.name}`).join(",")})`));
|
|
217
|
+
return (0, keccak_js_1.keccak256)(concat(typeHash, ...fields.map((f) => encodeField(f.type, value[f.name]))));
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* The EIP-712 digest of flat typed data (strings, addresses, unsigned
|
|
221
|
+
* integers, bytes32), computed here: viem's signTypedData refuses the type
|
|
222
|
+
* name "HyperliquidTransaction:ApproveBuilderFee" (a colon), so a viem key
|
|
223
|
+
* signs this digest instead (account.sign({ hash: digest })).
|
|
224
|
+
*/
|
|
225
|
+
function eip712Digest(typed) {
|
|
226
|
+
const fields = typed.types[typed.primaryType];
|
|
227
|
+
if (!fields)
|
|
228
|
+
throw new errors_js_1.RouteError(`EIP-712: no type ${typed.primaryType}`);
|
|
229
|
+
return (0, keccak_js_1.toHex)((0, keccak_js_1.keccak256)(concat(Uint8Array.of(0x19, 0x01), hashStruct("EIP712Domain", EIP712_DOMAIN, typed.domain), hashStruct(typed.primaryType, fields, typed.message))));
|
|
230
|
+
}
|
|
231
|
+
function typedJson(typed) {
|
|
232
|
+
return JSON.stringify({ types: { EIP712Domain: EIP712_DOMAIN, ...typed.types }, primaryType: typed.primaryType, domain: typed.domain, message: typed.message }, (_k, v) => (typeof v === "bigint" ? v.toString() : v));
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* An unsigned Hyperliquid order. Sign `typedData` with the key that trades
|
|
236
|
+
* (an API wallet), then POST `postBody(signature)` to `exchangeUrl`. Nothing
|
|
237
|
+
* here was signed or sent.
|
|
238
|
+
*/
|
|
239
|
+
class HyperliquidOrder {
|
|
240
|
+
action;
|
|
241
|
+
nonce;
|
|
242
|
+
connectionId;
|
|
243
|
+
typedData;
|
|
244
|
+
vaultAddress;
|
|
245
|
+
expiresAfter;
|
|
246
|
+
exchangeUrl;
|
|
247
|
+
venue = "hyperliquid";
|
|
248
|
+
constructor(action, nonce, connectionId, typedData, vaultAddress, expiresAfter, exchangeUrl) {
|
|
249
|
+
this.action = action;
|
|
250
|
+
this.nonce = nonce;
|
|
251
|
+
this.connectionId = connectionId;
|
|
252
|
+
this.typedData = typedData;
|
|
253
|
+
this.vaultAddress = vaultAddress;
|
|
254
|
+
this.expiresAfter = expiresAfter;
|
|
255
|
+
this.exchangeUrl = exchangeUrl;
|
|
256
|
+
}
|
|
257
|
+
/** The typed data as eth_signTypedData_v4 takes it: JSON, the EIP712Domain type included. */
|
|
258
|
+
typedDataJson() {
|
|
259
|
+
return typedJson(this.typedData);
|
|
260
|
+
}
|
|
261
|
+
/** The EIP-712 digest of typedData, for a signer that signs a hash. */
|
|
262
|
+
get digest() {
|
|
263
|
+
return eip712Digest(this.typedData);
|
|
264
|
+
}
|
|
265
|
+
/** The POST /exchange body: action, nonce, the signature split as the official SDK sends it, vaultAddress and expiresAfter. */
|
|
266
|
+
postBody(signature) {
|
|
267
|
+
return { action: this.action, nonce: this.nonce, signature: splitSignature(signature), vaultAddress: this.vaultAddress, expiresAfter: this.expiresAfter };
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
exports.HyperliquidOrder = HyperliquidOrder;
|
|
271
|
+
function hyperliquidOrder(input) {
|
|
272
|
+
onlyFields("hyperliquid", input, exports.HYPERLIQUID_FIELDS);
|
|
273
|
+
const asset = hyperliquidOutcomeAsset(input.outcome, input.side);
|
|
274
|
+
if (typeof input.isBuy !== "boolean")
|
|
275
|
+
throw new errors_js_1.RouteError("isBuy: true to buy the side's token, false to sell it");
|
|
276
|
+
const reduceOnly = input.reduceOnly ?? false;
|
|
277
|
+
if (typeof reduceOnly !== "boolean")
|
|
278
|
+
throw new errors_js_1.RouteError("reduceOnly: true or false");
|
|
279
|
+
const tif = input.tif ?? "Gtc";
|
|
280
|
+
if (!exports.HYPERLIQUID_TIFS.includes(tif))
|
|
281
|
+
throw new errors_js_1.RouteError(`tif: one of ${exports.HYPERLIQUID_TIFS.join(", ")}`);
|
|
282
|
+
const sz = input.szDecimals;
|
|
283
|
+
if (sz !== undefined && (!Number.isInteger(sz) || sz < 0 || sz > exports.HYPERLIQUID_SPOT_MAX_DECIMALS)) {
|
|
284
|
+
throw new errors_js_1.RouteError(`szDecimals: an integer from 0 to ${exports.HYPERLIQUID_SPOT_MAX_DECIMALS}`);
|
|
285
|
+
}
|
|
286
|
+
const maxPriceDecimals = exports.HYPERLIQUID_SPOT_MAX_DECIMALS - (sz ?? 0);
|
|
287
|
+
const maxSizeDecimals = sz ?? exports.HYPERLIQUID_SPOT_MAX_DECIMALS;
|
|
288
|
+
const price = num("price", input.price);
|
|
289
|
+
if (!price.integer) {
|
|
290
|
+
if (price.significant > exports.HYPERLIQUID_PRICE_SIG_FIGS)
|
|
291
|
+
throw new errors_js_1.RouteError(`price: at most ${exports.HYPERLIQUID_PRICE_SIG_FIGS} significant figures, not ${price.wire}`);
|
|
292
|
+
if (price.decimals > maxPriceDecimals)
|
|
293
|
+
throw new errors_js_1.RouteError(`price: at most ${maxPriceDecimals} decimals, not ${price.wire}`);
|
|
294
|
+
}
|
|
295
|
+
const size = num("size", input.size);
|
|
296
|
+
if (size.decimals > maxSizeDecimals)
|
|
297
|
+
throw new errors_js_1.RouteError(`size: at most ${maxSizeDecimals} decimals, not ${size.wire}`);
|
|
298
|
+
const wire = { a: asset, b: input.isBuy, p: price.wire, s: size.wire, r: reduceOnly, t: { limit: { tif } } };
|
|
299
|
+
if (input.cloid !== undefined) {
|
|
300
|
+
if (typeof input.cloid !== "string" || !CLOID.test(input.cloid))
|
|
301
|
+
throw new errors_js_1.RouteError("cloid: a 0x-prefixed 128-bit hex string");
|
|
302
|
+
wire.c = input.cloid.toLowerCase();
|
|
303
|
+
}
|
|
304
|
+
const action = { type: "order", orders: [wire], grouping: "na" };
|
|
305
|
+
if ((input.builder === undefined) !== (input.builderFee === undefined)) {
|
|
306
|
+
throw new errors_js_1.RouteError("builder and builderFee: both, or neither (no default builder exists)");
|
|
307
|
+
}
|
|
308
|
+
if (input.builder !== undefined) {
|
|
309
|
+
const b = address("builder", input.builder);
|
|
310
|
+
const f = input.builderFee;
|
|
311
|
+
if (typeof f !== "number" || !Number.isInteger(f) || f < 0 || f > exports.HYPERLIQUID_BUILDER_FEE_MAX) {
|
|
312
|
+
throw new errors_js_1.RouteError(`builderFee: tenths of a basis point, an integer from 0 to ${exports.HYPERLIQUID_BUILDER_FEE_MAX} (1%, spot's cap)`);
|
|
313
|
+
}
|
|
314
|
+
action.builder = { b, f };
|
|
315
|
+
}
|
|
316
|
+
const nonce = input.nonce === undefined ? Date.now() : u64("nonce", input.nonce, "milliseconds since the epoch, a positive integer");
|
|
317
|
+
const vault = input.vaultAddress === undefined ? null : address("vaultAddress", input.vaultAddress);
|
|
318
|
+
const expires = input.expiresAfter === undefined ? null : u64("expiresAfter", input.expiresAfter, "milliseconds since the epoch, a positive integer");
|
|
319
|
+
const mainnet = input.mainnet ?? true;
|
|
320
|
+
if (typeof mainnet !== "boolean")
|
|
321
|
+
throw new errors_js_1.RouteError("mainnet: true or false");
|
|
322
|
+
const connectionId = hyperliquidActionHash(action, nonce, vault, expires);
|
|
323
|
+
return new HyperliquidOrder(action, nonce, connectionId, hyperliquidL1TypedData(connectionId, mainnet), vault, expires, mainnet ? exports.HYPERLIQUID_EXCHANGE_URL : exports.HYPERLIQUID_TESTNET_EXCHANGE_URL);
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* An unsigned approveBuilderFee: the user's main wallet signs `typedData`
|
|
327
|
+
* once per builder before orders carry its code. A browser wallet signs
|
|
328
|
+
* `typedDataJson()` with eth_signTypedData_v4. viem's signTypedData refuses
|
|
329
|
+
* the type name (it holds a colon): a viem key signs `digest` instead.
|
|
330
|
+
*/
|
|
331
|
+
class HyperliquidApproveBuilderFee {
|
|
332
|
+
action;
|
|
333
|
+
nonce;
|
|
334
|
+
typedData;
|
|
335
|
+
exchangeUrl;
|
|
336
|
+
constructor(action, nonce, typedData, exchangeUrl) {
|
|
337
|
+
this.action = action;
|
|
338
|
+
this.nonce = nonce;
|
|
339
|
+
this.typedData = typedData;
|
|
340
|
+
this.exchangeUrl = exchangeUrl;
|
|
341
|
+
}
|
|
342
|
+
typedDataJson() {
|
|
343
|
+
return typedJson(this.typedData);
|
|
344
|
+
}
|
|
345
|
+
/** The EIP-712 digest of typedData (see the class comment). */
|
|
346
|
+
get digest() {
|
|
347
|
+
return eip712Digest(this.typedData);
|
|
348
|
+
}
|
|
349
|
+
/** The POST /exchange body; a user-signed action takes no vault and no expiresAfter. */
|
|
350
|
+
postBody(signature) {
|
|
351
|
+
return { action: this.action, nonce: this.nonce, signature: splitSignature(signature), vaultAddress: null, expiresAfter: null };
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
exports.HyperliquidApproveBuilderFee = HyperliquidApproveBuilderFee;
|
|
355
|
+
/**
|
|
356
|
+
* The unsigned approveBuilderFee the user's main wallet signs once before
|
|
357
|
+
* orders carry `builder`'s code. maxFeeRate: a percent string ("0.001%" is
|
|
358
|
+
* f=1, the official SDK's example; at most "1%"). signatureChainId: the chain
|
|
359
|
+
* the wallet signs on (42161, 0xa4b1, for Arbitrum One, the docs' example).
|
|
360
|
+
*/
|
|
361
|
+
function hyperliquidApproveBuilderFee(input) {
|
|
362
|
+
onlyFields("hyperliquid approveBuilderFee", input, exports.HYPERLIQUID_APPROVE_FIELDS);
|
|
363
|
+
const builder = address("builder", input.builder);
|
|
364
|
+
const rate = input.maxFeeRate;
|
|
365
|
+
const m = typeof rate === "string" ? /^(\d+)(?:\.(\d+))?%$/.exec(rate) : null;
|
|
366
|
+
if (!m)
|
|
367
|
+
throw new errors_js_1.RouteError('maxFeeRate: a percent string such as "0.01%"');
|
|
368
|
+
// Compared exactly, as Python's Decimal does: above 1 is a whole part above
|
|
369
|
+
// 1, or 1 with any nonzero decimal.
|
|
370
|
+
const whole = BigInt(m[1] ?? "0");
|
|
371
|
+
if (whole > 1n || (whole === 1n && /[1-9]/.test(m[2] ?? "")))
|
|
372
|
+
throw new errors_js_1.RouteError("maxFeeRate: at most 1% (spot's cap; outcomes inherit it)");
|
|
373
|
+
const chainId = input.signatureChainId;
|
|
374
|
+
if (!Number.isSafeInteger(chainId) || chainId <= 0)
|
|
375
|
+
throw new errors_js_1.RouteError("signatureChainId: the chain id the signing wallet is on, a positive integer");
|
|
376
|
+
const mainnet = input.mainnet ?? true;
|
|
377
|
+
if (typeof mainnet !== "boolean")
|
|
378
|
+
throw new errors_js_1.RouteError("mainnet: true or false");
|
|
379
|
+
const nonce = input.nonce === undefined ? Date.now() : u64("nonce", input.nonce, "milliseconds since the epoch, a positive integer");
|
|
380
|
+
const chain = mainnet ? "Mainnet" : "Testnet";
|
|
381
|
+
return new HyperliquidApproveBuilderFee({ type: "approveBuilderFee", hyperliquidChain: chain, signatureChainId: `0x${chainId.toString(16)}`, maxFeeRate: rate, builder, nonce }, nonce, {
|
|
382
|
+
domain: { name: exports.HYPERLIQUID_SIGN_DOMAIN_NAME, version: "1", chainId, verifyingContract: "0x0000000000000000000000000000000000000000" },
|
|
383
|
+
types: { "HyperliquidTransaction:ApproveBuilderFee": exports.HYPERLIQUID_APPROVE_BUILDER_FEE_TYPE },
|
|
384
|
+
primaryType: "HyperliquidTransaction:ApproveBuilderFee",
|
|
385
|
+
message: { hyperliquidChain: chain, maxFeeRate: rate, builder, nonce: BigInt(nonce) },
|
|
386
|
+
}, mainnet ? exports.HYPERLIQUID_EXCHANGE_URL : exports.HYPERLIQUID_TESTNET_EXCHANGE_URL);
|
|
387
|
+
}
|