Skip to content
markpaper

src/orders/types.ts

v0.3.0 · 3.1 KB

Download file
// Structural types for the exchange client. The kit never imports @nktkas/hyperliquid at runtime
// (it is an optional peer): every function takes the caller's own `ExchangeClient`, which satisfies
// these minimal interfaces (checked by sdk-contract.test.ts against SDK 0.33.3).

import type { AssetInfo } from '../assets/index.js';
import type { Cloid } from './cloid.js';

/** Time in force of a limit order. `FrontendMarket` (SDK 0.33.3) is not used by this module. */
export type Tif = 'Gtc' | 'Ioc' | 'Alo';

/** `t` of a wire order. */
export type WireOrderType =
  | { limit: { tif: Tif } }
  | { trigger: { isMarket: boolean; triggerPx: string; tpsl: 'tp' | 'sl' } };

/** One element of `orders` in the `order` action, with quantized decimal strings. */
export interface WireOrder {
  /** Asset id. */
  a: number;
  /** `true` = buy. */
  b: boolean;
  /** Limit price (for a trigger order: worst accepted price after triggering). */
  p: string;
  /** Size. */
  s: string;
  /** reduceOnly. */
  r: boolean;
  t: WireOrderType;
  /** Client order id. */
  c?: Cloid;
}

/** Builder fee attached to an `order` action. */
export interface WireBuilder {
  /** Builder address, lowercase. */
  b: `0x${string}`;
  /** Fee in tenths of a basis point. */
  f: number;
}

/** Parameters of the `order` action. `positionTpsl` is not produced by this module. */
export interface OrderActionParams {
  orders: WireOrder[];
  grouping: 'na' | 'normalTpsl';
  builder?: WireBuilder;
}

/** Per-call options of an L1 exchange action (SDK `ExchangeClient` second argument). */
export interface ExchangeCallOptions {
  signal?: AbortSignal;
  /** Sub-account or vault to act for. */
  vaultAddress?: `0x${string}`;
  expiresAfter?: number;
}

/** Anything with an SDK-compatible `order` method (an `ExchangeClient`). */
export interface OrderExchange {
  order(params: OrderActionParams, opts?: ExchangeCallOptions): PromiseLike<unknown>;
}

/** Anything with SDK-compatible `cancel` / `cancelByCloid` methods (an `ExchangeClient`). */
export interface CancelExchange {
  cancel(params: { cancels: { a: number; o: number }[] }, opts?: ExchangeCallOptions): PromiseLike<unknown>;
  cancelByCloid(params: { cancels: { asset: number; cloid: Cloid }[] }, opts?: ExchangeCallOptions): PromiseLike<unknown>;
}

/** Anything with an SDK-compatible `scheduleCancel` method (an `ExchangeClient`). */
export interface ScheduleCancelExchange {
  scheduleCancel(params?: { time?: number }, opts?: ExchangeCallOptions): PromiseLike<unknown>;
  scheduleCancel(opts?: ExchangeCallOptions): PromiseLike<unknown>;
}

/** Anything with SDK-compatible `twapOrder` / `twapCancel` methods (an `ExchangeClient`). */
export interface TwapExchange {
  twapOrder(
    params: { twap: { a: number; b: boolean; s: string; r: boolean; m: number; t: boolean } },
    opts?: ExchangeCallOptions,
  ): PromiseLike<unknown>;
  twapCancel(params: { a: number; t: number }, opts?: ExchangeCallOptions): PromiseLike<unknown>;
}

/** The part of an `AssetRegistry` this module needs. */
export interface AssetResolver {
  resolve(coin: string, opts?: { rejectDelisted?: boolean }): Promise<AssetInfo>;
}
All files