@nktkas/hyperliquid 0.19.1 → 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CONTRIBUTING.md +4 -2
- package/README.md +36 -35
- package/esm/_dnt.polyfills.d.ts +20 -0
- package/esm/_dnt.polyfills.d.ts.map +1 -0
- package/esm/_dnt.polyfills.js +12 -0
- package/esm/mod.d.ts +3 -0
- package/esm/mod.d.ts.map +1 -1
- package/esm/mod.js +2 -0
- package/esm/src/base.d.ts +1 -47
- package/esm/src/base.d.ts.map +1 -1
- package/esm/src/base.js +1 -8
- package/esm/src/clients/event.d.ts +6 -4
- package/esm/src/clients/event.d.ts.map +1 -1
- package/esm/src/clients/event.js +58 -77
- package/esm/src/clients/public.d.ts +26 -5
- package/esm/src/clients/public.d.ts.map +1 -1
- package/esm/src/clients/public.js +29 -41
- package/esm/src/clients/wallet.d.ts +200 -26
- package/esm/src/clients/wallet.d.ts.map +1 -1
- package/esm/src/clients/wallet.js +306 -284
- package/esm/src/signing.d.ts +80 -5
- package/esm/src/signing.d.ts.map +1 -1
- package/esm/src/signing.js +96 -7
- package/esm/src/transports/base.d.ts +49 -0
- package/esm/src/transports/base.d.ts.map +1 -0
- package/esm/src/transports/base.js +8 -0
- package/esm/src/transports/http/http_transport.d.ts +8 -5
- package/esm/src/transports/http/http_transport.d.ts.map +1 -1
- package/esm/src/transports/http/http_transport.js +15 -62
- package/esm/src/transports/websocket/_hyperliquid_event_target.d.ts +36 -39
- package/esm/src/transports/websocket/_hyperliquid_event_target.d.ts.map +1 -1
- package/esm/src/transports/websocket/_reconnecting_websocket.d.ts +18 -25
- package/esm/src/transports/websocket/_reconnecting_websocket.d.ts.map +1 -1
- package/esm/src/transports/websocket/_reconnecting_websocket.js +80 -179
- package/{script/src/transports/websocket/_websocket_request_dispatcher.d.ts → esm/src/transports/websocket/_websocket_async_request.d.ts} +14 -18
- package/esm/src/transports/websocket/_websocket_async_request.d.ts.map +1 -0
- package/esm/src/transports/websocket/{_websocket_request_dispatcher.js → _websocket_async_request.js} +42 -75
- package/esm/src/transports/websocket/websocket_transport.d.ts +38 -28
- package/esm/src/transports/websocket/websocket_transport.d.ts.map +1 -1
- package/esm/src/transports/websocket/websocket_transport.js +61 -76
- package/esm/src/types/exchange/requests.d.ts +278 -66
- package/esm/src/types/exchange/requests.d.ts.map +1 -1
- package/esm/src/types/info/assets.d.ts +0 -48
- package/esm/src/types/info/assets.d.ts.map +1 -1
- package/esm/src/types/info/markets.d.ts +52 -0
- package/esm/src/types/info/markets.d.ts.map +1 -0
- package/esm/src/types/info/markets.js +1 -0
- package/esm/src/types/info/orders.d.ts +1 -1
- package/esm/src/types/info/orders.d.ts.map +1 -1
- package/esm/src/types/info/requests.d.ts +14 -5
- package/esm/src/types/info/requests.d.ts.map +1 -1
- package/esm/src/types/mod.d.ts +4 -0
- package/esm/src/types/mod.d.ts.map +1 -1
- package/esm/src/types/mod.js +3 -1
- package/esm/src/types/subscriptions/requests.d.ts +2 -0
- package/esm/src/types/subscriptions/requests.d.ts.map +1 -1
- package/package.json +12 -9
- package/script/_dnt.polyfills.d.ts +20 -0
- package/script/_dnt.polyfills.d.ts.map +1 -0
- package/script/_dnt.polyfills.js +23 -0
- package/script/mod.d.ts +3 -0
- package/script/mod.d.ts.map +1 -1
- package/script/mod.js +3 -1
- package/script/src/base.d.ts +1 -47
- package/script/src/base.d.ts.map +1 -1
- package/script/src/base.js +2 -10
- package/script/src/clients/event.d.ts +6 -4
- package/script/src/clients/event.d.ts.map +1 -1
- package/script/src/clients/event.js +58 -77
- package/script/src/clients/public.d.ts +26 -5
- package/script/src/clients/public.d.ts.map +1 -1
- package/script/src/clients/public.js +29 -41
- package/script/src/clients/wallet.d.ts +200 -26
- package/script/src/clients/wallet.d.ts.map +1 -1
- package/script/src/clients/wallet.js +305 -283
- package/script/src/signing.d.ts +80 -5
- package/script/src/signing.d.ts.map +1 -1
- package/script/src/signing.js +148 -58
- package/script/src/transports/base.d.ts +49 -0
- package/script/src/transports/base.d.ts.map +1 -0
- package/script/src/transports/base.js +22 -0
- package/script/src/transports/http/http_transport.d.ts +8 -5
- package/script/src/transports/http/http_transport.d.ts.map +1 -1
- package/script/src/transports/http/http_transport.js +16 -63
- package/script/src/transports/websocket/_hyperliquid_event_target.d.ts +36 -39
- package/script/src/transports/websocket/_hyperliquid_event_target.d.ts.map +1 -1
- package/script/src/transports/websocket/_reconnecting_websocket.d.ts +18 -25
- package/script/src/transports/websocket/_reconnecting_websocket.d.ts.map +1 -1
- package/script/src/transports/websocket/_reconnecting_websocket.js +81 -180
- package/{esm/src/transports/websocket/_websocket_request_dispatcher.d.ts → script/src/transports/websocket/_websocket_async_request.d.ts} +14 -18
- package/script/src/transports/websocket/_websocket_async_request.d.ts.map +1 -0
- package/script/src/transports/websocket/{_websocket_request_dispatcher.js → _websocket_async_request.js} +45 -78
- package/script/src/transports/websocket/websocket_transport.d.ts +38 -28
- package/script/src/transports/websocket/websocket_transport.d.ts.map +1 -1
- package/script/src/transports/websocket/websocket_transport.js +63 -78
- package/script/src/types/exchange/requests.d.ts +278 -66
- package/script/src/types/exchange/requests.d.ts.map +1 -1
- package/script/src/types/info/assets.d.ts +0 -48
- package/script/src/types/info/assets.d.ts.map +1 -1
- package/script/src/types/info/markets.d.ts +52 -0
- package/script/src/types/info/markets.d.ts.map +1 -0
- package/script/{deps/jsr.io/@noble/hashes/1.8.0/src/crypto.js → src/types/info/markets.js} +0 -2
- package/script/src/types/info/orders.d.ts +1 -1
- package/script/src/types/info/orders.d.ts.map +1 -1
- package/script/src/types/info/requests.d.ts +14 -5
- package/script/src/types/info/requests.d.ts.map +1 -1
- package/script/src/types/mod.d.ts +4 -0
- package/script/src/types/mod.d.ts.map +1 -1
- package/script/src/types/mod.js +25 -22
- package/script/src/types/subscriptions/requests.d.ts +2 -0
- package/script/src/types/subscriptions/requests.d.ts.map +1 -1
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/_u64.d.ts +0 -55
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/_u64.d.ts.map +0 -1
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/_u64.js +0 -66
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/crypto.d.ts +0 -2
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/crypto.d.ts.map +0 -1
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/crypto.js +0 -1
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/sha3.d.ts +0 -53
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/sha3.d.ts.map +0 -1
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/sha3.js +0 -294
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/utils.d.ts +0 -161
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/utils.d.ts.map +0 -1
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/utils.js +0 -280
- package/esm/src/transports/websocket/_websocket_request_dispatcher.d.ts.map +0 -1
- package/script/deps/jsr.io/@noble/hashes/1.8.0/src/_u64.d.ts +0 -55
- package/script/deps/jsr.io/@noble/hashes/1.8.0/src/_u64.d.ts.map +0 -1
- package/script/deps/jsr.io/@noble/hashes/1.8.0/src/_u64.js +0 -99
- package/script/deps/jsr.io/@noble/hashes/1.8.0/src/crypto.d.ts +0 -2
- package/script/deps/jsr.io/@noble/hashes/1.8.0/src/crypto.d.ts.map +0 -1
- package/script/deps/jsr.io/@noble/hashes/1.8.0/src/sha3.d.ts +0 -53
- package/script/deps/jsr.io/@noble/hashes/1.8.0/src/sha3.d.ts.map +0 -1
- package/script/deps/jsr.io/@noble/hashes/1.8.0/src/sha3.js +0 -309
- package/script/deps/jsr.io/@noble/hashes/1.8.0/src/utils.d.ts +0 -161
- package/script/deps/jsr.io/@noble/hashes/1.8.0/src/utils.d.ts.map +0 -1
- package/script/deps/jsr.io/@noble/hashes/1.8.0/src/utils.js +0 -322
- package/script/src/transports/websocket/_websocket_request_dispatcher.d.ts.map +0 -1
package/esm/src/signing.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* This module contains functions for generating Hyperliquid transaction signatures
|
|
3
3
|
* and interfaces to various wallet implementations.
|
|
4
|
+
*
|
|
4
5
|
* @example
|
|
5
6
|
* ```ts
|
|
6
7
|
* import { signL1Action } from "@nktkas/hyperliquid/signing";
|
|
@@ -45,12 +46,16 @@
|
|
|
45
46
|
* chainId: parseInt(action.signatureChainId, 16),
|
|
46
47
|
* });
|
|
47
48
|
* ```
|
|
49
|
+
*
|
|
48
50
|
* @module
|
|
49
51
|
*/
|
|
52
|
+
import "../_dnt.polyfills.js";
|
|
50
53
|
import { type ValueMap, type ValueType } from "../deps/jsr.io/@std/msgpack/1.0.3/encode.js";
|
|
51
54
|
import type { Hex } from "./base.js";
|
|
52
55
|
export type { Hex };
|
|
53
56
|
export type { ValueMap, ValueType };
|
|
57
|
+
/** Abstract interface for a wallet that can sign typed data. */
|
|
58
|
+
export type AbstractWallet = AbstractViemWalletClient | AbstractEthersSigner | AbstractEthersV5Signer | AbstractExtendedViemWalletClient | AbstractWindowEthereum;
|
|
54
59
|
/** Abstract interface for a [viem wallet](https://viem.sh/docs/clients/wallet). */
|
|
55
60
|
export interface AbstractViemWalletClient {
|
|
56
61
|
signTypedData(params: {
|
|
@@ -98,7 +103,7 @@ export interface AbstractEthersV5Signer {
|
|
|
98
103
|
}[];
|
|
99
104
|
}, value: Record<string, unknown>): Promise<string>;
|
|
100
105
|
}
|
|
101
|
-
/** Abstract interface for an extended [viem wallet](https://viem.sh/docs/clients/wallet) (e.g. privy [useSignTypedData](https://docs.privy.io/
|
|
106
|
+
/** Abstract interface for an extended [viem wallet](https://viem.sh/docs/clients/wallet) (e.g. privy [useSignTypedData](https://docs.privy.io/wallets/using-wallets/ethereum/sign-typed-data)). */
|
|
102
107
|
export interface AbstractExtendedViemWalletClient {
|
|
103
108
|
signTypedData(params: {
|
|
104
109
|
domain: {
|
|
@@ -146,7 +151,7 @@ export declare function createL1ActionHash(action: ValueType, nonce: number, vau
|
|
|
146
151
|
* import { signL1Action } from "@nktkas/hyperliquid/signing";
|
|
147
152
|
* import { privateKeyToAccount } from "viem/accounts";
|
|
148
153
|
*
|
|
149
|
-
* const wallet = privateKeyToAccount("0x..."); //
|
|
154
|
+
* const wallet = privateKeyToAccount("0x..."); // Your private key
|
|
150
155
|
*
|
|
151
156
|
* const action = {
|
|
152
157
|
* type: "cancel",
|
|
@@ -173,7 +178,7 @@ export declare function createL1ActionHash(action: ValueType, nonce: number, vau
|
|
|
173
178
|
*/
|
|
174
179
|
export declare function signL1Action(args: {
|
|
175
180
|
/** Wallet to sign the action. */
|
|
176
|
-
wallet:
|
|
181
|
+
wallet: AbstractWallet;
|
|
177
182
|
/** The action to be signed. */
|
|
178
183
|
action: ValueType;
|
|
179
184
|
/** Unique request identifier (recommended current timestamp in ms). */
|
|
@@ -193,6 +198,7 @@ export declare function signL1Action(args: {
|
|
|
193
198
|
* Sign a user-signed action.
|
|
194
199
|
*
|
|
195
200
|
* Note: Signature generation depends on the order of types.
|
|
201
|
+
*
|
|
196
202
|
* @param args - Arguments for signing the action.
|
|
197
203
|
* @returns The signature components r, s, and v.
|
|
198
204
|
* @example
|
|
@@ -200,7 +206,7 @@ export declare function signL1Action(args: {
|
|
|
200
206
|
* import { signUserSignedAction } from "@nktkas/hyperliquid/signing";
|
|
201
207
|
* import { privateKeyToAccount } from "viem/accounts";
|
|
202
208
|
*
|
|
203
|
-
* const wallet = privateKeyToAccount("0x..."); //
|
|
209
|
+
* const wallet = privateKeyToAccount("0x..."); // Your private key
|
|
204
210
|
*
|
|
205
211
|
* const action = {
|
|
206
212
|
* type: "approveAgent",
|
|
@@ -235,7 +241,7 @@ export declare function signL1Action(args: {
|
|
|
235
241
|
*/
|
|
236
242
|
export declare function signUserSignedAction(args: {
|
|
237
243
|
/** Wallet to sign the action. */
|
|
238
|
-
wallet:
|
|
244
|
+
wallet: AbstractWallet;
|
|
239
245
|
/** The action to be signed. */
|
|
240
246
|
action: Record<string, unknown>;
|
|
241
247
|
/** The types of the action. */
|
|
@@ -252,6 +258,75 @@ export declare function signUserSignedAction(args: {
|
|
|
252
258
|
s: Hex;
|
|
253
259
|
v: number;
|
|
254
260
|
}>;
|
|
261
|
+
/**
|
|
262
|
+
* Sign a multi-signature action.
|
|
263
|
+
*
|
|
264
|
+
* Note: Signature generation depends on the order of the action keys.
|
|
265
|
+
*
|
|
266
|
+
* @param args - Arguments for signing the action.
|
|
267
|
+
* @returns The signature components r, s, and v.
|
|
268
|
+
* @example
|
|
269
|
+
* ```ts
|
|
270
|
+
* import { signL1Action, signMultiSigAction } from "@nktkas/hyperliquid/signing";
|
|
271
|
+
* import { privateKeyToAccount } from "viem/accounts";
|
|
272
|
+
*
|
|
273
|
+
* const wallet = privateKeyToAccount("0x..."); // Your private key
|
|
274
|
+
* const multiSigUser = "0x..."; // Multi-sig user address
|
|
275
|
+
*
|
|
276
|
+
* const nonce = Date.now();
|
|
277
|
+
* const action = { // Example action
|
|
278
|
+
* type: "scheduleCancel",
|
|
279
|
+
* time: Date.now() + 10000
|
|
280
|
+
* };
|
|
281
|
+
*
|
|
282
|
+
* // First, create signature from one of the authorized signers
|
|
283
|
+
* const signature = await signL1Action({
|
|
284
|
+
* wallet,
|
|
285
|
+
* action: [multiSigUser.toLowerCase(), wallet.address.toLowerCase(), action],
|
|
286
|
+
* nonce,
|
|
287
|
+
* isTestnet: true,
|
|
288
|
+
* });
|
|
289
|
+
*
|
|
290
|
+
* // Then use it in the multi-sig action
|
|
291
|
+
* const multiSigSignature = await signMultiSigAction({
|
|
292
|
+
* wallet,
|
|
293
|
+
* action: {
|
|
294
|
+
* type: "multiSig",
|
|
295
|
+
* signatureChainId: "0x66eee",
|
|
296
|
+
* signatures: [signature],
|
|
297
|
+
* payload: {
|
|
298
|
+
* multiSigUser,
|
|
299
|
+
* outerSigner: wallet.address,
|
|
300
|
+
* action,
|
|
301
|
+
* }
|
|
302
|
+
* },
|
|
303
|
+
* nonce,
|
|
304
|
+
* hyperliquidChain: "Testnet",
|
|
305
|
+
* signatureChainId: "0x66eee",
|
|
306
|
+
* });
|
|
307
|
+
* ```
|
|
308
|
+
* @unstable May not behave as expected and the interface may change in the future.
|
|
309
|
+
*/
|
|
310
|
+
export declare function signMultiSigAction(args: {
|
|
311
|
+
/** Wallet to sign the action. */
|
|
312
|
+
wallet: AbstractWallet;
|
|
313
|
+
/** The action to be signed. */
|
|
314
|
+
action: ValueMap;
|
|
315
|
+
/** Unique request identifier (recommended current timestamp in ms). */
|
|
316
|
+
nonce: number;
|
|
317
|
+
/** Optional vault address used in the action. */
|
|
318
|
+
vaultAddress?: Hex;
|
|
319
|
+
/** Optional expiration time of the action in milliseconds since the epoch. */
|
|
320
|
+
expiresAfter?: number;
|
|
321
|
+
/** HyperLiquid network ("Mainnet" or "Testnet"). */
|
|
322
|
+
hyperliquidChain: "Mainnet" | "Testnet";
|
|
323
|
+
/** Chain ID used for signing. */
|
|
324
|
+
signatureChainId: Hex;
|
|
325
|
+
}): Promise<{
|
|
326
|
+
r: Hex;
|
|
327
|
+
s: Hex;
|
|
328
|
+
v: number;
|
|
329
|
+
}>;
|
|
255
330
|
/** Checks if the given value is an abstract viem wallet. */
|
|
256
331
|
export declare function isAbstractViemWalletClient(client: unknown): client is AbstractViemWalletClient;
|
|
257
332
|
/** Checks if the given value is an abstract ethers signer. */
|
package/esm/src/signing.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"signing.d.ts","sourceRoot":"","sources":["../../src/src/signing.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"signing.d.ts","sourceRoot":"","sources":["../../src/src/signing.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkDG;AACH,OAAO,sBAAsB,CAAC;AAI9B,OAAO,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;GAgDG;AACH,wBAAsB,kBAAkB,CAAC,IAAI,EAAE;IAC3C,iCAAiC;IACjC,MAAM,EAAE,cAAc,CAAC;IACvB,+BAA+B;IAC/B,MAAM,EAAE,QAAQ,CAAC;IACjB,uEAAuE;IACvE,KAAK,EAAE,MAAM,CAAC;IACd,iDAAiD;IACjD,YAAY,CAAC,EAAE,GAAG,CAAC;IACnB,8EAA8E;IAC9E,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,oDAAoD;IACpD,gBAAgB,EAAE,SAAS,GAAG,SAAS,CAAC;IACxC,iCAAiC;IACjC,gBAAgB,EAAE,GAAG,CAAC;CACzB,GAAG,OAAO,CAAC;IAAE,CAAC,EAAE,GAAG,CAAC;IAAC,CAAC,EAAE,GAAG,CAAC;IAAC,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC,CA+BzC;AAoGD,4DAA4D;AAC5D,wBAAgB,0BAA0B,CAAC,MAAM,EAAE,OAAO,GAAG,MAAM,IAAI,wBAAwB,CAI9F;AAED,8DAA8D;AAC9D,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,OAAO,GAAG,MAAM,IAAI,oBAAoB,CAItF;AAED,iEAAiE;AACjE,wBAAgB,wBAAwB,CAAC,MAAM,EAAE,OAAO,GAAG,MAAM,IAAI,sBAAsB,CAI1F;AAED,qGAAqG;AACrG,wBAAgB,kCAAkC,CAAC,MAAM,EAAE,OAAO,GAAG,MAAM,IAAI,wBAAwB,CAItG;AAED,yEAAyE;AACzE,wBAAgB,wBAAwB,CAAC,MAAM,EAAE,OAAO,GAAG,MAAM,IAAI,sBAAsB,CAI1F"}
|
package/esm/src/signing.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* This module contains functions for generating Hyperliquid transaction signatures
|
|
3
3
|
* and interfaces to various wallet implementations.
|
|
4
|
+
*
|
|
4
5
|
* @example
|
|
5
6
|
* ```ts
|
|
6
7
|
* import { signL1Action } from "@nktkas/hyperliquid/signing";
|
|
@@ -45,9 +46,11 @@
|
|
|
45
46
|
* chainId: parseInt(action.signatureChainId, 16),
|
|
46
47
|
* });
|
|
47
48
|
* ```
|
|
49
|
+
*
|
|
48
50
|
* @module
|
|
49
51
|
*/
|
|
50
|
-
import
|
|
52
|
+
import "../_dnt.polyfills.js";
|
|
53
|
+
import { keccak_256 } from "@noble/hashes/sha3";
|
|
51
54
|
import { encode as encodeMsgpack } from "../deps/jsr.io/@std/msgpack/1.0.3/encode.js";
|
|
52
55
|
import { decodeHex, encodeHex } from "../deps/jsr.io/@std/encoding/1.0.10/hex.js";
|
|
53
56
|
import { concat } from "../deps/jsr.io/@std/bytes/1.0.6/concat.js";
|
|
@@ -68,14 +71,28 @@ export function createL1ActionHash(action, nonce, vaultAddress, expiresAfter) {
|
|
|
68
71
|
const nonceBytes = new Uint8Array(8);
|
|
69
72
|
new DataView(nonceBytes.buffer).setBigUint64(0, BigInt(nonce));
|
|
70
73
|
// 3. Vault address
|
|
71
|
-
|
|
72
|
-
|
|
74
|
+
let vaultMarker;
|
|
75
|
+
let vaultBytes;
|
|
76
|
+
if (vaultAddress) {
|
|
77
|
+
vaultMarker = Uint8Array.of(1);
|
|
78
|
+
vaultBytes = decodeHex(vaultAddress.slice(2));
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
vaultMarker = new Uint8Array(1);
|
|
82
|
+
vaultBytes = new Uint8Array();
|
|
83
|
+
}
|
|
73
84
|
// 4. Expires after
|
|
74
|
-
|
|
75
|
-
|
|
85
|
+
let expiresMarker;
|
|
86
|
+
let expiresBytes;
|
|
76
87
|
if (expiresAfter !== undefined) {
|
|
88
|
+
expiresMarker = new Uint8Array(1);
|
|
89
|
+
expiresBytes = new Uint8Array(8);
|
|
77
90
|
new DataView(expiresBytes.buffer).setBigUint64(0, BigInt(expiresAfter));
|
|
78
91
|
}
|
|
92
|
+
else {
|
|
93
|
+
expiresMarker = new Uint8Array();
|
|
94
|
+
expiresBytes = new Uint8Array();
|
|
95
|
+
}
|
|
79
96
|
// Create a keccak256 hash
|
|
80
97
|
const chunks = [
|
|
81
98
|
actionBytes,
|
|
@@ -117,7 +134,7 @@ function normalizeIntegersForMsgPack(obj) {
|
|
|
117
134
|
* import { signL1Action } from "@nktkas/hyperliquid/signing";
|
|
118
135
|
* import { privateKeyToAccount } from "viem/accounts";
|
|
119
136
|
*
|
|
120
|
-
* const wallet = privateKeyToAccount("0x..."); //
|
|
137
|
+
* const wallet = privateKeyToAccount("0x..."); // Your private key
|
|
121
138
|
*
|
|
122
139
|
* const action = {
|
|
123
140
|
* type: "cancel",
|
|
@@ -168,6 +185,7 @@ export async function signL1Action(args) {
|
|
|
168
185
|
* Sign a user-signed action.
|
|
169
186
|
*
|
|
170
187
|
* Note: Signature generation depends on the order of types.
|
|
188
|
+
*
|
|
171
189
|
* @param args - Arguments for signing the action.
|
|
172
190
|
* @returns The signature components r, s, and v.
|
|
173
191
|
* @example
|
|
@@ -175,7 +193,7 @@ export async function signL1Action(args) {
|
|
|
175
193
|
* import { signUserSignedAction } from "@nktkas/hyperliquid/signing";
|
|
176
194
|
* import { privateKeyToAccount } from "viem/accounts";
|
|
177
195
|
*
|
|
178
|
-
* const wallet = privateKeyToAccount("0x..."); //
|
|
196
|
+
* const wallet = privateKeyToAccount("0x..."); // Your private key
|
|
179
197
|
*
|
|
180
198
|
* const action = {
|
|
181
199
|
* type: "approveAgent",
|
|
@@ -219,6 +237,77 @@ export async function signUserSignedAction(args) {
|
|
|
219
237
|
const signature = await abstractSignTypedData({ wallet, domain, types, message: action });
|
|
220
238
|
return splitSignature(signature);
|
|
221
239
|
}
|
|
240
|
+
/**
|
|
241
|
+
* Sign a multi-signature action.
|
|
242
|
+
*
|
|
243
|
+
* Note: Signature generation depends on the order of the action keys.
|
|
244
|
+
*
|
|
245
|
+
* @param args - Arguments for signing the action.
|
|
246
|
+
* @returns The signature components r, s, and v.
|
|
247
|
+
* @example
|
|
248
|
+
* ```ts
|
|
249
|
+
* import { signL1Action, signMultiSigAction } from "@nktkas/hyperliquid/signing";
|
|
250
|
+
* import { privateKeyToAccount } from "viem/accounts";
|
|
251
|
+
*
|
|
252
|
+
* const wallet = privateKeyToAccount("0x..."); // Your private key
|
|
253
|
+
* const multiSigUser = "0x..."; // Multi-sig user address
|
|
254
|
+
*
|
|
255
|
+
* const nonce = Date.now();
|
|
256
|
+
* const action = { // Example action
|
|
257
|
+
* type: "scheduleCancel",
|
|
258
|
+
* time: Date.now() + 10000
|
|
259
|
+
* };
|
|
260
|
+
*
|
|
261
|
+
* // First, create signature from one of the authorized signers
|
|
262
|
+
* const signature = await signL1Action({
|
|
263
|
+
* wallet,
|
|
264
|
+
* action: [multiSigUser.toLowerCase(), wallet.address.toLowerCase(), action],
|
|
265
|
+
* nonce,
|
|
266
|
+
* isTestnet: true,
|
|
267
|
+
* });
|
|
268
|
+
*
|
|
269
|
+
* // Then use it in the multi-sig action
|
|
270
|
+
* const multiSigSignature = await signMultiSigAction({
|
|
271
|
+
* wallet,
|
|
272
|
+
* action: {
|
|
273
|
+
* type: "multiSig",
|
|
274
|
+
* signatureChainId: "0x66eee",
|
|
275
|
+
* signatures: [signature],
|
|
276
|
+
* payload: {
|
|
277
|
+
* multiSigUser,
|
|
278
|
+
* outerSigner: wallet.address,
|
|
279
|
+
* action,
|
|
280
|
+
* }
|
|
281
|
+
* },
|
|
282
|
+
* nonce,
|
|
283
|
+
* hyperliquidChain: "Testnet",
|
|
284
|
+
* signatureChainId: "0x66eee",
|
|
285
|
+
* });
|
|
286
|
+
* ```
|
|
287
|
+
* @unstable May not behave as expected and the interface may change in the future.
|
|
288
|
+
*/
|
|
289
|
+
export async function signMultiSigAction(args) {
|
|
290
|
+
const { wallet, action, nonce, hyperliquidChain, signatureChainId, vaultAddress, expiresAfter, } = args;
|
|
291
|
+
const multiSigActionHash = createL1ActionHash(action, nonce, vaultAddress, expiresAfter);
|
|
292
|
+
const message = {
|
|
293
|
+
multiSigActionHash,
|
|
294
|
+
hyperliquidChain,
|
|
295
|
+
signatureChainId,
|
|
296
|
+
nonce,
|
|
297
|
+
};
|
|
298
|
+
return await signUserSignedAction({
|
|
299
|
+
wallet,
|
|
300
|
+
action: message,
|
|
301
|
+
types: {
|
|
302
|
+
"HyperliquidTransaction:SendMultiSig": [
|
|
303
|
+
{ name: "hyperliquidChain", type: "string" },
|
|
304
|
+
{ name: "multiSigActionHash", type: "bytes32" },
|
|
305
|
+
{ name: "nonce", type: "uint64" },
|
|
306
|
+
],
|
|
307
|
+
},
|
|
308
|
+
chainId: parseInt(signatureChainId, 16),
|
|
309
|
+
});
|
|
310
|
+
}
|
|
222
311
|
/** Signs typed data with the provided wallet using EIP-712. */
|
|
223
312
|
async function abstractSignTypedData(args) {
|
|
224
313
|
const { wallet, domain, types, message } = args;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { HyperliquidError } from "../base.js";
|
|
2
|
+
/**
|
|
3
|
+
* Interface representing a REST transport.
|
|
4
|
+
*
|
|
5
|
+
* Handles communication with Hyperliquid API endpoints.
|
|
6
|
+
*
|
|
7
|
+
* @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint | Info endpoint}
|
|
8
|
+
* @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint | Exchange endpoint}
|
|
9
|
+
* @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/post-requests | Websocket post requests}
|
|
10
|
+
*/
|
|
11
|
+
export interface IRequestTransport extends Partial<AsyncDisposable> {
|
|
12
|
+
/**
|
|
13
|
+
* Sends a request to the Hyperliquid API.
|
|
14
|
+
* @param endpoint - The API endpoint to send the request to.
|
|
15
|
+
* @param payload - The payload to send with the request.
|
|
16
|
+
* @param signal - An ptional abort signal.
|
|
17
|
+
* @returns A promise that resolves with parsed JSON response body.
|
|
18
|
+
*/
|
|
19
|
+
request<T>(endpoint: "info" | "exchange" | "explorer", payload: unknown, signal?: AbortSignal): Promise<T>;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Interface representing an event subscription transport.
|
|
23
|
+
* Handles WebSocket subscriptions for real-time updates.
|
|
24
|
+
* @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/subscriptions | Websocket subscriptions}
|
|
25
|
+
*/
|
|
26
|
+
export interface ISubscriptionTransport extends Partial<AsyncDisposable> {
|
|
27
|
+
/**
|
|
28
|
+
* Subscribes to a Hyperliquid event channel.
|
|
29
|
+
* @param channel - The event channel to listen to.
|
|
30
|
+
* @param payload - The payload to send with the subscription request.
|
|
31
|
+
* @param listener - The function to call when the event is dispatched.
|
|
32
|
+
* @param signal - An optional abort signal for canceling the subscription request.
|
|
33
|
+
* @returns A promise that resolves with a {@link Subscription} object to manage the subscription lifecycle.
|
|
34
|
+
*/
|
|
35
|
+
subscribe<T>(channel: string, payload: unknown, listener: (data: CustomEvent<T>) => void, signal?: AbortSignal): Promise<Subscription>;
|
|
36
|
+
}
|
|
37
|
+
/** Controls event subscription lifecycle. */
|
|
38
|
+
export interface Subscription {
|
|
39
|
+
/**
|
|
40
|
+
* Unsubscribes from the event and sends an unsubscribe request to the server.
|
|
41
|
+
* @param signal - An optional abort signal for canceling the unsubscribe request.
|
|
42
|
+
*/
|
|
43
|
+
unsubscribe(signal?: AbortSignal): Promise<void>;
|
|
44
|
+
}
|
|
45
|
+
/** Base class for all transport-related errors. */
|
|
46
|
+
export declare class TransportError extends HyperliquidError {
|
|
47
|
+
constructor(message?: string);
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=base.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../../src/src/transports/base.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAE9C;;;;;;;;GAQG;AACH,MAAM,WAAW,iBAAkB,SAAQ,OAAO,CAAC,eAAe,CAAC;IAC/D;;;;;;OAMG;IACH,OAAO,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,GAAG,UAAU,GAAG,UAAU,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;CAC9G;AAED;;;;GAIG;AACH,MAAM,WAAW,sBAAuB,SAAQ,OAAO,CAAC,eAAe,CAAC;IACpE;;;;;;;OAOG;IACH,SAAS,CAAC,CAAC,EACP,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,CAAC,IAAI,EAAE,WAAW,CAAC,CAAC,CAAC,KAAK,IAAI,EACxC,MAAM,CAAC,EAAE,WAAW,GACrB,OAAO,CAAC,YAAY,CAAC,CAAC;CAC5B;AAED,6CAA6C;AAC7C,MAAM,WAAW,YAAY;IACzB;;;OAGG;IACH,WAAW,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACpD;AAED,mDAAmD;AACnD,qBAAa,cAAe,SAAQ,gBAAgB;gBACpC,OAAO,CAAC,EAAE,MAAM;CAI/B"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import type { MaybePromise } from "../../base.js";
|
|
2
|
+
import { type IRequestTransport, TransportError } from "../base.js";
|
|
2
3
|
/**
|
|
3
4
|
* Error thrown when an HTTP response is deemed invalid:
|
|
4
5
|
* - Non-200 status code
|
|
@@ -22,12 +23,14 @@ export interface HttpTransportOptions {
|
|
|
22
23
|
*/
|
|
23
24
|
isTestnet?: boolean;
|
|
24
25
|
/**
|
|
25
|
-
* Request timeout in ms.
|
|
26
|
-
* Set to `null` to disable.
|
|
26
|
+
* Request timeout in ms. Set to `null` to disable.
|
|
27
27
|
* @defaultValue `10_000`
|
|
28
28
|
*/
|
|
29
29
|
timeout?: number | null;
|
|
30
|
-
/**
|
|
30
|
+
/**
|
|
31
|
+
* Custom server to use for API requests.
|
|
32
|
+
* @defaultValue `https://api.hyperliquid.xyz` for mainnet and `https://api.hyperliquid-testnet.xyz` for testnet.
|
|
33
|
+
*/
|
|
31
34
|
server?: {
|
|
32
35
|
mainnet?: {
|
|
33
36
|
api?: string | URL;
|
|
@@ -84,6 +87,6 @@ export declare class HttpTransport implements IRequestTransport, HttpTransportOp
|
|
|
84
87
|
* @throws {HttpRequestError} - Thrown when an HTTP response is deemed invalid.
|
|
85
88
|
* @throws May throw {@link https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch#exceptions | fetch errors}.
|
|
86
89
|
*/
|
|
87
|
-
request(endpoint: "info" | "exchange" | "explorer", payload: unknown, signal?: AbortSignal): Promise<
|
|
90
|
+
request<T>(endpoint: "info" | "exchange" | "explorer", payload: unknown, signal?: AbortSignal): Promise<T>;
|
|
88
91
|
}
|
|
89
92
|
//# sourceMappingURL=http_transport.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http_transport.d.ts","sourceRoot":"","sources":["../../../../src/src/transports/http/http_transport.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"http_transport.d.ts","sourceRoot":"","sources":["../../../../src/src/transports/http/http_transport.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAClD,OAAO,EAAE,KAAK,iBAAiB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEpE;;;;GAIG;AACH,qBAAa,gBAAiB,SAAQ,cAAc;IAM7B,QAAQ,EAAE,QAAQ;IAAS,YAAY,CAAC,EAAE,MAAM;IALnE;;;;OAIG;gBACgB,QAAQ,EAAE,QAAQ,EAAS,YAAY,CAAC,EAAE,MAAM,YAAA;CAOtE;AAED,0DAA0D;AAC1D,MAAM,WAAW,oBAAoB;IACjC;;;OAGG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;IAEpB;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAA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G,IAAI,GAAG,IAAI,GAAG,SAAS,CAAC,CAAC;IAClF,UAAU,CAAC,EAAE,CAAC,QAAQ,EAAE,QAAQ,KAAK,YAAY,CAAC,QAAQ,GAAG,IAAI,GAAG,IAAI,GAAG,SAAS,CAAC,CAAC;IAEtF;;;OAGG;gBACS,OAAO,CAAC,EAAE,oBAAoB;IAkB1C;;;;;;;;OAQG;IACG,OAAO,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,GAAG,UAAU,GAAG,UAAU,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,CAAC,CAAC;CAuDnH"}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import { TransportError } from "
|
|
1
|
+
import { TransportError } from "../base.js";
|
|
2
2
|
/**
|
|
3
3
|
* Error thrown when an HTTP response is deemed invalid:
|
|
4
4
|
* - Non-200 status code
|
|
5
5
|
* - Unexpected content type
|
|
6
6
|
*/
|
|
7
7
|
export class HttpRequestError extends TransportError {
|
|
8
|
+
response;
|
|
9
|
+
responseBody;
|
|
8
10
|
/**
|
|
9
11
|
* Creates a new HTTP request error.
|
|
10
12
|
* @param response - The failed HTTP response.
|
|
@@ -15,64 +17,24 @@ export class HttpRequestError extends TransportError {
|
|
|
15
17
|
if (responseBody)
|
|
16
18
|
message += `, body "${responseBody}"`;
|
|
17
19
|
super(message);
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
configurable: true,
|
|
21
|
-
writable: true,
|
|
22
|
-
value: response
|
|
23
|
-
});
|
|
24
|
-
Object.defineProperty(this, "responseBody", {
|
|
25
|
-
enumerable: true,
|
|
26
|
-
configurable: true,
|
|
27
|
-
writable: true,
|
|
28
|
-
value: responseBody
|
|
29
|
-
});
|
|
20
|
+
this.response = response;
|
|
21
|
+
this.responseBody = responseBody;
|
|
30
22
|
this.name = "HttpRequestError";
|
|
31
23
|
}
|
|
32
24
|
}
|
|
33
25
|
/** HTTP implementation of the REST transport interface. */
|
|
34
26
|
export class HttpTransport {
|
|
27
|
+
isTestnet;
|
|
28
|
+
timeout;
|
|
29
|
+
server;
|
|
30
|
+
fetchOptions;
|
|
31
|
+
onRequest;
|
|
32
|
+
onResponse;
|
|
35
33
|
/**
|
|
36
34
|
* Creates a new HTTP transport instance.
|
|
37
35
|
* @param options - Configuration options for the HTTP transport layer.
|
|
38
36
|
*/
|
|
39
37
|
constructor(options) {
|
|
40
|
-
Object.defineProperty(this, "isTestnet", {
|
|
41
|
-
enumerable: true,
|
|
42
|
-
configurable: true,
|
|
43
|
-
writable: true,
|
|
44
|
-
value: void 0
|
|
45
|
-
});
|
|
46
|
-
Object.defineProperty(this, "timeout", {
|
|
47
|
-
enumerable: true,
|
|
48
|
-
configurable: true,
|
|
49
|
-
writable: true,
|
|
50
|
-
value: void 0
|
|
51
|
-
});
|
|
52
|
-
Object.defineProperty(this, "server", {
|
|
53
|
-
enumerable: true,
|
|
54
|
-
configurable: true,
|
|
55
|
-
writable: true,
|
|
56
|
-
value: void 0
|
|
57
|
-
});
|
|
58
|
-
Object.defineProperty(this, "fetchOptions", {
|
|
59
|
-
enumerable: true,
|
|
60
|
-
configurable: true,
|
|
61
|
-
writable: true,
|
|
62
|
-
value: void 0
|
|
63
|
-
});
|
|
64
|
-
Object.defineProperty(this, "onRequest", {
|
|
65
|
-
enumerable: true,
|
|
66
|
-
configurable: true,
|
|
67
|
-
writable: true,
|
|
68
|
-
value: void 0
|
|
69
|
-
});
|
|
70
|
-
Object.defineProperty(this, "onResponse", {
|
|
71
|
-
enumerable: true,
|
|
72
|
-
configurable: true,
|
|
73
|
-
writable: true,
|
|
74
|
-
value: void 0
|
|
75
|
-
});
|
|
76
38
|
this.isTestnet = options?.isTestnet ?? false;
|
|
77
39
|
this.timeout = options?.timeout === undefined ? 10_000 : options.timeout;
|
|
78
40
|
this.server = {
|
|
@@ -105,7 +67,6 @@ export class HttpTransport {
|
|
|
105
67
|
body: JSON.stringify(payload),
|
|
106
68
|
headers: {
|
|
107
69
|
"Accept-Encoding": "gzip, deflate, br, zstd",
|
|
108
|
-
"Connection": "keep-alive",
|
|
109
70
|
"Content-Type": "application/json",
|
|
110
71
|
},
|
|
111
72
|
keepalive: true,
|
|
@@ -143,29 +104,21 @@ export class HttpTransport {
|
|
|
143
104
|
return body;
|
|
144
105
|
}
|
|
145
106
|
}
|
|
146
|
-
/**
|
|
147
|
-
* Merges multiple `HeadersInit` objects into one.
|
|
148
|
-
* @param inits - A list of `HeadersInit` objects to merge.
|
|
149
|
-
* @returns A new `Headers` object that contains all headers from the input objects.
|
|
150
|
-
*/
|
|
107
|
+
/** Merges multiple {@linkcode HeadersInit} into one {@linkcode Headers}. */
|
|
151
108
|
function mergeHeadersInit(...inits) {
|
|
152
109
|
if (inits.length === 0 || inits.length === 1) {
|
|
153
110
|
return new Headers(inits[0]);
|
|
154
111
|
}
|
|
155
112
|
const merged = new Headers();
|
|
156
113
|
for (const headers of inits) {
|
|
157
|
-
const
|
|
158
|
-
for (const [key, value] of
|
|
114
|
+
const iterator = Symbol.iterator in headers ? headers : Object.entries(headers);
|
|
115
|
+
for (const [key, value] of iterator) {
|
|
159
116
|
merged.set(key, value);
|
|
160
117
|
}
|
|
161
118
|
}
|
|
162
119
|
return merged;
|
|
163
120
|
}
|
|
164
|
-
/**
|
|
165
|
-
* Merges multiple `RequestInit` objects into one.
|
|
166
|
-
* @param inits - A list of `RequestInit` objects to merge.
|
|
167
|
-
* @returns A new `RequestInit` object that contains all properties from the input objects.
|
|
168
|
-
*/
|
|
121
|
+
/** Merges multiple {@linkcode RequestInit} into one {@linkcode RequestInit}. */
|
|
169
122
|
function mergeRequestInit(...inits) {
|
|
170
123
|
const merged = inits.reduce((acc, init) => ({ ...acc, ...init }), {});
|
|
171
124
|
const headersList = inits.map((init) => init.headers)
|
|
@@ -1,51 +1,48 @@
|
|
|
1
1
|
import { TypedEventTarget } from "../../../deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.js";
|
|
2
2
|
import type { BlockDetails, TxDetails } from "../../types/explorer/responses.js";
|
|
3
3
|
import type { BaseExchangeResponse } from "../../types/exchange/responses.js";
|
|
4
|
+
/** Response to subscribe to or unsubscribe from an event. */
|
|
5
|
+
interface SubscriptionResponse {
|
|
6
|
+
/** Type of subscription operation. */
|
|
7
|
+
method: "subscribe" | "unsubscribe";
|
|
8
|
+
/** Original subscription request. */
|
|
9
|
+
subscription: unknown;
|
|
10
|
+
}
|
|
11
|
+
/** Response to post request. */
|
|
12
|
+
interface PostResponse {
|
|
13
|
+
/** Unique request identifier. */
|
|
14
|
+
id: number;
|
|
15
|
+
/** Server response. */
|
|
16
|
+
response:
|
|
17
|
+
/** Response containing requested information. */
|
|
18
|
+
{
|
|
19
|
+
/** Indicates that this is an informational response. */
|
|
20
|
+
type: "info";
|
|
21
|
+
/** Contains the information data. */
|
|
22
|
+
payload: {
|
|
23
|
+
/** Type of information being returned. */
|
|
24
|
+
type: string;
|
|
25
|
+
/** Information specific data. */
|
|
26
|
+
data: unknown;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/** Response containing action result. */
|
|
30
|
+
| {
|
|
31
|
+
/** Indicates that this is an action response. */
|
|
32
|
+
type: "action";
|
|
33
|
+
/** Action result. */
|
|
34
|
+
payload: BaseExchangeResponse;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
4
37
|
/** Base system events and dynamic channel events for Hyperliquid WebSocket API. */
|
|
5
38
|
interface HyperliquidEventMap {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
/** Type of subscription operation. */
|
|
9
|
-
method: "subscribe" | "unsubscribe";
|
|
10
|
-
/** Original subscription request. */
|
|
11
|
-
subscription: unknown;
|
|
12
|
-
}>;
|
|
13
|
-
/** Response to post request event. */
|
|
14
|
-
post: CustomEvent<{
|
|
15
|
-
/** Unique request identifier. */
|
|
16
|
-
id: number;
|
|
17
|
-
/** Server response. */
|
|
18
|
-
response:
|
|
19
|
-
/** Response containing requested information. */
|
|
20
|
-
{
|
|
21
|
-
/** Indicates that this is an informational response. */
|
|
22
|
-
type: "info";
|
|
23
|
-
/** Contains the information data. */
|
|
24
|
-
payload: {
|
|
25
|
-
/** Type of information being returned. */
|
|
26
|
-
type: string;
|
|
27
|
-
/** Information specific data. */
|
|
28
|
-
data: unknown;
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
/** Response containing action result. */
|
|
32
|
-
| {
|
|
33
|
-
/** Indicates that this is an action response. */
|
|
34
|
-
type: "action";
|
|
35
|
-
/** Action result. */
|
|
36
|
-
payload: BaseExchangeResponse;
|
|
37
|
-
};
|
|
38
|
-
}>;
|
|
39
|
-
/** Error response for message event. */
|
|
39
|
+
subscriptionResponse: CustomEvent<SubscriptionResponse>;
|
|
40
|
+
post: CustomEvent<PostResponse>;
|
|
40
41
|
error: CustomEvent<string>;
|
|
41
|
-
/** Pong response event. */
|
|
42
42
|
pong: CustomEvent<undefined>;
|
|
43
|
-
/** Block explorer update event. */
|
|
44
43
|
_explorerBlock: CustomEvent<Omit<BlockDetails, "txs">[]>;
|
|
45
|
-
/** Transaction explorer update event. */
|
|
46
44
|
_explorerTxs: CustomEvent<TxDetails[]>;
|
|
47
|
-
|
|
48
|
-
[key: string]: CustomEvent<unknown>;
|
|
45
|
+
[key: string]: CustomEvent<any>;
|
|
49
46
|
}
|
|
50
47
|
/** Listens for WebSocket messages and sends them as Hyperliquid typed events. */
|
|
51
48
|
export declare class HyperliquidEventTarget extends TypedEventTarget<HyperliquidEventMap> {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_hyperliquid_event_target.d.ts","sourceRoot":"","sources":["../../../../src/src/transports/websocket/_hyperliquid_event_target.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,oEAAoE,CAAC;AACtG,OAAO,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,mCAAmC,CAAC;AACjF,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,mCAAmC,CAAC;AAU9E,
|
|
1
|
+
{"version":3,"file":"_hyperliquid_event_target.d.ts","sourceRoot":"","sources":["../../../../src/src/transports/websocket/_hyperliquid_event_target.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,oEAAoE,CAAC;AACtG,OAAO,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,mCAAmC,CAAC;AACjF,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,mCAAmC,CAAC;AAU9E,6DAA6D;AAC7D,UAAU,oBAAoB;IAC1B,sCAAsC;IACtC,MAAM,EAAE,WAAW,GAAG,aAAa,CAAC;IACpC,qCAAqC;IACrC,YAAY,EAAE,OAAO,CAAC;CACzB;AAED,gCAAgC;AAChC,UAAU,YAAY;IAClB,iCAAiC;IACjC,EAAE,EAAE,MAAM,CAAC;IACX,uBAAuB;IACvB,QAAQ;IACJ,iDAAiD;IAC/C;QACE,wDAAwD;QACxD,IAAI,EAAE,MAAM,CAAC;QACb,qCAAqC;QACrC,OAAO,EAAE;YACL,0CAA0C;YAC1C,IAAI,EAAE,MAAM,CAAC;YACb,iCAAiC;YACjC,IAAI,EAAE,OAAO,CAAC;SACjB,CAAC;KACL;IACD,yCAAyC;OACvC;QACE,iDAAiD;QACjD,IAAI,EAAE,QAAQ,CAAC;QACf,qBAAqB;QACrB,OAAO,EAAE,oBAAoB,CAAC;KACjC,CAAC;CACT;AAED,mFAAmF;AACnF,UAAU,mBAAmB;IACzB,oBAAoB,EAAE,WAAW,CAAC,oBAAoB,CAAC,CAAC;IACxD,IAAI,EAAE,WAAW,CAAC,YAAY,CAAC,CAAC;IAChC,KAAK,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IAC3B,IAAI,EAAE,WAAW,CAAC,SAAS,CAAC,CAAC;IAC7B,cAAc,EAAE,WAAW,CAAC,IAAI,CAAC,YAAY,EAAE,KAAK,CAAC,EAAE,CAAC,CAAC;IACzD,YAAY,EAAE,WAAW,CAAC,SAAS,E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