Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v24.18.0
v24.22.3
268 changes: 136 additions & 132 deletions llms.txt
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,6 @@ File: .npmrc
================
min-release-age=30

================
File: .nvmrc
================
v24.18.0

================
File: .prettierrc
================
Expand Down Expand Up @@ -989,6 +984,11 @@ export interface L1ActionOptions {
vaultAddress?: string | null;
}

================
File: .nvmrc
================
v24.22.3

================
File: tsconfig.extensions.json
================
Expand Down Expand Up @@ -3740,6 +3740,132 @@ examples/tstests
localtest.sh
rest-ts.ts

================
File: src/WebsocketClient.ts
================
import { inflateSync, strFromU8 } from 'fflate';
⋮----
import { MAINNET_WS_URL, TESTNET_WS_URL } from './constants.js';
import type { ResolvedAsset } from './lib/asset-resolver.js';
import {
BaseWebsocketClient,
type EmittableEvent,
type WSOperation,
} from './lib/BaseWSClient.js';
import { HyperliquidApiError } from './lib/errors.js';
import { normalizeAddresses } from './lib/normalize-addresses.js';
import { HyperliquidWebSocketError } from './lib/websocket/errors.js';
import {
DefaultLogger,
type DefaultLogger as Logger,
} from './lib/websocket/logger.js';
import { sanitizeObjectForWire } from './lib/wire.js';
import { RestClient } from './RestClient.js';
import type { ExchangeResponse } from './types/response/exchange.types.js';
import type { RequestOptions } from './types/shared.types.js';
import type {
WebsocketMessageEvent,
WebsocketPostResponseEvent,
WebsocketResponseEvent,
} from './types/websockets/ws-events.js';
import type {
WebsocketClientOptions,
WsKey,
} from './types/websockets/ws-general.js';
import type { WebsocketSubscription } from './types/websockets/ws-subscriptions.js';
⋮----
function normalizeSubscriptionFields(
subscription: WebsocketSubscription,
): WebsocketSubscription
⋮----
function recordValue(value: unknown): Record<string, unknown> | undefined
⋮----
function postErrorRequestId(message: string): number | undefined
⋮----
function decompressFastAssetCtxs(data: unknown): unknown
⋮----
export class WebsocketClient extends BaseWebsocketClient<
⋮----
constructor(
options: WebsocketClientOptions = {},
logger: Logger = DefaultLogger,
)
⋮----
connect(wsKey: WsKey = 'public'): Promise<void>
⋮----
/** Connect all cached connection groups, or the public connection if none exist. */
async connectAll(): Promise<void>
⋮----
isConnected(wsKey: WsKey = 'public'): boolean
⋮----
/** Reject pending work immediately; emit 'close' when the socket confirms closure. */
close(wsKey?: WsKey): void
⋮----
/**
* Subscribe to one or more wire topics. Listen for data with on('message').
* Topics are cached before connecting and restored automatically after reconnect.
* The promise confirms local sending; server acknowledgements emit 'response'.
* Resolve display aliases explicitly with nameToCoin() when needed.
*/
async subscribe(
subscriptions: WebsocketSubscription | readonly WebsocketSubscription[],
): Promise<void>
⋮----
/** Remove cached topics independently of event listeners, including while disconnected. */
async unsubscribe(
subscriptions: WebsocketSubscription | readonly WebsocketSubscription[],
): Promise<void>
⋮----
async sendWSAPIRequest<T>(
endpoint: 'action' | 'info',
body: unknown,
options: RequestOptions = {},
): Promise<T>
⋮----
resolveAsset(identifier: number | string): Promise<ResolvedAsset>
⋮----
nameToCoin(name: string): Promise<string>
⋮----
refreshAssetMetadata(): Promise<void>
⋮----
private groupSubscriptions(
subscriptions: WebsocketSubscription | readonly WebsocketSubscription[],
): Map<WsKey, WebsocketSubscription[]>
⋮----
protected override getWsUrl(): string
⋮----
protected override getMaxConnections(): number
⋮----
protected override getWsRequestEvents(
_wsKey: WsKey,
operation: WSOperation,
topics: WebsocketSubscription[],
): object[]
⋮----
protected override sendPingEvent(wsKey: WsKey): boolean
⋮----
protected override resolveEmittableEvents(
wsKey: WsKey,
message: string,
): EmittableEvent<WsKey>[]
⋮----
// Emit each wire message once. Subscription ownership never controls listeners.
// Keep upstream channel names (including user and activeSpotAssetCtx) unchanged.
⋮----
private handlePostMessage(
record: Record<string, unknown>,
): 'exception' | 'response'
⋮----
private logUnmatchedResponse(
requestId: unknown,
parsed: Record<string, unknown>,
): void
⋮----
// IDs are monotonic for this client. A known ID without a pending promise has
// already settled (for example by timeout or abort); its late reply is expected.
⋮----
private rejectRequest(id: number, error: unknown): void

================
File: src/types/response/info.types.ts
================
Expand Down Expand Up @@ -4001,6 +4127,8 @@ export type OrderState =
| 'delistedCanceled'
| 'filled'
| 'insufficientSpotBalanceRejected'
/** Returned by Hyperliquid, not yet in their docs. */
| 'insufficientWeiCanceled'
| 'internalCancel'
| 'iocCancelRejected'
| 'liquidatedCanceled'
Expand Down Expand Up @@ -4028,6 +4156,8 @@ export type OrderState =
| 'triggered'
| 'vaultWithdrawalCanceled';
⋮----
/** Returned by Hyperliquid, not yet in their docs. */
⋮----
export interface OrderStatusDetails {
order: FrontendOpenOrder;
status: OrderState;
Expand Down Expand Up @@ -4880,132 +5010,6 @@ export interface WebData2 {
user: string;
}

================
File: src/WebsocketClient.ts
================
import { inflateSync, strFromU8 } from 'fflate';
⋮----
import { MAINNET_WS_URL, TESTNET_WS_URL } from './constants.js';
import type { ResolvedAsset } from './lib/asset-resolver.js';
import {
BaseWebsocketClient,
type EmittableEvent,
type WSOperation,
} from './lib/BaseWSClient.js';
import { HyperliquidApiError } from './lib/errors.js';
import { normalizeAddresses } from './lib/normalize-addresses.js';
import { HyperliquidWebSocketError } from './lib/websocket/errors.js';
import {
DefaultLogger,
type DefaultLogger as Logger,
} from './lib/websocket/logger.js';
import { sanitizeObjectForWire } from './lib/wire.js';
import { RestClient } from './RestClient.js';
import type { ExchangeResponse } from './types/response/exchange.types.js';
import type { RequestOptions } from './types/shared.types.js';
import type {
WebsocketMessageEvent,
WebsocketPostResponseEvent,
WebsocketResponseEvent,
} from './types/websockets/ws-events.js';
import type {
WebsocketClientOptions,
WsKey,
} from './types/websockets/ws-general.js';
import type { WebsocketSubscription } from './types/websockets/ws-subscriptions.js';
⋮----
function normalizeSubscriptionFields(
subscription: WebsocketSubscription,
): WebsocketSubscription
⋮----
function recordValue(value: unknown): Record<string, unknown> | undefined
⋮----
function postErrorRequestId(message: string): number | undefined
⋮----
function decompressFastAssetCtxs(data: unknown): unknown
⋮----
export class WebsocketClient extends BaseWebsocketClient<
⋮----
constructor(
options: WebsocketClientOptions = {},
logger: Logger = DefaultLogger,
)
⋮----
connect(wsKey: WsKey = 'public'): Promise<void>
⋮----
/** Connect all cached connection groups, or the public connection if none exist. */
async connectAll(): Promise<void>
⋮----
isConnected(wsKey: WsKey = 'public'): boolean
⋮----
/** Reject pending work immediately; emit 'close' when the socket confirms closure. */
close(wsKey?: WsKey): void
⋮----
/**
* Subscribe to one or more wire topics. Listen for data with on('message').
* Topics are cached before connecting and restored automatically after reconnect.
* The promise confirms local sending; server acknowledgements emit 'response'.
* Resolve display aliases explicitly with nameToCoin() when needed.
*/
async subscribe(
subscriptions: WebsocketSubscription | readonly WebsocketSubscription[],
): Promise<void>
⋮----
/** Remove cached topics independently of event listeners, including while disconnected. */
async unsubscribe(
subscriptions: WebsocketSubscription | readonly WebsocketSubscription[],
): Promise<void>
⋮----
async sendWSAPIRequest<T>(
endpoint: 'action' | 'info',
body: unknown,
options: RequestOptions = {},
): Promise<T>
⋮----
resolveAsset(identifier: number | string): Promise<ResolvedAsset>
⋮----
nameToCoin(name: string): Promise<string>
⋮----
refreshAssetMetadata(): Promise<void>
⋮----
private groupSubscriptions(
subscriptions: WebsocketSubscription | readonly WebsocketSubscription[],
): Map<WsKey, WebsocketSubscription[]>
⋮----
protected override getWsUrl(): string
⋮----
protected override getMaxConnections(): number
⋮----
protected override getWsRequestEvents(
_wsKey: WsKey,
operation: WSOperation,
topics: WebsocketSubscription[],
): object[]
⋮----
protected override sendPingEvent(wsKey: WsKey): boolean
⋮----
protected override resolveEmittableEvents(
wsKey: WsKey,
message: string,
): EmittableEvent<WsKey>[]
⋮----
// Emit each wire message once. Subscription ownership never controls listeners.
// Keep upstream channel names (including user and activeSpotAssetCtx) unchanged.
⋮----
private handlePostMessage(
record: Record<string, unknown>,
): 'exception' | 'response'
⋮----
private logUnmatchedResponse(
requestId: unknown,
parsed: Record<string, unknown>,
): void
⋮----
// IDs are monotonic for this client. A known ID without a pending promise has
// already settled (for example by timeout or abort); its late reply is expected.
⋮----
private rejectRequest(id: number, error: unknown): void

================
File: src/RestClient.ts
================
Expand Down Expand Up @@ -6186,7 +6190,7 @@ File: package.json
================
{
"name": "@siebly/hyperliquid-api",
"version": "1.0.3",
"version": "1.0.4",
"description": "Professional JavaScript & Node.js SDK for Hyperliquid's REST APIs and WebSockets, built in TypeScript with strong end to end tests.",
"scripts": {
"clean": "node scripts/clean.mjs",
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@siebly/hyperliquid-api",
"version": "1.0.3",
"version": "1.0.4",
"description": "Professional JavaScript & Node.js SDK for Hyperliquid's REST APIs and WebSockets, built in TypeScript with strong end to end tests.",
"scripts": {
"clean": "node scripts/clean.mjs",
Expand Down
16 changes: 12 additions & 4 deletions src/lib/BaseRestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,18 @@ export abstract class BaseRestClient {
const response = await this.axios.post<T>(path, body);

if (isHyperliquidErrorResponse(response.data)) {
throw new HyperliquidApiError(
response.data.response || 'Hyperliquid rejected the request.',
{ response: response.data, status: response.status },
);
const message =
response.data.response || 'Hyperliquid rejected the request.';
const exchangeCode = /Unexpected error \(code=(\d+)\)/.exec(
message,
)?.[1];

throw new HyperliquidApiError(message, {
exchangeCode:
exchangeCode === undefined ? undefined : Number(exchangeCode),
response: response.data,
status: response.status,
});
}

return response.data;
Expand Down
5 changes: 5 additions & 0 deletions src/lib/errors.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
export interface HyperliquidApiErrorOptions {
cause?: unknown;
code?: string | undefined;
exchangeCode?: number | undefined;
response?: unknown;
status?: number | undefined;
}

export class HyperliquidApiError extends Error {
readonly code: string | undefined;

/** Numeric error code reported by the Hyperliquid node in "Unexpected error (code=N)" messages. */
readonly exchangeCode?: number | undefined;

readonly response?: unknown;

readonly status: number | undefined;
Expand All @@ -16,6 +20,7 @@ export class HyperliquidApiError extends Error {
super(message, { cause: options.cause });
this.name = 'HyperliquidApiError';
this.code = options.code;
this.exchangeCode = options.exchangeCode;
this.response = options.response;
this.status = options.status;
}
Expand Down
Loading
Loading