@rabby-wallet/hyperliquid-sdk 1.0.0-beta.1 → 1.0.0-beta.3

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.
@@ -5,9 +5,9 @@ import type { OrderResponse, CancelResponse, ExchangeClientConfig, PlaceOrderPar
5
5
  */
6
6
  export declare class ExchangeClient {
7
7
  private readonly httpClient;
8
- private agentPrivateKey;
9
- private agentPublicKey;
10
- private agentName;
8
+ private agentPrivateKey?;
9
+ private agentPublicKey?;
10
+ private agentName?;
11
11
  private readonly isTestnet;
12
12
  private readonly masterAddress;
13
13
  private builder?;
@@ -17,6 +17,8 @@ export declare class ExchangeClient {
17
17
  * Get the wallet address
18
18
  */
19
19
  get address(): string;
20
+ updateExchangeAgent(agentPrivateKey: string, agentPublicKey: string, agentName?: string): void;
21
+ private getAgentPrivateKey;
20
22
  updateBuilder(address: string, fee: number): Promise<any>;
21
23
  updateLeverage(params: UpdateLeverageParams): Promise<any>;
22
24
  /**
@@ -86,4 +88,5 @@ export declare class ExchangeClient {
86
88
  * Withdraw funds to L1 need use master wallet
87
89
  */
88
90
  prepareWithdraw(params: WithdrawParams): PrepareApproveBuilderFeeResult;
91
+ sendWithdraw(params: SendApproveParams): Promise<any>;
89
92
  }
@@ -47,6 +47,7 @@ const ethUtil = __importStar(require("@ethereumjs/util"));
47
47
  const http_client_1 = require("./http-client");
48
48
  const constants_1 = require("../types/constants");
49
49
  const signer_1 = require("../utils/signer");
50
+ const symbolConversion_1 = require("./symbolConversion");
50
51
  /**
51
52
  * Client for executing trades on Hyperliquid (perpetuals only)
52
53
  * Only includes essential trading APIs as specified
@@ -58,11 +59,25 @@ class ExchangeClient {
58
59
  isTestnet: config.isTestnet,
59
60
  timeout: config.timeout,
60
61
  });
61
- this.agentPrivateKey = config.agentPrivateKey.startsWith('0x') ? config.agentPrivateKey : ethUtil.addHexPrefix(config.agentPrivateKey);
62
- this.agentPublicKey = config.agentPublicKey.startsWith('0x') ? config.agentPublicKey : ethUtil.addHexPrefix(config.agentPublicKey);
63
- this.agentName = config.agentName || 'Rabby';
62
+ if (!config.symbolConversion) {
63
+ this.symbolConversion = new symbolConversion_1.SymbolConversion({
64
+ isTestnet: config.isTestnet,
65
+ timeout: config.timeout,
66
+ });
67
+ }
68
+ else {
69
+ this.symbolConversion = config.symbolConversion;
70
+ }
71
+ if (config.agentPrivateKey) {
72
+ this.agentPrivateKey = config.agentPrivateKey.startsWith('0x') ? config.agentPrivateKey : ethUtil.addHexPrefix(config.agentPrivateKey);
73
+ }
74
+ if (config.agentPublicKey) {
75
+ this.agentPublicKey = config.agentPublicKey.startsWith('0x') ? config.agentPublicKey : ethUtil.addHexPrefix(config.agentPublicKey);
76
+ }
77
+ if (config.agentName) {
78
+ this.agentName = config.agentName;
79
+ }
64
80
  this.isTestnet = config.isTestnet || false;
65
- this.symbolConversion = config.symbolConversion;
66
81
  }
67
82
  /**
68
83
  * Get the wallet address
@@ -70,6 +85,17 @@ class ExchangeClient {
70
85
  get address() {
71
86
  return this.masterAddress;
72
87
  }
88
+ updateExchangeAgent(agentPrivateKey, agentPublicKey, agentName) {
89
+ this.agentPrivateKey = agentPrivateKey.startsWith('0x') ? agentPrivateKey : ethUtil.addHexPrefix(agentPrivateKey);
90
+ this.agentPublicKey = agentPublicKey.startsWith('0x') ? agentPublicKey : ethUtil.addHexPrefix(agentPublicKey);
91
+ this.agentName = agentName || '';
92
+ }
93
+ getAgentPrivateKey() {
94
+ if (!this.agentPrivateKey) {
95
+ throw new Error('Agent private key is not set');
96
+ }
97
+ return this.agentPrivateKey;
98
+ }
73
99
  // 50 = '0.05%'
74
100
  updateBuilder(address, fee) {
75
101
  return __awaiter(this, void 0, void 0, function* () {
@@ -91,7 +117,7 @@ class ExchangeClient {
91
117
  isCross: params.isCross || false,
92
118
  leverage: params.leverage,
93
119
  };
94
- const signature = (0, signer_1.signL1AgentAction)(this.agentPrivateKey, action, this.isTestnet, nonce);
120
+ const signature = (0, signer_1.signL1AgentAction)(this.getAgentPrivateKey(), action, this.isTestnet, nonce);
95
121
  return this.httpClient.exchange({
96
122
  action,
97
123
  nonce,
@@ -124,7 +150,7 @@ class ExchangeClient {
124
150
  f: this.builder.fee,
125
151
  };
126
152
  }
127
- const signature = (0, signer_1.signL1AgentAction)(this.agentPrivateKey, action, this.isTestnet, nonce);
153
+ const signature = (0, signer_1.signL1AgentAction)(this.getAgentPrivateKey(), action, this.isTestnet, nonce);
128
154
  return this.httpClient.exchange({
129
155
  action,
130
156
  nonce,
@@ -257,7 +283,7 @@ class ExchangeClient {
257
283
  f: this.builder.fee,
258
284
  };
259
285
  }
260
- const signature = (0, signer_1.signL1AgentAction)(this.agentPrivateKey, action, this.isTestnet, nonce);
286
+ const signature = (0, signer_1.signL1AgentAction)(this.getAgentPrivateKey(), action, this.isTestnet, nonce);
261
287
  return this.httpClient.exchange({
262
288
  action,
263
289
  nonce,
@@ -326,7 +352,7 @@ class ExchangeClient {
326
352
  type: constants_1.ExchangeType.CANCEL,
327
353
  cancels: cancels,
328
354
  };
329
- const signature = (0, signer_1.signL1AgentAction)(this.agentPrivateKey, action, this.isTestnet, nonce);
355
+ const signature = (0, signer_1.signL1AgentAction)(this.getAgentPrivateKey(), action, this.isTestnet, nonce);
330
356
  return this.httpClient.exchange({
331
357
  action,
332
358
  nonce,
@@ -355,7 +381,7 @@ class ExchangeClient {
355
381
  type: constants_1.ExchangeType.MODIFY,
356
382
  modifies: [modify],
357
383
  };
358
- const signature = (0, signer_1.signL1AgentAction)(this.agentPrivateKey, action, this.isTestnet, nonce);
384
+ const signature = (0, signer_1.signL1AgentAction)(this.getAgentPrivateKey(), action, this.isTestnet, nonce);
359
385
  return this.httpClient.exchange({
360
386
  action,
361
387
  nonce,
@@ -374,7 +400,7 @@ class ExchangeClient {
374
400
  type: constants_1.ExchangeType.SET_REFERRER,
375
401
  code,
376
402
  };
377
- const signature = (0, signer_1.signL1AgentAction)(this.agentPrivateKey, action, this.isTestnet, nonce);
403
+ const signature = (0, signer_1.signL1AgentAction)(this.getAgentPrivateKey(), action, this.isTestnet, nonce);
378
404
  return this.httpClient.exchange({
379
405
  action,
380
406
  nonce,
@@ -517,5 +543,12 @@ class ExchangeClient {
517
543
  nonce,
518
544
  };
519
545
  }
546
+ sendWithdraw(params) {
547
+ return this.httpClient.exchange({
548
+ action: params.action,
549
+ nonce: params.nonce,
550
+ signature: params.signature,
551
+ });
552
+ }
520
553
  }
521
554
  exports.ExchangeClient = ExchangeClient;
@@ -41,6 +41,9 @@ class HttpClient {
41
41
  throw new Error(`HTTP ${response.status}: ${response.statusText}`);
42
42
  }
43
43
  const result = yield response.json();
44
+ if (result.status === 'error') {
45
+ throw new Error(JSON.stringify(result.response));
46
+ }
44
47
  return result;
45
48
  }
46
49
  catch (error) {
@@ -1,10 +1,8 @@
1
- import type { Meta, AssetCtx, ClearinghouseState, UserFills, CandleSnapshot, AllMids, ExtraAgent, OpenOrder, FeeResponse } from '../types';
2
- import { SymbolConversion } from './symbolConversion';
1
+ import type { Meta, AssetCtx, ClearinghouseState, UserFills, CandleSnapshot, AllMids, ExtraAgent, OpenOrder, FeeResponse, UserNonFundingLedgerUpdates } from '../types';
3
2
  export interface InfoClientConfig {
4
3
  isTestnet?: boolean;
5
4
  timeout?: number;
6
5
  masterAddress: string;
7
- symbolConversion: SymbolConversion;
8
6
  }
9
7
  /**
10
8
  * Client for querying Hyperliquid info endpoints (perpetuals only)
@@ -13,7 +11,6 @@ export interface InfoClientConfig {
13
11
  export declare class InfoClient {
14
12
  private readonly httpClient;
15
13
  private readonly masterAddress;
16
- private readonly symbolConversion;
17
14
  constructor(config: InfoClientConfig);
18
15
  /**
19
16
  * Get all mid prices for all assets
@@ -23,12 +20,13 @@ export declare class InfoClient {
23
20
  * Get metadata and asset contexts
24
21
  */
25
22
  metaAndAssetCtxs(): Promise<[Meta, AssetCtx[]]>;
26
- getClearingHouseState(): Promise<ClearinghouseState>;
27
- getFrontendOpenOrders(): Promise<OpenOrder[]>;
23
+ getClearingHouseState(address?: string): Promise<ClearinghouseState>;
24
+ getFrontendOpenOrders(address?: string): Promise<OpenOrder[]>;
25
+ getUserNonFundingLedgerUpdates(address?: string, startTime?: number, endTime?: number): Promise<UserNonFundingLedgerUpdates[]>;
28
26
  /**
29
27
  * Get user's fill history
30
28
  */
31
- getUserFills(): Promise<UserFills>;
29
+ getUserFills(address?: string): Promise<UserFills>;
32
30
  /**
33
31
  * Get candlestick data snapshot
34
32
  * Supported intervals: "1m", "3m", "5m", "15m", "30m", "1h", "2h", "4h", "8h", "12h", "1d", "3d", "1w", "1M"
@@ -37,13 +35,19 @@ export declare class InfoClient {
37
35
  /**
38
36
  * Check builder fee approval status
39
37
  */
40
- checkBuilderFeeApproval(builder: string): Promise<any>;
38
+ checkBuilderFeeApproval(builder: string, address?: string): Promise<any>;
39
+ /**
40
+ * Get user role
41
+ */
42
+ getUserRole(address?: string): Promise<{
43
+ role: 'user' | 'agent' | 'vault' | 'subAccount' | 'missing';
44
+ }>;
41
45
  /**
42
46
  * Get user fees information
43
47
  */
44
- getUsersFees(): Promise<FeeResponse>;
48
+ getUsersFees(address?: string): Promise<FeeResponse>;
45
49
  /**
46
50
  * Get user's approved persistent agents
47
51
  */
48
- extraAgents(): Promise<ExtraAgent[]>;
52
+ extraAgents(address?: string): Promise<ExtraAgent[]>;
49
53
  }
@@ -23,7 +23,6 @@ class InfoClient {
23
23
  isTestnet: config.isTestnet,
24
24
  timeout: config.timeout,
25
25
  });
26
- this.symbolConversion = config.symbolConversion;
27
26
  }
28
27
  /**
29
28
  * Get all mid prices for all assets
@@ -45,20 +44,20 @@ class InfoClient {
45
44
  });
46
45
  });
47
46
  }
48
- getClearingHouseState() {
47
+ getClearingHouseState(address) {
49
48
  return __awaiter(this, void 0, void 0, function* () {
50
49
  return this.httpClient.info({
51
50
  type: constants_1.InfoType.CLEARINGHOUSE_STATE,
52
- user: this.masterAddress,
51
+ user: address || this.masterAddress,
53
52
  });
54
53
  });
55
54
  }
56
55
  // frontendOpenOrders
57
- getFrontendOpenOrders() {
56
+ getFrontendOpenOrders(address) {
58
57
  return __awaiter(this, void 0, void 0, function* () {
59
58
  return this.httpClient.info({
60
59
  type: constants_1.InfoType.FRONTEND_OPEN_ORDERS,
61
- user: this.masterAddress,
60
+ user: address || this.masterAddress,
62
61
  });
63
62
  });
64
63
  }
@@ -69,14 +68,24 @@ class InfoClient {
69
68
  // user: this.masterAddress,
70
69
  // });
71
70
  // }
71
+ getUserNonFundingLedgerUpdates(address, startTime, endTime) {
72
+ return __awaiter(this, void 0, void 0, function* () {
73
+ return this.httpClient.info({
74
+ type: constants_1.InfoType.USER_NON_FUNDING_LEDGER_UPDATES,
75
+ user: address || this.masterAddress,
76
+ startTime: startTime || 0,
77
+ endTime,
78
+ });
79
+ });
80
+ }
72
81
  /**
73
82
  * Get user's fill history
74
83
  */
75
- getUserFills() {
84
+ getUserFills(address) {
76
85
  return __awaiter(this, void 0, void 0, function* () {
77
86
  return this.httpClient.info({
78
87
  type: constants_1.InfoType.USER_FILLS,
79
- user: this.masterAddress,
88
+ user: address || this.masterAddress,
80
89
  });
81
90
  });
82
91
  }
@@ -103,23 +112,35 @@ class InfoClient {
103
112
  /**
104
113
  * Check builder fee approval status
105
114
  */
106
- checkBuilderFeeApproval(builder) {
115
+ checkBuilderFeeApproval(builder, address) {
107
116
  return __awaiter(this, void 0, void 0, function* () {
108
117
  return this.httpClient.info({
109
118
  type: constants_1.InfoType.CHECK_BUILDER_FEE_APPROVAL,
110
- user: this.masterAddress,
119
+ user: address || this.masterAddress,
111
120
  builder,
112
121
  });
113
122
  });
114
123
  }
124
+ /**
125
+ * Get user role
126
+ */
127
+ getUserRole(address) {
128
+ return __awaiter(this, void 0, void 0, function* () {
129
+ const res = yield this.httpClient.info({
130
+ type: constants_1.InfoType.USER_ROLE,
131
+ user: address || this.masterAddress,
132
+ });
133
+ return res;
134
+ });
135
+ }
115
136
  /**
116
137
  * Get user fees information
117
138
  */
118
- getUsersFees() {
139
+ getUsersFees(address) {
119
140
  return __awaiter(this, void 0, void 0, function* () {
120
141
  const res = yield this.httpClient.info({
121
142
  type: constants_1.InfoType.USER_FEES,
122
- user: this.masterAddress,
143
+ user: address || this.masterAddress,
123
144
  });
124
145
  // const perpFee = Number(res.userCrossRate) * (1 - Number(res.activeReferralDiscount));
125
146
  return res;
@@ -128,11 +149,11 @@ class InfoClient {
128
149
  /**
129
150
  * Get user's approved persistent agents
130
151
  */
131
- extraAgents() {
152
+ extraAgents(address) {
132
153
  return __awaiter(this, void 0, void 0, function* () {
133
154
  return this.httpClient.info({
134
155
  type: constants_1.InfoType.EXTRA_AGENTS,
135
- user: this.masterAddress,
156
+ user: address || this.masterAddress,
136
157
  });
137
158
  });
138
159
  }
@@ -1,12 +1,10 @@
1
1
  import type { AllMids, L2Book, Candle, WsFill, WsOrder, WebData2 } from '../types';
2
- import { SymbolConversion } from './symbolConversion';
3
2
  export interface WebSocketClientConfig {
4
3
  masterAddress: string;
5
4
  isTestnet?: boolean;
6
5
  autoReconnect?: boolean;
7
6
  reconnectDelay?: number;
8
7
  maxReconnectAttempts?: number;
9
- symbolConversion: SymbolConversion;
10
8
  }
11
9
  export type SubscriptionCallback<T = any> = (data: T) => void;
12
10
  export interface Subscription {
@@ -24,7 +22,6 @@ export declare class WebSocketClient {
24
22
  private reconnectAttempts;
25
23
  private isConnecting;
26
24
  private pendingSubscriptions;
27
- private symbolConversion;
28
25
  constructor(config: WebSocketClientConfig);
29
26
  /**
30
27
  * Connect to the WebSocket
@@ -24,7 +24,6 @@ class WebSocketClient {
24
24
  this.pendingSubscriptions = [];
25
25
  this.config = Object.assign({ isTestnet: false, autoReconnect: true, reconnectDelay: 3000, maxReconnectAttempts: 5 }, config);
26
26
  this.url = this.config.isTestnet ? constants_1.WSS_URLS.TESTNET : constants_1.WSS_URLS.PRODUCTION;
27
- this.symbolConversion = config.symbolConversion;
28
27
  }
29
28
  /**
30
29
  * Connect to the WebSocket
@@ -29,7 +29,7 @@ export declare class HyperliquidSDK {
29
29
  * Get wallet address (only available if private key was provided)
30
30
  */
31
31
  get address(): string;
32
- updateExchangeAgent(agentPrivateKey: string, agentPublicKey: string, agentName: string): void;
32
+ updateExchangeAgent(agentPrivateKey: string, agentPublicKey: string, agentName?: string): void;
33
33
  /**
34
34
  * Connect to WebSocket for real-time data
35
35
  */
@@ -30,7 +30,6 @@ class HyperliquidSDK {
30
30
  masterAddress: config.masterAddress,
31
31
  isTestnet: config.isTestnet,
32
32
  timeout: config.timeout,
33
- symbolConversion: this.symbolConversion,
34
33
  });
35
34
  this.masterAddress = config.masterAddress;
36
35
  this.configParams = config;
@@ -47,7 +46,7 @@ class HyperliquidSDK {
47
46
  });
48
47
  }
49
48
  // Initialize WebSocket client (full functionality preserved)
50
- this.ws = new websocket_client_1.WebSocketClient(Object.assign({ masterAddress: config.masterAddress, isTestnet: config.isTestnet, symbolConversion: this.symbolConversion }, config.wsConfig));
49
+ this.ws = new websocket_client_1.WebSocketClient(Object.assign({ masterAddress: config.masterAddress, isTestnet: config.isTestnet }, config.wsConfig));
51
50
  }
52
51
  /**
53
52
  * Get wallet address (only available if private key was provided)
@@ -27,6 +27,7 @@ export declare enum ExchangeType {
27
27
  }
28
28
  export declare enum InfoType {
29
29
  ALL_MIDS = "allMids",
30
+ USER_NON_FUNDING_LEDGER_UPDATES = "userNonFundingLedgerUpdates",
30
31
  USER_OPEN_ORDERS = "openOrders",
31
32
  USER_FILLS = "userFills",
32
33
  USER_FILLS_BY_TIME = "userFillsByTime",
@@ -35,7 +36,6 @@ export declare enum InfoType {
35
36
  CANDLES_SNAPSHOT = "candleSnapshot",
36
37
  META_AND_ASSET_CTXS = "metaAndAssetCtxs",
37
38
  USER_FUNDING = "userFunding",
38
- USER_NON_FUNDING_LEDGER_UPDATES = "userNonFundingLedgerUpdates",
39
39
  FUNDING_HISTORY = "fundingHistory",
40
40
  CLEARINGHOUSE_STATE = "clearinghouseState",
41
41
  META = "meta",
@@ -44,6 +44,7 @@ export declare enum InfoType {
44
44
  ACTIVE_ASSET_DATA = "activeAssetData",
45
45
  CHECK_BUILDER_FEE_APPROVAL = "checkBuilderFeeApproval",
46
46
  USER_FEES = "userFees",
47
+ USER_ROLE = "userRole",
47
48
  EXTRA_AGENTS = "extraAgents"
48
49
  }
49
50
  export declare enum OrderType {
@@ -35,6 +35,7 @@ var ExchangeType;
35
35
  var InfoType;
36
36
  (function (InfoType) {
37
37
  InfoType["ALL_MIDS"] = "allMids";
38
+ InfoType["USER_NON_FUNDING_LEDGER_UPDATES"] = "userNonFundingLedgerUpdates";
38
39
  InfoType["USER_OPEN_ORDERS"] = "openOrders";
39
40
  InfoType["USER_FILLS"] = "userFills";
40
41
  InfoType["USER_FILLS_BY_TIME"] = "userFillsByTime";
@@ -43,7 +44,6 @@ var InfoType;
43
44
  InfoType["CANDLES_SNAPSHOT"] = "candleSnapshot";
44
45
  InfoType["META_AND_ASSET_CTXS"] = "metaAndAssetCtxs";
45
46
  InfoType["USER_FUNDING"] = "userFunding";
46
- InfoType["USER_NON_FUNDING_LEDGER_UPDATES"] = "userNonFundingLedgerUpdates";
47
47
  InfoType["FUNDING_HISTORY"] = "fundingHistory";
48
48
  InfoType["CLEARINGHOUSE_STATE"] = "clearinghouseState";
49
49
  InfoType["META"] = "meta";
@@ -52,6 +52,7 @@ var InfoType;
52
52
  InfoType["ACTIVE_ASSET_DATA"] = "activeAssetData";
53
53
  InfoType["CHECK_BUILDER_FEE_APPROVAL"] = "checkBuilderFeeApproval";
54
54
  InfoType["USER_FEES"] = "userFees";
55
+ InfoType["USER_ROLE"] = "userRole";
55
56
  InfoType["EXTRA_AGENTS"] = "extraAgents";
56
57
  })(InfoType || (exports.InfoType = InfoType = {}));
57
58
  // Order types
@@ -121,6 +121,24 @@ export interface UserFill {
121
121
  export type UserFills = {
122
122
  [asset: string]: UserFill[];
123
123
  };
124
+ export type UserNonFundingLedgerUpdates = {
125
+ time: number;
126
+ hash: string;
127
+ delta: {
128
+ type: string;
129
+ usdc?: string;
130
+ vault?: string;
131
+ user?: string;
132
+ token?: string;
133
+ amount?: string;
134
+ requestedUsd?: string;
135
+ commission?: string;
136
+ closingCost?: string;
137
+ basis?: string;
138
+ netWithdrawnUsd?: string;
139
+ toPerp?: boolean;
140
+ };
141
+ };
124
142
  export interface OrderResponse {
125
143
  status: 'ok';
126
144
  response: {
@@ -274,12 +292,12 @@ export interface ExtraAgent {
274
292
  }
275
293
  export interface ExchangeClientConfig {
276
294
  masterAddress: string;
277
- agentPrivateKey: string;
278
- agentPublicKey: string;
295
+ agentPrivateKey?: string;
296
+ agentPublicKey?: string;
279
297
  agentName?: string;
280
298
  isTestnet?: boolean;
281
299
  timeout?: number;
282
- symbolConversion: SymbolConversion;
300
+ symbolConversion?: SymbolConversion;
283
301
  }
284
302
  export interface PlaceOrderParams {
285
303
  coin: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rabby-wallet/hyperliquid-sdk",
3
- "version": "1.0.0-beta.1",
3
+ "version": "1.0.0-beta.3",
4
4
  "description": "Simplified Hyperliquid Perpetuals Trading SDK for Frontend Applications",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",