@nktkas/hyperliquid 0.22.1 → 0.23.0

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.
Files changed (230) hide show
  1. package/README.md +125 -70
  2. package/esm/mod.d.ts +1 -1
  3. package/esm/mod.d.ts.map +1 -1
  4. package/esm/src/base.d.ts +4 -1
  5. package/esm/src/base.d.ts.map +1 -1
  6. package/esm/src/base.js +2 -2
  7. package/esm/src/clients/exchange.d.ts +420 -368
  8. package/esm/src/clients/exchange.d.ts.map +1 -1
  9. package/esm/src/clients/exchange.js +721 -551
  10. package/esm/src/clients/info.d.ts +137 -41
  11. package/esm/src/clients/info.d.ts.map +1 -1
  12. package/esm/src/clients/info.js +124 -28
  13. package/esm/src/clients/multiSign.d.ts +406 -484
  14. package/esm/src/clients/multiSign.d.ts.map +1 -1
  15. package/esm/src/clients/multiSign.js +639 -734
  16. package/esm/src/clients/subscription.d.ts +38 -3
  17. package/esm/src/clients/subscription.d.ts.map +1 -1
  18. package/esm/src/clients/subscription.js +36 -3
  19. package/esm/src/signing/_signTypedData/ethers.d.ts +33 -0
  20. package/esm/src/signing/_signTypedData/ethers.d.ts.map +1 -0
  21. package/esm/src/signing/_signTypedData/ethers.js +12 -0
  22. package/esm/src/signing/_signTypedData/mod.d.ts +35 -0
  23. package/esm/src/signing/_signTypedData/mod.d.ts.map +1 -0
  24. package/esm/src/signing/_signTypedData/mod.js +59 -0
  25. package/esm/src/signing/_signTypedData/private_key.d.ts +26 -0
  26. package/esm/src/signing/_signTypedData/private_key.d.ts.map +1 -0
  27. package/esm/src/signing/_signTypedData/private_key.js +144 -0
  28. package/esm/src/signing/_signTypedData/viem.d.ts +23 -0
  29. package/esm/src/signing/_signTypedData/viem.d.ts.map +1 -0
  30. package/esm/src/signing/_signTypedData/viem.js +6 -0
  31. package/esm/src/signing/_signTypedData/window.d.ts +29 -0
  32. package/esm/src/signing/_signTypedData/window.d.ts.map +1 -0
  33. package/esm/src/signing/_signTypedData/window.js +30 -0
  34. package/esm/src/signing/_sorter.d.ts +128 -0
  35. package/esm/src/signing/_sorter.d.ts.map +1 -0
  36. package/esm/src/{signing.js → signing/_sorter.js} +36 -449
  37. package/esm/src/signing/mod.d.ts +272 -0
  38. package/esm/src/signing/mod.d.ts.map +1 -0
  39. package/esm/src/signing/mod.js +306 -0
  40. package/esm/src/transports/base.d.ts +1 -1
  41. package/esm/src/transports/base.d.ts.map +1 -1
  42. package/esm/src/transports/base.js +2 -2
  43. package/esm/src/transports/http/http_transport.d.ts +9 -15
  44. package/esm/src/transports/http/http_transport.d.ts.map +1 -1
  45. package/esm/src/transports/http/http_transport.js +64 -59
  46. package/esm/src/transports/websocket/_hyperliquid_event_target.d.ts +1 -1
  47. package/esm/src/transports/websocket/_hyperliquid_event_target.d.ts.map +1 -1
  48. package/esm/src/transports/websocket/_hyperliquid_event_target.js +1 -1
  49. package/esm/src/transports/websocket/_reconnecting_websocket.d.ts.map +1 -1
  50. package/esm/src/transports/websocket/_reconnecting_websocket.js +17 -4
  51. package/esm/src/transports/websocket/_websocket_async_request.d.ts +0 -9
  52. package/esm/src/transports/websocket/_websocket_async_request.d.ts.map +1 -1
  53. package/esm/src/transports/websocket/_websocket_async_request.js +14 -20
  54. package/esm/src/transports/websocket/websocket_transport.d.ts +8 -4
  55. package/esm/src/transports/websocket/websocket_transport.d.ts.map +1 -1
  56. package/esm/src/transports/websocket/websocket_transport.js +83 -54
  57. package/esm/src/types/exchange/requests.d.ts +27 -51
  58. package/esm/src/types/exchange/requests.d.ts.map +1 -1
  59. package/esm/src/types/explorer/requests.d.ts +5 -8
  60. package/esm/src/types/explorer/requests.d.ts.map +1 -1
  61. package/esm/src/types/info/accounts.d.ts +5 -5
  62. package/esm/src/types/info/accounts.d.ts.map +1 -1
  63. package/esm/src/types/info/delegations.d.ts +1 -1
  64. package/esm/src/types/info/delegations.d.ts.map +1 -1
  65. package/esm/src/types/info/orders.d.ts +1 -1
  66. package/esm/src/types/info/orders.d.ts.map +1 -1
  67. package/esm/src/types/info/requests.d.ts +49 -96
  68. package/esm/src/types/info/requests.d.ts.map +1 -1
  69. package/esm/src/types/subscriptions/requests.d.ts +72 -18
  70. package/esm/src/types/subscriptions/requests.d.ts.map +1 -1
  71. package/esm/src/types/subscriptions/responses.d.ts +2 -0
  72. package/esm/src/types/subscriptions/responses.d.ts.map +1 -1
  73. package/package.json +12 -8
  74. package/script/mod.d.ts +1 -1
  75. package/script/mod.d.ts.map +1 -1
  76. package/script/mod.js +13 -23
  77. package/script/src/base.d.ts +4 -1
  78. package/script/src/base.d.ts.map +1 -1
  79. package/script/src/base.js +10 -20
  80. package/script/src/clients/exchange.d.ts +420 -368
  81. package/script/src/clients/exchange.d.ts.map +1 -1
  82. package/script/src/clients/exchange.js +2000 -1840
  83. package/script/src/clients/info.d.ts +137 -41
  84. package/script/src/clients/info.d.ts.map +1 -1
  85. package/script/src/clients/info.js +1296 -1210
  86. package/script/src/clients/multiSign.d.ts +406 -484
  87. package/script/src/clients/multiSign.d.ts.map +1 -1
  88. package/script/src/clients/multiSign.js +2043 -2148
  89. package/script/src/clients/subscription.d.ts +38 -3
  90. package/script/src/clients/subscription.d.ts.map +1 -1
  91. package/script/src/clients/subscription.js +568 -545
  92. package/script/src/signing/_signTypedData/ethers.d.ts +33 -0
  93. package/script/src/signing/_signTypedData/ethers.d.ts.map +1 -0
  94. package/script/src/signing/_signTypedData/ethers.js +16 -0
  95. package/script/src/signing/_signTypedData/mod.d.ts +35 -0
  96. package/script/src/signing/_signTypedData/mod.d.ts.map +1 -0
  97. package/script/src/signing/_signTypedData/mod.js +67 -0
  98. package/script/src/signing/_signTypedData/private_key.d.ts +26 -0
  99. package/script/src/signing/_signTypedData/private_key.d.ts.map +1 -0
  100. package/script/src/signing/_signTypedData/private_key.js +148 -0
  101. package/script/src/signing/_signTypedData/viem.d.ts +23 -0
  102. package/script/src/signing/_signTypedData/viem.d.ts.map +1 -0
  103. package/script/src/signing/_signTypedData/viem.js +9 -0
  104. package/script/src/signing/_signTypedData/window.d.ts +29 -0
  105. package/script/src/signing/_signTypedData/window.d.ts.map +1 -0
  106. package/script/src/signing/_signTypedData/window.js +34 -0
  107. package/script/src/signing/_sorter.d.ts +128 -0
  108. package/script/src/signing/_sorter.d.ts.map +1 -0
  109. package/script/src/signing/_sorter.js +698 -0
  110. package/script/src/signing/mod.d.ts +272 -0
  111. package/script/src/signing/mod.d.ts.map +1 -0
  112. package/script/src/signing/mod.js +331 -0
  113. package/script/src/transports/base.d.ts +1 -1
  114. package/script/src/transports/base.d.ts.map +1 -1
  115. package/script/src/transports/base.js +11 -21
  116. package/script/src/transports/http/http_transport.d.ts +9 -15
  117. package/script/src/transports/http/http_transport.d.ts.map +1 -1
  118. package/script/src/transports/http/http_transport.js +97 -102
  119. package/script/src/transports/websocket/_hyperliquid_event_target.d.ts +1 -1
  120. package/script/src/transports/websocket/_hyperliquid_event_target.d.ts.map +1 -1
  121. package/script/src/transports/websocket/_hyperliquid_event_target.js +51 -61
  122. package/script/src/transports/websocket/_reconnecting_websocket.d.ts.map +1 -1
  123. package/script/src/transports/websocket/_reconnecting_websocket.js +271 -268
  124. package/script/src/transports/websocket/_websocket_async_request.d.ts +0 -9
  125. package/script/src/transports/websocket/_websocket_async_request.d.ts.map +1 -1
  126. package/script/src/transports/websocket/_websocket_async_request.js +157 -174
  127. package/script/src/transports/websocket/websocket_transport.d.ts +8 -4
  128. package/script/src/transports/websocket/websocket_transport.d.ts.map +1 -1
  129. package/script/src/transports/websocket/websocket_transport.js +209 -189
  130. package/script/src/types/exchange/requests.d.ts +27 -51
  131. package/script/src/types/exchange/requests.d.ts.map +1 -1
  132. package/script/src/types/exchange/requests.js +2 -12
  133. package/script/src/types/exchange/responses.js +2 -12
  134. package/script/src/types/explorer/requests.d.ts +5 -8
  135. package/script/src/types/explorer/requests.d.ts.map +1 -1
  136. package/script/src/types/explorer/requests.js +2 -12
  137. package/script/src/types/explorer/responses.js +2 -12
  138. package/script/src/types/info/accounts.d.ts +5 -5
  139. package/script/src/types/info/accounts.d.ts.map +1 -1
  140. package/script/src/types/info/accounts.js +2 -12
  141. package/script/src/types/info/assets.js +2 -12
  142. package/script/src/types/info/delegations.d.ts +1 -1
  143. package/script/src/types/info/delegations.d.ts.map +1 -1
  144. package/script/src/types/info/delegations.js +2 -12
  145. package/script/src/types/info/markets.js +2 -12
  146. package/script/src/types/info/orders.d.ts +1 -1
  147. package/script/src/types/info/orders.d.ts.map +1 -1
  148. package/script/src/types/info/orders.js +2 -12
  149. package/script/src/types/info/requests.d.ts +49 -96
  150. package/script/src/types/info/requests.d.ts.map +1 -1
  151. package/script/src/types/info/requests.js +2 -12
  152. package/script/src/types/info/vaults.js +2 -12
  153. package/script/src/types/mod.js +2 -12
  154. package/script/src/types/subscriptions/requests.d.ts +72 -18
  155. package/script/src/types/subscriptions/requests.d.ts.map +1 -1
  156. package/script/src/types/subscriptions/requests.js +2 -12
  157. package/script/src/types/subscriptions/responses.d.ts +2 -0
  158. package/script/src/types/subscriptions/responses.d.ts.map +1 -1
  159. package/script/src/types/subscriptions/responses.js +2 -12
  160. package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.d.ts +0 -2
  161. package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.d.ts.map +0 -1
  162. package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.js +0 -1
  163. package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.d.ts +0 -95
  164. package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.d.ts.map +0 -1
  165. package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.js +0 -10
  166. package/esm/deps/jsr.io/@std/async/1.0.13/delay.d.ts +0 -43
  167. package/esm/deps/jsr.io/@std/async/1.0.13/delay.d.ts.map +0 -1
  168. package/esm/deps/jsr.io/@std/async/1.0.13/delay.js +0 -63
  169. package/esm/deps/jsr.io/@std/bytes/1.0.6/_types.d.ts +0 -9
  170. package/esm/deps/jsr.io/@std/bytes/1.0.6/_types.d.ts.map +0 -1
  171. package/esm/deps/jsr.io/@std/bytes/1.0.6/_types.js +0 -2
  172. package/esm/deps/jsr.io/@std/bytes/1.0.6/concat.d.ts +0 -21
  173. package/esm/deps/jsr.io/@std/bytes/1.0.6/concat.d.ts.map +0 -1
  174. package/esm/deps/jsr.io/@std/bytes/1.0.6/concat.js +0 -32
  175. package/esm/deps/jsr.io/@std/encoding/1.0.10/_common16.d.ts +0 -23
  176. package/esm/deps/jsr.io/@std/encoding/1.0.10/_common16.d.ts.map +0 -1
  177. package/esm/deps/jsr.io/@std/encoding/1.0.10/_common16.js +0 -51
  178. package/esm/deps/jsr.io/@std/encoding/1.0.10/_common_detach.d.ts +0 -4
  179. package/esm/deps/jsr.io/@std/encoding/1.0.10/_common_detach.d.ts.map +0 -1
  180. package/esm/deps/jsr.io/@std/encoding/1.0.10/_common_detach.js +0 -13
  181. package/esm/deps/jsr.io/@std/encoding/1.0.10/_types.d.ts +0 -9
  182. package/esm/deps/jsr.io/@std/encoding/1.0.10/_types.d.ts.map +0 -1
  183. package/esm/deps/jsr.io/@std/encoding/1.0.10/_types.js +0 -2
  184. package/esm/deps/jsr.io/@std/encoding/1.0.10/hex.d.ts +0 -39
  185. package/esm/deps/jsr.io/@std/encoding/1.0.10/hex.d.ts.map +0 -1
  186. package/esm/deps/jsr.io/@std/encoding/1.0.10/hex.js +0 -87
  187. package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts +0 -9
  188. package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts.map +0 -1
  189. package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.js +0 -2
  190. package/esm/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts +0 -39
  191. package/esm/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts.map +0 -1
  192. package/esm/deps/jsr.io/@std/msgpack/1.0.3/encode.js +0 -237
  193. package/esm/src/signing.d.ts +0 -463
  194. package/esm/src/signing.d.ts.map +0 -1
  195. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.d.ts +0 -2
  196. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.d.ts.map +0 -1
  197. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.js +0 -27
  198. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.d.ts +0 -95
  199. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.d.ts.map +0 -1
  200. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.js +0 -24
  201. package/script/deps/jsr.io/@std/async/1.0.13/delay.d.ts +0 -43
  202. package/script/deps/jsr.io/@std/async/1.0.13/delay.d.ts.map +0 -1
  203. package/script/deps/jsr.io/@std/async/1.0.13/delay.js +0 -76
  204. package/script/deps/jsr.io/@std/bytes/1.0.6/_types.d.ts +0 -9
  205. package/script/deps/jsr.io/@std/bytes/1.0.6/_types.d.ts.map +0 -1
  206. package/script/deps/jsr.io/@std/bytes/1.0.6/_types.js +0 -13
  207. package/script/deps/jsr.io/@std/bytes/1.0.6/concat.d.ts +0 -21
  208. package/script/deps/jsr.io/@std/bytes/1.0.6/concat.d.ts.map +0 -1
  209. package/script/deps/jsr.io/@std/bytes/1.0.6/concat.js +0 -45
  210. package/script/deps/jsr.io/@std/encoding/1.0.10/_common16.d.ts +0 -23
  211. package/script/deps/jsr.io/@std/encoding/1.0.10/_common16.d.ts.map +0 -1
  212. package/script/deps/jsr.io/@std/encoding/1.0.10/_common16.js +0 -67
  213. package/script/deps/jsr.io/@std/encoding/1.0.10/_common_detach.d.ts +0 -4
  214. package/script/deps/jsr.io/@std/encoding/1.0.10/_common_detach.d.ts.map +0 -1
  215. package/script/deps/jsr.io/@std/encoding/1.0.10/_common_detach.js +0 -26
  216. package/script/deps/jsr.io/@std/encoding/1.0.10/_types.d.ts +0 -9
  217. package/script/deps/jsr.io/@std/encoding/1.0.10/_types.d.ts.map +0 -1
  218. package/script/deps/jsr.io/@std/encoding/1.0.10/_types.js +0 -13
  219. package/script/deps/jsr.io/@std/encoding/1.0.10/hex.d.ts +0 -39
  220. package/script/deps/jsr.io/@std/encoding/1.0.10/hex.d.ts.map +0 -1
  221. package/script/deps/jsr.io/@std/encoding/1.0.10/hex.js +0 -101
  222. package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts +0 -9
  223. package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts.map +0 -1
  224. package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.js +0 -13
  225. package/script/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts +0 -39
  226. package/script/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts.map +0 -1
  227. package/script/deps/jsr.io/@std/msgpack/1.0.3/encode.js +0 -250
  228. package/script/src/signing.d.ts +0 -463
  229. package/script/src/signing.d.ts.map +0 -1
  230. package/script/src/signing.js +0 -1129
package/README.md CHANGED
@@ -1,9 +1,9 @@
1
1
  # Hyperliquid API TypeScript SDK
2
2
 
3
- [![NPM](https://img.shields.io/npm/v/@nktkas/hyperliquid?style=flat-square&color=blue)](https://www.npmjs.com/package/@nktkas/hyperliquid)
4
- [![JSR](https://img.shields.io/jsr/v/@nktkas/hyperliquid?style=flat-square&color=blue)](https://jsr.io/@nktkas/hyperliquid)
5
- [![Coveralls](https://img.shields.io/coverallsCoverage/github/nktkas/hyperliquid?style=flat-square)](https://coveralls.io/github/nktkas/hyperliquid)
6
- [![bundlejs](https://img.shields.io/bundlejs/size/@nktkas/hyperliquid?style=flat-square)](https://bundlejs.com/?q=@nktkas/hyperliquid)
3
+ [![npm](https://img.shields.io/npm/v/@nktkas/hyperliquid?style=flat-square&color=blue)](https://www.npmjs.com/package/@nktkas/hyperliquid)
4
+ [![jsr](https://img.shields.io/jsr/v/@nktkas/hyperliquid?style=flat-square&color=blue)](https://jsr.io/@nktkas/hyperliquid)
5
+ [![coveralls](https://img.shields.io/coverallsCoverage/github/nktkas/hyperliquid?style=flat-square)](https://coveralls.io/github/nktkas/hyperliquid)
6
+ [![bundlephobia](https://img.shields.io/bundlephobia/minzip/@nktkas/hyperliquid?style=flat-square)](https://bundlephobia.com/package/@nktkas/hyperliquid)
7
7
 
8
8
  Unofficial [Hyperliquid API](https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api) SDK for all major JS
9
9
  runtimes, written in TypeScript and provided with tests.
@@ -44,10 +44,71 @@ deno add jsr:@nktkas/hyperliquid
44
44
  ```html
45
45
  <script type="module">
46
46
  import * as hl from "https://esm.sh/jsr/@nktkas/hyperliquid";
47
- // Use hl.InfoClient, hl.ExchangeClient, etc.
48
47
  </script>
49
48
  ```
50
49
 
50
+ ### React Native
51
+
52
+ <details>
53
+ <summary>For React Native, you need to import polyfills before importing the SDK:</summary>
54
+
55
+ ```js
56
+ // React Native v0.76.3 / Expo v52
57
+ // Issues:
58
+ // - signing: does not support private keys directly, use viem or ethers
59
+ import { Event, EventTarget } from "event-target-shim";
60
+
61
+ if (!globalThis.EventTarget || !globalThis.Event) {
62
+ globalThis.EventTarget = EventTarget;
63
+ globalThis.Event = Event;
64
+ }
65
+
66
+ if (!globalThis.CustomEvent) {
67
+ globalThis.CustomEvent = function (type, params) {
68
+ params = params || {};
69
+ const event = new Event(type, params);
70
+ event.detail = params.detail || null;
71
+ return event;
72
+ };
73
+ }
74
+
75
+ if (!AbortSignal.timeout) {
76
+ AbortSignal.timeout = function (delay) {
77
+ const controller = new AbortController();
78
+ setTimeout(() => controller.abort(), delay);
79
+ return controller.signal;
80
+ };
81
+ }
82
+
83
+ if (!Promise.withResolvers) {
84
+ Promise.withResolvers = function () {
85
+ let resolve, reject;
86
+ const promise = new Promise((res, rej) => {
87
+ resolve = res;
88
+ reject = rej;
89
+ });
90
+ return { promise, resolve, reject };
91
+ };
92
+ }
93
+
94
+ if (!ArrayBuffer.prototype.transfer) {
95
+ ArrayBuffer.prototype.transfer = function (newByteLength) {
96
+ const length = newByteLength ?? this.byteLength;
97
+ const newBuffer = new ArrayBuffer(length);
98
+ const oldView = new Uint8Array(this);
99
+ const newView = new Uint8Array(newBuffer);
100
+
101
+ newView.set(oldView.subarray(0, Math.min(oldView.length, length)));
102
+
103
+ Object.defineProperty(this, "byteLength", { value: 0 });
104
+
105
+ return newBuffer;
106
+ };
107
+ }
108
+ ```
109
+
110
+ </details>
111
+
51
112
  ## Quick Start
52
113
 
53
114
  #### Info endpoint
@@ -65,12 +126,11 @@ const openOrders = await infoClient.openOrders({ user: "0x..." });
65
126
 
66
127
  ```ts
67
128
  import * as hl from "@nktkas/hyperliquid";
68
- import { privateKeyToAccount } from "viem/accounts"; // or other wallet libraries
69
129
 
70
- const wallet = privateKeyToAccount("0x...");
130
+ const privateKey = "0x..."; // or `viem`, `ethers`
71
131
 
72
132
  const transport = new hl.HttpTransport();
73
- const exchClient = new hl.ExchangeClient({ wallet, transport });
133
+ const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
74
134
 
75
135
  const result = await exchClient.order({
76
136
  orders: [{
@@ -108,27 +168,22 @@ await sub.unsubscribe(); // unsubscribe from the event
108
168
 
109
169
  ```ts
110
170
  import * as hl from "@nktkas/hyperliquid";
111
- import { privateKeyToAccount } from "viem/accounts"; // or other wallet libraries
112
171
 
113
172
  const multiSignAddress = "0x...";
114
173
  const signers = [
115
- privateKeyToAccount("0x..."), // first is leader
116
- privateKeyToAccount("0x..."), // can be a custom async wallet
117
- // ...
118
- privateKeyToAccount("0x..."),
119
- ];
174
+ "0x...", // Private key; or any other wallet libraries
175
+ ] as const;
120
176
 
121
177
  const transport = new hl.HttpTransport();
122
- const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers }); // extends ExchangeClient
178
+ const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers }); // extends `ExchangeClient`
123
179
 
124
- const data = await multiSignClient.approveAgent({ // same API as ExchangeClient
180
+ const data = await multiSignClient.approveAgent({ // same API as `ExchangeClient`
125
181
  agentAddress: "0x...",
126
182
  agentName: "agentName",
127
183
  });
128
184
  ```
129
185
 
130
- <details>
131
- <summary><h2>Usage</h2></summary>
186
+ ## Usage
132
187
 
133
188
  ### 1) Initialize Transport
134
189
 
@@ -137,11 +192,11 @@ First, choose and configure your transport layer (more details in the [API Refer
137
192
  ```ts
138
193
  import * as hl from "@nktkas/hyperliquid";
139
194
 
140
- // HTTP Transport
141
- const httpTransport = new hl.HttpTransport(); // Accepts optional parameters
195
+ // 1. HTTP Transport: suitable for one-time requests or serverless environments
196
+ const httpTransport = new hl.HttpTransport(); // Accepts optional parameters (e.g. isTestnet, timeout, etc.)
142
197
 
143
- // WebSocket Transport
144
- const wsTransport = new hl.WebSocketTransport(); // Accepts optional parameters
198
+ // 2. WebSocket Transport: has better network latency than HTTP transport
199
+ const wsTransport = new hl.WebSocketTransport(); // Accepts optional parameters (e.g. url, timeout, reconnect, etc.)
145
200
  ```
146
201
 
147
202
  ### 2) Initialize Client
@@ -153,7 +208,7 @@ Next, initialize a client with the transport layer (more details in the [API Ref
153
208
  ```ts
154
209
  import * as hl from "@nktkas/hyperliquid";
155
210
 
156
- const transport = new hl.HttpTransport(); // or WebSocketTransport
211
+ const transport = new hl.HttpTransport(); // or `WebSocketTransport`
157
212
  const infoClient = new hl.InfoClient({ transport });
158
213
  ```
159
214
 
@@ -165,22 +220,26 @@ import { createWalletClient, custom } from "viem";
165
220
  import { privateKeyToAccount } from "viem/accounts";
166
221
  import { ethers } from "ethers";
167
222
 
168
- const transport = new hl.HttpTransport(); // or WebSocketTransport
223
+ const transport = new hl.HttpTransport(); // or `WebSocketTransport`
224
+
225
+ // 1. Using private key directly
226
+ const privateKey = "0x...";
227
+ const exchClient_privateKey = new hl.ExchangeClient({ wallet: privateKey, transport });
169
228
 
170
- // 1. Using Viem with private key
229
+ // 2. Using Viem
171
230
  const viemAccount = privateKeyToAccount("0x...");
172
231
  const exchClient_viem = new hl.ExchangeClient({ wallet: viemAccount, transport });
173
232
 
174
- // 2. Using Ethers (or Ethers V5) with private key
233
+ // 3. Using Ethers (or Ethers V5)
175
234
  const ethersWallet = new ethers.Wallet("0x...");
176
235
  const exchClient_ethers = new hl.ExchangeClient({ wallet: ethersWallet, transport });
177
236
 
178
- // 3. Using external wallet (e.g. MetaMask) via Viem
237
+ // 4. Using external wallet (e.g. MetaMask) via Viem
179
238
  const [account] = await window.ethereum.request({ method: "eth_requestAccounts" });
180
239
  const externalWallet = createWalletClient({ account, transport: custom(window.ethereum) });
181
240
  const exchClient_viemMetamask = new hl.ExchangeClient({ wallet: externalWallet, transport });
182
241
 
183
- // 4. Using external wallet (e.g. MetaMask) via `window.ethereum` (EIP-1193)
242
+ // 5. Using external wallet (e.g. MetaMask) via `window.ethereum`
184
243
  const exchClient_windowMetamask = new hl.ExchangeClient({ wallet: window.ethereum, transport });
185
244
  ```
186
245
 
@@ -189,7 +248,7 @@ const exchClient_windowMetamask = new hl.ExchangeClient({ wallet: window.ethereu
189
248
  ```ts
190
249
  import * as hl from "@nktkas/hyperliquid";
191
250
 
192
- const transport = new hl.WebSocketTransport(); // only WebSocketTransport
251
+ const transport = new hl.WebSocketTransport(); // only `WebSocketTransport`
193
252
  const subsClient = new hl.SubscriptionClient({ transport });
194
253
  ```
195
254
 
@@ -203,8 +262,9 @@ import { ethers } from "ethers";
203
262
  const multiSignAddress = "0x...";
204
263
  const signers = [
205
264
  privateKeyToAccount("0x..."), // first is leader for multi-sign transaction, must contain own address
265
+ new ethers.Wallet("0x..."),
206
266
  { // can be a custom async wallet
207
- signTypedData(params: {
267
+ async signTypedData(params: {
208
268
  domain: {
209
269
  name: string;
210
270
  version: string;
@@ -221,15 +281,14 @@ const signers = [
221
281
  message: Record<string, unknown>;
222
282
  }): Promise<Hex> {
223
283
  // Custom signer logic
224
- return "0x..."; // return signature
284
+ return "0x..."; // return hex signature
225
285
  },
226
286
  },
227
- // ...
228
- new ethers.Wallet("0x..."),
287
+ "0x...", // private key directly
229
288
  ];
230
289
 
231
290
  const transport = new hl.HttpTransport();
232
- const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers }); // extends ExchangeClient
291
+ const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers }); // extends `ExchangeClient`
233
292
  ```
234
293
 
235
294
  ### 3) Use Client
@@ -258,12 +317,11 @@ const openOrders = await infoClient.openOrders({ user: "0x..." });
258
317
 
259
318
  ```ts
260
319
  import * as hl from "@nktkas/hyperliquid";
261
- import { privateKeyToAccount } from "viem/accounts";
262
320
 
263
- const account = privateKeyToAccount("0x...");
321
+ const privateKey = "0x..."; // or `viem`, `ethers`
264
322
 
265
323
  const transport = new hl.HttpTransport();
266
- const exchClient = new hl.ExchangeClient({ wallet: account, transport });
324
+ const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
267
325
 
268
326
  // Place an orders
269
327
  const result = await exchClient.order({
@@ -323,15 +381,16 @@ const sub = await subsClient.candle({ coin: "BTC", interval: "1h" }, (data) => {
323
381
 
324
382
  ```ts
325
383
  import * as hl from "@nktkas/hyperliquid";
326
- import { privateKeyToAccount } from "viem/accounts";
327
384
 
328
385
  const multiSignAddress = "0x...";
329
- const signers = [privateKeyToAccount("0x...")];
386
+ const signers = [
387
+ "0x...", // Private keys
388
+ ] as const;
330
389
 
331
390
  const transport = new hl.HttpTransport();
332
391
  const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers });
333
392
 
334
- // Interaction is the same as with ExchangeClient
393
+ // Interaction is the same as with `ExchangeClient`
335
394
 
336
395
  // Place an orders
337
396
  const result = await multiSignClient.order({
@@ -363,10 +422,7 @@ const result = await multiSignClient.withdraw3({
363
422
  });
364
423
  ```
365
424
 
366
- </details>
367
-
368
- <details>
369
- <summary><h2>API Reference</h2></summary>
425
+ ## API Reference
370
426
 
371
427
  ### Clients
372
428
 
@@ -454,6 +510,7 @@ class ExchangeClient {
454
510
  constructor(args: {
455
511
  transport: HttpTransport | WebSocketTransport;
456
512
  wallet:
513
+ | Hex // Private key directly
457
514
  | AbstractViemWalletClient // viem
458
515
  | AbstractEthersSigner // ethers
459
516
  | AbstractEthersV5Signer // ethers v5
@@ -491,6 +548,7 @@ class ExchangeClient {
491
548
 
492
549
  // Transfer
493
550
  perpDexClassTransfer(args: PerpDexClassTransferParameters): Promise<SuccessResponse>;
551
+ perpDexTransfer(args: PerpDexTransferParameters): Promise<SuccessResponse>;
494
552
  spotSend(args: SpotSendParameters): Promise<SuccessResponse>;
495
553
  subAccountSpotTransfer(args: SubAccountSpotTransferParameters): Promise<SuccessResponse>;
496
554
  subAccountTransfer(args: SubAccountTransferParameters): Promise<SuccessResponse>;
@@ -555,7 +613,7 @@ class SubscriptionClient {
555
613
 
556
614
  // Explorer
557
615
  explorerBlock(listener: (data: WsBlockDetails[]) => void): Promise<Subscription>;
558
- explorerTx(listener: (data: TxDetails[]) => void): Promise<Subscription>;
616
+ explorerTxs(listener: (data: TxDetails[]) => void): Promise<Subscription>;
559
617
  }
560
618
  ```
561
619
  <!-- deno-fmt-ignore-end -->
@@ -576,16 +634,18 @@ class MultiSignClient extends ExchangeClient {
576
634
  },
577
635
  );
578
636
 
579
- // Same methods as ExchangeClient
637
+ // Same methods as `ExchangeClient`
580
638
  }
581
639
  ```
582
640
 
583
641
  ### Transports
584
642
 
585
- Transport acts as a layer between the class and Hyperliquid servers.
643
+ Transport acts as a layer between class requests and Hyperliquid servers.
586
644
 
587
645
  #### HTTP Transport
588
646
 
647
+ HTTP transport is suitable for one-off requests or serverless environments.
648
+
589
649
  ```ts
590
650
  class HttpTransport {
591
651
  constructor(options?: {
@@ -604,6 +664,8 @@ class HttpTransport {
604
664
 
605
665
  #### WebSocket Transport
606
666
 
667
+ WebSocket transport has better network latency than HTTP transport.
668
+
607
669
  ```ts
608
670
  class WebSocketTransport {
609
671
  constructor(options?: {
@@ -627,10 +689,7 @@ class WebSocketTransport {
627
689
  }
628
690
  ```
629
691
 
630
- </details>
631
-
632
- <details>
633
- <summary><h2>Additional Import Points</h2></summary>
692
+ ## Additional Import Points
634
693
 
635
694
  ### `/types`
636
695
 
@@ -647,26 +706,25 @@ The import point gives access to functions that generate signatures for Hyperliq
647
706
 
648
707
  ```ts
649
708
  import { actionSorter, signL1Action } from "@nktkas/hyperliquid/signing";
650
- import { privateKeyToAccount } from "viem/accounts"; // or other wallet libraries
651
709
 
652
- const wallet = privateKeyToAccount("0x...");
710
+ const privateKey = "0x..."; // or `viem`, `ethers`
653
711
 
712
+ const nonce = Date.now();
654
713
  const action = {
655
714
  type: "cancel",
656
715
  cancels: [
657
716
  { a: 0, o: 12345 },
658
717
  ],
659
- };
660
- const nonce = Date.now();
718
+ } as const;
661
719
 
662
720
  const signature = await signL1Action({
663
- wallet,
664
- action: actionSorter[action.type](action), // key order affects signature
721
+ wallet: privateKey,
722
+ action: actionSorter[action.type](action),
665
723
  nonce,
666
- isTestnet: true, // change to `false` for mainnet
667
724
  });
668
725
 
669
- const response = await fetch("https://api.hyperliquid-testnet.xyz/exchange", {
726
+ // Send the signed action to the Hyperliquid API
727
+ const response = await fetch("https://api.hyperliquid.xyz/exchange", {
670
728
  method: "POST",
671
729
  headers: { "Content-Type": "application/json" },
672
730
  body: JSON.stringify({ action, signature, nonce }),
@@ -678,27 +736,26 @@ const body = await response.json();
678
736
 
679
737
  ```ts
680
738
  import { signUserSignedAction, userSignedActionEip712Types } from "@nktkas/hyperliquid/signing";
681
- import { privateKeyToAccount } from "viem/accounts"; // or other wallet libraries
682
739
 
683
- const wallet = privateKeyToAccount("0x...");
740
+ const privateKey = "0x..."; // or `viem`, `ethers`
684
741
 
685
742
  const action = {
686
743
  type: "approveAgent",
687
- signatureChainId: "0x66eee", // must match the current wallet network
688
- hyperliquidChain: "Testnet", // Mainnet | Testnet
744
+ signatureChainId: "0x66eee",
745
+ hyperliquidChain: "Mainnet",
689
746
  agentAddress: "0x...",
690
747
  agentName: "Agent",
691
748
  nonce: Date.now(),
692
- };
749
+ } as const;
693
750
 
694
751
  const signature = await signUserSignedAction({
695
- wallet,
752
+ wallet: privateKey,
696
753
  action,
697
- types: userSignedActionEip712Types[action.type], // key order affects signature
698
- chainId: parseInt(action.signatureChainId, 16),
754
+ types: userSignedActionEip712Types[action.type],
699
755
  });
700
756
 
701
- const response = await fetch("https://api.hyperliquid-testnet.xyz/exchange", {
757
+ // Send the signed action to the Hyperliquid API
758
+ const response = await fetch("https://api.hyperliquid.xyz/exchange", {
702
759
  method: "POST",
703
760
  headers: { "Content-Type": "application/json" },
704
761
  body: JSON.stringify({ action, signature, nonce: action.nonce }),
@@ -706,8 +763,6 @@ const response = await fetch("https://api.hyperliquid-testnet.xyz/exchange", {
706
763
  const body = await response.json();
707
764
  ```
708
765
 
709
- </details>
710
-
711
766
  ## Contributing
712
767
 
713
768
  We appreciate your help! To contribute, please read the [contributing instructions](CONTRIBUTING.md).
package/esm/mod.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export * from "./src/base.js";
2
2
  export * from "./src/transports/base.js";
3
- export type { AbstractEthersSigner, AbstractEthersV5Signer, AbstractViemWalletClient, AbstractWindowEthereum, } from "./src/signing.js";
3
+ export type { AbstractEthersSigner, AbstractEthersV5Signer, AbstractViemWalletClient, AbstractWindowEthereum, } from "./src/signing/mod.js";
4
4
  export * from "./src/clients/exchange.js";
5
5
  export * from "./src/clients/info.js";
6
6
  export * from "./src/clients/multiSign.js";
package/esm/mod.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AACA,cAAc,eAAe,CAAC;AAC9B,cAAc,0BAA0B,CAAC;AAGzC,YAAY,EACR,oBAAoB,EACpB,sBAAsB,EACtB,wBAAwB,EACxB,sBAAsB,GACzB,MAAM,kBAAkB,CAAC;AAG1B,cAAc,2BAA2B,CAAC;AAC1C,cAAc,uBAAuB,CAAC;AACtC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,+BAA+B,CAAC;AAG9C,cAAc,yCAAyC,CAAC;AACxD,cAAc,mDAAmD,CAAC;AAGlE,mBAAmB,mCAAmC,CAAC;AACvD,mBAAmB,mCAAmC,CAAC;AACvD,mBAAmB,8BAA8B,CAAC;AAClD,mBAAmB,4BAA4B,CAAC;AAChD,mBAAmB,iCAAiC,CAAC;AACrD,mBAAmB,6BAA6B,CAAC;AACjD,mBAAmB,4BAA4B,CAAC;AAChD,mBAAmB,4BAA4B,CAAC;AAChD,mBAAmB,wCAAwC,CAAC"}
1
+ {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AACA,cAAc,eAAe,CAAC;AAC9B,cAAc,0BAA0B,CAAC;AAGzC,YAAY,EACR,oBAAoB,EACpB,sBAAsB,EACtB,wBAAwB,EACxB,sBAAsB,GACzB,MAAM,sBAAsB,CAAC;AAG9B,cAAc,2BAA2B,CAAC;AAC1C,cAAc,uBAAuB,CAAC;AACtC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,+BAA+B,CAAC;AAG9C,cAAc,yCAAyC,CAAC;AACxD,cAAc,mDAAmD,CAAC;AAGlE,mBAAmB,mCAAmC,CAAC;AACvD,mBAAmB,mCAAmC,CAAC;AACvD,mBAAmB,8BAA8B,CAAC;AAClD,mBAAmB,4BAA4B,CAAC;AAChD,mBAAmB,iCAAiC,CAAC;AACrD,mBAAmB,6BAA6B,CAAC;AACjD,mBAAmB,4BAA4B,CAAC;AAChD,mBAAmB,4BAA4B,CAAC;AAChD,mBAAmB,wCAAwC,CAAC"}
package/esm/src/base.d.ts CHANGED
@@ -1,7 +1,10 @@
1
1
  export type Hex = `0x${string}`;
2
2
  export type MaybePromise<T> = T | Promise<T>;
3
+ export type DeepImmutable<T> = {
4
+ readonly [K in keyof T]: DeepImmutable<T[K]>;
5
+ };
3
6
  /** Base error class for all SDK errors. */
4
7
  export declare class HyperliquidError extends Error {
5
- constructor(message?: string);
8
+ constructor(message?: string, options?: ErrorOptions);
6
9
  }
7
10
  //# sourceMappingURL=base.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../src/src/base.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,GAAG,GAAG,KAAK,MAAM,EAAE,CAAC;AAEhC,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;AAE7C,2CAA2C;AAC3C,qBAAa,gBAAiB,SAAQ,KAAK;gBAC3B,OAAO,CAAC,EAAE,MAAM;CAI/B"}
1
+ {"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../src/src/base.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,GAAG,GAAG,KAAK,MAAM,EAAE,CAAC;AAEhC,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;AAG7C,MAAM,MAAM,aAAa,CAAC,CAAC,IAAI;IAC3B,QAAQ,EAAE,CAAC,IAAI,MAAM,CAAC,GAAG,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CAC/C,CAAC;AAEF,2CAA2C;AAC3C,qBAAa,gBAAiB,SAAQ,KAAK;gBAC3B,OAAO,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY;CAIvD"}
package/esm/src/base.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /** Base error class for all SDK errors. */
2
2
  export class HyperliquidError extends Error {
3
- constructor(message) {
4
- super(message);
3
+ constructor(message, options) {
4
+ super(message, options);
5
5
  this.name = "HyperliquidError";
6
6
  }
7
7
  }