@rabby-wallet/hyperliquid-sdk 1.1.2-beta.15 → 1.1.2-beta.16

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.
@@ -1,5 +1,5 @@
1
1
  import { ExchangeType } from '../types/constants';
2
- import type { OrderResponse, CancelResponse, ExchangeClientConfig, PlaceOrderParams, MultiOrderParams, CancelOrderParams, ModifyOrderParams, WithdrawParams, ApproveBuilderFeeParams, PrepareApproveBuilderFeeResult, SendApproveParams, MarketOrderParams, UpdateLeverageParams, BindTpslByOrderIdParams, UpdateIsolatedMarginParams, TwapOrderParams, TwapCancelParams, TwapOrderResponse, PlaceTPSlMarketOrderParams, PlaceTPSlLimitOrderParams, ClearinghouseState, UpdateTpslByOrderIdParams } from '../types';
2
+ import type { OrderResponse, CancelResponse, ExchangeClientConfig, PlaceOrderParams, MultiOrderParams, CancelOrderParams, ModifyOrderParams, WithdrawParams, ApproveBuilderFeeParams, PrepareApproveBuilderFeeResult, SendApproveParams, MarketOrderParams, UpdateLeverageParams, BindTpslByOrderIdParams, UpdateIsolatedMarginParams, TwapOrderParams, TwapCancelParams, TwapOrderResponse, PlaceTPSlMarketOrderParams, PlaceTPSlLimitOrderParams, ClearinghouseState, UpdateTpslByOrderIdParams, LimitOrderParams } from '../types';
3
3
  /**
4
4
  * Client for executing trades on Hyperliquid (perpetuals only)
5
5
  * Only includes essential trading APIs as specified
@@ -10,11 +10,19 @@ export declare class ExchangeClient {
10
10
  private agentPublicKey?;
11
11
  private agentName?;
12
12
  private readonly isTestnet;
13
+ slippage: number;
13
14
  private masterAddress;
14
15
  private builder?;
15
16
  private readonly symbolConversion;
16
17
  constructor(config: ExchangeClientConfig);
17
18
  initAccount(masterAddress: string, agentPrivateKey?: string, agentPublicKey?: string, agentName?: string): void;
19
+ /**
20
+ * Set the slippage for the exchange client
21
+ * default is 0.08
22
+ * 0 - 0.1
23
+ * @param slippage - The slippage to set
24
+ */
25
+ setSlippage(slippage?: number): void;
18
26
  initOrUpdateAgent(agentPrivateKey: string, agentPublicKey: string, agentName?: string): void;
19
27
  /**
20
28
  * Check if agent is set
@@ -47,7 +55,7 @@ export declare class ExchangeClient {
47
55
  * @param isBuy - True for buy orders, false for sell orders
48
56
  * @param szDecimals - Size decimals from meta endpoint (default 0)
49
57
  */
50
- private getSlippagePx;
58
+ getSlippagePx(midPx: string, slippage: number, isBuy: boolean, szDecimals?: number): string;
51
59
  /**
52
60
  * Round a number to specified decimal places
53
61
  */
@@ -59,6 +67,12 @@ export declare class ExchangeClient {
59
67
  */
60
68
  marketOrderOpen(params: MarketOrderParams): Promise<OrderResponse>;
61
69
  marketOrderClose(params: Omit<MarketOrderParams, 'tpTriggerPx' | 'slTriggerPx'>): Promise<OrderResponse>;
70
+ /**
71
+ * Place a market order
72
+ * default slippage is 0.08
73
+ * need to update leverage before place order
74
+ */
75
+ limitOrderOpen(params: LimitOrderParams): Promise<OrderResponse>;
62
76
  closeAllPositions(clearinghouseState: ClearinghouseState, slippage?: number, builder?: {
63
77
  address: string;
64
78
  fee: number;
@@ -70,7 +84,6 @@ export declare class ExchangeClient {
70
84
  placeTPSlMarketOrder(params: PlaceTPSlMarketOrderParams): Promise<OrderResponse>;
71
85
  /**
72
86
  * Place a take profit or stop loss limit order
73
- * default slippage is 0.08
74
87
  */
75
88
  placeTPSlLimitOrder(params: PlaceTPSlLimitOrderParams): Promise<OrderResponse>;
76
89
  /**
@@ -55,6 +55,7 @@ const number_1 = require("../utils/number");
55
55
  */
56
56
  class ExchangeClient {
57
57
  constructor(config) {
58
+ this.slippage = constants_1.SLIPPAGE;
58
59
  this.masterAddress = config.masterAddress;
59
60
  this.httpClient = new http_client_1.HttpClient({
60
61
  isTestnet: config.isTestnet,
@@ -93,6 +94,21 @@ class ExchangeClient {
93
94
  this.agentName = undefined;
94
95
  }
95
96
  }
97
+ /**
98
+ * Set the slippage for the exchange client
99
+ * default is 0.08
100
+ * 0 - 0.1
101
+ * @param slippage - The slippage to set
102
+ */
103
+ setSlippage(slippage = constants_1.SLIPPAGE) {
104
+ if (Number.isNaN(slippage)) {
105
+ throw new Error('Slippage must be a number');
106
+ }
107
+ if (slippage < 0 || slippage > 0.1) {
108
+ throw new Error('Slippage must be between 0 and 0.1');
109
+ }
110
+ this.slippage = slippage;
111
+ }
96
112
  initOrUpdateAgent(agentPrivateKey, agentPublicKey, agentName) {
97
113
  this.agentPrivateKey = agentPrivateKey.startsWith('0x') ? agentPrivateKey : ethUtil.addHexPrefix(agentPrivateKey);
98
114
  this.agentPublicKey = agentPublicKey.startsWith('0x') ? agentPublicKey : ethUtil.addHexPrefix(agentPublicKey);
@@ -222,7 +238,7 @@ class ExchangeClient {
222
238
  marketOrderOpen(params) {
223
239
  return __awaiter(this, void 0, void 0, function* () {
224
240
  try {
225
- const slippage = params.slippage || constants_1.SLIPPAGE;
241
+ const slippage = params.slippage || this.slippage;
226
242
  const szDecimals = yield this.symbolConversion.getAssetSzDecimals(params.coin);
227
243
  const px = this.getSlippagePx((0, number_1.removeTrailingZeros)(params.midPx), slippage, params.isBuy, szDecimals);
228
244
  const orders = [{
@@ -238,7 +254,7 @@ class ExchangeClient {
238
254
  coin: params.coin,
239
255
  isBuy: !params.isBuy,
240
256
  sz: (0, number_1.removeTrailingZeros)(params.size),
241
- limitPx: (0, number_1.removeTrailingZeros)(params.tpTriggerPx),
257
+ limitPx: this.getSlippagePx((0, number_1.removeTrailingZeros)(params.tpTriggerPx), slippage, !params.isBuy, szDecimals),
242
258
  reduceOnly: true,
243
259
  orderType: {
244
260
  trigger: {
@@ -255,7 +271,7 @@ class ExchangeClient {
255
271
  coin: params.coin,
256
272
  isBuy: !params.isBuy,
257
273
  sz: (0, number_1.removeTrailingZeros)(params.size),
258
- limitPx: (0, number_1.removeTrailingZeros)(params.slTriggerPx),
274
+ limitPx: this.getSlippagePx((0, number_1.removeTrailingZeros)(params.slTriggerPx), slippage, !params.isBuy, szDecimals),
259
275
  reduceOnly: true,
260
276
  orderType: {
261
277
  trigger: {
@@ -282,7 +298,7 @@ class ExchangeClient {
282
298
  return __awaiter(this, void 0, void 0, function* () {
283
299
  var _a;
284
300
  try {
285
- const slippage = params.slippage || constants_1.SLIPPAGE;
301
+ const slippage = params.slippage || this.slippage;
286
302
  const szDecimals = yield this.symbolConversion.getAssetSzDecimals(params.coin);
287
303
  const px = this.getSlippagePx(params.midPx, slippage, params.isBuy, szDecimals);
288
304
  const orders = [{
@@ -304,6 +320,70 @@ class ExchangeClient {
304
320
  }
305
321
  });
306
322
  }
323
+ /**
324
+ * Place a market order
325
+ * default slippage is 0.08
326
+ * need to update leverage before place order
327
+ */
328
+ limitOrderOpen(params) {
329
+ return __awaiter(this, void 0, void 0, function* () {
330
+ try {
331
+ const slippage = params.slippage || this.slippage;
332
+ const szDecimals = yield this.symbolConversion.getAssetSzDecimals(params.coin);
333
+ // const px = this.getSlippagePx(removeTrailingZeros(params.limitPx), slippage, params.isBuy, szDecimals);
334
+ const orders = [{
335
+ coin: params.coin,
336
+ isBuy: params.isBuy,
337
+ sz: (0, number_1.removeTrailingZeros)(params.size),
338
+ limitPx: (0, number_1.removeTrailingZeros)(params.limitPx),
339
+ reduceOnly: params.reduceOnly || false,
340
+ orderType: { limit: { tif: 'Gtc' } },
341
+ }];
342
+ if (params.tpTriggerPx) {
343
+ const tpOrder = {
344
+ coin: params.coin,
345
+ isBuy: !params.isBuy,
346
+ sz: (0, number_1.removeTrailingZeros)(params.size),
347
+ limitPx: this.getSlippagePx((0, number_1.removeTrailingZeros)(params.tpTriggerPx), slippage, !params.isBuy, szDecimals),
348
+ reduceOnly: true,
349
+ orderType: {
350
+ trigger: {
351
+ isMarket: true,
352
+ triggerPx: (0, number_1.removeTrailingZeros)(params.tpTriggerPx),
353
+ tpsl: 'tp',
354
+ }
355
+ }
356
+ };
357
+ orders.push(tpOrder);
358
+ }
359
+ if (params.slTriggerPx) {
360
+ const slOrder = {
361
+ coin: params.coin,
362
+ isBuy: !params.isBuy,
363
+ sz: (0, number_1.removeTrailingZeros)(params.size),
364
+ limitPx: this.getSlippagePx((0, number_1.removeTrailingZeros)(params.slTriggerPx), slippage, !params.isBuy, szDecimals),
365
+ reduceOnly: true,
366
+ orderType: {
367
+ trigger: {
368
+ isMarket: true,
369
+ triggerPx: (0, number_1.removeTrailingZeros)(params.slTriggerPx),
370
+ tpsl: 'sl',
371
+ }
372
+ }
373
+ };
374
+ orders.push(slOrder);
375
+ }
376
+ const res = yield this.multiOrder({
377
+ orders,
378
+ builder: params.builder,
379
+ });
380
+ return res;
381
+ }
382
+ catch (error) {
383
+ throw error;
384
+ }
385
+ });
386
+ }
307
387
  closeAllPositions(clearinghouseState_1) {
308
388
  return __awaiter(this, arguments, void 0, function* (clearinghouseState, slippage = constants_1.SLIPPAGE, builder) {
309
389
  try {
@@ -344,7 +424,7 @@ class ExchangeClient {
344
424
  placeTPSlMarketOrder(params) {
345
425
  return __awaiter(this, void 0, void 0, function* () {
346
426
  try {
347
- const slippage = params.slippage || constants_1.SLIPPAGE;
427
+ const slippage = params.slippage || this.slippage;
348
428
  const szDecimals = yield this.symbolConversion.getAssetSzDecimals(params.coin);
349
429
  const px = this.getSlippagePx((0, number_1.removeTrailingZeros)(params.triggerPx), slippage, params.isBuy, szDecimals);
350
430
  const orders = [{
@@ -374,7 +454,6 @@ class ExchangeClient {
374
454
  }
375
455
  /**
376
456
  * Place a take profit or stop loss limit order
377
- * default slippage is 0.08
378
457
  */
379
458
  placeTPSlLimitOrder(params) {
380
459
  return __awaiter(this, void 0, void 0, function* () {
@@ -455,13 +534,14 @@ class ExchangeClient {
455
534
  bindTpslByOrderId(params) {
456
535
  return __awaiter(this, void 0, void 0, function* () {
457
536
  let orders = [];
537
+ const slippage = params.slippage || this.slippage;
458
538
  const szDecimals = yield this.symbolConversion.getAssetSzDecimals(params.coin);
459
539
  if (params.tpTriggerPx) {
460
540
  orders.push({
461
541
  coin: params.coin,
462
542
  isBuy: !params.isBuy,
463
543
  sz: '0',
464
- limitPx: this.getSlippagePx((0, number_1.removeTrailingZeros)(params.tpTriggerPx), constants_1.SLIPPAGE, !params.isBuy, szDecimals),
544
+ limitPx: this.getSlippagePx((0, number_1.removeTrailingZeros)(params.tpTriggerPx), slippage, !params.isBuy, szDecimals),
465
545
  reduceOnly: true,
466
546
  orderType: {
467
547
  trigger: {
@@ -477,7 +557,7 @@ class ExchangeClient {
477
557
  coin: params.coin,
478
558
  isBuy: !params.isBuy,
479
559
  sz: '0',
480
- limitPx: this.getSlippagePx((0, number_1.removeTrailingZeros)(params.slTriggerPx), constants_1.SLIPPAGE, !params.isBuy, szDecimals),
560
+ limitPx: this.getSlippagePx((0, number_1.removeTrailingZeros)(params.slTriggerPx), slippage, !params.isBuy, szDecimals),
481
561
  reduceOnly: true,
482
562
  orderType: {
483
563
  trigger: {
@@ -503,8 +583,9 @@ class ExchangeClient {
503
583
  let orders = [];
504
584
  const szDecimals = yield this.symbolConversion.getAssetSzDecimals(params.coin);
505
585
  const isBuy = !params.isBuy;
586
+ const slippage = params.slippage || this.slippage;
506
587
  if (params.tp) {
507
- const limitPx = params.tp.limitPx || this.getSlippagePx((0, number_1.removeTrailingZeros)(params.tp.triggerPx), constants_1.SLIPPAGE, isBuy, szDecimals);
588
+ const limitPx = params.tp.limitPx || this.getSlippagePx((0, number_1.removeTrailingZeros)(params.tp.triggerPx), slippage, isBuy, szDecimals);
508
589
  orders.push({
509
590
  oid: params.tp.oid,
510
591
  coin: params.coin,
@@ -522,7 +603,7 @@ class ExchangeClient {
522
603
  });
523
604
  }
524
605
  if (params.sl) {
525
- const limitPx = params.sl.limitPx || this.getSlippagePx((0, number_1.removeTrailingZeros)(params.sl.triggerPx), constants_1.SLIPPAGE, isBuy, szDecimals);
606
+ const limitPx = params.sl.limitPx || this.getSlippagePx((0, number_1.removeTrailingZeros)(params.sl.triggerPx), slippage, isBuy, szDecimals);
526
607
  orders.push({
527
608
  oid: params.sl.oid,
528
609
  coin: params.coin,
@@ -209,6 +209,20 @@ export interface OrderResponse {
209
209
  };
210
210
  };
211
211
  }
212
+ export interface LimitOrderParams {
213
+ coin: string;
214
+ isBuy: boolean;
215
+ size: string;
216
+ limitPx: string;
217
+ reduceOnly?: boolean;
218
+ tpTriggerPx?: string;
219
+ slTriggerPx?: string;
220
+ slippage?: number;
221
+ builder?: {
222
+ address: string;
223
+ fee: number;
224
+ };
225
+ }
212
226
  export interface MarketOrderParams {
213
227
  coin: string;
214
228
  isBuy: boolean;
@@ -452,6 +466,7 @@ export interface BindTpslByOrderIdParams {
452
466
  slTriggerPx?: string;
453
467
  coin: string;
454
468
  isBuy: boolean;
469
+ slippage?: number;
455
470
  builder?: {
456
471
  address: string;
457
472
  fee: number;
@@ -470,6 +485,7 @@ export interface UpdateTpslByOrderIdParams {
470
485
  };
471
486
  coin: string;
472
487
  isBuy: boolean;
488
+ slippage?: number;
473
489
  }
474
490
  export interface MultiOrderParams {
475
491
  orders: PlaceOrderParams[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rabby-wallet/hyperliquid-sdk",
3
- "version": "1.1.2-beta.15",
3
+ "version": "1.1.2-beta.16",
4
4
  "description": "Simplified Hyperliquid Perpetuals Trading SDK for Frontend Applications",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",