Skip to content

Commit 8b10b5b

Browse files
committed
Improved method types
The order, cancel, cancelByCloid, batchModify methods return types without error variants
1 parent 1780d44 commit 8b10b5b

File tree

1 file changed

+63
-11
lines changed

1 file changed

+63
-11
lines changed

src/exchange.ts

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,59 @@ export interface VaultTransferParameters {
259259
usd: number;
260260
}
261261

262+
/**
263+
* Successful variant of {@link CancelResponse}.
264+
*/
265+
export interface CancelResponseSuccess extends CancelResponse {
266+
status: "ok";
267+
response: {
268+
type: "cancel";
269+
data: {
270+
statuses: "success"[];
271+
};
272+
};
273+
}
274+
275+
/**
276+
* Successful variant of {@link OrderResponse}.
277+
*/
278+
export interface OrderResponseSuccess extends OrderResponse {
279+
status: "ok";
280+
response: {
281+
type: "order";
282+
data: {
283+
statuses: (
284+
| {
285+
/** Status for a resting order. */
286+
resting: {
287+
/** Order ID. */
288+
oid: number;
289+
290+
/** Client Order ID. */
291+
cloid?: Hex;
292+
};
293+
}
294+
| {
295+
/** Status for a filled order. */
296+
filled: {
297+
/** Total size filled. */
298+
totalSz: string;
299+
300+
/** Average price of fill. */
301+
avgPx: string;
302+
303+
/** Order ID. */
304+
oid: number;
305+
306+
/** Client Order ID. */
307+
cloid?: Hex;
308+
};
309+
}
310+
)[];
311+
};
312+
};
313+
}
314+
262315
/**
263316
* A client to interact with the Hyperliquid exchange APIs.
264317
*/
@@ -295,7 +348,7 @@ export class HyperliquidExchangeClient {
295348
* @requestWeight 1
296349
* @throws {HyperliquidBatchAPIError} If the API returns an error.
297350
*/
298-
async order(args: OrderParameters): Promise<OrderResponse> {
351+
async order(args: OrderParameters): Promise<OrderResponseSuccess> {
299352
const action: OrderRequest["action"] = {
300353
type: "order",
301354
orders: args.orders.map((order) => {
@@ -329,7 +382,7 @@ export class HyperliquidExchangeClient {
329382
* @requestWeight 1
330383
* @throws {HyperliquidBatchAPIError} If the API returns an error.
331384
*/
332-
async cancel(args: CancelParameters): Promise<CancelResponse> {
385+
async cancel(args: CancelParameters): Promise<CancelResponseSuccess> {
333386
const action: CancelRequest["action"] = {
334387
type: "cancel",
335388
cancels: args.cancels.map((cancel) => ({ a: cancel.a, o: cancel.o })),
@@ -345,7 +398,7 @@ export class HyperliquidExchangeClient {
345398
* @requestWeight 1
346399
* @throws {HyperliquidBatchAPIError} If the API returns an error.
347400
*/
348-
async cancelByCloid(args: CancelByCloidParameters): Promise<CancelResponse> {
401+
async cancelByCloid(args: CancelByCloidParameters): Promise<CancelResponseSuccess> {
349402
const action: CancelByCloidRequest["action"] = {
350403
type: "cancelByCloid",
351404
cancels: args.cancels.map((cancel) => ({ asset: cancel.asset, cloid: cancel.cloid })),
@@ -408,7 +461,7 @@ export class HyperliquidExchangeClient {
408461
* @requestWeight 1
409462
* @throws {HyperliquidBatchAPIError} If the API returns an error.
410463
*/
411-
async batchModify(args: BatchModifyParameters): Promise<OrderResponse> {
464+
async batchModify(args: BatchModifyParameters): Promise<OrderResponseSuccess> {
412465
const action: BatchModifyRequest["action"] = {
413466
type: "batchModify",
414467
modifies: args.modifies.map((modify) => {
@@ -600,12 +653,12 @@ export class HyperliquidExchangeClient {
600653
return await this.request({ action, signature, nonce });
601654
}
602655

603-
protected async request<T extends OrderRequest>(body: T): Promise<OrderResponse>;
604-
protected async request<T extends CancelRequest>(body: T): Promise<CancelResponse>;
605-
protected async request<T extends CancelByCloidRequest>(body: T): Promise<CancelResponse>;
656+
protected async request<T extends OrderRequest>(body: T): Promise<OrderResponseSuccess>;
657+
protected async request<T extends CancelRequest>(body: T): Promise<CancelResponseSuccess>;
658+
protected async request<T extends CancelByCloidRequest>(body: T): Promise<CancelResponseSuccess>;
606659
protected async request<T extends ScheduleCancelRequest>(body: T): Promise<SuccessResponse>;
607660
protected async request<T extends ModifyRequest>(body: T): Promise<SuccessResponse>;
608-
protected async request<T extends BatchModifyRequest>(body: T): Promise<OrderResponse>;
661+
protected async request<T extends BatchModifyRequest>(body: T): Promise<OrderResponseSuccess>;
609662
protected async request<T extends UpdateLeverageRequest>(body: T): Promise<SuccessResponse>;
610663
protected async request<T extends UpdateIsolatedMarginRequest>(body: T): Promise<SuccessResponse>;
611664
protected async request<T extends UsdSendRequest>(body: T): Promise<SuccessResponse>;
@@ -629,11 +682,10 @@ export class HyperliquidExchangeClient {
629682

630683
const data = await res.json() as SuccessResponse | ErrorResponse | OrderResponse | CancelResponse;
631684

632-
if (data.status === "err") {
685+
if (data.status !== "ok") {
633686
throw new HyperliquidAPIError(data.response);
634687
}
635688
if (
636-
data.status === "ok" &&
637689
data.response.type !== "default" &&
638690
data.response.data.statuses.some((status) => typeof status === "object" && status !== null && "error" in status)
639691
) {
@@ -643,7 +695,7 @@ export class HyperliquidExchangeClient {
643695
throw new HyperliquidBatchAPIError(messages);
644696
}
645697

646-
return data;
698+
return data as SuccessResponse | OrderResponseSuccess | CancelResponseSuccess;
647699
}
648700

649701
protected async signL1Action(

0 commit comments

Comments
 (0)