@rabby-wallet/hyperliquid-sdk 1.0.10 → 1.1.0-beta.2
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.
- package/README.md +139 -139
- package/dist/client/exchange-client.d.ts +6 -2
- package/dist/client/exchange-client.js +21 -5
- package/dist/client/websocket-client.d.ts +3 -1
- package/dist/client/websocket-client.js +74 -12
- package/dist/hyperliquid-sdk.d.ts +6 -1
- package/dist/hyperliquid-sdk.js +15 -0
- package/dist/types/index.d.ts +5 -0
- package/package.json +37 -37
package/README.md
CHANGED
|
@@ -1,140 +1,140 @@
|
|
|
1
|
-
# Hyperliquid Perpetuals SDK
|
|
2
|
-
|
|
3
|
-
一个简化的 Hyperliquid 永续合约交易 SDK,专为前端应用设计。
|
|
4
|
-
|
|
5
|
-
## 特性
|
|
6
|
-
|
|
7
|
-
- ✅ 仅核心API:最重要的交易与查询能力
|
|
8
|
-
- ✅ 永续合约专用:不包含现货功能
|
|
9
|
-
- ✅ TypeScript:完善的类型定义
|
|
10
|
-
- ✅ WebSocket:实时数据订阅
|
|
11
|
-
- ✅ 前端友好:基于 fetch API 的轻量实现
|
|
12
|
-
|
|
13
|
-
## 安装
|
|
14
|
-
|
|
15
|
-
```bash
|
|
16
|
-
yarn add @debank/hyperliquid-perp-sdk
|
|
17
|
-
# or
|
|
18
|
-
npm i @debank/hyperliquid-perp-sdk
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
## 快速开始
|
|
22
|
-
|
|
23
|
-
### 仅查询(无需私钥)
|
|
24
|
-
```ts
|
|
25
|
-
import { HyperliquidSDK } from '@debank/hyperliquid-perp-sdk';
|
|
26
|
-
|
|
27
|
-
const sdk = new HyperliquidSDK({
|
|
28
|
-
masterAddress: '0xYourEOA',
|
|
29
|
-
isTestnet: true,
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
// 获取所有中间价
|
|
33
|
-
const prices = await sdk.info.getAllMids();
|
|
34
|
-
|
|
35
|
-
// 获取市场元数据与资产上下文
|
|
36
|
-
const canUseCache = true; //默认使用缓存,多次请求并发去重 ,需要刷新传false
|
|
37
|
-
const [meta, assetCtxs] = await sdk.info.metaAndAssetCtxs(canUseCache);
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
// 获取账户综合状态
|
|
41
|
-
const account = await sdk.info.getClearingHouseState();
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
### 交易(需要提供代理私钥、公钥、名称)
|
|
45
|
-
```ts
|
|
46
|
-
const sdk = new HyperliquidSDK({
|
|
47
|
-
masterAddress: '0xYourEOA',
|
|
48
|
-
agentPrivateKey: '0xYourAgentPrivKey',
|
|
49
|
-
agentPublicKey: '0xYourAgentPubKey',
|
|
50
|
-
agentName: 'MyAgent',
|
|
51
|
-
isTestnet: true,
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
// 限价下单
|
|
55
|
-
await sdk.exchange!.placeOrder({
|
|
56
|
-
coin: 'BTC',
|
|
57
|
-
isBuy: true,
|
|
58
|
-
sz: '0.1',
|
|
59
|
-
limitPx: '45000',
|
|
60
|
-
orderType: { limit: { tif: 'Gtc' } },
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
// 批量下单
|
|
64
|
-
await sdk.exchange!.multiOrder({
|
|
65
|
-
orders: [
|
|
66
|
-
{ coin: 'ETH', isBuy: true, sz: '1', limitPx: '3000', orderType: { limit: { tif: 'Ioc' } } },
|
|
67
|
-
{ coin: 'SOL', isBuy: false, sz: '10', limitPx: '150', orderType: { limit: { tif: 'Ioc' } } },
|
|
68
|
-
],
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
// 市价开仓(带可选的 TP/SL 触发单)
|
|
72
|
-
await sdk.exchange!.marketOrderOpen({
|
|
73
|
-
coin: 'BTC',
|
|
74
|
-
isBuy: true,
|
|
75
|
-
size: '0.05',
|
|
76
|
-
midPx: '45200',
|
|
77
|
-
tpTriggerPx: '47000',
|
|
78
|
-
slTriggerPx: '44000',
|
|
79
|
-
// slippage 默认 0.08
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
// 市价平仓
|
|
83
|
-
await sdk.exchange!.marketOrderClose({
|
|
84
|
-
coin: 'BTC',
|
|
85
|
-
isBuy: false,
|
|
86
|
-
size: '0.05',
|
|
87
|
-
midPx: '45000',
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
// 绑定 TP/SL 到已有持仓(按位置绑定)
|
|
91
|
-
await sdk.exchange!.bindTpslByOrderId({
|
|
92
|
-
coin: 'BTC',
|
|
93
|
-
isBuy: true, // 持仓方向
|
|
94
|
-
tpTriggerPx: '47000',
|
|
95
|
-
slTriggerPx: '44000',
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
// 更新杠杆
|
|
99
|
-
await sdk.exchange!.updateLeverage({
|
|
100
|
-
coin: 'BTC',
|
|
101
|
-
leverage: 5,
|
|
102
|
-
isCross: true,
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
// 撤单(可批量)
|
|
106
|
-
await sdk.exchange!.cancelOrder([
|
|
107
|
-
{ coin: 'BTC', oid: 12345 },
|
|
108
|
-
{ coin: 'ETH', oid: 67890 },
|
|
109
|
-
]);
|
|
110
|
-
```
|
|
111
|
-
|
|
112
|
-
### 授权持久化代理(主钱包签名)
|
|
113
|
-
```ts
|
|
114
|
-
// 准备主钱包签名的 EIP-712 数据(Approve Agent)
|
|
115
|
-
const approve = sdk.exchange!.prepareApproveAgent();
|
|
116
|
-
// 业务方使用主钱包签名(示例)
|
|
117
|
-
const signature = await mainWallet.signTypedData(approve.domain, approve.types, approve.message);
|
|
118
|
-
// 发送请求
|
|
119
|
-
await sdk.exchange!.sendApproveAgent({ action: approve.message, nonce: approve.nonce, signature });
|
|
120
|
-
|
|
121
|
-
// 准备并签名 Builder 费用
|
|
122
|
-
const builderFee = sdk.exchange!.prepareApproveBuilderFee({ builder: '0xBuilder', maxFee: '0.1%' });
|
|
123
|
-
const sig2 = await mainWallet.signTypedData(builderFee.domain, builderFee.types, builderFee.message);
|
|
124
|
-
await sdk.exchange!.sendApproveBuilderFee({ action: builderFee.message, nonce: builderFee.nonce, signature: sig2 });
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
### WebSocket 实时数据
|
|
128
|
-
```ts
|
|
129
|
-
await sdk.connectWebSocket();
|
|
130
|
-
|
|
131
|
-
// 订阅价格
|
|
132
|
-
sdk.ws.subscribeToAllMids((prices) => {
|
|
133
|
-
console.log('价格更新:', prices);
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
// 订阅用户综合数据
|
|
137
|
-
sdk.ws.subscribeToWebData2('0xYourEOA', (webData2) => {
|
|
138
|
-
console.log('用户数据:', webData2);
|
|
139
|
-
});
|
|
1
|
+
# Hyperliquid Perpetuals SDK
|
|
2
|
+
|
|
3
|
+
一个简化的 Hyperliquid 永续合约交易 SDK,专为前端应用设计。
|
|
4
|
+
|
|
5
|
+
## 特性
|
|
6
|
+
|
|
7
|
+
- ✅ 仅核心API:最重要的交易与查询能力
|
|
8
|
+
- ✅ 永续合约专用:不包含现货功能
|
|
9
|
+
- ✅ TypeScript:完善的类型定义
|
|
10
|
+
- ✅ WebSocket:实时数据订阅
|
|
11
|
+
- ✅ 前端友好:基于 fetch API 的轻量实现
|
|
12
|
+
|
|
13
|
+
## 安装
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
yarn add @debank/hyperliquid-perp-sdk
|
|
17
|
+
# or
|
|
18
|
+
npm i @debank/hyperliquid-perp-sdk
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## 快速开始
|
|
22
|
+
|
|
23
|
+
### 仅查询(无需私钥)
|
|
24
|
+
```ts
|
|
25
|
+
import { HyperliquidSDK } from '@debank/hyperliquid-perp-sdk';
|
|
26
|
+
|
|
27
|
+
const sdk = new HyperliquidSDK({
|
|
28
|
+
masterAddress: '0xYourEOA',
|
|
29
|
+
isTestnet: true,
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// 获取所有中间价
|
|
33
|
+
const prices = await sdk.info.getAllMids();
|
|
34
|
+
|
|
35
|
+
// 获取市场元数据与资产上下文
|
|
36
|
+
const canUseCache = true; //默认使用缓存,多次请求并发去重 ,需要刷新传false
|
|
37
|
+
const [meta, assetCtxs] = await sdk.info.metaAndAssetCtxs(canUseCache);
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
// 获取账户综合状态
|
|
41
|
+
const account = await sdk.info.getClearingHouseState();
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### 交易(需要提供代理私钥、公钥、名称)
|
|
45
|
+
```ts
|
|
46
|
+
const sdk = new HyperliquidSDK({
|
|
47
|
+
masterAddress: '0xYourEOA',
|
|
48
|
+
agentPrivateKey: '0xYourAgentPrivKey',
|
|
49
|
+
agentPublicKey: '0xYourAgentPubKey',
|
|
50
|
+
agentName: 'MyAgent',
|
|
51
|
+
isTestnet: true,
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// 限价下单
|
|
55
|
+
await sdk.exchange!.placeOrder({
|
|
56
|
+
coin: 'BTC',
|
|
57
|
+
isBuy: true,
|
|
58
|
+
sz: '0.1',
|
|
59
|
+
limitPx: '45000',
|
|
60
|
+
orderType: { limit: { tif: 'Gtc' } },
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// 批量下单
|
|
64
|
+
await sdk.exchange!.multiOrder({
|
|
65
|
+
orders: [
|
|
66
|
+
{ coin: 'ETH', isBuy: true, sz: '1', limitPx: '3000', orderType: { limit: { tif: 'Ioc' } } },
|
|
67
|
+
{ coin: 'SOL', isBuy: false, sz: '10', limitPx: '150', orderType: { limit: { tif: 'Ioc' } } },
|
|
68
|
+
],
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// 市价开仓(带可选的 TP/SL 触发单)
|
|
72
|
+
await sdk.exchange!.marketOrderOpen({
|
|
73
|
+
coin: 'BTC',
|
|
74
|
+
isBuy: true,
|
|
75
|
+
size: '0.05',
|
|
76
|
+
midPx: '45200',
|
|
77
|
+
tpTriggerPx: '47000',
|
|
78
|
+
slTriggerPx: '44000',
|
|
79
|
+
// slippage 默认 0.08
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// 市价平仓
|
|
83
|
+
await sdk.exchange!.marketOrderClose({
|
|
84
|
+
coin: 'BTC',
|
|
85
|
+
isBuy: false,
|
|
86
|
+
size: '0.05',
|
|
87
|
+
midPx: '45000',
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
// 绑定 TP/SL 到已有持仓(按位置绑定)
|
|
91
|
+
await sdk.exchange!.bindTpslByOrderId({
|
|
92
|
+
coin: 'BTC',
|
|
93
|
+
isBuy: true, // 持仓方向
|
|
94
|
+
tpTriggerPx: '47000',
|
|
95
|
+
slTriggerPx: '44000',
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
// 更新杠杆
|
|
99
|
+
await sdk.exchange!.updateLeverage({
|
|
100
|
+
coin: 'BTC',
|
|
101
|
+
leverage: 5,
|
|
102
|
+
isCross: true,
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// 撤单(可批量)
|
|
106
|
+
await sdk.exchange!.cancelOrder([
|
|
107
|
+
{ coin: 'BTC', oid: 12345 },
|
|
108
|
+
{ coin: 'ETH', oid: 67890 },
|
|
109
|
+
]);
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### 授权持久化代理(主钱包签名)
|
|
113
|
+
```ts
|
|
114
|
+
// 准备主钱包签名的 EIP-712 数据(Approve Agent)
|
|
115
|
+
const approve = sdk.exchange!.prepareApproveAgent();
|
|
116
|
+
// 业务方使用主钱包签名(示例)
|
|
117
|
+
const signature = await mainWallet.signTypedData(approve.domain, approve.types, approve.message);
|
|
118
|
+
// 发送请求
|
|
119
|
+
await sdk.exchange!.sendApproveAgent({ action: approve.message, nonce: approve.nonce, signature });
|
|
120
|
+
|
|
121
|
+
// 准备并签名 Builder 费用
|
|
122
|
+
const builderFee = sdk.exchange!.prepareApproveBuilderFee({ builder: '0xBuilder', maxFee: '0.1%' });
|
|
123
|
+
const sig2 = await mainWallet.signTypedData(builderFee.domain, builderFee.types, builderFee.message);
|
|
124
|
+
await sdk.exchange!.sendApproveBuilderFee({ action: builderFee.message, nonce: builderFee.nonce, signature: sig2 });
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### WebSocket 实时数据
|
|
128
|
+
```ts
|
|
129
|
+
await sdk.connectWebSocket();
|
|
130
|
+
|
|
131
|
+
// 订阅价格
|
|
132
|
+
sdk.ws.subscribeToAllMids((prices) => {
|
|
133
|
+
console.log('价格更新:', prices);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
// 订阅用户综合数据
|
|
137
|
+
sdk.ws.subscribeToWebData2('0xYourEOA', (webData2) => {
|
|
138
|
+
console.log('用户数据:', webData2);
|
|
139
|
+
});
|
|
140
140
|
```
|
|
@@ -13,12 +13,16 @@ export declare class ExchangeClient {
|
|
|
13
13
|
private builder?;
|
|
14
14
|
private readonly symbolConversion;
|
|
15
15
|
constructor(config: ExchangeClientConfig);
|
|
16
|
-
initAccount(masterAddress: string, agentPrivateKey
|
|
16
|
+
initAccount(masterAddress: string, agentPrivateKey?: string, agentPublicKey?: string, agentName?: string): void;
|
|
17
|
+
initOrUpdateAgent(agentPrivateKey: string, agentPublicKey: string, agentName?: string): void;
|
|
18
|
+
/**
|
|
19
|
+
* Check if agent is set
|
|
20
|
+
*/
|
|
21
|
+
get isHaveAgent(): boolean;
|
|
17
22
|
/**
|
|
18
23
|
* Get the wallet address
|
|
19
24
|
*/
|
|
20
25
|
get address(): string;
|
|
21
|
-
updateExchangeAgent(agentPrivateKey: string, agentPublicKey: string, agentName?: string): void;
|
|
22
26
|
private getAgentPrivateKey;
|
|
23
27
|
/**
|
|
24
28
|
* @deprecated This method is deprecated. Use the new builder configuration in constructor instead.
|
|
@@ -82,21 +82,34 @@ class ExchangeClient {
|
|
|
82
82
|
}
|
|
83
83
|
initAccount(masterAddress, agentPrivateKey, agentPublicKey, agentName) {
|
|
84
84
|
this.masterAddress = masterAddress;
|
|
85
|
+
if (agentPrivateKey && agentPublicKey) {
|
|
86
|
+
this.agentPrivateKey = agentPrivateKey.startsWith('0x') ? agentPrivateKey : ethUtil.addHexPrefix(agentPrivateKey);
|
|
87
|
+
this.agentPublicKey = agentPublicKey.startsWith('0x') ? agentPublicKey : ethUtil.addHexPrefix(agentPublicKey);
|
|
88
|
+
this.agentName = agentName || '';
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
this.agentPrivateKey = undefined;
|
|
92
|
+
this.agentPublicKey = undefined;
|
|
93
|
+
this.agentName = undefined;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
initOrUpdateAgent(agentPrivateKey, agentPublicKey, agentName) {
|
|
85
97
|
this.agentPrivateKey = agentPrivateKey.startsWith('0x') ? agentPrivateKey : ethUtil.addHexPrefix(agentPrivateKey);
|
|
86
98
|
this.agentPublicKey = agentPublicKey.startsWith('0x') ? agentPublicKey : ethUtil.addHexPrefix(agentPublicKey);
|
|
87
99
|
this.agentName = agentName || '';
|
|
88
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* Check if agent is set
|
|
103
|
+
*/
|
|
104
|
+
get isHaveAgent() {
|
|
105
|
+
return !!this.agentPrivateKey && !!this.agentPublicKey;
|
|
106
|
+
}
|
|
89
107
|
/**
|
|
90
108
|
* Get the wallet address
|
|
91
109
|
*/
|
|
92
110
|
get address() {
|
|
93
111
|
return this.masterAddress;
|
|
94
112
|
}
|
|
95
|
-
updateExchangeAgent(agentPrivateKey, agentPublicKey, agentName) {
|
|
96
|
-
this.agentPrivateKey = agentPrivateKey.startsWith('0x') ? agentPrivateKey : ethUtil.addHexPrefix(agentPrivateKey);
|
|
97
|
-
this.agentPublicKey = agentPublicKey.startsWith('0x') ? agentPublicKey : ethUtil.addHexPrefix(agentPublicKey);
|
|
98
|
-
this.agentName = agentName || '';
|
|
99
|
-
}
|
|
100
113
|
getAgentPrivateKey() {
|
|
101
114
|
if (!this.agentPrivateKey) {
|
|
102
115
|
throw new Error('Agent private key is not set');
|
|
@@ -484,6 +497,9 @@ class ExchangeClient {
|
|
|
484
497
|
* Returns typed data that needs to be signed by the main wallet
|
|
485
498
|
*/
|
|
486
499
|
prepareApproveAgent() {
|
|
500
|
+
if (!this.isHaveAgent) {
|
|
501
|
+
throw new Error('account not have agent, please set agent first.');
|
|
502
|
+
}
|
|
487
503
|
const nonce = Date.now();
|
|
488
504
|
const action = {
|
|
489
505
|
type: constants_1.ExchangeType.APPROVE_AGENT,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AllMids, L2Book, Candle, WsOrder, WebData2, WsActiveAssetCtx, WsUserFills } from '../types';
|
|
1
|
+
import type { AllMids, L2Book, Candle, WsOrder, WebData2, WsActiveAssetCtx, WsUserFills, WsClearinghouseState } from '../types';
|
|
2
2
|
export interface WebSocketClientConfig {
|
|
3
3
|
masterAddress?: string;
|
|
4
4
|
isTestnet?: boolean;
|
|
@@ -43,6 +43,7 @@ export declare class WebSocketClient {
|
|
|
43
43
|
* Disconnect from the WebSocket
|
|
44
44
|
*/
|
|
45
45
|
disconnect(): void;
|
|
46
|
+
private getSubscriptionKey;
|
|
46
47
|
/**
|
|
47
48
|
* Subscribe to a channel
|
|
48
49
|
*/
|
|
@@ -84,6 +85,7 @@ export declare class WebSocketClient {
|
|
|
84
85
|
* Includes positions, margin summary, open orders, and other user data
|
|
85
86
|
*/
|
|
86
87
|
subscribeToWebData2(callback: SubscriptionCallback<WebData2>): Subscription;
|
|
88
|
+
subscribeToClearinghouseState(user: string | string[], callback: SubscriptionCallback<WsClearinghouseState>): Subscription;
|
|
87
89
|
/**
|
|
88
90
|
* Check if WebSocket is connected
|
|
89
91
|
*/
|
|
@@ -77,8 +77,9 @@ class WebSocketClient {
|
|
|
77
77
|
this.pendingSubscriptions.forEach(({ message, callback }) => {
|
|
78
78
|
var _a;
|
|
79
79
|
if (((_a = this.ws) === null || _a === void 0 ? void 0 : _a.readyState) === WebSocket.OPEN) {
|
|
80
|
+
const subKey = this.getSubscriptionKey(message.subscription);
|
|
80
81
|
this.subscriptions.set(message.subscription.type, callback);
|
|
81
|
-
this.activeSubscriptions.set(
|
|
82
|
+
this.activeSubscriptions.set(subKey, { message, callback });
|
|
82
83
|
this.ws.send(JSON.stringify(Object.assign(Object.assign({}, message), { method: 'subscribe' })));
|
|
83
84
|
}
|
|
84
85
|
});
|
|
@@ -87,6 +88,7 @@ class WebSocketClient {
|
|
|
87
88
|
resolve();
|
|
88
89
|
};
|
|
89
90
|
this.ws.onmessage = (event) => {
|
|
91
|
+
var _a;
|
|
90
92
|
try {
|
|
91
93
|
const data = JSON.parse(event.data);
|
|
92
94
|
// Handle pong response
|
|
@@ -95,9 +97,40 @@ class WebSocketClient {
|
|
|
95
97
|
return;
|
|
96
98
|
}
|
|
97
99
|
const type = data.channel;
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
100
|
+
// Iterate through active subscriptions to find matching ones
|
|
101
|
+
let hasDispatched = false;
|
|
102
|
+
for (const { message, callback } of this.activeSubscriptions.values()) {
|
|
103
|
+
const subParams = message.subscription;
|
|
104
|
+
if (subParams.type === type) {
|
|
105
|
+
let isMatch = true;
|
|
106
|
+
// Filter based on subscription parameters
|
|
107
|
+
if (type === 'l2Book' && data.data.coin !== subParams.coin)
|
|
108
|
+
isMatch = false;
|
|
109
|
+
if (type === 'trades' && ((_a = data.data[0]) === null || _a === void 0 ? void 0 : _a.coin) !== subParams.coin)
|
|
110
|
+
isMatch = false;
|
|
111
|
+
if (type === 'candle' && (data.data.s !== subParams.coin || data.data.i !== subParams.interval))
|
|
112
|
+
isMatch = false;
|
|
113
|
+
if (type === 'activeAssetCtx' && data.data.coin !== subParams.coin)
|
|
114
|
+
isMatch = false;
|
|
115
|
+
// For user-specific data
|
|
116
|
+
if (subParams.user) {
|
|
117
|
+
// If response data contains user, verify it matches
|
|
118
|
+
if (data.data && data.data.user && data.data.user.toLowerCase() !== subParams.user.toLowerCase()) {
|
|
119
|
+
isMatch = false;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
if (isMatch) {
|
|
123
|
+
callback(data.data);
|
|
124
|
+
hasDispatched = true;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
// Fallback to simple type map if no active subscription matched (backward compatibility or if activeSubscriptions missing)
|
|
129
|
+
if (!hasDispatched) {
|
|
130
|
+
const callback = this.subscriptions.get(type);
|
|
131
|
+
if (callback) {
|
|
132
|
+
callback(data.data);
|
|
133
|
+
}
|
|
101
134
|
}
|
|
102
135
|
}
|
|
103
136
|
catch (error) {
|
|
@@ -113,8 +146,9 @@ class WebSocketClient {
|
|
|
113
146
|
if (!this.isManualDisconnect && this.config.autoReconnect) {
|
|
114
147
|
// Save current active subscriptions to pending for reconnection
|
|
115
148
|
if (this.activeSubscriptions.size > 0) {
|
|
116
|
-
this.activeSubscriptions.forEach((subscription
|
|
117
|
-
const
|
|
149
|
+
this.activeSubscriptions.forEach((subscription) => {
|
|
150
|
+
const subKey = this.getSubscriptionKey(subscription.message.subscription);
|
|
151
|
+
const existingPending = this.pendingSubscriptions.find(sub => this.getSubscriptionKey(sub.message.subscription) === subKey);
|
|
118
152
|
if (!existingPending) {
|
|
119
153
|
this.pendingSubscriptions.push(subscription);
|
|
120
154
|
}
|
|
@@ -178,15 +212,20 @@ class WebSocketClient {
|
|
|
178
212
|
this.pendingSubscriptions = [];
|
|
179
213
|
this.reconnectAttempts = 0; // 重置重连计数
|
|
180
214
|
}
|
|
215
|
+
getSubscriptionKey(subscription) {
|
|
216
|
+
return JSON.stringify(subscription);
|
|
217
|
+
}
|
|
181
218
|
/**
|
|
182
219
|
* Subscribe to a channel
|
|
183
220
|
*/
|
|
184
221
|
subscribe(message, callback) {
|
|
185
222
|
var _a;
|
|
186
223
|
const subscriptionData = { message, callback };
|
|
224
|
+
const subKey = this.getSubscriptionKey(message.subscription);
|
|
225
|
+
const type = message.subscription.type;
|
|
187
226
|
if (((_a = this.ws) === null || _a === void 0 ? void 0 : _a.readyState) === WebSocket.OPEN) {
|
|
188
|
-
this.subscriptions.set(
|
|
189
|
-
this.activeSubscriptions.set(
|
|
227
|
+
this.subscriptions.set(type, callback);
|
|
228
|
+
this.activeSubscriptions.set(subKey, subscriptionData);
|
|
190
229
|
this.ws.send(JSON.stringify(Object.assign(Object.assign({}, message), { method: 'subscribe' })));
|
|
191
230
|
}
|
|
192
231
|
else {
|
|
@@ -200,14 +239,23 @@ class WebSocketClient {
|
|
|
200
239
|
return {
|
|
201
240
|
unsubscribe: () => {
|
|
202
241
|
var _a;
|
|
203
|
-
|
|
204
|
-
this
|
|
205
|
-
|
|
242
|
+
this.activeSubscriptions.delete(subKey);
|
|
243
|
+
// Only remove the global callback if no other active subscriptions use this channel type
|
|
244
|
+
let hasOtherSubs = false;
|
|
245
|
+
for (const sub of this.activeSubscriptions.values()) {
|
|
246
|
+
if (sub.message.subscription.type === type) {
|
|
247
|
+
hasOtherSubs = true;
|
|
248
|
+
break;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
if (!hasOtherSubs) {
|
|
252
|
+
this.subscriptions.delete(type);
|
|
253
|
+
}
|
|
206
254
|
if (((_a = this.ws) === null || _a === void 0 ? void 0 : _a.readyState) === WebSocket.OPEN) {
|
|
207
255
|
this.ws.send(JSON.stringify(Object.assign(Object.assign({}, message), { method: 'unsubscribe' })));
|
|
208
256
|
}
|
|
209
257
|
// Remove from pending subscriptions
|
|
210
|
-
this.pendingSubscriptions = this.pendingSubscriptions.filter(sub => sub.message.subscription
|
|
258
|
+
this.pendingSubscriptions = this.pendingSubscriptions.filter(sub => this.getSubscriptionKey(sub.message.subscription) !== subKey);
|
|
211
259
|
},
|
|
212
260
|
};
|
|
213
261
|
}
|
|
@@ -296,6 +344,20 @@ class WebSocketClient {
|
|
|
296
344
|
subscription: { type: 'webData2', user: this.config.masterAddress },
|
|
297
345
|
}, callback);
|
|
298
346
|
}
|
|
347
|
+
subscribeToClearinghouseState(user, callback) {
|
|
348
|
+
const users = Array.isArray(user) ? user : [user];
|
|
349
|
+
if (users.length === 0) {
|
|
350
|
+
throw new Error('user address is empty');
|
|
351
|
+
}
|
|
352
|
+
const subscriptions = users.map(u => this.subscribe({
|
|
353
|
+
subscription: { type: 'clearinghouseState', user: u || this.config.masterAddress },
|
|
354
|
+
}, callback));
|
|
355
|
+
return {
|
|
356
|
+
unsubscribe: () => {
|
|
357
|
+
subscriptions.forEach(sub => sub.unsubscribe());
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
}
|
|
299
361
|
/**
|
|
300
362
|
* Check if WebSocket is connected
|
|
301
363
|
*/
|
|
@@ -29,7 +29,12 @@ export declare class HyperliquidSDK {
|
|
|
29
29
|
* Get wallet address (only available if private key was provided)
|
|
30
30
|
*/
|
|
31
31
|
get address(): string;
|
|
32
|
-
initAccount(masterAddress: string, agentPrivateKey
|
|
32
|
+
initAccount(masterAddress: string, agentPrivateKey?: string, agentPublicKey?: string, agentName?: string): void;
|
|
33
|
+
/**
|
|
34
|
+
* Check if agent is set for trading
|
|
35
|
+
*/
|
|
36
|
+
get isHaveAgent(): boolean;
|
|
37
|
+
initOrUpdateAgent(agentPrivateKey: string, agentPublicKey: string, agentName?: string): void;
|
|
33
38
|
/**
|
|
34
39
|
* Connect to WebSocket for real-time data
|
|
35
40
|
*/
|
package/dist/hyperliquid-sdk.js
CHANGED
|
@@ -71,6 +71,21 @@ class HyperliquidSDK {
|
|
|
71
71
|
});
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* Check if agent is set for trading
|
|
76
|
+
*/
|
|
77
|
+
get isHaveAgent() {
|
|
78
|
+
var _a;
|
|
79
|
+
return ((_a = this.exchange) === null || _a === void 0 ? void 0 : _a.isHaveAgent) || false;
|
|
80
|
+
}
|
|
81
|
+
initOrUpdateAgent(agentPrivateKey, agentPublicKey, agentName) {
|
|
82
|
+
if (!this.exchange) {
|
|
83
|
+
throw new Error('no exchange client, first initAccount');
|
|
84
|
+
}
|
|
85
|
+
if (this.exchange) {
|
|
86
|
+
this.exchange.initOrUpdateAgent(agentPrivateKey, agentPublicKey, agentName);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
74
89
|
/**
|
|
75
90
|
* Connect to WebSocket for real-time data
|
|
76
91
|
*/
|
package/dist/types/index.d.ts
CHANGED
|
@@ -96,6 +96,11 @@ export type Meta = {
|
|
|
96
96
|
universe: AssetInfo[];
|
|
97
97
|
marginTables: [number, MarginTable][];
|
|
98
98
|
};
|
|
99
|
+
export interface WsClearinghouseState {
|
|
100
|
+
dex: any;
|
|
101
|
+
user: string;
|
|
102
|
+
clearinghouseState: ClearinghouseState;
|
|
103
|
+
}
|
|
99
104
|
export interface ClearinghouseState {
|
|
100
105
|
assetPositions: AssetPosition[];
|
|
101
106
|
crossMaintenanceMarginUsed: string;
|
package/package.json
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@rabby-wallet/hyperliquid-sdk",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "Simplified Hyperliquid Perpetuals Trading SDK for Frontend Applications",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"types": "dist/index.d.ts",
|
|
7
|
-
"files": [
|
|
8
|
-
"dist"
|
|
9
|
-
],
|
|
10
|
-
"publishConfig": {
|
|
11
|
-
"access": "public"
|
|
12
|
-
},
|
|
13
|
-
"scripts": {
|
|
14
|
-
"build": "rimraf dist && tsc",
|
|
15
|
-
"preversion": "npm run build",
|
|
16
|
-
"prepublishOnly": "npm run build"
|
|
17
|
-
},
|
|
18
|
-
"author": "",
|
|
19
|
-
"license": "UNLICENSED",
|
|
20
|
-
"dependencies": {
|
|
21
|
-
"@ethereumjs/util": "9.0.0",
|
|
22
|
-
"@metamask/eth-sig-util": "5.1.0",
|
|
23
|
-
"@msgpack/msgpack": "3.1.2",
|
|
24
|
-
"@noble/hashes": "1.8.0",
|
|
25
|
-
"ethereum-cryptography": "2.2.1"
|
|
26
|
-
},
|
|
27
|
-
"devDependencies": {
|
|
28
|
-
"@types/node": "24.3.1",
|
|
29
|
-
"prettier": "2.7.1",
|
|
30
|
-
"rimraf": "6.0.0",
|
|
31
|
-
"typescript": "5.9.2"
|
|
32
|
-
},
|
|
33
|
-
"engines": {
|
|
34
|
-
"node": ">=16.0.0"
|
|
35
|
-
},
|
|
36
|
-
"packageManager": "yarn@1.22.22"
|
|
37
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@rabby-wallet/hyperliquid-sdk",
|
|
3
|
+
"version": "1.1.0-beta.2",
|
|
4
|
+
"description": "Simplified Hyperliquid Perpetuals Trading SDK for Frontend Applications",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "rimraf dist && tsc",
|
|
15
|
+
"preversion": "npm run build",
|
|
16
|
+
"prepublishOnly": "npm run build"
|
|
17
|
+
},
|
|
18
|
+
"author": "",
|
|
19
|
+
"license": "UNLICENSED",
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@ethereumjs/util": "9.0.0",
|
|
22
|
+
"@metamask/eth-sig-util": "5.1.0",
|
|
23
|
+
"@msgpack/msgpack": "3.1.2",
|
|
24
|
+
"@noble/hashes": "1.8.0",
|
|
25
|
+
"ethereum-cryptography": "2.2.1"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/node": "24.3.1",
|
|
29
|
+
"prettier": "2.7.1",
|
|
30
|
+
"rimraf": "6.0.0",
|
|
31
|
+
"typescript": "5.9.2"
|
|
32
|
+
},
|
|
33
|
+
"engines": {
|
|
34
|
+
"node": ">=16.0.0"
|
|
35
|
+
},
|
|
36
|
+
"packageManager": "yarn@1.22.22"
|
|
37
|
+
}
|